omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Anyone able to direct me to pickle in Pythonista resources?

    Pythonista
    3
    4
    1283
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Kevin254
      Kevin254 last edited by Kevin254

      I am very new to programming, and working on my first solo project that’s not part of a course or exercise. It’s just a text based collection of various functions (calculator, bill tracker, calendar, etc.).

      From lots of googling it looks like I need to “Pickle” the data so that it persists between when I run the program. I use iOS shortcuts to run the Python script for my program from Pythonista, but I have no idea where to go to find a “newbie friendly” explanation of pickling and how to implement it or something like it into Pythonista.

      TLDR: looking for easier to understand resources on how to save data entered into program in Pythonista so it is usable next time I run it (on iOS) without having to re-enter the data (i.e. an event added to a calendar, or new key: value pairs in dictionary variables). 1https://19216881.org/ https://1921681001.link/ https://19216801.digital/

      Any/all help is greatly appreciated!

      cvp 2 Replies Last reply Reply Quote 0
      • cvp
        cvp @Kevin254 last edited by

        @Kevin254 for this kind of process, you can use the keychain module to store some user data in a memory block on your iDevice. At next run, you can read this memory and reset your variables at the values they had at previous run.

        1 Reply Last reply Reply Quote 0
        • bennr01
          bennr01 last edited by ccc

          Pickling refers to using the pickle module for persisting data. You don't need to implement it yourself, it's included in a module by default.

          First, you can access the documentation by swiping from right to left to open the console. There you'll find a button labeld ?. Press it to open the documentation. The documentation contains a lot of explanations, how-tos and references of modules. You'll need to search for pickle, then you'll get a guide on how to use it.

          If you don't want to look it up:

          import pickle  # load the pickle module
          
          my_data = ...  # whatever you want to save
          s = pickle.dumps()  # s is now a string from which you can rebuild my_data
          with open("data.bin", "wb") as fout:  # open file 'data.bin' for binary writing
              fout.write(s)  # write string s
          # let's load it
          with open("data.bin", "rb") as fin:  # open file 'data.bin' for binary reading
              loaded_s = fin.read()  # read string from file
          loaded = pickle.loads(loaded_s)  # unpickle the string, returning a copy of my_data
          

          The above code stores some data and reloads it. You could also change the code to use pickle.dump() instead of pickle.dumps(), which is better when working with large data sets, but I think this explains it better.

          However, I don't think pickle is the go-to solution. I don't know what exactly you are attempting to save, but generally pickle should be avoided. Pickle is python specific and very flexible, but also insecure. If an attacker can modify a file containing a pickle dump, arbitrary code execution becomes possible. You should instead think about using the json module, which can't store custom classes but is still one of the most common data formats, using files directly (requires a bit more work) or using a database like anydbm or sqlite3.

          1 Reply Last reply Reply Quote 0
          • cvp
            cvp @Kevin254 last edited by cvp

            @Kevin254 I didn't know the pickle module, like a lot of Python features.
            But if you want to try my solution without file, for small amount of data, try this example with several runs

            import keychain
            import ast
            
            my_data_str = keychain.get_password('myscript', 'pickle')
            print(my_data_str)
            if my_data_str:
            	my_data = ast.literal_eval(my_data_str)
            else:
            	my_data = {'var1':1, 'var2':2}
            #....process
            my_data['var1'] += 1
            my_data['var2'] += 2
            my_data_str = str(my_data)
            keychain.set_password('myscript', 'pickle', my_data_str)
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Powered by NodeBB Forums | Contributors