Aug 3, 2012

Expert Python Programming: Best practices for designing, coding, and distributing your Python software - Tarek Ziadé - 2008


context, contextlib http://www.doughellmann.com/PyMOTW/contextlib/index.html

Multiple Inheritance Best Practices

__mro__: If __mro__ is available, have a quick look at the code of the constructor of each class involved in the MRO. If super is used everywhere, it is super! You can use it. If not, try to be consistent.

__slots__ : An interesting feature that is almost never used by developers is slots. They allow you to set a static attribute list for a given class with the __slots__ attribute, and skip the creation of the __dict__ list in each instance of the class. They were intended to save memory space for classes with a very few attributes, since __dict__ is not created at every instance.

The latter can be avoided with the "-O" option of the interpreter. In that case, all assertions are removed from the code before the byte code is created, so that the checking is lost.

Anyhow, many DbC libraries exist in Python for people that are fond of it. You can have a look at Contracts for Python.

Another approach towards this is "fuzz testing", where random pieces of data are sent to the program to detect its weaknesses. When a new defect is found, the code can be fixed to take care of that, together with a new test.

The warnings module will warn the user on the first call and will ignore the next calls. Another nice feature about this module is that filters can be created to manage warnings that are impacting the application. For example, warnings can be automatically ignored or turned into exceptions to make the changes mandatory. See http://docs.python.org/lib/warning-filter.html http://docs.python.org/library/warnings.html#available-functions http://django-notes.blogspot.com/2012/05/deprecationwarning-django-14.html

The web location used to find the package is the same as that used by easy_install is http://pypi.python.org/simple, which is a web page not intended for humans that contains a list of package links that can be browsed automatically.

dia
autodia -l python -f manage.py
autodia -l python -d apps/integrations/salesforce/ -r

Munin is a great system-monitoring tool that you can use to get a snapshot of the system health.

Gprof2Dot can be used to turn profiler data into a dot graph. You can download this simple script from http://jrfonseca.googlecode.com/svn/trunk/gprof2dot/gprof2dot.py and use it on the stats as long as Graphviz  is installed in your box

KcacheGrind is also a great vizualization tool to display profile data.

The PyMetrics project from Reg Charney provides a nice script to calculate the cyclomatic complexity

The Twisted framework, which comes with a callback-based programming philosophy, has ready-to-use patterns for server programming. Last, eventlet is another interesting approach, probably simpler than Twisted.

No comments: