Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- from pprint import pprint
- class GamePrefs:
- def __init__(self, name = 'undefined', email = 'undefined', resolution = '1024x768', volume = '100'):
- self.name = name
- self.email = email
- self.resolution = resolution
- self.volume = volume
- userPrefs = GamePrefs()
- print("Pref object with default values")
- pprint(vars(userPrefs))
- userPrefs.name = "Joe Mama"
- userPrefs.email = "joe.mama@roche.com"
- userPrefs.resolution = "1920x1080"
- userPrefs.volume = "50"
- print("Pref object with user values")
- pprint(vars(userPrefs))
- filename = 'prefs.cfg'
- # because we're saving an object we need to open the file in binary mode
- filehandler = open(filename, 'wb')
- pickle.dump(userPrefs,filehandler)
- filehandler.close()
- filehandler = open(filename, 'rb')
- myPrefs = pickle.load(filehandler)
- filehandler.close()
- print("Pref object after pickle dump and load")
- pprint(vars(myPrefs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement