Archive for : April, 2013

One line if statements in Python

Every once in a while it is kind of nice to put a one-liner if statement in a Python program.  I don’t use these very often as they can make the code harder to read.  However I do think they have their place.  Mainly I use these when I need set the value of a variable based on a condition.  So lets say I start with a variable called dummy, and set it to None.

[python]

dummy = None

[/python]

And I want to set dummy to something, but only if dummy is still none, so:

[python]

dummy = (something if dummy is None else dummy)

[/python]

This is the same as writing:

[python]
if dummy is None:
dummy = something
else:
dummy = dummy
[/python]

The one line expression is a much simpler way to express this condition.

Installing MYSQL-python on Mac OS X

I like to do my web development on my MacBook; I find it much easier than working on my Windows machine.  Sometimes things don’t go so well when installing things on my Mac, at least not as easy as they are when installing on Linux.  One of these such things is installing MYSQL-python into a virtual-env for use in my Django apps.  When I run it usually get something like this:

[code]

(thegame)strix:online_app cmoravec$ pip install mysql-python

File "setup_posix.py", line 25, in mysql_config

raise EnvironmentError("%s not found" % (mysql_config.path,))

EnvironmentError: mysql_config not found

[/code]

The answer is actually pretty easy, before running pip (or permanently if you wanted) run:

[code]

export PATH=$PATH:/usr/local/mysql/bin

[/code]

This assumes that your mysql is installed in /usr/local/mysql/bin.  I installed MySQL here to get the binaries and headers so that I could install things that depend on them, I actually use MAMP to run my database.  I understand that if you use the bitnami MAMP then you don’t have to go through this trouble, oh well, Live and Learn!

The most usefully link for information about this is from stackoverflow.