github twitter email rss
Python
0001 Jun 1
4 minutes read

Python

https://jyotiska.github.io/blog/posts/python_libraries.html
http://blog.rtwilson.com/my-top-5-new-python-modules-of-2015/

Packaging

Virtual environments venv virtualenv

https://virtualenv.pypa.io/en/latest/userguide.html#usage

virtualenv --system-site-packages
virtualenv env && source env/bin/activate
virtualenv -p python3 env && source env/bin/activate

http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html#command
virtualenvwrapper
workon
mkvirtualenv temp
virtualenv
virtualenvwrapper

https://docs.python.org/3/library/venv.html

python3 -m venv <DIR> && source <DIR>/bin/activate

Adding extrenal site-packages to virtualenv

  • Use path configuration (.pth) file, entries will be appended to the system path.
  • Modify PYTHONPATH (entries from it go to the beginning of system path).
  • Modify sys.path directly from your Python script, i.e. append required folders to it.

virtualenv -p python3 env --system-site-packages

env/lib/python3.5/site-packages/pyqt.pth
import site; site.addsitedir("/usr/local/Cellar/pyqt/4.11.4/lib/python3.5/site-packages")

def enable_global_distpackages():
    import sys
    sys.path.append('/usr/lib/python2.7/dist-packages')
    sys.path.append('/usr/local/lib/python2.7/dist-packages')

And then in init.py file of your package:

enable_global_distpackages()

Documentation

pydoc -g
pydoc3 -b

Search available versions

yolk -V django

Packages, pip

pip search <package_name>
pip install <package_name>

requirements.txt

Flask==0.8
Jinja2==2.6
Werkzeug==0.8.3
certifi==0.0.8
chardet==1.0.1
distribute==0.6.24
gunicorn==0.14.2
requests==0.11.1
git+git://github.com/kennethreitz/requests.git
git+https://user:password@github.com/nsa/secret.git
git+git://github.com/kennethreitz/requests.git@develop
https://site.org/files/package.zip
https://github.com/django/django/tarball/master

#should contain setup.py
./path/to/distribution 

# editable mode, if you make changes to the library
-e ./path/to/distribution #

-r ./path/to/prod-requirements.txt
-e .

setup.py


python setup.py install

Virtual environments, venv, virtualenv

virtualenv env && source env/bin/activate

https://virtualenv.pypa.io/en/latest/userguide.html#usage

virtualenv -p python3 env
source env/bin/activate

http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html#command

virtualenvwrapper
workon
mkvirtualenv temp
virtualenv
virtualenvwrapper

Tips

Swapping variables

x, y = y, x

Trinary operator x ? y : z

[z, y][bool(x)]
bool(x) and y or z

String formatting

w is the width of the field and d is the number of digits after the decimal point.
wd Integer
w.df Floating point notation
w.de Exponential notation

io

’r’ Read from an existing file.
’w’ Write to a file. If filename does not exist, it is created.
’a’ Append to the end of the file.
’r+’ Read to and write from an existing file.
’w+’ Same as ’r+’, but filename is created if it does not exist.
’a+’ Same as ’w+’, but data is appended to the end of the file.

numpy

array(list, type)

.linalg
matrix inversion and solution of simultaneous equations
dot product, inner product, outer product

Class

2 vs 3

read stdin

raw_input() #py2
input()     #py3

reload modules

from imp import reload
reload(module_name)

importing modules

import module
from module import something
dir(module)

modules location

modulename.__file__ 

modules lookup path

import sys
sys.path # PYTHONPATH
__path__ attribute

arguments

complex(real=3, imag=5)
complex(**{'real': 3, 'imag': 5})

list comprehension

['{:#04x}'.format(x) for x in range(256) if x % 2 == 0]

Package

init.py

setup.py

from distutils.core import setup
 
setup(
    name='TowelStuff',
    version='0.1.0',
    author='J. Random Hacker',
    author_email='jrh@example.com',
    packages=['towelstuff', 'towelstuff.test'],
    scripts=['bin/stowe-towels.py','bin/wash-towels.py'],
    url='http://pypi.python.org/pypi/TowelStuff/',
    license='LICENSE.txt',
    description='Useful towel-related stuff.',
    long_description=open('README.txt').read(),
    install_requires=[
        "Django >= 1.1.1",
        "caldav == 0.1.4",
    ],
)

import dis

iPython

import ipdb; set_trace()

%timeit

CPython Memory management

  • Reference counting
  • Garbage collector

http://python3wos.appspot.com/

https://docs.python.org/dev/library/venv.html
https://docs.python.org/3.5/library/asyncio.html
https://docs.python.org/3.5/library/selectors.html
https://docs.python.org/3.5/library/tracemalloc.html

http://www.dabeaz.com/generators-uk/index.html
http://www.dabeaz.com/coroutines/index.html

3.3 3.4

David Beazley: Generators: The Final Frontier - PyCon 2014 https://www.youtube.com/watch?v=D1twn9kLmYg
Generators and Coroutines for Streaming Data Processing - V James Powell http://vimeo.com/53039281

performance

http://hilpisch.com/YH_Performance_Python_Slides.html#/
https://www.quora.com/Why-does-Google-prefer-the-Java-stack-for-its-products-instead-of-Python
GIL в Python: зачем он нужен и как с этим жить https://www.youtube.com/watch?v=AWX4JnAnjBE

plugins

http://stevedore.readthedocs.org/en/latest/

https://docs.python.org/3/library/gc.html
https://docs.python.org/3.4/c-api/index.html
http://patshaughnessy.net/2013/10/24/visualizing-garbage-collection-in-ruby-and-python

pep8
https://wiki.python.org/moin/TimeComplexity

https://docs.python.org/3/reference/index.html

https://docs.python.org/3/reference/datamodel.html
https://docs.python.org/3/reference/expressions.html
https://docs.python.org/3/reference/compound_stmts.html

https://www.python.org/download/releases/2.3/mro/

https://docs.python.org/3/library/index.html

https://docs.python.org/3/library/functions.html
https://docs.python.org/3/library/stdtypes.html
https://docs.python.org/3/library/datatypes.html
    https://docs.python.org/3/library/heapq.html
    https://docs.python.org/3/library/collections.html
https://docs.python.org/3/library/functional.html
    https://docs.python.org/3/library/itertools.html
    https://docs.python.org/3/library/operator.html

https://docs.python.org/3/library/numeric.html

https://docs.python.org/3/library/numbers.html
    https://docs.python.org/3/library/math.html
    https://docs.python.org/3/library/cmath.html

https://docs.python.org/3/library/abc.html
https://docs.python.org/3/library/allos.html

https://docs.python.org/3/library/io.html
https://docs.python.org/3/library/time.html

https://docs.python.org/3/library/importlib.html
https://docs.python.org/3/library/i18n.html

https://docs.python.org/3/library/locale.html

https://docs.python.org/3/library/sys.html

https://docs.python.org/3/howto/index.html

https://docs.python.org/3/c-api/index.html

os.popen
os.system

Packages

  • NumPy, the fundamental package for numerical computation. It defines the numerical array and matrix types and basic operations on them.
    The SciPy library*, a collection of numerical algorithms and domain-specific toolboxes, including signal processing, optimization, statistics and much more.
  • Matplotlib, a mature and popular plotting package, that provides publication-quality 2D plotting as well as rudimentary 3D plotting
  • SymPy, for symbolic mathematics and computer algebra.
  • IPython, a rich interactive interface, letting you quickly process data and test ideas. The IPython notebook works in your web browser, allowing you to document your computation in an easily reproducible form.

  • nltk

  • pil

  • Scikits are extra packages for more specific functionality.

    • scikits.audiolab is a python package for audio file IO using numpy arrays.




printing

print vars(foo)

import pprint
pprint.pprint(globals())
pprint.pprint(locals())

from inspect import getmembers
from pprint import pprint
pprint(getmembers(yourObj))

repr()

https://packaging.python.org/en/latest/index.html

http://learnpythonthehardway.org/book/ex46.html

http://people.csail.mit.edu/pgbovine/python/
http://docs.python.org/library/sqlite3.html
http://code.google.com/p/pyftpdlib/wiki/Tutorial

http://docs.python-requests.org/en/latest/index.html
http://docs.python.org/library/urllib2.html
https://developers.google.com/appengine/docs/python/urlfetch/
http://docs.python.org/library/httplib.html
http://docs.python.org/library/json.html
http://www.makotemplates.org/
http://en.wikipedia.org/wiki/Pylons_(Web_framework)
http://flask.pocoo.org/

http://plumbum.readthedocs.org/en/latest/

A Robot Exclusion Rules Parser for Python http://nikitathespider.com/python/rerp/
http://www.crummy.com/software/BeautifulSoup/
Bottle is a fast, simple and lightweight WSGI micro web-framework for Python. It is distributed as a single file module and has no dependencies other than the Python Standard Library.
http://bottlepy.org/docs/dev/

http://docs.python.org/reference/datamodel.html#types

  • sys
  • os
  • urlib/urlib2
  • time, datetime (and calendar)
  • fileinput
  • stat
  • filecmp
  • glob (to use wildcards)
  • shutil
  • gzip
  • tarfile
  • hashlib, md5, crypt
  • logging
  • curses
  • smtplib and email
  • cmd



call function by name

https://stackoverflow.com/questions/3061/calling-a-function-of-a-module-from-a-string-with-the-functions-name-in-python

import foo
methodToCall = getattr(foo, 'bar')
result = methodToCall()

Back to posts


comments powered by Disqus