User:Jarle Pahr/Python: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
 
(48 intermediate revisions by the same user not shown)
Line 1: Line 1:
=API=
https://pypi.python.org/pypi/restkit
=From MATLAB to Python=
http://mathesaurus.sourceforge.net/matlab-numpy.html
http://phillipmfeldman.org/Python/Advantages_of_Python_Over_Matlab.html
http://vnoel.wordpress.com/2008/05/03/bye-matlab-hello-python-thanks-sage/
http://web.bryant.edu/~bblais/bryant/numerical_computing/python_matlab.pdf
https://ep2013.europython.eu/media/conference/slides/pythonxy-python-next-matlab.pdf
http://abandonmatlab.wordpress.com/
http://web.bryant.edu/~bblais/bryant/numerical_computing/python_matlab.pdf
https://sites.google.com/site/pythonforscientists/python-vs-matlab
'''Loading matlab files:'''
https://sites.google.com/site/kittipat/programming-with-python/howtoimportamatlabmatfileintopython
http://stackoverflow.com/questions/874461/read-mat-files-in-python
http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html
import scipy.io
x = scipy.io.loadmat('path/filename.mat')
'''Optimization:'''
http://stackoverflow.com/questions/49926/open-source-alternative-to-matlabs-fmincon-function
http://scicomp.stackexchange.com/questions/83/is-there-a-high-quality-nonlinear-programming-solver-for-python
http://forum.openopt.org/viewtopic.php?id=205
http://mail.scipy.org/pipermail/scipy-user/2008-December/019183.html
http://forge.scilab.org/index.php/p/fmincont/
http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html
http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin.html
=Scientific computing using Python=
=Scientific computing using Python=


Line 10: Line 60:


Python for scientific computing: Where to start: http://sjbyrnes.com/?page_id=67
Python for scientific computing: Where to start: http://sjbyrnes.com/?page_id=67
Python for scientific computing:
http://fperez.org/py4science/index.html


=Official documentation=
=Official documentation=
Line 18: Line 71:
Python standard library (Python 3.3): http://docs.python.org/3.3/library/
Python standard library (Python 3.3): http://docs.python.org/3.3/library/


Style guide: http://www.python.org/dev/peps/pep-0008/


Python 2. 7 Documentation: http://docs.python.org/2.7/
Python 2. 7 Documentation: http://docs.python.org/2.7/
Line 159: Line 213:


http://www.tutorialspoint.com/python/python_files_io.htm
http://www.tutorialspoint.com/python/python_files_io.htm
=Comparing, identifying & Searching=
http://stackoverflow.com/questions/2116286/python-find-identical-items-in-multiple-lists
http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches
=Sorting=
https://wiki.python.org/moin/HowTo/Sorting/


=Data strutures=
=Data strutures=




Dictionaries
==Alternatives to MATLAB structures==
 
http://docs.scipy.org/numpy/docs/numpy.doc.structured_arrays/
 
http://stackoverflow.com/questions/14922586/matlab-struct-array-to-python
 
http://stackoverflow.com/questions/11637045/complex-matlab-like-data-structure-in-python-numpy-scipy
 
==Dictionaries==
 
Check if key exists in dictionary:
 
key in dict
 
Check if value exists in dictionary:


d = {key:value}
value in d.values()


Lists
==Lists==




Tuples:
==Tuples==


==Arrays==
==Named tuples==




Line 210: Line 294:
http://pythonconquerstheuniverse.wordpress.com/2011/03/28/why-import-star-is-a-bad-idea/
http://pythonconquerstheuniverse.wordpress.com/2011/03/28/why-import-star-is-a-bad-idea/


http://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script


'''Creating modules:'''
'''Creating modules:'''
Line 216: Line 301:


http://docs.python.org/2/tutorial/modules.html
http://docs.python.org/2/tutorial/modules.html
http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html
http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
http://greeennotebook.com/2010/06/how-to-change-pythonpath-in-windows-and-ubuntu/
http://www.bdnyc.org/2012/09/17/editing-pythonpath-to-import-modules/
http://greeennotebook.com/2010/06/using-__name__-in-python/


=NumPy & SciPy=
=NumPy & SciPy=
Line 252: Line 347:


www.biocodershub.net/community/hitchhikers-guide-to-biopython-seqrecords/
www.biocodershub.net/community/hitchhikers-guide-to-biopython-seqrecords/
http://telliott99.blogspot.no/


==Other==
==Other==
Line 286: Line 383:


=Functions/methods=
=Functions/methods=
'''Arguments:'''
http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean


'''Function definition:'''
'''Function definition:'''
Line 374: Line 474:


=Classes=
=Classes=
http://docs.python.org/2/tutorial/classes.html


http://www.tutorialspoint.com/python/python_classes_objects.htm
http://www.tutorialspoint.com/python/python_classes_objects.htm
Line 392: Line 494:
Inheritance in Python: http://www.packtpub.com/article/inheritance-python
Inheritance in Python: http://www.packtpub.com/article/inheritance-python


http://stackoverflow.com/questions/1325673/python-how-to-add-property-to-a-class-dynamically
'''Defining classes:'''
http://www.sthurlow.com/python/lesson08/
http://www.tutorialspoint.com/python/python_classes_objects.htm


'''Creating instances:'''
'''Creating instances:'''
Line 414: Line 523:


http://luisartola.com/software/2010/method-resolution-order-blues/
http://luisartola.com/software/2010/method-resolution-order-blues/
'''Attributes and methods:'''
http://stackoverflow.com/questions/18087030/how-do-we-get-an-optional-class-attribute-in-python




Line 425: Line 538:


=Input/Output=
=Input/Output=
==Execution==
Executing command line commands:
import os
os.system("script.py argument")
Opening files:
os.startfile(filepath)
==Command line input==
http://www.daniweb.com/software-development/python/threads/12326/how-to-do-input-in-python
http://stackoverflow.com/questions/70797/python-and-user-input
==Opening & Reading files==
Open file object methods:
.read() : Returns one line as a string
.readlines() Reads the entire file, returns a list of every line with each line as one string element.
By using a with as block, the file will be automatically closed when the block ends:
with open('workfile', 'r') as f:
  read_data = f.read()
http://docs.python.org/2/tutorial/inputoutput.html
http://en.wikibooks.org/wiki/Python_Programming/Files
Pickle: Module which converts objects to strings.
http://www.diveintopython.net/file_handling/file_objects.html
=Writing to files=


=Interfacing=
=Interfacing=


http://www.pythonforbeginners.com/python-on-the-web/how-to-access-various-web-services-in-python/
http://www.pythonforbeginners.com/python-on-the-web/how-to-access-various-web-services-in-python/
==Databases==
https://www.simple-talk.com/sql/database-administration/python-for-the-sql-server-dba/
http://code.google.com/p/pyodbc/
http://www.biostars.org/p/1288/
https://wiki.python.org/moin/ODBC
http://code.google.com/p/pyodbc/wiki/GettingStarted


=Tips & Tricks=
=Tips & Tricks=
Execute python scripts online: http://www.compileonline.com/execute_python_online.php
Get current working directory:
import os
os.getcwd()
Set current working directory:
os.chdir(aPath)


To stay in the Python interpreter environment after running a script, add - i before the script name:
To stay in the Python interpreter environment after running a script, add - i before the script name:
Line 438: Line 617:


backwards = my_list[::-1]
backwards = my_list[::-1]
Calling file from interpreter command line:
command = "find /tmp -name '*.jpg'"
subprocess.call(command.split())




Line 518: Line 702:
http://stackoverflow.com/questions/1313812/raise-exception-vs-return-none-in-python-functions
http://stackoverflow.com/questions/1313812/raise-exception-vs-return-none-in-python-functions


http://stackoverflow.com/questions/730764/try-catch-in-python-how-to-properly-ignore-exceptions


=Examples=
=Examples=
Line 556: Line 741:


http://stackoverflow.com/questions/4440516/in-python-is-there-an-elegant-way-to-print-a-list-in-a-custom-format-without-ex
http://stackoverflow.com/questions/4440516/in-python-is-there-an-elegant-way-to-print-a-list-in-a-custom-format-without-ex
http://docs.python.org/2/tutorial/inputoutput.html
http://www.shocksolution.com/2011/11/python-string-format-examples/


=Division operations=
=Division operations=


http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch05s06.html
http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch05s06.html
=Plotting & animation=
Several ways to create an animation:
Using easyviz:
import scitools.easyviz as ev
counter = 0
for i in range(10):
    counter +=1
    y = 0.1*i*x
    ev.plot(x,y,'go', axis=[0,100,0,100],savefig ='jpa%04d.png' % counter)   
    ev.show()
    time.sleep(0.5)
ev.movie('jpa*.png')
Using matplotlib.animation:
http://matplotlib.org/api/animation_api.html
http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/
http://stackoverflow.com/questions/16732379/stop-start-pause-in-python-matplotlib-animation
http://scipyscriptrepo.com/wp/?p=9
==Easyviz==
http://folk.uio.no/hpl/easyviz/easyviz.html
http://scitools.googlecode.com/hg-history/e7dc9172a38b8c0c7963315be46b36084d9edd87/doc/api/sphinx-html/movie.html
http://code.google.com/p/scitools/wiki/EasyvizDocumentation
=Performance optimization=
http://www.skymind.com/~ocrow/python_string/

Latest revision as of 15:32, 12 January 2014

API

https://pypi.python.org/pypi/restkit

From MATLAB to Python

http://mathesaurus.sourceforge.net/matlab-numpy.html

http://phillipmfeldman.org/Python/Advantages_of_Python_Over_Matlab.html

http://vnoel.wordpress.com/2008/05/03/bye-matlab-hello-python-thanks-sage/

http://web.bryant.edu/~bblais/bryant/numerical_computing/python_matlab.pdf

https://ep2013.europython.eu/media/conference/slides/pythonxy-python-next-matlab.pdf

http://abandonmatlab.wordpress.com/

http://web.bryant.edu/~bblais/bryant/numerical_computing/python_matlab.pdf

https://sites.google.com/site/pythonforscientists/python-vs-matlab


Loading matlab files:

https://sites.google.com/site/kittipat/programming-with-python/howtoimportamatlabmatfileintopython

http://stackoverflow.com/questions/874461/read-mat-files-in-python

http://docs.scipy.org/doc/scipy/reference/generated/scipy.io.loadmat.html

import scipy.io
x = scipy.io.loadmat('path/filename.mat')

Optimization:

http://stackoverflow.com/questions/49926/open-source-alternative-to-matlabs-fmincon-function

http://scicomp.stackexchange.com/questions/83/is-there-a-high-quality-nonlinear-programming-solver-for-python

http://forum.openopt.org/viewtopic.php?id=205

http://mail.scipy.org/pipermail/scipy-user/2008-December/019183.html

http://forge.scilab.org/index.php/p/fmincont/

http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html

http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fmin.html

Scientific computing using Python

PeptideBuilder: A simple Python library to generate model peptides: https://peerj.com/articles/80/

PDB file parser and structure class implemented in Python: http://bioinformatics.oxfordjournals.org/content/19/17/2308

http://bioinformatics.oxfordjournals.org/content/25/11/1422


Python for scientific computing: Where to start: http://sjbyrnes.com/?page_id=67

Python for scientific computing: http://fperez.org/py4science/index.html

Official documentation

http://www.python.org/

Python standard library (Python 3.3): http://docs.python.org/3.3/library/

Style guide: http://www.python.org/dev/peps/pep-0008/

Python 2. 7 Documentation: http://docs.python.org/2.7/

Python 3.3 tutorial: http://docs.python.org/3.3/tutorial/

Python 3.3 glossary: http://docs.python.org/3.3/glossary.html#glossary

Python on Windows FAQ: http://docs.python.org/3.3/faq/windows.html

Python 3.3 Built-in functions: http://docs.python.org/3.3/library/functions.html#execfile

Learning Python

Beginning Python for bioinformatics Wiki: http://wiki.genedrift.org

http://www.codecademy.com/tracks/python

Bioinformatics Programming Using Python Practical Programming for Biological Data: http://shop.oreilly.com/product/9780596154516.do?

http://python-course.eu/

http://www.python-course.eu/matrix_arithmetic.php

http://www.daniweb.com/software-development/python/114

http://en.wikibooks.org/wiki/Python_Programming

http://cafepy.com/

http://bioinformaticsonline.com/news/view/140/python-education-material

http://interactivepython.org/courselib/static/thinkcspy/index.html

http://www.codeskulptor.org/

Python for developers: http://ricardoduarte.github.io/python-for-developers/

A crash course in Python for scientists: http://nbviewer.ipython.org/5920182

Misc

PyRegex: http://pyregex.com/

http://www.python.org/dev/peps/

http://stackoverflow.com/questions/4583367/how-to-run-multiple-python-version-on-windows

Article: Programming biological models in Python using PySB /doku.php?id=beginning_python_for_bioinformatics

http://ubuntuforums.org/showthread.php?t=127426

http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch20s05.html

http://www.syntagmatic.net/matrix-multiplication-in-python/

Stackoverflow - Python questions: http://stackoverflow.com/questions/tagged/python

Python books: http://wiki.python.org/moin/IntroductoryBooks

Python basic syntax: http://www.tutorialspoint.com/python/python_basic_syntax.htm

http://en.wikipedia.org/wiki/Python_syntax_and_semantics

Simple Python programs: http://wiki.python.org/moin/SimplePrograms

http://www.bin-co.com/blog/2008/03/python-hello-script/

About Python shells: http://python.about.com/od/programmingglossary/g/defshell.htm

Popular Python recipes: http://code.activestate.com/recipes/langs/python/

O'reilly Python devcenter: http://www.onlamp.com/python/

Differences between Python 2 and Python 3

  • Print is changed from a statement (print "something") to a function (print("something"))
  • raw_input() is replaced by input()
  • Integer/float division.

http://wiki.python.org/moin/Python2orPython3

http://docs.python.org/3/whatsnew/3.0.html

http://www.cs.ucsb.edu/~pconrad/cs8/textbooks/MillerRanum/python2.x.issues/

Q&A /How-tos

http://stackoverflow.com/questions/3786881/what-is-a-method-in-python

http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops

http://love-python.blogspot.no/2008/09/remove-duplicate-items-from-list-using.html

http://stackoverflow.com/questions/5711452/how-do-i-slice-a-string-every-3-indices

http://stackoverflow.com/questions/1303347/getting-a-map-to-return-a-list-in-python-3-1

http://stackoverflow.com/questions/674764/examples-for-string-find-in-python

http://stackoverflow.com/questions/8113684/python-itertools-permutations

http://www.petercollingridge.co.uk/python-tricks/list-comprehensions

http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python

http://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks

http://code.activestate.com/recipes/499304-hamming-distance/

http://python.about.com/od/simplerscripts/qt/Getlinebynumber.htm

http://stackoverflow.com/questions/3939361/remove-specific-characters-from-a-string-in-python

http://stackoverflow.com/questions/118260/how-to-start-idle-python-editor-without-using-the-shortcut-on-windows-vista

http://stackoverflow.com/questions/9831097/is-open-read-safe

http://stackoverflow.com/questions/3173915/modification-of-the-list-items-in-the-loop-python

http://stackoverflow.com/questions/4081217/how-to-modify-list-entries-during-for-loop

http://stackoverflow.com/questions/3591807/how-can-i-stop-python-exe-from-closing-immediatly-after-i-get-an-output

http://stackoverflow.com/questions/6513967/running-python-script-from-idle-on-windows-7-64-bit

http://stackoverflow.com/questions/1027714/how-to-execute-a-file-within-the-python-interpreter

http://stackoverflow.com/questions/2545397/converting-a-string-into-a-list-in-python

http://stackoverflow.com/questions/12336105/python-reverse-list

http://stackoverflow.com/questions/4481724/python-convert-list-of-char-into-string

Printing commands:

http://stackoverflow.com/questions/11266068/python-avoid-new-line-with-print-command

http://docs.python.org/2/tutorial/inputoutput.html

http://www.tutorialspoint.com/python/python_files_io.htm

Comparing, identifying & Searching

http://stackoverflow.com/questions/2116286/python-find-identical-items-in-multiple-lists

http://stackoverflow.com/questions/1388818/how-can-i-compare-two-lists-in-python-and-return-matches

Sorting

https://wiki.python.org/moin/HowTo/Sorting/

Data strutures

Alternatives to MATLAB structures

http://docs.scipy.org/numpy/docs/numpy.doc.structured_arrays/

http://stackoverflow.com/questions/14922586/matlab-struct-array-to-python

http://stackoverflow.com/questions/11637045/complex-matlab-like-data-structure-in-python-numpy-scipy

Dictionaries

Check if key exists in dictionary:

key in dict

Check if value exists in dictionary:

d = {key:value}
value in d.values()

Lists

Tuples

Arrays

Named tuples

Applications

http://www.black-glass.com/

Hidden Markov MOdels. http://www.black-glass.com/?p=299

Monte Carlo simulations: http://www.black-glass.com/?p=404

Modules and libraries

TkInter GUI: http://wiki.python.org/moin/TkInter

Automating Microsoft Office with Python: http://nbviewer.ipython.org/urls/raw.github.com/sanand0/ipython-notebooks/master/Office.ipynb


HTseq: http://seqanswers.com/forums/showthread.php?t=4805

http://www.tutorialspoint.com/python/python_modules.htm

Installation:

http://docs.python.org/3.3/install/index.html

http://wiki.python.org/moin/Distutils/Tutorial

http://stackoverflow.com/questions/1471994/what-is-setup-py


Importing modules and libraries:

http://effbot.org/zone/import-confusion.htm

http://docs.python.org/dev/reference/import.html

http://pythonconquerstheuniverse.wordpress.com/2011/03/28/why-import-star-is-a-bad-idea/

http://stackoverflow.com/questions/1186789/what-is-the-best-way-to-call-a-python-script-from-another-python-script

Creating modules:

http://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it

http://docs.python.org/2/tutorial/modules.html

http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html

http://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH

http://greeennotebook.com/2010/06/how-to-change-pythonpath-in-windows-and-ubuntu/

http://www.bdnyc.org/2012/09/17/editing-pythonpath-to-import-modules/

http://greeennotebook.com/2010/06/using-__name__-in-python/

NumPy & SciPy

http://www.numpy.org/

SciPy is a packafe for scientific computing which depends on the NumPy package.

http://www.scipy.org/Topical_Software

SciPy: www.scipy.org/SciPy

Documentation: http://docs.scipy.org/doc/

http://www.scipy.org/more_about_SciPy

Projects using NumPy and/or SciPy: http://www.scipy.org/Projects

http://www.scipy.org/NumPy_for_Matlab_Users

http://scipy-central.org/

NumPy & SciPy FAQ: http://www.scipy.org/FAQ

http://www.youtube.com/watch?v=oYTs9HwFGbY

Biology & Bioinformatics

Biopython:http://biopython.org/wiki/Main_Page

Cock PJ, Antao T, Chang JT, Chapman BA, Cox CJ, et al. (2009) Biopython: freely available Python tools for computational molecular biology and bioinformatics. Bioinformatics 25: 1422–1423.


Biopython tutorial: http://biopython.org/DIST/docs/tutorial/Tutorial.html


www.biocodershub.net/community/hitchhikers-guide-to-biopython-seqrecords/

http://telliott99.blogspot.no/

Other

PyPI - Python package index: https://pypi.python.org/pypi

Python.org - useful modules: http://wiki.python.org/moin/UsefulModules

http://code.google.com/p/pythonxy/

http://matplotlib.org/

PyDoc: http://docs.python.org/2/library/pydoc


PyLab is part of matplotlib: http://stackoverflow.com/questions/12987624/confusion-between-numpy-scipy-matplotlib-and-pylab

iPython Notebook viewer: http://nbviewer.ipython.org/

Development environments

http://code.google.com/p/spyderlib/

http://ipython.org/ (See iPython)

Distributions

EPD free edition: http://www.enthought.com/products/epd_free.php

Includes Python, iPython, IDLE, matplotlib, numpy and SciPy.

WinPython: https://code.google.com/p/winpython/

Includes NumPy, SciPy, matplotlib and Spyder.

Functions/methods

Arguments: http://stackoverflow.com/questions/287085/what-do-args-and-kwargs-mean

Function definition:

http://docs.python.org/3/tutorial/controlflow.html#defining-functions


Docstrings:

http://www.python.org/dev/peps/pep-0257/

String methods:

string.split("delimiter"): Splits a string into a list of strings. Ex: sequenc = ATGCA seq = sequence.split("G") --> seq = ['AT', 'CA']

string.find() :


string.strip([chars]): "Return a copy of the string with the leading and trailing characters removed." Character to be removed are specified by [chars]. If no argument given, the chars argument defaults to removing whitespace.

Array/list methods:

Enumerate: http://docs.python.org/2/library/functions.html#enumerate

list.append(x): Append object x to the end of list.

list.reverse(): Reverse order of elements in a list.

range / xrange(start, stop[,step]): returns a list of plain integers [start, start + step, start + 2 * step, ...]

"".join(list): Join list elements into a string.

Slice: slice(stop) slice(start, stop[, step])

"Returns a slice object representing the set of indices specified by range(start, stop, step)"

dictionary.item() Returns an array of tuples with each tuple consisting of a key/value pair from the dictionary:

map(function,iterable): Apply function to every item in iterable and return the result.


Anonymous functions/lambda functions:

http://www.secnetix.de/olli/Python/lambda_functions.hawk

filter(function, iterable): Constructs a list from those elements of iterable for which the condition in function evaluates to true.


sum(): From official Python documentation: "Sums start and the items of an iterable from left to right and returns the total. Start defaults to 0"

Input/Output (I/O):


From http://www.python.org/dev/peps/pep-0234/ :

" Because the file iterator uses an internal buffer, mixing this

   with other file operations (e.g. file.readline()) doesn't work
   right:


Fore more on iteration over files, see:

http://www.bogotobogo.com/python/python_iterators.php

Class-related functions:

isinstance.

Keywords and statements

in :

with ... as


Indentation

http://stackoverflow.com/questions/119562/tabs-versus-spaces-in-python-programming

http://www.secnetix.de/olli/Python/block_indentation.hawk

http://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation

http://stackoverflow.com/questions/1024435/howto-fix-python-indentation

Classes

http://docs.python.org/2/tutorial/classes.html

http://www.tutorialspoint.com/python/python_classes_objects.htm

http://www.cafepy.com/article/python_types_and_objects/python_types_and_objects.html

http://www.w3resource.com/python/python-object-classes.php

http://en.wikibooks.org/wiki/Python_Programming/Classes

A simple class inheritance example: http://www.daniweb.com/software-development/python/code/216596/a-simple-class-inheritance-example-python


An introduction to classes and inheritance in Python: http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/

http://en.wikibooks.org/wiki/Think_Python/Inheritance

Inheritance in Python: http://www.packtpub.com/article/inheritance-python

http://stackoverflow.com/questions/1325673/python-how-to-add-property-to-a-class-dynamically

Defining classes:

http://www.sthurlow.com/python/lesson08/

http://www.tutorialspoint.com/python/python_classes_objects.htm

Creating instances:

http://stackoverflow.com/questions/2136760/creating-an-instance-of-a-class-with-a-variable-in-python

Class scope:

http://www.codecademy.com/courses/python-intermediate-en-WL8e4/1/2#

Super:

Things to know about Python Super: http://www.artima.com/weblogs/viewpost.jsp?thread=236275


Method Resolution Order:

http://www.cafepy.com/article/python_attributes_and_methods/ch02.html

http://python-history.blogspot.no/2010/06/method-resolution-order.html


http://luisartola.com/software/2010/method-resolution-order-blues/

Attributes and methods:

http://stackoverflow.com/questions/18087030/how-do-we-get-an-optional-class-attribute-in-python


Misc.:

http://stackoverflow.com/questions/3625726/python-error-nameerror-global-name-self-is-not-defined-when-calling-another

http://stackoverflow.com/questions/2424451/compound-assignment-to-python-class-and-instance-variables

http://www.python.org/doc/essays/ppt/acm-ws/sld051.htm

Input/Output

Execution

Executing command line commands:

import os
os.system("script.py argument")

Opening files:

os.startfile(filepath)

Command line input

http://www.daniweb.com/software-development/python/threads/12326/how-to-do-input-in-python

http://stackoverflow.com/questions/70797/python-and-user-input

Opening & Reading files

Open file object methods:

.read() : Returns one line as a string

.readlines() Reads the entire file, returns a list of every line with each line as one string element.

By using a with as block, the file will be automatically closed when the block ends:

with open('workfile', 'r') as f:
  read_data = f.read()


http://docs.python.org/2/tutorial/inputoutput.html

http://en.wikibooks.org/wiki/Python_Programming/Files

Pickle: Module which converts objects to strings.

http://www.diveintopython.net/file_handling/file_objects.html


Writing to files

Interfacing

http://www.pythonforbeginners.com/python-on-the-web/how-to-access-various-web-services-in-python/


Databases

https://www.simple-talk.com/sql/database-administration/python-for-the-sql-server-dba/

http://code.google.com/p/pyodbc/

http://www.biostars.org/p/1288/

https://wiki.python.org/moin/ODBC

http://code.google.com/p/pyodbc/wiki/GettingStarted

Tips & Tricks

Execute python scripts online: http://www.compileonline.com/execute_python_online.php

Get current working directory:

import os
os.getcwd()

Set current working directory:

os.chdir(aPath)

To stay in the Python interpreter environment after running a script, add - i before the script name: python -i script.py

A list can be reversed by the reverse function, or by list slicing with a negative stride:

backwards = my_list[::-1]

Calling file from interpreter command line:

command = "find /tmp -name '*.jpg'"
subprocess.call(command.split()) 


29 common errors: http://pythonforbiologists.com/index.php/29-common-beginner-python-errors-on-one-page/


Style guide: https://www.draftable.com/compare/9spTfcM?utm_source=buffer&utm_campaign=Buffer&utm_content=buffere23fd&utm_medium=twitter

http://pythonarticles.com/?utm_content=buffer08069&utm_source=buffer&utm_medium=twitter&utm_campaign=Buffer

Common newbie mistakes part 1: http://blog.amir.rachum.com/post/54770419679/python-common-newbie-mistakes-part-1

Developing your own scientific Python code: http://www.linuxjournal.com/content/developing-your-own-scientific-python-code?utm_content=buffer1d018&utm_source=buffer&utm_medium=twitter&utm_campaign=Buffer

About iterations: http://nedbatchelder.com/text/iter/iter.html#13

Python on Android

http://kivy.org/#home

http://google-opensource.blogspot.no/2009/06/introducing-android-scripting.html

http://code.google.com/p/android-scripting/

http://qpython.com/

https://ep2013.europython.eu/conference/talks/developing-android-apps-completely-in-python

Packaging

http://guide.python-distribute.org/introduction.html

http://stackoverflow.com/questions/6344076/differences-between-distribute-distutils-setuptools-and-distutils2

https://python-packaging-user-guide.readthedocs.org/en/latest/index.html

http://answers.yahoo.com/question/index?qid=20110705223937AA8vFXz

Installing Python modules: http://docs.python.org/3/install/index.html

Distributing Python modules: http://docs.python.org/3/distutils/index.html

PIP

http://stackoverflow.com/questions/4750806/how-to-install-pip-on-windows

http://en.wikipedia.org/wiki/Pip_%28Python%29

http://stackoverflow.com/questions/3849762/python-3-easy-install-pip-and-pypi

easy_install

  • module bundled with setup_tools

http://peak.telecommunity.com/DevCenter/EasyInstall

http://en.wikipedia.org/wiki/EasyInstall

Distribute

Setuptools

  • Replaced by Distribute

Python Package index (PyPi)

https://pypi.python.org/pypi

Error handling

http://wiki.python.org/moin/HandlingExceptions

http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch17s03.html

http://stackoverflow.com/questions/2052390/how-do-i-manually-throw-raise-an-exception-in-python

http://docs.python.org/3.3/tutorial/errors.html http://stackoverflow.com/questions/1313812/raise-exception-vs-return-none-in-python-functions

http://stackoverflow.com/questions/730764/try-catch-in-python-how-to-properly-ignore-exceptions

Examples

Get index of list element:

    for i,v in enumerate(line_segments):
      print i, v


Use of zip:

x = [1,2,3]
y = [2,3,4]
f = zip(x,y)


String formatting

http://stackoverflow.com/questions/3089038/python-not-all-of-arguments-converted-during-string-formatting

http://stackoverflow.com/questions/7351270/control-a-print-format-when-printing-a-list-in-python

http://stackoverflow.com/questions/15575359/print-list-elements-with-comprehension-python

http://stackoverflow.com/questions/7568627/using-python-string-formatting-with-lists

http://www.velocityreviews.com/forums/t677371-type-float-problem.html

http://stackoverflow.com/questions/9535954/python-printing-lists-as-tabular-data

http://stackoverflow.com/questions/3018295/how-do-i-print-a-table-in-python

http://stackoverflow.com/questions/12078802/python-how-do-i-dynamically-print-a-table-of-strings-in-the-terminal

http://stackoverflow.com/questions/2762058/format-all-elements-of-a-list

http://stackoverflow.com/questions/4440516/in-python-is-there-an-elegant-way-to-print-a-list-in-a-custom-format-without-ex


http://docs.python.org/2/tutorial/inputoutput.html

http://www.shocksolution.com/2011/11/python-string-format-examples/

Division operations

http://www.linuxtopia.org/online_books/programming_books/python_programming/python_ch05s06.html

Plotting & animation

Several ways to create an animation:

Using easyviz:

import scitools.easyviz as ev
counter = 0
for i in range(10):
   counter +=1
   y = 0.1*i*x
   ev.plot(x,y,'go', axis=[0,100,0,100],savefig ='jpa%04d.png' % counter)    
   ev.show()
   time.sleep(0.5)
ev.movie('jpa*.png')

Using matplotlib.animation:

http://matplotlib.org/api/animation_api.html

http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/

http://stackoverflow.com/questions/16732379/stop-start-pause-in-python-matplotlib-animation

http://scipyscriptrepo.com/wp/?p=9

Easyviz

http://folk.uio.no/hpl/easyviz/easyviz.html

http://scitools.googlecode.com/hg-history/e7dc9172a38b8c0c7963315be46b36084d9edd87/doc/api/sphinx-html/movie.html

http://code.google.com/p/scitools/wiki/EasyvizDocumentation


Performance optimization

http://www.skymind.com/~ocrow/python_string/