summaryrefslogtreecommitdiffstats
path: root/software/python.page
blob: 9675873a6e6ac526bdc85dc5cf3b2db5c8a90e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
======================
Python
======================

iPython tricks
--------------

Use the ``cpaste'' command to copy in blocks of raw python code, even if the
indenting is a little weird. 

Style
-------
Python PEP-008 <http://www.python.org/dev/peps/pep-0008/>: Style Guide for
Python Code

pylint <http://pypi.python.org/pypi/pylint>, a Python syntax checker. Very
verbose, use pylint -E (errors only) or at least pylint -r no (no report). Eg,
"pylint -r no file.py -d W0614 -d C -d R".

For docstring documentation, refer to
PEP-257<http://www.python.org/dev/peps/pep-0257/> and the Sphinx
documentation<http://packages.python.org/an_example_pypi_project/sphinx.html
 >; specifically, document script functionality in a top level (above imports,
below any hashbang) docstring.

Use leading "#:" style comments to document important non-object/non-function
element definitions (eg, static variables) in a way that will get pulled out
into Sphinx.  Use "Google-style" function argument/return documentation instead
of "Sphinx style". For example:

    def public_fn_with_googley_docstring(name, state=None):
        """This function does something.

        Args:
            name (str):  The name to use.

        Kwargs:
            state (bool): Current state to be in.

        Returns:
            int.  The return code::

                0 -- Success!
                1 -- No good.
                2 -- Try again.

        Raises:
            AttributeError, KeyError

        A really great idea.  A way you might use me is

        >>> print public_fn_with_googley_docstring(name='foo', state=None)
        0

        BTW, this always returns 0.  **NEVER** use with :class:`MyPublicClass`.

        """
        return 0

autopep8 is a tool to automatically pep8-ify a file:

    sudo pip install autopep8
    autopep8 --in-place --select=W293,W191,W291 *.py

pep8radius is sort of similar, but only applies to code that you are going to
commit (using VCS info).

Packaging
-----------

Flask app packaging advice, including ``MANIFEST.in`` and non-PyPi dependancy
advice: http://flask.pocoo.org/docs/patterns/distribute/

Use ``console_scripts`` in ``setup.py`` to install system-wide scripts:
http://packages.python.org/distribute/setuptools.html#automatic-script-creation

For debian packaging, use [stdeb](http://pypi.python.org/pypi/stdeb)
(via [stackoverflow thread](http://stackoverflow.com/questions/7110604/standard-way-to-create-debian-packages-for-distributing-python-programs)).

"Fucking" String Encoding
---------------------------
(str/unicode errors in python are very prevalent and give me the rage)

The ``codecs`` package has some helpers; see for example
``open(f,mode,encoding)``.

ASCII
----------------
'ord' is the function that takes a single ASCII character and returns the value
number (as an int).

RunSnakeRun
-------------

$ python -m cProfile -o ./dump.profile myscript.py --script-option blah
$ # run to completion or Ctrl-C, then
$ runsnakerun ./dump.profile