Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-01-2005, 06:19 PM
|
#1
|
|
Member
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183
Rep:
|
Python question
I've a little problem with my first python script... I've two files, one which has two functions and another one which I use for testing them. Here are the frunction:
Code:
def loaddb(file):
""" loaddb function"""
handle = open(file, "rb")
info = pickle.loads(handle)
return info
def writedb(tupple, file):
""" writing the db """
info = pickle.dumps(tupple)
handle = open(file, "wb")
result = handle.write(handle)
and my testing source file
Code:
from fileio import *
import pickle
record = ["name", "phone"]
writedb(record, "test.txt")
If I run my first file no exeptions are raised... but If I run the second one it gives this:
Code:
Traceback (most recent call last):
File "scriptutils.py", line 307, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "debugger.py", line 595, in run
exec cmd in globals, locals
File "filestruct.py", line 5, in ?
writedb(phone, "test.txt")
File "fileio.py", line 9, in writedb
info = pickle.dumps(tupple)
NameError: global name 'pickle' is not defined
The purpose of the writedb function is to pickle a tupple. If anyone could tell me where's the problem, it would be great.
Thanks very much
|
|
|
|
01-01-2005, 08:10 PM
|
#2
|
|
Member
Registered: Jun 2003
Distribution: Gentoo (main), Gentoo (server)
Posts: 94
Rep:
|
I believe you have to import pickle in your fileio.py file.
|
|
|
|
01-02-2005, 06:08 AM
|
#3
|
|
Member
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183
Original Poster
Rep:
|
It doesn't work also... 
|
|
|
|
01-02-2005, 08:26 AM
|
#4
|
|
Member
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 649
Rep:
|
I maybe wrong but have you defined a pickle file somewhere eg
pickle_file = open(filename, "rb")
and in your first function the next line would be :-
info = Pickle.load(pickle_file)
BTW cPickle is quicker than pickle and works just as well. Here is an example from a program I wrote :-
def writedict(self, dictent):
#write dictionary to file
pickle_file = open(dictfile, "wb")
cPickle.dump(dictent, pickle_file)
pickle_file.close()
|
|
|
|
01-02-2005, 06:51 PM
|
#5
|
|
Member
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183
Original Poster
Rep:
|
Quote:
Originally posted by davholla
I maybe wrong but have you defined a pickle file somewhere eg
pickle_file = open(filename, "rb")
and in your first function the next line would be :-
info = Pickle.load(pickle_file)
BTW cPickle is quicker than pickle and works just as well. Here is an example from a program I wrote :-
def writedict(self, dictent):
#write dictionary to file
pickle_file = open(dictfile, "wb")
cPickle.dump(dictent, pickle_file)
pickle_file.close()
|
BTW why are you using self in the parameters list of the function? Just curious...
PS I fixed my problem
|
|
|
|
01-03-2005, 05:21 AM
|
#6
|
|
Member
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 649
Rep:
|
I used self as the function was part of a class.
How did you solve the problem ?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:33 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|