LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 01-01-2005, 06:19 PM   #1
gamehack
Member
 
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183

Rep: Reputation: 30
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
 
Old 01-01-2005, 08:10 PM   #2
Dodgeram01
Member
 
Registered: Jun 2003
Distribution: Gentoo (main), Gentoo (server)
Posts: 94

Rep: Reputation: 15
I believe you have to import pickle in your fileio.py file.
 
Old 01-02-2005, 06:08 AM   #3
gamehack
Member
 
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183

Original Poster
Rep: Reputation: 30
It doesn't work also...
 
Old 01-02-2005, 08:26 AM   #4
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 652

Rep: Reputation: 31
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()
 
Old 01-02-2005, 06:51 PM   #5
gamehack
Member
 
Registered: Jun 2003
Location: Sevenoaks, UK
Distribution: Ubuntu
Posts: 183

Original Poster
Rep: Reputation: 30
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
 
Old 01-03-2005, 05:21 AM   #6
davholla
Member
 
Registered: Jun 2003
Location: London
Distribution: Mandriva 2008 Spring
Posts: 652

Rep: Reputation: 31
I used self as the function was part of a class.
How did you solve the problem ?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python variable question bendeco13 Programming 6 02-03-2005 07:22 AM
Python question bluefire Programming 5 09-28-2004 12:30 PM
python question ywchen2000 Programming 1 04-20-2004 11:46 PM
A Python question gamehack Programming 2 03-17-2004 09:09 PM
Python question m4rccd Programming 1 08-24-2003 06:04 AM


All times are GMT -5. The time now is 08:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration