473,382 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

pickle.load not working?

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
4 1983
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
oh, well how do I make "derek" be an instance of 'chatuser' ?

Aug 18 '05 #3
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
I get it, I missed the little parintheses or however you spell it. () .

Aug 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Matt Doucleff | last post by:
Hi everyone! I must be doing something wrong here :) I have a tarball that contains a single file whose contents are a pickled object. I would like to unpickle the object directly from the...
1
by: A.B., Khalid | last post by:
I wonder if someone can explain what is wrong here. I am pickling a list of dictionaries (see code attached) and unpickling it back using the HIGHEST_PROTOCOL of pickle and cPickle. I am getting an...
28
by: Grant Edwards | last post by:
I finally figured out why one of my apps sometimes fails under Win32 when it always works fine under Linux: Under Win32, the pickle module only works with a subset of floating point values. In...
0
by: Mike P. | last post by:
Hi all, I'm working on a simulation (can be considered a game) in Python where I want to be able to dump the simulation state to a file and be able to load it up later. I have used the standard...
4
by: Shi Mu | last post by:
I got a sample code and tested it but really can not understand the use of pickle and dump: >>> import pickle >>> f = open("try.txt", "w") >>> pickle.dump(3.14, f) >>> pickle.dump(, f) >>>...
2
by: Boris Borcic | last post by:
Assuming that the items of my_stream share no content (they are dumps of db cursor fetches), is there a simple way to do the equivalent of def pickles(my_stream) : from cPickle import...
10
by: crystalattice | last post by:
I'm creating an RPG for experience and practice. I've finished a character creation module and I'm trying to figure out how to get the file I/O to work. I've read through the python newsgroup...
1
by: 5N1P3R | last post by:
Hi Im trying to add save and load functionality to my program and I am using pickle to take the information and dump it in a file. I have been able to pickle everything successfully except for my...
0
by: Gabriel Genellina | last post by:
En Thu, 17 Jul 2008 09:40:29 -0300, <DWebre@dotd.la.govescribi�: The example doesn't have any readline(). Make sure you open the file in binary format ('wb' or 'rb'). If you have a list of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.