Thread: saving...

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

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11

    saving...


    is there a way to make a program that when u run it it will save youre variables?

    When u close it then reopen it youre variables would still be the same.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    you've got a few options. you can store them in a plain text file and devise some algorithm for loading them into your program correctly, you could use the shelve module to store them as a kind of persistent dictionary, or you store them in a file with the pickle module. there's a few different avenues to explore, check out http://docs.python.org/modindex.html for info on the pickle and shelve modules.
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    If you want a pre-packaged solution you could try my ptypes module. It provides a persistent dictionary and list.
    http://www.peck.org.uk/p/python/
    There is a link for more info on how it works and it is self documented.

    grim
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    why use a third party module to create a persistent dictionary when that's exactly what the shelve module does? what other advantages does it offer? I see you can store a list too, but that's easy to do with the shelve module. ie

    Code:
    import shelve
    db = shelve.open('test.dbm')
    db['list_obj'] = [1, 2, 3]
    db.close()
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    It's just one more option to consider

    I like the idea of a datatype that can save/reload itself.
    Code:
    my_programs_configuration = ptypes.pdict("my_prog")
    ....
    my_programs_configuration['window_size'] = "100x200"
    ......
    my_programs_configuration.save()
    seems tidy to me and follows in the spirit of:
    my_list.sort()
    my_string.replace("*","+")

    grim
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    fair enough
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11
    Originally Posted by rebbit
    why use a third party module to create a persistent dictionary when that's exactly what the shelve module does? what other advantages does it offer? I see you can store a list too, but that's easy to do with the shelve module. ie

    Code:
    import shelve
    db = shelve.open('test.dbm')
    db['list_obj'] = [1, 2, 3]
    db.close()
    i like this idea the most. It suites my needs the best.
    But just a simple noob question- What is the dbm on test.dbm?

    also- if test.dbm is made for shelves, will it create the test.dbm if it is not already made?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    DBM stands for DataBase Manager, and yes if the file does not already exist it will be created.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11
    thx alot this helps a bunch.

    but... i have a questions
    when i try to open test.dbm theres nothing in it, is that normal?(i have put a few things in it)
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    there should be data in it, yes, but nothing that makes any sense to read. what editor are you opening it with? but anyway, so long as you can save data to it and then read the data back again it's fine.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    58
    Rep Power
    11
    thx alot it helped out.

IMN logo majestic logo threadwatch logo seochat tools logo