By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,703 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,703 IT Pros & Developers. It's quick & easy.

pickle.load not working?

P: n/a
I have the following code for controlling access:
----------------------
#check login credentials...
def cklogin(ipaddy, user, authcoded):
try:
print "Opening file:", user
f = file(user,'r+') #load in the user from the file
cusr = pickle.load(f)
print "User Authcode:", cusr.authcode
print "Supplied:", authcoded
print "Login:", cusr.login
if cusr.authcode == authcoded:
print "User", user, "successfully logged in."
cusr.loggedin=TRUE
cusr.invalid_logins = 0
cusr.ip = ipaddy
pickle.dump(cusr, f) # save the new status...
del cusr
f.close()
return mkcookie(ipaddy)
else:
print "Invalid login attempt."
del cusr
f.close()
except IOError:
print "User does not exist!"
return FALSE
----------------------
it reads a file saved this way:
----------------------
import pickle

class chatuser: #container for storing user information...
login = ""
authcode = ""
cookie = ""
ip = ""
loggedin = 0
invalid_logins = 0
allow_login = 1
status = ""
realname = ""
phone = ""
email = ""

derek = chatuser
derek.login = "username"
derek.authcode = "password"
derek.cookie = "123456"
derek.ip = "127.0.0.1"
derek.loggedin = 0
derek.invalid_logins = 0
derek.allow_login = 1
derek.status = "here"
derek.realname = "Derek W."
derek.phone = "480-XXX-XXXX"
derek.email = "XX*****@BBB.com"
f = file(derek.login, 'w')
pickle.dump(derek, f)
f.close()
----------------------
When I try to read it via the interpreter, it works like this...
----------------------
import pickle
f = file("spiderx", 'r')
c = pickle.load(f)
c <class __main__.chatuser at 0x00F1B600> c.login 'username' c.authcode

'password'
----------------------
With the code above, the login and authcode fields are blank... so, I
don't understand why it doesn't work. Is there anything wrong with my
code?

Aug 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
In <11**********************@z14g2000cwz.googlegroups .com>,
sp*****@gmail.com wrote:
it reads a file saved this way:
----------------------
import pickle

class chatuser: #container for storing user information...
login = ""
authcode = ""
cookie = ""
ip = ""
loggedin = 0
invalid_logins = 0
allow_login = 1
status = ""
realname = ""
phone = ""
email = ""

derek = chatuser


If this is copy and pasted and not just a typo then `derek` is just
another name for the `chatuser` class now and not an *instance* of it.

Ciao,
Marc 'BlackJack' Rintsch
Aug 14 '05 #2

P: n/a
oh, well how do I make "derek" be an instance of 'chatuser' ?

Aug 18 '05 #3

P: n/a
In <11*********************@f14g2000cwb.googlegroups. com>,
sp*****@gmail.com wrote:
oh, well how do I make "derek" be an instance of 'chatuser' ?


Spot the difference::

In [228]: class A: pass
.228.:

In [229]: a = A

In [230]: repr(a)
Out[230]: '<class __main__.A at 0x4078883c>'

In [231]: b = A()

In [232]: repr(b)
Out[232]: '<__main__.A instance at 0x4075e52c>'

Ciao,
Marc 'BlackJack' Rintsch
Aug 19 '05 #4

P: n/a
I get it, I missed the little parintheses or however you spell it. () .

Aug 25 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.