ParTecs Weblog

Source inspector

February 27th, 2006

The .pythonrc.py is the script that executes when interactive interpreter starts up. You need to unsure that an environment variable PYTHONSTARTUP contains the path to that file.

export PYTHONSTARTUP=/home/partecs/.pythonrc.py

Here is a handy code snippet that one could add into your .pythonrc.py file.

import pydoc
import inspect
import rlcompleter, readline

readline.parse_and_bind('tab: complete')

def source(obj):
"""source of the obj."""
try:
pydoc.pipepager(inspect.getsource(obj), 'less')
except IOError:
pass

Now for the fun part, run the Python interpreter and hit the tab button twice. You have the tab complete feature. Also try out the following statements.

>>> import os

>>> source(os)

You should be able to view the source of the os module.

You can also use your pythonrc.py file in Zope’s debug mode. To start up Zope’s debug mode run the following statement.

./zope/instancehome/bin/zopectl debug

On the interpreter run the following statement.

>>> execfile('/path/to/.pythonrc.py')

If you don’t like running execfile everytime you run Zope in the debug mode you could simply edit and modify

lib/python/Zope2/Startup/zopectl.py and replace the line
"import Zope2; app=Zope2.app())" with
"import Zope2; app=Zope2.app(); execfile('/home/partecs/.pythonrc.py')".

2 Comments »

  1. Maurits van Rees says

    Great tip; I should have known that earlier.

    The same can be done with the pdb.

    Instead of execfile(‘/home/partecs/.pythonrc.py’) you can add ‘import user’. That’s the standard python way to load that file; it is slightly more robust and more flexible. I vote for putting that into standard Zope.

    More info on my blog; click on my name in the comment header.

    October 17th, 2006 | #

  2. WouterVH says

    ipython, interactive python, has this functionality built-in:

    >>> import os
    >>> os??

    March 22nd, 2007 | #

Leave a comment

:mrgreen: :neutral: :twisted: :shock: :smile: :???: :cool: :evil: :grin: :oops: :razz: :roll: :wink: :cry: :eek: :lol: :mad: :sad:

RSS feed for these comments. | TrackBack URI

Feedburner RSS

Enter your email address:

Delivered by FeedBurner

Blogroll

Categories

RSS Telematics Freedom

February 2006
M T W T F S S
    Mar »
 12345
6789101112
13141516171819
20212223242526
2728  

Archive

Sky Sponsored by Web Hosting