Simlply save a variable
How would you save a variable such that it is remembered after the app is closed or deleted from the multitasking menu?
#how do you "save" a? a = 10
This thread has a solution for that:
There might be others.
Better use file with name starting with . (dot) and (de)serialize json or whatever there. Keychain is not good idea for this purpose. It contains passwords, certs, tokens, ..., is synced across devices via cloud, ... Unless you do want to store this kind of data I would choose another solution.
If all you want to store is a few values or simple data structures, a JSON file is probably the easiest option. JSON's data types are basically the same as Python's
dict, and JSON can be read or created from/to such objects using the
jsonmodule. This is especially useful if the data you want to store is already a
dictcontaining simple values.
To store more complicated Python objects the
picklemodule may be a better option. Many built-in and custom objects can be pickled, though if you've written complex custom classes you may need to add some pickling support yourself so that the objects are properly saved and restored.
If you're working with large sets of data and know a thing or two about databases and SQL, you could also use a database and the
sqlitemodule. Building a database is not as easy and straightforward as the other two options though.
I have also used the Python ConfigParser module for very simple settings. Like .ini files on Windows.
If a new file is created using f.write() for example, how does iOS handle it? If a user restores a phone from iCloud backup, will that file be restored with the app?
Or do I have to do something to configure this?
The file is stored in the iOS file system (yes, iOS does have a file system). Now, where it is saved depends on what the current directory is. When you first run your script, the current directory is the folder your script is in. Your script can change that, though, by calling
os.chdir("some directory here")(you'll have to import the
import os). Remember, Pythonista only has access to its own app container/folder. This means that your script will return a permission denied error if you try to change directories into either other apps' folders or iOS system folders. Your script can only access files and folders that are part of Pythonista, including Pythonista's own system files that make the app run. You (probably) won't have access to any of omz's code, but still do not muck around with files and folders that are not in your script library unless you know what you're doing.
To answer your other question about iCloud backups, assuming the user has enabled the backup of app data to iCloud, your files should be restored, but don't count on anything. Always be prepared to regenerate the files if necessary.
Just do this:
Make sure you have a file named "a.txt".
Put the value of the variable a in the first line. In the following code, it ignores all other lines in the file.
In your code:
line = open('a.txt').readlines()
The above code does not close the file which
- leaks memory,
- relies on the interpreter to properly flush the writes, etc. to file.
Properly closing files is even more important in Pythonista than with a regular CPython runtime. Because the Pythonista app runs a single Python process until it is unloaded, the file object might not get garbage-collected even if you run a new script.