March 19th, 2004, 10:33 PM
Hello fellow Python programmers! Im a newbie that just joined. Anyways I need help. Im making a text editor program. The first problem Ive had so far is saving/getting the prefferences. How could I do that? Like maybe I have stuff like HTML tag colors for santax highlighting, and if they want to view line numbers just for a few examples. If anyone could please lead me in the right direction it would be greatly appreciated.
BTW: I just started learning Python and its awesome! way better than C++, or C# combined! Im glad my friend told me about it. I just wish it was a little more popular so it would be easier to find support and stuff for it.
March 20th, 2004, 03:48 AM
Do you want the options file to be editable by the user?
If so then take a look at the ConfigParser module in the standard library. This lets you read config files in a similar format to Windows .ini files, e.g.
However the module does not handle writing out the config file - you have to do that yourself.
key = value
If you are not concerned about having the config editable by the user, then I would use the pickle module. Create an object to hold all the options - this could be a dictionary or a class. Then save and load it like this:
import pickle #(or import cPickle as pickle, which is faster)
def saveOptions(options, filename):
pickle.dump(options, file(filename, 'wb'), -1)
def loadOptions(options, filename):
return pickle.load(file(filename, 'rb'))
Dave - The Developers' Coach
March 20th, 2004, 06:25 AM
Like DevCoach says, pickle is the normal way to save data.
It just so happens my ptypes module here provides dictionaries and lists with built in pickleing.
The nice thing about it is you can use them as normal and when you want to save the data you just call their Save() method.
I know some people already have ptypes, the 0.3 version is a big update.
March 20th, 2004, 05:12 PM
Ok thanks. I don't care if the user edits it so Ill go with the ConfigParser but I need a good tutorial on it or at least a source code to reference. Best regards !
Originally Posted by DevCoach