Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What are the best python programming patterns and practices you are using every day?

I don't mean only design patterns (there are lot of OO design patterns that can be used in python), I mean pure coding patterns and practices like:

  • the best way to organize modules
  • the best way for data parsing (performance wise)
  • usage of private functions (does this make sense, because you can access private functions outside the class)
  • what are the best modules for GUI, database etc.

Are there any new books or other kind of literature on this matter (2010/2011)?

Of course question can not be answered in one answer or one book, I thought of this question like a discussion on this matter because there is not really much written about python programming patterns and it is a very powerful language.

share|improve this question

closed as too broad by gnat, GlenH7, Giorgio, MichaelT, Kilian Foth Oct 7 '13 at 11:09

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

    
I like reading google style guides just to see what their take on it is. See: google-styleguide.googlecode.com/svn/trunk/pyguide.html –  c00w Jun 5 '11 at 8:47
add comment

1 Answer

Practices

Tests : unit tests, integration tests and functional tests are the way you must do your job.

The most surprising is unit tests is far more complex to do than others because you need to create a code structure that let you replace your dependencies on the framework by fake implementation:

For exemple you can check this code: https://github.com/collective/collective.gallery/tree/master/collective/gallery/tests

utils.py contains fake components I next use in the setup to be sure I only use THE class I'm testing.

Packaging

I'm using 'egg' to package my code and make it easy to use. So you can publish your add-on on the website http://pypi.python.org

To create an egg you can use PasteScript: http://pypi.python.org/pypi/PasteScript

To understand packaging you have to understand this:

  • distutils is just the setup.py file to say what are your dependencies and how to build & install the package
  • setuptools add the easy_install command with pypi as default egg repository (kind of apt-get). Today there is a fork of setuptools named distribute.
  • buildout add a way to get multiple version of the same eggs and it build the system path inside 'scripts' put in a bin folder: http://www.buildout.org/

Performance

No matter what performances are never checked first and this is bad. We are working on performances when there are performances issues.

http://pypi.python.org/pypi/funkload is a great tool to create performance tests.

Python provide tools to profile your code. I let you google that.

Private

Never hide sth to other developers, useless and boring for them. 'property' make your public variable wrapped by accessor&mutator and do the job you want (never broke things)

http://docs.python.org/library/functions.html#property

Database

I'm use to http://pypi.python.org/pypi/psycopg2 with postgresql. Python embed the sqlite binding since 2.5 to build tests

You should also consider that the http://zodb.org/ZODB can be used outside of Zope framework and is one of the oldest nosql database on python.

Books

My favorite one: http://www.packtpub.com/expert-python-programming/book

Favorties packages & tools

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.