Following on from the comments above two things I've found really
helpful are the __doc__ strings and the exec command.

for example:

>>> a = 'a random string'
>>> dir(a)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__',
'__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__',
'__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__',
'__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count',
'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index',
'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace',
'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
'upper', 'zfill']
>>> print a.strip.__doc__
S.strip([chars]) -> string or unicode

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping
>>>

exec is also very useful. It allows you to run python code that is in
a string, for example (Be it a simple and rather useless example!) the
string; "print 1 + 2 ":

>>> exec("print 1 + 2")
3
>>>

Taking both one step further if you can extract all the __doc__
strings for all the objects listed from the dir of an object:

a = 'a random string'
for i in dir(a):
    command = "print str." + i + ".__doc__"
    exec(command)

This will print out all the __doc__ strings for functions you can call
on your string object a. This is particually helpful when you know
what you want to do to something (for instance capitalise the first
letter each word in a string) but don't know what function to call.

Another instance when exec comes in handy is when receiving input from
a user in a user interface. If used in this way you should be careful
to check the data (parse) to ensure the user isn't running code that
will cause your program problems. For example exec("import
sys\nsys.exit()") would close the python interpreter, which will lead
to your program crashing.

[EMAIL PROTECTED] ~]$ python
Python 2.5 (r25:51908, Apr 10 2007, 10:27:40)
[GCC 4.1.2 20070403 (Red Hat 4.1.2-8)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exec("import sys\nsys.exit()")
[EMAIL PROTECTED] ~]$

Cheers,

Wesley.

On 06/11/2007, bhaaluu <[EMAIL PROTECTED]> wrote:
> Greetings,
>
> On Nov 6, 2007 4:15 AM, Timmie <[EMAIL PROTECTED]> wrote:
> > Hello,
> > I am stepping forward into learning python and write my first programs now.
> > To facilitate my development I have a question:
> >
> > Is there a tool which I can run on my code and then get a flow chart from 
> > it or
> > visualize its structure in another form?
> >
>
> I have found that a very simple and inexpensive way to look at Python
> code while it's running is to insert a couple of lines in the code at the
> points you want to look at:
>
> print variableName
> raw_input("Pause")
>
> The 'print variableName' will print the value the variable is pointing to,
> and 'raw_input("Pause") acts like a breakpoint, stopping program execution
> so you can check out what's happening. Two other items of interest are:
>
> dir (itemName)
> type (itemName)
>
> Both are extremely helpful when you're first learning Python. They're useful
> for discovering the modules and attributes in classes, and checking the
> type of objects.
>
> You can use these in addition to any graphical output tools you find.
>
> (Just Another Noob.)
> --
> b h a a l u u at g m a i l dot c o m
> http://www.geocities.com/ek.bhaaluu/python/index.html
>
> >
> > There was a discussion about that soem time ago.
> > OT: Flow chart -
> > http://news.gmane.org/find-root.php?message_id=%3c1103452504.92b04ebcjerimed%40myrealbox.com%3e
> >
> > Is there any solution that can be used without leaning UML?
> >
> > Kind regards,
> > Timmie
> >
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to