The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Saving Preferences
Discuss Saving Preferences in the Python Programming forum on Dev Shed. Saving Preferences Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 19th, 2004, 09:33 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Saving Preferences
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, 02:48 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
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.
Code:
[SectionName]
key = value
#or
key: value
However the module does not handle writing out the config file - you have to do that yourself.
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:
Code:
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'))
Regards,
Dave - The Developers' Coach
|

March 20th, 2004, 05:25 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
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.
Grim
|

March 20th, 2004, 04:12 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by DevCoach 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.
Code:
[SectionName]
key = value
#or
key: value
However the module does not handle writing out the config file - you have to do that yourself.
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:
Code:
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'))
Regards,
Dave - The Developers' Coach |
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  !
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|