Archive

Archive for March, 2013

How to avoid *.pyc files?

March 23, 2013 Leave a comment

Problem
Your project directories are littered with .pyc files. How to tell the interpreter to stop creating them?

Solution
Since Python 2.6:

“Python can now be prevented from writing .pyc or .pyo files by supplying the -B switch to the Python interpreter, or by setting the PYTHONDONTWRITEBYTECODE environment variable before running the interpreter. This setting is available to Python programs as the sys.dont_write_bytecode variable, and Python code can change the value to modify the interpreter’s behaviour.” (tip from here)

So I added the following lines to my .bashrc file:

# don't create .pyc and .pyo files
PYTHONDONTWRITEBYTECODE=True
export PYTHONDONTWRITEBYTECODE

Python 3.2 can save .pyc files in a dedicated subfolder called “__pycache__“.

Categories: python Tags: , ,

PyCon US 2013 videos

March 21, 2013 Leave a comment

The PyCon US 2013 conference just ended. I hope I can attend this conference in the future.

The videos of the presentations are here.

Update #1: the slides are here.

Update #2: I found an efficient way how to watch these videos during the next few weeks.

Categories: python Tags: ,

Profiling your script

March 18, 2013 Leave a comment

This presentation shows three ways for profiling your Python programs. When I have some time I will sum them up here.

Categories: python Tags:

Speeding up Python

March 17, 2013 Leave a comment
Categories: python Tags: ,

Python Central

March 13, 2013 3 comments

pythoncentral-logoI found this site yesterday: http://pythoncentral.org/.

According to the FAQ, Python Central “…serves as a central place for Python enthusiasts, for anything and everything to do with the Python programming language. The content on the site is a combination of documentation of the library, a guide to learning the language, tips and tricks, and recipes.

Categories: python Tags:

PHP or Python?

March 8, 2013 Leave a comment
Categories: python Tags: , ,

docopt: a pythonic command line arguments parser that kicks your ass

March 7, 2013 Leave a comment

Docopt is a parser for command line arguments. There are other alternatives like optparse and argparse but docopt is the king. Why? Because it cannot be any simpler :) All you have to do is write an interface description to your script that is well-known from man pages and from the help of other command line applications.

Example (taken from the docopt site):

Naval Fate.

Usage:
  naval_fate.py ship new <name>...
  naval_fate.py ship <name> move <x> <y> [--speed=<kn>]
  naval_fate.py ship shoot <x> <y>
  naval_fate.py mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate.py -h | --help
  naval_fate.py --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.

Then you simply pass this string to docopt. When you execute the script, the parameters will be parsed by docopt according to the textual description above. Say you launch this script with the following parameters:

./naval_fate.py ship Guardian move 10 50 --speed=20

Docopt will parse it and return the following dictionary:

{"--drifting": false,
 "--help": false,
 "--moored": false,
 "--speed": "20",
 "--version": false,
 "<name>": ["Guardian"],
 "<x>": "10",
 "<y>": "50",
 "mine": false,
 "move": true,
 "new": false,
 "remove": false,
 "set": false,
 "ship": true,
 "shoot": false}

That’s all. You can try docopt online here.

If you got interested, watch the presentation of its author (you will find it on the top of the home page of the project).

It’s available on GitHub, where you can also check out some more examples.

Usage: either install it via pip, or just simply add the file docopt.py next to your script.