Real Ultimate Programming

The Home for People Who Like to Flip Out and Write Code

Notes From PyATL 2009-08-20

What follows is essentially a stream-of-consciousness dump from the PyATL meeting on August 20.

Introduction to Python and Sqlite3

Presented by Alfredo Deza

  • For simple things, it’s blazing fast
  • It’s local, so no network necessary
  • Serializes to a single file
  • Uses DB-API 2.0 (see PEP 249). Once you have your cursor, you can start running SQL.
  • NOTE: Use sqlite3.connect(':memory:', isolation_level='exclusive') to use an in-memory DB
  • The iterdump() method of the sqlite3 connection gives you an iterable that lets you export the in-memory database. This is handy if you want to write them all to a file.
  • Although the sqlite3 docs claim that the concurrency situation has improved, the Internet at large seems to indicate that problems still abound.


Presented by Skylar Saveland

  • Recommend you use PostGIS w/ PostgresQL
  • OpenLayers is a nice library for embedding map widgets on any webpage.
  • Core team is available on IRC (#geodjango on Freenode)
  • They make it fairly easy to integrate with Google Maps
  • Instead of standard Django models, you use the one from GeoDjango and then you get the Point, Polygon etc. so that you can store GIS data right with your Django models
  • You can do distance, intersection, touches, etc. queries right in the ORM

Nuts & Bolts

Presented by Brandon Rhodes

  • PyCon is 6 months away

  • String formatting
    • New in 2.6
    • str and unicode objects now have a format() method, and that’s the encouraged way to do string formatting; % is right out.
    • At least one of the benefits of the new method is that it’s easier to read the name-based format
    • It lets you use dot-notation to access attributes of parameters
    • Also supports indexing into lists, etc.
    • I need to look this up, pronto. It’s covered briefly in the sequence types in the standard docs and links to a dedicated page for the new syntax.
  • lxml != ElementTree
    • The goal of ElementTree is to be a Pythonic XML library
    • It’s probably a dead project, since the current version is 2 years old
    • lxml uses the “ElementTree” object model, but it’s built on top of libxml2 and libxslt so it’s blazing fast.
    • Remember these two imports:
      • from lxml import etree
      • from lxml.cssselect import CSSSelector as css
    • lxml also supports CSS selectors
    • Don’t forget the default for lxml is an XML parser; you want the HTML parser
    • Ian Bicking says lxml is better than BeautifulSoup

Making Code More Testable

Presented by Brandon Rhodes

  • Based on Writing Testable Code; showing Python examples of how some of these actually look when you put them in action
Don’t Mix Object Graph Construction with Application Logic
  • This one makes it really hard to test classes in isolation, because instantiating one starts instantiating a bunch of objects we don’t want to test.
  • On a side note, nose handily shows you captured stdout when a test fails, and doesn’t show you when the test is successful. That way you can you leave your debugging print statements. It does not capture stderr.
  • The simple fix for this in Python is to accept dependencies in the __init__ method. You might recognize this as constructor-based dependency injection. In Java, mutator-based dependency injection is more common.
Avoid “Global State”
  • Even though every programmer (should) know global state is evil, you still see this quite often in web apps where the global state is something like the request and/or session.
  • The breakage is quite often far more extensive than you would expect, e.g., a test in one file that doesn’t handle global state well can break tests in other files, even if they are doing a good job of handling the global state.
  • The fix is to pass the state explicitly, even if the state has to be passed to a lot of functions. This is inconvenient at times, but is usually worth it.

Back to flipping out…