Re: [Tutor] problems pickling functions

2007-01-11 Thread Hugo González Monteverde
Arild B. Næss wrote:
 I haven't found out how to change the working directory in IDLE,  
 though – and for some reason it seems to be a different one this  
 session from the last one. Does anyone know?
   (I use a mac by the way.)


take a look at os.chdir()

This changes the interpreter's working dir (and thus whatever you're 
writing into the prompt in IDLE)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problems pickling functions

2006-12-09 Thread Arild B. Næss

Den 8. des. 2006 kl. 15.43 skrev Kent Johnson:

 Arild B. Næss wrote:
 Den 8. des. 2006 kl. 14.05 skrev Kent Johnson:
 Why do you need to pickle the function? Is it created  
 dynamically?  Can you just pickle the data?

 Kent

 Thanks.
 I guess it's not absolutely necessary to pickle the function. I  
 tried  to do this because I wanted to use the function in the  
 interpreter  without having to write it in there line by line.
 I'm used to working in R and Matlab, where you often run scripts  
 from  the active interpreter. In that way  you can actually  
 examine the  data a script generates, instead of having the script  
 print it to  screen or file.
 I'm having trouble getting used to python like this because I get   
 trouble trying to paste in several lines at once from emacs, and  
 I  haven't found a way to run scripts in the interpreter.

 Two suggestions:
 - Use an editor / IDE that allows you to run Python scripts. IDLE  
 will do this. I think emacs has good support for Python too but  
 someone who uses emacs will have to help you with that one.

 - Save your function in a module and import the module from the  
 interpreter. Then you can run the function in the interpreter.

 For example if you have funcs.py in the working directory and it  
 contains a function
 def doItAll():
   pass

 then in the interpreter you can type
  import funcs
  funcs.doItAll()

 to run the function.

 If you change the function in an external editor you have to reload  
 it in the interpreter to get the revised version:
  reload(funcs)

Thanks. That worked.

I haven't found out how to change the working directory in IDLE,  
though – and for some reason it seems to be a different one this  
session from the last one. Does anyone know?
(I use a mac by the way.)


 PS Please reply to the list, not to me directly.

Sorry about that. I didn't realize my client picked your adress and  
not the list's when I clicked reply. My mistake.

regards,
Arild Næss
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] problems pickling functions

2006-12-08 Thread Arild B. Næss
Hi,

I'm writing a program for tagging which requires a long time to  
calculate the parameters. I have therefore tried to write a long  
program that pickles all the data, and pickles a function that uses  
these parameters to tag an input sentence.

But I'm having trouble with loading the function. The odd thing is  
that it works fine in the interpreter to pickle and load a function:

  import pickle
  def simple():
... print This works
...
  f=open(simple.txt,w)
  pickle.dump(simple,f)
  f.close()
  s()
Traceback (most recent call last):
   File stdin, line 1, in module
NameError: name 's' is not defined
  f=open(simple.txt,r)
  s=pickle.load(f)
  s()
This works

However when I try to do this with the script simple.py (with the  
exact same commands as above) it doesn't work:

$ cat simple.py

import pickle

def simple():
 print This works

f = open(simple.txt,w)
pickle.dump(simple,f)
f.close()

$ python simple.py
$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type help, copyright, credits or license for more information.
  import pickle
  f2 = open(simple.txt,r)
  s
Traceback (most recent call last):
   File stdin, line 1, in module
NameError: name 's' is not defined
  s = pickle.load(f2)
Traceback (most recent call last):
   File stdin, line 1, in module
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/pickle.py, line 1370, in load
 return Unpickler(file).load()
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/pickle.py, line 858, in load
 dispatch[key](self)
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/pickle.py, line 1090, in load_global
 klass = self.find_class(module, name)
   File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
python2.5/pickle.py, line 1126, in find_class
 klass = getattr(mod, name)
AttributeError: 'module' object has no attribute 'simple'
 

I don't get this error message, and I'm annoyed – because I'm used to  
that things that work in the interpreter also work when written as a  
program.

Can anyone help?

regards,
Arild Næss

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problems pickling functions

2006-12-08 Thread Kent Johnson
Arild B. Næss wrote:
 Hi,
 
 I'm writing a program for tagging which requires a long time to  
 calculate the parameters. I have therefore tried to write a long  
 program that pickles all the data, and pickles a function that uses  
 these parameters to tag an input sentence.
 
 But I'm having trouble with loading the function. The odd thing is  
 that it works fine in the interpreter to pickle and load a function:
 
   import pickle
   def simple():
 ... print This works
 ...
   f=open(simple.txt,w)
   pickle.dump(simple,f)
   f.close()
   s()
 Traceback (most recent call last):
File stdin, line 1, in module
 NameError: name 's' is not defined
   f=open(simple.txt,r)
   s=pickle.load(f)
   s()
 This works
 
 However when I try to do this with the script simple.py (with the  
 exact same commands as above) it doesn't work:
 
 $ cat simple.py
 
 import pickle
 
 def simple():
  print This works
 
 f = open(simple.txt,w)
 pickle.dump(simple,f)
 f.close()
 
 $ python simple.py
 $ python
 Python 2.5 (r25:51918, Sep 19 2006, 08:49:13)
 [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
 Type help, copyright, credits or license for more information.
   import pickle
   f2 = open(simple.txt,r)
   s
 Traceback (most recent call last):
File stdin, line 1, in module
 NameError: name 's' is not defined
   s = pickle.load(f2)
 Traceback (most recent call last):
File stdin, line 1, in module
File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
 python2.5/pickle.py, line 1370, in load
  return Unpickler(file).load()
File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
 python2.5/pickle.py, line 858, in load
  dispatch[key](self)
File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
 python2.5/pickle.py, line 1090, in load_global
  klass = self.find_class(module, name)
File /Library/Frameworks/Python.framework/Versions/2.5/lib/ 
 python2.5/pickle.py, line 1126, in find_class
  klass = getattr(mod, name)
 AttributeError: 'module' object has no attribute 'simple'
  
 
 I don't get this error message, and I'm annoyed – because I'm used to  
 that things that work in the interpreter also work when written as a  
 program.

 From the docs for pickle (13.1.4 What can be pickled and unpickled?):

Note that functions (built-in and user-defined) are pickled by ``fully 
qualified'' name reference, not by value. This means that only the 
function name is pickled, along with the name of module the function is 
defined in. Neither the function's code, nor any of its function 
attributes are pickled. Thus the defining module must be importable in 
the unpickling environment, and the module must contain the named 
object, otherwise an exception will be raised.

Your code works from the interpreter because 'simple' is still defined. 
If you 'del simple' before you unpickle I think you will get the same 
error as you get in the script.

Searching comp.lang.python for 'pickle function' yields a few possible 
workarounds but they are messy.

Why do you need to pickle the function? Is it created dynamically? Can 
you just pickle the data?

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] problems pickling functions

2006-12-08 Thread Kent Johnson
Arild B. Næss wrote:
 Den 8. des. 2006 kl. 14.05 skrev Kent Johnson:
 Why do you need to pickle the function? Is it created dynamically?  
 Can you just pickle the data?

 Kent

 
 Thanks.
 
 I guess it's not absolutely necessary to pickle the function. I tried  
 to do this because I wanted to use the function in the interpreter  
 without having to write it in there line by line.
 
 I'm used to working in R and Matlab, where you often run scripts from  
 the active interpreter. In that way  you can actually examine the  
 data a script generates, instead of having the script print it to  
 screen or file.
 
 I'm having trouble getting used to python like this because I get  
 trouble trying to paste in several lines at once from emacs, and I  
 haven't found a way to run scripts in the interpreter.

Two suggestions:
- Use an editor / IDE that allows you to run Python scripts. IDLE will 
do this. I think emacs has good support for Python too but someone who 
uses emacs will have to help you with that one.

- Save your function in a module and import the module from the 
interpreter. Then you can run the function in the interpreter.

For example if you have funcs.py in the working directory and it 
contains a function
def doItAll():
   pass

then in the interpreter you can type
  import funcs
  funcs.doItAll()

to run the function.

If you change the function in an external editor you have to reload it 
in the interpreter to get the revised version:
  reload(funcs)

Kent

PS Please reply to the list, not to me directly.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor