PDA

View Full Version : Write python functions to .py



HarrisonNapper
December 11th, 2009, 03:56 PM
Anyone know of a reference for writing a function from interactive mode to a file? I read something about pickling that will probably do the trick, but if anyone knows a succint way to accomplish this or any modules that make it easy, that would be helpful.

So, in summary, if I create class 'Class' with function 'function', I want to be able to have something along the lines of:


f=open('test.py','r+')
f.write(Class)

#or even

f.write(Class.function())


Cheers and thanks in advance for the help. Once I get off of work, I can look it up online, but if you have a few seconds and have a particular method you like, I would appreciate it if you shared. :)

Mickeysofine1972
December 11th, 2009, 05:33 PM
You can use pickling but I dont think it will serialise your functions, it will only write and restore the data in your objects


import pickle

f = open( "my_file", "w" )
c = AClass()
pickle.dump( c, f )

to restore:


import pickle

f = open( "my_file", "r" )
c = pickle.load(f)

Hope that helps

Mike

wmcbrine
December 11th, 2009, 05:43 PM
Yeah, this is one area where Python isn't the new BASIC -- no "SAVE" equivalent that I know of.

Probably your best bet is to scroll up, select, and paste it to a file. If it's still in the scroll buffer.

nvteighen
December 11th, 2009, 06:07 PM
Seems not even IPython has this feature :(

HarrisonNapper
December 11th, 2009, 10:45 PM
Thanks for all of your replies; they are greatly appreciated. Perhaps this will be something for me to tackle down the road, as it would be an epic module. I wonder if there are restrictions inherent in the language that prevent this? Anyway, thanks again for all of your(pl) input!

doas777
December 11th, 2009, 10:49 PM
thats part of why I don't do the interactive thing most of the time

HarrisonNapper
December 11th, 2009, 11:46 PM
thats part of why I don't do the interactive thing most of the time

I'm just starting out with the language and, in a real sense, any language, so interactive mode provides an environment where I can play around with the code. See things that work more efficiently or that don't work, test out ideas others have had and see whether their methods work with my code, etc. It's basically a controlled environment for me to isolate blocks of code. Given, it's not even really as efficient as edit/run/edit/run with emacs (which I have at work (*******) and at home (Linux)), but it's useful in some situations.

jpkotta
December 17th, 2009, 07:46 PM
You can do this in ipython.


In [1]: def foo():
...: print "foo"
...:
...:



In [10]: save foo 1 # input #1
The following commands were written to file `foo.py`:
def foo():
print "foo"


You can specify lists and ranges of lines. There is also the edit magic command, which is similar.

http://ipython.scipy.org/doc/rel-0.9.1/html/interactive/reference.html#magic-commands

Can+~
December 17th, 2009, 09:25 PM
I also found this post in stackoverflow, I forgot to post it:

http://stackoverflow.com/questions/947810/how-to-save-a-python-interactive-session

It offers IPython, bPython and reinteract.