2
2
Tips for using python
3
3
*********************
7
.. focus on newbies, intro dir and help
5
.. sectionauthor:: Doug Wendel
7
If you are new to Python, this is the right place for you. This is not a comprehensive guide, rather this section is intended help you install an appropriate version of Python and get you started using the language with PyCogent.
12
.. note:: If you are running OSX 10.5 or later you only need to install the Apple Developer tools which come with OSX and you'll have a suitable version.
14
At a minimum, you need to be using Python 2.5.x. If you have Python installed, open a terminal window and type ``python``. The first few lines of text should tell you what version you're running. For example:
19
Python 2.6.4 (r264:75706, Mar 11 2010, 12:48:01)
20
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
21
Type "help", "copyright", "credits" or "license" for more information.
23
In this case, Python 2.6.4 is installed. If you don't have Python installed or your version is older than 2.5.x, download the most recent 2.x release of Python `here <http://www.python.org/download/>`_.
25
.. warning:: **DO NOT** install Python 3.x or above. This version of the language was significantly restructured and is will currently not work with PyCogent.
30
Python comes with a built-in help utility. To access it, open a terminal window and type ``python`` to enter Python's interactive mode:
36
Welcome to Python 2.6! This is the online help utility.
38
If this is your first time using Python, you should definitely check out
39
the tutorial on the Internet at http://docs.python.org/tutorial/.
41
Enter the name of any module, keyword, or topic to get help on writing
42
Python programs and using Python modules. To quit this help utility and
43
return to the interpreter, just type "quit".
45
To get a list of available modules, keywords, or topics, type "modules",
46
"keywords", or "topics". Each module also comes with a one-line summary
47
of what it does; to list the modules whose summaries contain a given word
48
such as "spam", type "modules spam".
52
Note that you are now in the interactive help mode as noted by the prompt ``help>``. In this mode you can type in the names of various objects and get additional information on them. For example, if I want to know something about the ``map`` function:
58
Help on built-in function map in module __builtin__:
61
map(function, sequence[, sequence, ...]) -> list
63
Return a list of the results of applying the function to the items of
64
the argument sequence(s). If more than one sequence is given, the
65
function is called with an argument list consisting of the corresponding
66
item of each sequence, substituting None for missing values when not all
67
sequences have the same length. If the function is None, return a list of
68
the items of the sequence (or a list of tuples if more than one sequence).
71
To exit the interactive help mode, simply enter a blank line at the ``help>`` prompt:
77
You are now leaving help and returning to the Python interpreter.
78
If you want to ask for help on a particular object directly from the
79
interpreter, you can type "help(object)". Executing "help('string')"
80
has the same effect as typing a particular string at the help> prompt.
82
As the parting message suggests, you can also invoke help on a specific object directly:
88
Help on built-in function abs in module __builtin__:
93
Return the absolute value of the argument.
96
To quit help in this case just press ``q``.
98
Using the dir() function
99
========================
101
Another useful built-in function is ``dir()``. As the name implies, it's use is to list defined names in the current scope. To list the currently defined names:
106
['__builtins__', '__doc__', '__name__', '__package__']
108
The list shows which names are currently defined. This list includes all imported modules and variable names. For example, if I define a new variable, it will also show up in this list:
112
>>> my_variable = 'Just testing'
114
['__builtins__', '__doc__', '__name__', '__package__', 'my_variable']
116
Imported modules will also be reflected in this list:
123
['__builtins__', '__doc__', '__name__', '__package__', 'my_variable', 'os', 'sys']
125
``dir()`` can also be used to list the names defined within a module:
131
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__',...
133
It also works on variable types. For example, let's see what attributes the string class has as defined:
138
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__',...
140
You can also use ``dir()`` on a defined variable. It will inspect the variable's type and report the attributes for that type. In this case, we defined a variable ``my_variable`` of type ``str``. Calling ``dir(my_variable)`` will product the same result as calling ``dir(str)``:
144
>>> my_variable = 'Just testing'
146
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__',...
151
Now that we've gotten our feet wet, let's write a simple function that returns a friendly message. This is a simple function which takes in one parameter, ``your_name``, and outputs the user's name prefixed with a standard message. Calling your new function is as simple as typing the name of the function and supplying the appropriate variables:
155
>>> def hello_pycogent(your_name):
156
... message = 'PyCogent bids you welcome ' + your_name
159
>>> hello_pycogent('John Smith')
160
PyCogent bids you welcome John Smith
162
Enter each line as you see it and note that white space is important! There are no brackets or keywords to signal blocks of code. Instead, indentation is used to designate related lines of code.
164
Further Python documentation
165
============================
167
Now that you've got Python up and running and know a few commands, it might be useful to `browse the official documentation <http://docs.python.org>`_. There is a comprehensive list of information and some excellent tutorials to work though. There are also many code examples to be found in the `Python cookbook <http://code.activestate.com/recipes/langs/python>`_.