P
Pierre-Alain Dorange
Hello,
I'm new to python and i'm deelopping a small game with pygame.
I got lot of fun with python.
Trying to implement a config file to save user score and config.
Reading doc and some tutorial about file handling i read about pickle,
and yes it's very easy to implement.
But i thought i miss something with the roots of python.
I implement a Prefs class to handle config data and add it a load and
save method. It works but when reading self, it OK inside the load
function but outside the pref instance return to it's previus state...
I don't really understand.
Here's the test code :
#!/usr/bin/env python
import os, pickle
kFileName='test.ini'
class Prefs():
def __init__(self):
self.test=1
self.zorglub='bonjour'
def load(self):
if os.path.exists(kFileName):
try:
print 'test %d (before)' % self.test
f=open(kFileName,'r')
self=pickle.load(f)
f.close()
print 'test %d (after)' % self.test
except IOError:
return 1
return 0
def save(self):
f=open(kFileName,'w')
pickle.dump(self,f)
f.close()
return 0
def main():
p=Prefs()
p.load()
print 'test %d (after load)' % p.test
p.test+=1
print 'test %d (before save)' % p.test
p.save()
print '----------------------'
if __name__ == '__main__': main()
I'm new to python and i'm deelopping a small game with pygame.
I got lot of fun with python.
Trying to implement a config file to save user score and config.
Reading doc and some tutorial about file handling i read about pickle,
and yes it's very easy to implement.
But i thought i miss something with the roots of python.
I implement a Prefs class to handle config data and add it a load and
save method. It works but when reading self, it OK inside the load
function but outside the pref instance return to it's previus state...
I don't really understand.
Here's the test code :
#!/usr/bin/env python
import os, pickle
kFileName='test.ini'
class Prefs():
def __init__(self):
self.test=1
self.zorglub='bonjour'
def load(self):
if os.path.exists(kFileName):
try:
print 'test %d (before)' % self.test
f=open(kFileName,'r')
self=pickle.load(f)
f.close()
print 'test %d (after)' % self.test
except IOError:
return 1
return 0
def save(self):
f=open(kFileName,'w')
pickle.dump(self,f)
f.close()
return 0
def main():
p=Prefs()
p.load()
print 'test %d (after load)' % p.test
p.test+=1
print 'test %d (before save)' % p.test
p.save()
print '----------------------'
if __name__ == '__main__': main()