~ubuntu-branches/ubuntu/natty/python-cogent/natty

« back to all changes in this revision

Viewing changes to doc/cookbook/tips_for_using_python.rst

  • Committer: Bazaar Package Importer
  • Author(s): Steffen Moeller
  • Date: 2010-12-04 22:30:35 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20101204223035-j11kinhcrrdgg2p2
Tags: 1.5-1
* Bumped standard to 3.9.1, no changes required.
* New upstream version.
  - major additions to Cookbook
  - added AlleleFreqs attribute to ensembl Variation objects.
  - added getGeneByStableId method to genome objects.
  - added Introns attribute to Transcript objects and an Intron class.
  - added Mann-Whitney test and a Monte-Carlo version
  - exploratory and confirmatory period estimation techniques (suitable for
    symbolic and continuous data)
  - Information theoretic measures (AIC and BIC) added
  - drawing of trees with collapsed nodes
  - progress display indicator support for terminal and GUI apps
  - added parser for illumina HiSeq2000 and GAiix sequence files as 
    cogent.parse.illumina_sequence.MinimalIlluminaSequenceParser.
  - added parser to FASTQ files, one of the output options for illumina's
    workflow, also added cookbook demo.
  - added functionality for parsing of SFF files without the Roche tools in
    cogent.parse.binary_sff
  - thousand fold performance improvement to nmds
  - >10-fold performance improvements to some Table operations

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Tips for using python
3
3
*********************
4
4
 
5
 
*To be written.*
6
 
 
7
 
.. focus on newbies, intro dir and help
8
 
 
 
5
.. sectionauthor:: Doug Wendel
 
6
 
 
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.
 
8
 
 
9
Checking your version
 
10
=====================
 
11
 
 
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.
 
13
 
 
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: 
 
15
 
 
16
::
 
17
    
 
18
    $ python
 
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.
 
22
 
 
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/>`_.
 
24
 
 
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.
 
26
 
 
27
Getting help
 
28
============
 
29
 
 
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:
 
31
 
 
32
::
 
33
 
 
34
    >>> help()
 
35
 
 
36
    Welcome to Python 2.6!  This is the online help utility.
 
37
 
 
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/.
 
40
 
 
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".
 
44
 
 
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".
 
49
 
 
50
    help>
 
51
 
 
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:
 
53
 
 
54
::
 
55
    
 
56
    help> map
 
57
    
 
58
    Help on built-in function map in module __builtin__:
 
59
    
 
60
    map(...)
 
61
    map(function, sequence[, sequence, ...]) -> list
 
62
    
 
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).
 
69
    (END)
 
70
 
 
71
To exit the interactive help mode, simply enter a blank line at the ``help>`` prompt:
 
72
 
 
73
::
 
74
 
 
75
    help>
 
76
 
 
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.
 
81
 
 
82
As the parting message suggests, you can also invoke help on a specific object directly:
 
83
 
 
84
::
 
85
 
 
86
    >>> help(abs)
 
87
 
 
88
    Help on built-in function abs in module __builtin__:
 
89
 
 
90
    abs(...)
 
91
    abs(number) -> number
 
92
 
 
93
    Return the absolute value of the argument.
 
94
    (END)
 
95
 
 
96
To quit help in this case just press ``q``.
 
97
 
 
98
Using the dir() function
 
99
========================
 
100
 
 
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:
 
102
 
 
103
::
 
104
 
 
105
    >>> dir()
 
106
    ['__builtins__', '__doc__', '__name__', '__package__']
 
107
 
 
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:
 
109
 
 
110
::
 
111
 
 
112
    >>> my_variable = 'Just testing'
 
113
    >>> dir()
 
114
    ['__builtins__', '__doc__', '__name__', '__package__', 'my_variable']
 
115
 
 
116
Imported modules will also be reflected in this list:
 
117
 
 
118
::
 
119
 
 
120
    >>> import os
 
121
    >>> import sys
 
122
    >>> dir()
 
123
    ['__builtins__', '__doc__', '__name__', '__package__', 'my_variable', 'os', 'sys']
 
124
 
 
125
``dir()`` can also be used to list the names defined within a module:
 
126
 
 
127
::
 
128
 
 
129
    >>> import sys
 
130
    >>> dir(sys)
 
131
    ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__',...
 
132
 
 
133
It also works on variable types. For example, let's see what attributes the string class has as defined:
 
134
 
 
135
::
 
136
 
 
137
    >>> dir(str)
 
138
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__',...
 
139
 
 
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)``:
 
141
 
 
142
::
 
143
 
 
144
    >>> my_variable = 'Just testing'
 
145
    >>> dir(my_variable)
 
146
    ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__',...
 
147
 
 
148
Hello PyCogent!
 
149
===============
 
150
 
 
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:
 
152
 
 
153
.. doctest::
 
154
    
 
155
    >>> def hello_pycogent(your_name):
 
156
    ...     message = 'PyCogent bids you welcome ' + your_name
 
157
    ...     print message
 
158
    ... 
 
159
    >>> hello_pycogent('John Smith')
 
160
    PyCogent bids you welcome John Smith
 
161
 
 
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.
 
163
 
 
164
Further Python documentation
 
165
============================
 
166
 
 
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>`_.