Simeon Franklin

Javascript The Good Parts Talk

Modesto Scripting Languages Meetup is up to 22 members and 4 consecutive meetups!

Read More

Posted June 4th, 2012 in Programming (Comment)

What Generators are for

I teach Python classes and enjoy exploring language features from the perspective of newbie's to the language. Usually I can explain the rationale for Python language features by showing a compelling use case. But what about generator functions?

Read More

Posted May 22nd, 2012 in Programming Python (Comment)

Short tutorial on Django and Celery

I filmed a short tutorial on setting up a developer config of Celery with Django. Celery lets you call Python functions as asynchronous tasks, to be run by a separate process. Production usage of Celery involves installing a queue service of some kind (RabbitMQ, etc) but for development use it is possible to just use the Django ORM to store tasks in the database and run the celery daemon as a program in the foreground ala Django's runserver command. Video after the jump...

Read More

Posted April 26th, 2012 in Django Python (Comment)

Top 10 Rookie Mistakes in PHP

At the most recent Modesto Scripting Languages Meetup I presented a talk on the Top 10 Rookie Mistakes in PHP.

Read More

Posted April 26th, 2012 in Programming (Comment)

Fernando Perez and IPython at Baypiggies

Fernando Perez will be presenting at Baypiggies this coming Thursday (7:30 at Symantec Mountain View on Apr 19th, 2012). I'm a big fan of IPython and really enjoyed the IPython sessions I sat in @ PyCon2012 - come by and pick up IPython tips that will make you a more productive programmer daily!

Read More

Posted April 12th, 2012 in Python (Comment)

Getting started with Jython

A student in my Python Fundamentals class asked me to demonstrate how to get started with Jython - this is what I could figure out in 30 minutes...

Read More

Posted April 4th, 2012 in Python (Comment)

Pycon 2012

PyCon 2012 rocks. Dancing robots! Great tutorials, great talks, and 2,200+ Pythonistas!

Read More

Posted March 9th, 2012 in Python (Comment)

List Comprehensions for Modesto Scripting Languages Meetup

Monday night we had our second Modesto Scripting Languages Meetup. This was our first presentations-style meeting and all the presenters did a great job - James did a live demo of building a simple app in, Nathan covered the MVC pattern in web applications and I talked about list processing generally and list comprehensions in Python specifically.

Read More

Posted March 2nd, 2012 in Python (Comment)

Data Visualization with pyprocessing talk

Last Thursday I presented at Baypiggies. I have to say this was not one of my most polished presentations ever - I volunteered at the last minute when we didn't have a speaker. I had started a talk on the port of Processing to Python for PyCon and hadn't finished my sample programs or the talk structure when I didn't make the cut for PyCon. But taking my own advice not to waste a rejection I (sorta) finished up my talk in the couple of days before Baypiggies and filled in. Read More to see the slides and source code for the talk.

Read More

Posted February 28th, 2012 in Programming Python (Comment)

That's me, Python Expert

This is actually an announcement after the fact, but I am now a full time Python instructor for Marakana. Yes, my official job title is now Expert and instead of working as a freelance programmer I teach other programmers how to transition to Python. This has been a huge but welcome shift for me! Let me explain how I got here.

Read More

Posted February 14th, 2012 in Programming Python Technology (Comment)

Announcing the Modesto Scripting Language Meetup

Announcing the Modesto Scripting Languages Meetup. Central Valley Pythonistas, Rubyists, PHPers, etc all welcome.

Read More

Posted January 25th, 2012 in Programming Python (Comment)

Calling all PyCon Rejects

I recently received a very polite email from the PyCon program chair telling me what I already knew: my talk proposal didn't get accepted. My talk was rejected.

Let me explain how I already knew, why being rejected is not a bad thing, and why I'm challenging you to put your rejection to good use both personally and for the Python community.

Read More

Posted December 23rd, 2011 in Programming Python (Comment)

What is "Un-Pythonic"?

Tonight's Baypiggies topic is going to be What is Pythonic - Marilyn Davis is doing the presentation. I had a few thoughts but they're all in the opposite direction. I don't feel authoritative enough to define Pythonicness but as an instructor I frequently get to see code written by students new to Python that is obviously un-pythonic, for lack of a better term.

Read More

Posted December 15th, 2011 in Programming Python (Comment)

AHAH with Django and jQuery

I was recently asked about using AJAX via JQuery with Django and mentioned that I frequently use html fragments and a decorator to add Ajax functionality to existing views. Let's see how that works. (more after the jump).

Read More

Posted August 22nd, 2011 in Django Programming Python (Comment)

PDB Howto

Cool! See Max's awesome video editing skills render my pdb howto relatively stumble free! If you're curious about how to use the built in Python debugger this brief video tutorial should get you going.

Read More

Posted August 11th, 2011 in Programming Python (Comment)

The best way to OR a list of Django ORM Q objects

A co-worker asked me today about the best way to OR together a list of Q objects in the Django ORM. Usually you have a specific number of conditions and you can use Q objects and the bitwise OR operator to logical OR the query conditions together. The Q object is necessary because multiple arguments or successive calls to .filter are ANDed. But what if you have an arbitrary number of Q conditions?

One suggestion is to use the undocumented .add method of the Q object in a loop to add multiple query conditions together. I thought this might be a good use case for reduce and the operator module:

# Normal Usage with the | operator
from django.db.models import Q
qs = MyModel.objects.filter(Q(cond1=1) | Q(cond2="Y"))

#but given a list of Q conditions
from operator import __or__ as OR
lst = [Q(...), Q(...), Q(...)]
qs = MyModel.objects.filter(reduce(OR, lst))

Is this the most Pythonic approach?

Posted June 14th, 2011 in Programming (Comment)

Teaching Retrospective

As you can probably figure out from the last post, I have a new gig.

Thanks to the good folks at I recently taught a Python Fundamentals course in San Francisco.

This was my first time teaching for Marakana and I enjoyed the experience immensely - as did my students judging by their class reviews at the end! I had a blast and am looking forwards to more python classes, including an under-development Pro Django course. More details to come in this space - and thanks to Jas, Brenda, Mike, Chris, and Robert for being great first-time students. I hope you all go on to Pythonic success.

Posted June 14th, 2011 in Programming (Comment)

Python Fundamentals Resources

Deprecated: see the Python Fundamentals Page instead of this post

This will just be a grab bag of extra resources and notes for my students taking the Python Fundamentals Course at Marakana. Don't forget to grab the labs.

Additional Resources

Important Basics You Should Know

Python builtin functions: see the Or

import __builtin__

Keywords: see the docs Or

import keyword


Additional Documentation Referred to in Class

Don't forget the old labs - new labs and samples coming soon!

Posted May 30th, 2011 in Programming (Comment)

WSGI Mysteries

Recently I had a mysterious error cropping up on a Satchmo Store I set up for a client. Every so often I would have an HTTP 500 error with the error log indicating that a template could not be read because the specified charset wasn't available. I edited the Django template loader code to produce more information and only managed to push the exception down into the Python source code.

str =, encoding="utf-8").read()
File "/opt/python2.6/lib/python2.6/", line 865, in open
    file =, mode, buffering)
LookupError: unknown encoding: ANSI_X3.4-1968

The template that was being opened was valid UTF-8 and even stranger - as the same user and using the same virtualenv as the WSGI app I can open a file specifying an ANSI_X3.4-1968 encoding.

It occurred to me that it must be problem with the environment somehow so I spent some time reading the WSGI documentation. Eventually I changed one line in my wsgi config for the app and resolved my problem. The configuration:

    WSGIDaemonProcess sitename user=sitename threads=1 display-name=%{GROUP}
    WSGIProcessGroup sitename
    WSGIApplicationGroup sitename
    # Previous setting:
    # WSGIApplicationGroup %{GLOBAL}

If I understand correctly all my wsgi applications configured using %{GLOBAL} were sharing the same interpreter. Somehow another process must be messing up the state of the interpreter somehow - using WSGIApplicationGroup with a value forces this application to run in its own sub-interpreter and this cleared up my problem. What I don't have is any insight into why my original error was occuring. Any thought?

Posted April 28th, 2011 in Programming (Comment)

Hidden Django QuerySet Features II

Recently while reading a co-workers code I discovered that Django's Queryset can be OR'ed together - but this may not always be a good idea. Django's ORM overloads the bitwise OR operator to express logical OR and the documentation demonstrates this with a Q object. Let's query the ORM to find recently active users - users who joined this year or who have logged in this year:

>>> from django.db.models import Q
>>> from django.contrib.auth.models import User
>>> from datetime import date
>>> jan_1st = date(2011, 1, 1)
>>> recent = User.objects.filter(Q(last_login__gte=jan_1st) 
                             | Q(date_joined__gte=jan_1st))
>>> print(recent._as_sql())

('SELECT U0."id"
  FROM "auth_user" U0
  WHERE (U0."last_login" >= %s
          OR U0."date_joined" >= %s )',
 (u'2011-01-01 00:00:00', u'2011-01-01 00:00:00'))

The where clause in the generated SQL is exactly what we wanted. It turns out, however, that you can use the | operator on querysets directly to yield the same thing.

>>> from django.contrib.auth.models import User
>>> from datetime import date
>>> jan_1st = date(2011, 1, 1)
>>> recent_login = User.objects.filter(last_login__gte=jan_1st)
>>> recent_join = User.objects.filter(date_joined__gte=jan_1st)
>>> recent = recent_login | recent_join
>>> print(recent._as_sql())
('SELECT U0."id"
  FROM "auth_user" U0
  WHERE (U0."last_login" >= %s
          OR U0."date_joined" >= %s )',
 (u'2011-01-01 00:00:00', u'2011-01-01 00:00:00'))

Sweet! It's always bothered me that chained calls were AND'ed together and there was apparently no way to do a simple OR without importing an additional class. However this feature has to be used with care - you are OR'ing entire queries instead of clauses so careless usage might lead to expensive queries. For instance imagine that we want to only look at certain automatically created recent users whose usernames start with "applicant". OR'ing entire querysets works. Sort of.

>>> from django.contrib.auth.models import User
>>> from datetime import date
>>> jan_1st = date(2011, 1, 1)
>>> applicants = User.objects.filter(username__startswith="applicant")
>>> recent_login = applicants.filter(last_login__gte=jan_1st)
>>> recent_join = applicants.filter(date_joined__gte=jan_1st)
>>> recent = recent_login | recent_join
>>> print(recent._as_sql())
('SELECT U0."id"
  FROM "auth_user" U0
   ((U0."username"::text LIKE %s  AND U0."last_login" >= %s )
    (U0."username"::text LIKE %s  AND U0."date_joined" >= %s ))',
  u'2010-01-01 00:00:00',
  u'2010-01-01 00:00:00'))

Note that this did exactly what we asked - OR'ed two querysets. This results in duplication in the where clause - we really only want to run the LIKE search on the username once and then filter the results by OR'ing the two date criterion. The results from the this query will be correct but the execution (depending on your DB backend) may be slower as the LIKE operator is run on the entire data set twice. In my case I noticed an excessively complicated query in the SQL results pane of my django-debug-toolbar. Due to directly OR'ing two complicated querysets four or five expensive operations were being duplicated on a table with a million records. Switching to a Q object produced a much shorter (and faster) query with the same results.

Posted February 20th, 2011 in Programming (Comment)

Older | Newer