Python

Availability

We provide python 2.7 and modules from Continuum Analytics (Anaconda)<https://store.continuum.io/cshop/anaconda>`_.

Python modules

Please email support-neuro@berkeley.edu to install or update modules. If you prefer to install a module in your home directory, you may use pip:

pip install --user package_name

You may now access the python packages w/o any further modification.

Background

Ipython Tips and Tricks

  • Ipython: interactive computing environment

  • ? and ?? : allows you to get information about modules, functions, and even see source code:

    In [1]: import os
    
    In [2]: os.getcwd?
    Type:           builtin_function_or_method
    Base Class:     <type 'builtin_function_or_method'>
    String Form:    <built-in function getcwd>
    Namespace:      Interactive
    Docstring:
        getcwd() -> path
        Return a string representing the current working directory.

    An example of ??

    In [5]: os.path.split??
    Type:           function
    Base Class:     <type 'function'>
    String Form:    <function split at 0x26e9f0>
    Namespace:      Interactive
    File:           /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py
    Definition:     os.path.split(p)
    Source:
    def split(p):
        """Split a pathname.  Returns tuple "(head, tail)" where "tail" is
        everything after the final slash.  Either part may be empty."""
        i = p.rfind('/') + 1
        head, tail = p[:i], p[i:]
        if head and head != '/'*len(head):
            head = head.rstrip('/')
        return head, tail
  • run : allows you to run a script:

    In [4]: run myscript.py
  • who, whos : information about variables in workspace:

    In [5]: whos
    Variable     Type        Data/Info
    ----------------------------------
    dictionary   dict        {'a': 1, 'b': 2}
    name         str         this is a string
    os           module      <module 'os' from '/Libra<...>.7/lib/python2.7/os.pyc'>
    times_two    function    <function times_two at 0x122d630>
    x            int         1
    y            int         2
    z            int         3
  • psearch : Search for object in namespaces by wildcard:

    In [3]: psearch os.w*
    os.wait
    os.wait3
    os.wait4
    os.waitpid
    os.walk
    os.write
  • cpaste : Allows you to paste & execute a pre-formatted code block from clipboard. You must terminate the block with ‘–’ (two minus-signs) alone on the line:

    In [6]: cpaste
    Pasting code; enter '--' alone on the line to stop.
    :def times_two(input):
    :    """doubles input and returns result"""
    :    print 'Im in times_two'
    :    return input + 2
    :
    :--
  • hist : hist -r history of what you have typed:

    In [6]: hist -r
    1: pwd
    2: ls
    3: cd ..
    4: run myscript.py
    5: whos
    6: hist -r
  • macro : save lines into macro for reuse:

    In [7]: myname = 'cindee'
    In [8]: myname.upper()
    Out[8]: 'CINDEE'
    In [9]: macro nme 7-8
    Macro `nme` created. To execute, type its name (without quotes).
    Macro contents:
    myname = 'cindee'
    myname.upper()
    In [10]: nme
    -------> nme()
    Out[12]: 'CINDEE'
  • lsmagic : shows all ipython magic functions:

    In [13]: lsmagic
    Available magic functions:
    %Exit  %Pprint  %Quit  %alias  %autocall  %autoindent  %automagic
    %bg  %bookmark  %cd  %clear  %color_info  %colors  %cpaste  %debug
    %dhist  %dirs  %doctest_mode  %ed  %edit  %env  %exit  %hist  %history
    %logoff  %logon  %logstart  %logstate  %logstop  %lsmagic  %macro  %magic
    %p  %page  %paste  %pdb  %pdef  %pdoc  %pfile  %pinfo  %popd  %profile
    %prun  %psearch  %psource  %pushd  %pwd  %pycat  %quickref  %quit  %r
    %rehash  %rehashx  %rep  %reset  %run  %runlog  %save  %sc  %store  %sx
    %system_verbose  %time  %timeit  %unalias  %upgrade  %who  %who_ls
    %whos  %xmode
    
    Automagic is ON, % prefix NOT needed for magic functions.
  • pdb : starts the python debugger:

    In [14]: pdb
    Automatic pdb calling has been turned ON
    
    In [15]: jnk = 'abcdefg'
    
    In [16]: jnk.sort()
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    
    /Users/cindeem/Documents/Research/teaching/myscript.py in <module>()
    ----> 1
          2
          3
          4
          5
    
    AttributeError: 'str' object has no attribute 'sort'
    > <ipython console>(1)<module>()
    
    ipdb>