Saving your work from the python console
October 2018
Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
About
This site is an effort to share some of the base knowledge I have gathered through all this years working with Linux, FreeBSD, OpenBSD, Python or Zope, among others. So, take a look around and I hope you will find the contents useful.
Recent Entries
Recent Comments
Recent Trackbacks
Categories
OpenBSD (9 items)
BSD (0 items)
FreeBSD (19 items)
Linux (3 items)
Security (3 items)
Python (22 items)
Zope (13 items)
Daily (144 items)
e-shell (9 items)
Hacks (14 items)
PostgreSQL (3 items)
OSX (8 items)
Nintendo DS (0 items)
enlightenment (0 items)
Apache (3 items)
Nintendo Wii (1 items)
Django (24 items)
Music (12 items)
Plone (7 items)
Varnish (0 items)
Lugo (2 items)
Sendmail (0 items)
europython (7 items)
Cherokee (1 items)
self (1 items)
Nature (1 items)
Hiking (0 items)
uwsgi (0 items)
nginx (0 items)
cycling (10 items)
Networking (1 items)
DNS (0 items)
Archives

Syndicate this site (XML)

RSS/RDF 0.91

28 septiembre
2009

Saving your work from the python console

This is really a damn good trick

One of those things I always wanted to write about, and never found enough time to do it ;D.

One of the things I really like about Python is the python interpreter, usually called python shell or python console too.

It is a very useful feature of the programming language, because (as with any other shell like bash, tcsh or ksh) you can execute any source code in it, checking for results. I use it a lot for debugging/testing purposes (as you can even import modules, methods, etc).

Ok, now imagine you are testing a piece of code, or perhaps just writting some lines to get some information from, for example, data behind some django models. The actual code could be something like:

>>> import socialize.models as sclzmodels
>>> import socialize.utils as sclzutils
>>> sclzmodels.Person.objects.all().count()
>>> typeA = []
>>> typeB = []
>>> typeC = []
>>> for i in sclzmodels.Person.objects.all():
...     real_i = sclzutils.get_object_real_class_from_hierarchy(i, sclzmodels.Person)
...     if real_i.__class__.__name__ == 'TypeA':
...         typeA.append(real_i)
...     elif real_i.__class__.__name__ == 'TypeB':
...         typeB.append(real_i)
...     else:
...         typeC.append(real_i)

Do not pay too much attention on the code, as it is only an example. Basically it will filter a list (really a queryset) of objects and put them into three separate lists depending on some checks.

This is a piece of code really useful for me, that I could use quite often to get some valuable information. So, it could be very nice to put it in a script, just to execute it later without having to re-write those lines every time.

Now comes the handy trick.

As I've executed such code within the python shell, it is already in a buffer that I can easily export into a file, without having to copy/paste anything. The only thing I need to do is, within the same python shell:

>>> import readline
>>> readline.write_history_file('/path/to/where/I/want/to/save/history.txt')

After that you will have a file /path/to/where/I/want/to/save/history.txt that will contain the same code you executed in the python shell (without the >>> and ... promtps):

import socialize.models as sclzmodels
import socialize.utils as sclzutils
sclzmodels.Person.objects.all().count()
typeA = []
typeB = []
typeC = []
for i in sclzmodels.Person.objects.all():
    real_i = sclzutils.get_object_real_class_from_hierarchy(i, sclzmodels.Person)
    if real_i.__class__.__name__ == 'TypeA':
        typeA.append(real_i)
    elif real_i.__class__.__name__ == 'TypeB':
        typeB.append(real_i)
    else:
        typeC.append(real_i)

Nice, isn't it?

Posted by wu at 14:46 | Comments (1) | Trackbacks (0)
<< More nasty things about Firefox | Main | Todos los años la misma historia... >>
Comments
Re: Saving your work from the python console

I have to admit my top use case for the python shell is to check again, which way around the split() and join() methods work - I can never remember that stuff :-/

Posted by: betabug at septiembre 29,2009 08:19
Re: Saving your work from the python console

Great trick! Most google hits discuss techniques for doing this _before you have to save the history_, which is useless if you haven't prepared in advance.

Posted by: pythonlover at junio 11,2018 10:39
Trackbacks
Please send trackback to:http://blog.e-shell.org/174/tbping
There are no trackbacks.
Post a comment