SHARE
TWEET

Untitled

a guest May 21st, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pickle
  2. from pprint import pprint
  3.  
  4. class GamePrefs:
  5.     def __init__(self, name = 'undefined', email = 'undefined', resolution = '1024x768', volume = '100'):
  6.         self.name = name
  7.         self.email = email
  8.         self.resolution = resolution
  9.         self.volume = volume
  10.    
  11. userPrefs = GamePrefs()
  12.  
  13. print("Pref object with default values")
  14. pprint(vars(userPrefs))
  15.  
  16. userPrefs.name = "Joe Mama"
  17. userPrefs.email = "joe.mama@roche.com"
  18. userPrefs.resolution = "1920x1080"
  19. userPrefs.volume = "50"
  20.  
  21. print("Pref object with user values")
  22. pprint(vars(userPrefs))
  23.  
  24. filename = 'prefs.cfg'
  25. # because we're saving an object we need to open the file in binary mode
  26. filehandler = open(filename, 'wb')
  27. pickle.dump(userPrefs,filehandler)
  28. filehandler.close()
  29.  
  30. filehandler = open(filename, 'rb')
  31. myPrefs = pickle.load(filehandler)
  32. filehandler.close()
  33.  
  34. print("Pref object after pickle dump and load")
  35. pprint(vars(myPrefs))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top