Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather...

29
Python Documentation Release 0.1.0 Joon Ro March 28, 2014

Transcript of Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather...

Page 1: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python DocumentationRelease 0.1.0

Joon Ro

March 28, 2014

Page 2: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",
Page 3: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Contents

1 Cython 31.1 Passing a cdef function to another cdef function as an argument . . . . . . . . . . . . . . . . . . 3

2 date 52.1 Get week number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 matplotlib 73.1 dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.2 LaTeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.3 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73.4 Save to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4 Numpy 94.1 Find indices of duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5 Profiling 115.1 Get the profile stats with cProfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115.2 RunSnakeRun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115.3 KCachegrind with pyprof2calltree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

6 PyTables 136.1 Automatically close already open hdf5 files before opening . . . . . . . . . . . . . . . . . . . . . . 136.2 Generate a hdf5 file from a recarray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136.3 Compress and Compacting PyTables files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136.4 Modifying value in-place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

7 Web Crawling 157.1 Mechanize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

8 Indices and tables 17

i

Page 4: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

ii

Page 5: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

Contents:

Contents 1

Page 6: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

2 Contents

Page 7: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 1

Environment

1.1 Mayavi

Mayavi to use Qt rather than wx.

On Windows PowerShell:

[Environment]::SetEnvironmentVariable("ETS_TOOLKIT", "qt4", "User")

On GNU/Linux:

export ETS_TOOLKIT=qt4

3

Page 8: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

4 Chapter 1. Environment

Page 9: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 2

Cython

2.1 Passing a cdef function to another cdef function as an argument

http://stackoverflow.com/questions/14124049/is-there-any-type-for-function-in-cython

# Define a new type for a function-type that accepts an integer and# a string, returning an integerctypedef int (*f_type)(int, str)

# function to be passed; the same type as f_typecdef int do_stuff(int a, str b):

return 0

# note the f_type type in the argumentcdef void foo(f_type func):

print func(0, "bar")

5

Page 10: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

6 Chapter 2. Cython

Page 11: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 3

date

Python’s strftime directives

3.1 Get week number

import datetimetoday = datetime.date.today()print(today.strftime("%U"))

7

Page 12: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

8 Chapter 3. date

Page 13: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 4

docopt

http://docopt.org

Naval Fate.

Usage:naval_fate ship new <name>...naval_fate ship <name> move <x> <y> [--speed=<kn>]naval_fate ship shoot <x> <y>naval_fate mine (set|remove) <x> <y> [--moored|--drifting]naval_fate -h | --helpnaval_fate --version

Options:-h --help Show this screen.--version Show version.--speed=<kn> Speed in knots [default: 10].--moored Moored (anchored) mine.--drifting Drifting mine.

4.1 Positional argument

Usage: my_program <host> <port>

Words starting with <, ending with > or upper-case words are interpreted as positional arguments.

4.2 Options

-o --option

Words starting with one or two dashes (with exception of -, -- by themselves) are interpreted as short (one-letter) orlong options, respectively.

4.3 Command

All other words that do not follow the above conventions of --options or <arguments> are interpreted as(sub)commands.

9

Page 14: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

10 Chapter 4. docopt

Page 15: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 5

matplotlib

..highlight:: python

5.1 mpltools: Tools for Matplotlib

http://tonysyu.github.io/mpltools/

mpltools provides tools for Matplotlib that make it easier to adjust the style, choose colors, make specialized plots,etc.

5.2 dates

# date formattingyears = mdates.YearLocator() # every yearyearsFmt = mdates.DateFormatter(’%Y’, interval=1)

months = mdates.MonthLocator() # every monthmonthsFmt = mdates.DateFormatter(’%m’, interval=1)

weeks = mdates.WeekdayLocator(byweekday=SU) # every weekdayweeksFmt = mdates.DateFormatter(’%Y-%m-%d’, interval=1)

# format the ticksax.xaxis.set_major_locator(months)ax.xaxis.set_major_formatter(monthsFmt)ax.xaxis.set_minor_locator(weeks)

5.3 LaTeX

import matplotlib.pyplot as pltfrom matplotlib import rc

rc(’text’, usetex=True)

plt.sunplot(111)

plt.text(0.05, 0.90, r’\underline{\alpha}: ’, fontsize=12)

11

Page 16: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

5.4 Subplots

shape = (1, 1)figsize = (10, 6)fig, axs = plt.subplots(*shape,

sharex=False, sharey=False,figsize=figsize,squeeze=False)

ax = axs[0, 0]ax.plot(xdata, ydata, label=’label’, color=’blue’, linestyle=’-’)ax.set_title(’subplot title’)ax.set_xlabel(’xlabel’)ax.set_ylabel(’ylabel’)ax.legend(loc=’lower right’)

fig.suptitle(’figure title’)fig.tight_layout() # narrow margin

5.5 Save to PDF

from matplotlib.backends.backend_pdf import PdfPagespdffile = ’{}.pdf’.format(’path’)pp = PdfPages(pdffile)# pp.savefig can be called multiple times to save to multiple pagespp.savefig()pp.close()

12 Chapter 5. matplotlib

Page 17: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 6

Numpy

6.1 Find indices of duplicates

#----------------------------------------------------------------------def duplicate(arr):

’’’return indices of duplicated entries of a ndarray’’’

ix_dup = ones((arr.shape[0], ), dtype=bool)ix_dup[unique(arr, True)[1]] = False

ix_return = []

for ix in where(ix_dup)[0]:ix_return.append(tuple(where(arr==arr[ix])[0]))

return(ix_return)

13

Page 18: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

14 Chapter 6. Numpy

Page 19: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 7

Profiling

7.1 Get the profile stats with cProfile

import cProfileprof = cProfile.Profile()prof.runcall(func, *args, **kwargs)

Print out the stats:

prof.print_stats()

Dump the profile stats for GUI consumption:

prof.dump_stats(’stats.prof’)

7.1.1 RunSnakeRun to visualize the profile stats

Install RunSnakeRun for GUI view:

$ pip install RunSnakeRun

It needs wxPython:

$ conda install wxpython

Invoke RunSnakeRun:

$ runsnake stats.prof

• It is useful to look at Local column. This gives the time spent on a function itself, excluding the time spentcalling other functions during the function call. Hence it gives the net time spent.

• On the other hand, Cum shows the cumulative time spent on that function, including the time spent for otherfunction calls made during the function call.

7.2 KCachegrind with pyprof2calltree

Install KCachegrind and pyprof2calltree:

15

Page 20: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

$ zypper in kcachegrind$ sudo pip install pyprof2calltree

Invoke KCachegrind through pyprof2calltree:

$ pyprof2calltree -i stats.prof -k

16 Chapter 7. Profiling

Page 21: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 8

PyTables

8.1 Automatically close already open hdf5 files before opening

for hdf5 in [’hdf5_read’, ’hdf5_write’, ’hdf5_append’]:if hdf5 in globals():

globals()[hdf5].close()

8.2 Generate a hdf5 file from a recarray

hdf5 = tables.openFile(’panel.hdf5’, ’w’)

filters = tables.Filters(complib=’blosc’, complevel=5)table = hdf5.createTable(’/’, ’panel’, panel, filters=filters)

8.3 Compress and Compacting PyTables files

http://www.pytables.org/docs/manual-1.4/apc.html#ptrepackDescr

PyTables includes a handy utility called ptrepack which can be very useful not only to compact fragmented files, butalso to adjust some internal parameters in order to use better buffer and chunk sizes for optimum I/O speed.

$ ptrepack --complevel=5 --complib=blosc source.hdf5 target.hdf5

8.4 Modifying value in-place

http://permalink.gmane.org/gmane.comp.python.pytables.user/1703

In order to modify the value,

1. Use cols accessor:

table.cols.number[1] = 3

2. Use modifyRows() method:

17

Page 22: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

row = table[1]row[’number’] = 3table.modifyRows(1, rows=[row])

18 Chapter 8. PyTables

Page 23: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 9

Unicode

9.1 Remove unicode characters from a string

def removeNonAscii(s):return "".join(i for i in s if ord(i) < 128)

Also, You can filter all characters from the string that are not printable using string.printable

19

Page 24: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

20 Chapter 9. Unicode

Page 25: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 10

Web Crawling

10.1 Mechanize

http://stockrt.github.io/p/emulating-a-browser-in-python-with-mechanize/

import mechanizeimport cookielib

# Browserbr = mechanize.Browser()

# Cookie Jarcj = cookielib.LWPCookieJar()br.set_cookiejar(cj)

# Browser optionsbr.set_handle_equiv(True)br.set_handle_gzip(True)br.set_handle_redirect(True)br.set_handle_referer(True)br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

# Want debugging messages?#br.set_debug_http(True)#br.set_debug_redirects(True)#br.set_debug_responses(True)

# User-Agentuseragent = [(’User-agent’,

("Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 ""Firefox/7.0.1"))]

br.addheaders = useragent

21

Page 26: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

22 Chapter 10. Web Crawling

Page 27: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 11

Resources

• The Hitchhiker’s Guide to Python!

• Python Testing Frameworks

23

Page 28: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

Python Documentation, Release 0.1.0

24 Chapter 11. Resources

Page 29: Python Documentation - Read the Docs · CHAPTER 1 Environment 1.1Mayavi Mayavi to use Qt rather than wx. On Windows PowerShell: [Environment]::SetEnvironmentVariable("ETS_TOOLKIT",

CHAPTER 12

Indices and tables

• genindex

• modindex

• search

25