Archive for March, 2013

How to avoid *.pyc files?

March 23, 2013 Leave a comment

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

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

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:

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

  -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:

./ 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 next to your script.

Getting started with Python: Tips, Tools and Resources

March 6, 2013 Leave a comment
Categories: python

Problem Solving with Algorithms and Data Structures Using Python

March 6, 2013 Leave a comment

Good news everyone. The book Problem Solving with Algorithms and Data Structures Using Python is available online in HTML format. This is the 2nd edition that came out in 2011.

amazon link

Categories: book, python Tags: ,

Execute command, show its output, get its exit code

March 3, 2013 Leave a comment

You want to execute an external program, show its output, and get its exit code at the end.


import shlex
from subprocess import Popen, PIPE

cmd = "..."
process = Popen(shlex.split(cmd), stdout=PIPE)
process.communicate()    # execute it, the output goes to the stdout
exit_code = process.wait()    # when finished, get the exit code

Example: I used this code with FFmpeg. FFmpeg updates the output on the stdout regularly while doing a conversion. At the end I can ask if the conversion was successful (exit_code == 0) or not.

Categories: python Tags: , ,