#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0

    Question 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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    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
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    9
    Rep Power
    0
    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 !

IMN logo majestic logo threadwatch logo seochat tools logo