<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="/topics-files/atom2xhtml.xsl" type="text/xsl"?>
<!-- This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers.
This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers.
This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers.
This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers.
This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers.
This is a 512 byte XML comment that one must put into XML Atom feeds
such that browsers like Firefox 2.0 and IE7 will obey the XSL stylesheet.
Everybody hates overbearing browsers. -->
<feed xmlns="http://www.w3.org/2005/Atom"
><title
>Blog@Case Topics: Python</title
><link rel="self" href="http://blog.case.edu/topics/Python"
 /><id
>http://blog.case.edu/topics/Python</id
><category term="Python" label="Python"
 /><link rel="related" href="http://blog.case.edu/topics/python" title="python"
 /><link rel="related" href="http://blog.case.edu/topics/projects" title="projects"
 /><link rel="related" href="http://blog.case.edu/topics/programming" title="programming"
 /><link rel="related" href="http://blog.case.edu/topics/case" title="case"
 /><link rel="related" href="http://blog.case.edu/topics/google" title="google"
 /><link rel="related" href="http://blog.case.edu/topics/pagoda" title="pagoda"
 /><link rel="related" href="http://blog.case.edu/topics/geopy" title="geopy"
 /><link rel="related" href="http://blog.case.edu/topics/kde" title="kde"
 /><link rel="related" href="http://blog.case.edu/topics/summer%20of%20code" title="summer of code"
 /><link rel="related" href="http://blog.case.edu/topics/cleveland" title="cleveland"
 /><link rel="related" href="http://blog.case.edu/topics/clepy" title="clepy"
 /><contributor
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></contributor
><updated
>2007-09-26T04:07:35Z</updated
><entry
><title
>September Projects</title
><link href="http://blog.case.edu/bmb12/2007/09/september_projects"
 /><id
>http://blog.case.edu/bmb12/2007/09/september_projects</id
><published
>2007-09-26T03:45:29Z</published
><updated
>2007-09-26T04:07:35Z</updated
><category term="Pagoda" label="Pagoda"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="geopy" label="geopy"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>I'm in the situation 
<a href="http://blog.ianbicking.org">Ian Bicking</a> was in not long ago&#8212;I'm really tired of this blog design and software and it's making me not want to post any of the entries I have pending. This blog will soon redirect to something better. 
<a href="http://pagodacms.org/">Pagoda</a> should have a Developer Preview in October. Check out 
<a href="http://exogen.case.edu/clepy_pagoda/">my presentation</a> from the September meeting of the 
<a href="http://clepy.org/">Cleveland Python interest group</a>. Remember how Ian and I spent months thinking up hundreds of names for our company? We are now incorporated as 
<a href="http://unstoppablerocket.com">Unstoppable Rocket</a>&#8212;one of the first names that was suggested. If you've been following the 
<a href="http://groups.google.com/group/geopy">geopy list</a>, you've heard about the new release coming out. It should make things much more flexible and extendable, and fix all the issues from the past year or so. geopy 0.99 will be out this week. The geopy update is also getting me back into the 
<a href="http://exogen.case.edu/crime/recent/">campus crime map</a> and my Case geocoder service, which is going to be really smart. Updates there soon. I started a new project called Revisionist, which is like 
<a href="http://blog.case.edu/bmb12/2007/06/pagoda_revisions">Pagoda's revision model</a> except generalized and using SQLAlchemy 0.4. I'm hoping other people will be interested in using and improving such a project. With the right helpers it should make revisioning complex models really easy. If anyone has any neat suggestions for what Gary or I should talk about at the October Clepy meeting, let me know.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Content types in Pagoda, Part 1: The Model</title
><link href="http://blog.case.edu/bmb12/2007/07/content_types_in_pagoda_part_1_the_model"
 /><id
>http://blog.case.edu/bmb12/2007/07/content_types_in_pagoda_part_1_the_model</id
><published
>2007-07-04T20:31:00Z</published
><updated
>2007-07-05T00:10:42Z</updated
><category term="Pagoda" label="Pagoda"
 /><category term="Python" label="Python"
 /><category term="SQLAlchemy" label="SQLAlchemy"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<p>Any 
<a href="http://en.wikipedia.org/wiki/Content_management_system">content management system</a> will inevitably have to think about having different 
<strong>content types</strong>. Common content types include pages, attachments, calendars, events, and blog articles. Why make the distinction between different things that all appear as "pages" to the user? Because, of course, different content types must support different features and respond to different actions. For example, an event content type must have a date in order to show up on a calendar, and a calendar content type might support an iCalendar feed of its events.</p>
<p>Likewise, content types will all share similar features and actions. They all have a URL and a title. And if we take the common CMS approach of making a site as a hierarchy of objects, they all have a parent object and child objects. While sites might not be inherently hierarchical (URLs are just identifiers!), it's quite natural to create them this way&#8212;for example, if we move a page, we'd expect its entire tree of child pages to move with it.</p>
<p>One of the first things a web developer does when starting a project is to model its content types. Read any MVC (or, ahem, 
<a href="http://www2.jeffcroft.com/blog/2007/jan/11/django-and-mtv/">MTV</a>) web framework tutorial and there will be a Wiki model, or a Blog model, or a TodoList model&#8212;all content types. In this article I'll be talking about what it currently looks like to model a content type in 
<a href="http://pagodacms.org/">Pagoda</a>. Since Pagoda is based on 
<a href="http://www.pagodacms.org">TurboGears</a>, our goal is to make building your app alongside Pagoda no different than building your app with 
<a href="http://www.turbogears.org">TurboGears</a>, and so far I think we've done a pretty good job. (And by the way, since we're using SQLAlchemy, this part of Pagoda is 
<a href="http://www.blueskyonmars.com/2007/06/27/turbogears-2-a-reinvention-and-back-to-its-roots/">TG2 future-proof</a>.)</p>
<p>So, if it's supposed to be the same as just using TurboGears, why do I have to show you anything? The answer is that while you don't have to design your model with Pagoda in mind (existing apps will coexist just fine), doing so will make your model easily 
<a href="http://blog.case.edu/bmb12/2007/06/pagoda_revisions">localizable and revisionable</a>! That's a pretty big benefit in the world of content management. We'll be able to restore old content records from any point in their history, and make changes to locale-independent fields for all translations at once.</p>
<p>So, on to the code. I'll be modeling a simple Event content type. First, here's how you might do it with some plain old TurboGears and SQLAlchemy.</p>
<pre>
<code>
from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
from turbogears.database import metadata, session
from datetime import datetime

event_table = Table('event', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('start_date', DateTime, nullable=False, default=datetime.now),
    Column('end_date', DateTime, nullable=True),
    Column('title', Unicode(200), nullable=False),
    Column('url_slug', String(75), nullable=False),
    Column('description', TEXT, nullable=False, default=""),
    Column('show_in_calendar', Boolean, nullable=False, default=True)
)

class Event(object):
    def move_to_date(self, new_date):
        self.start_date = new_date
        if self.end_date:
           time_delta = new_date - self.start_date
           self.end_date += time_delta

assign_mapper(session.context, Event, event_table)
</code>
</pre>
<p>So, a pretty standard model with minimal event features. (One thing might not be obvious&#8212;the 
<code>url_slug</code> field is the short 
<a href="http://en.wikipedia.org/wiki/Latin-1">Latin-1</a> name of the event we'll show in the URL). Using the mapped Event class to use the model looks like this...</p>
<pre>
<code>
# Make an event.
bday_party = Event(start_date=datetime(2007, 10, 30, 19, 30),
    title="Brian turns twenty-three!", url_slug="brian_turns_23",
    description="The party will take place in my underwater hideout.",
    show_in_calendar=True)

# End at midnight!
bday_party.end_date = datetime(2007, 10, 31)

# Write it!
session.flush()
</code>
</pre>
<p>Now, how would it change with support for translations, revisions, and having parent and child objects? The first step is to split the table up into locale-dependent and locale-independent tables. If you read about our 
<a href="http://blog.case.edu/bmb12/2007/06/pagoda_revisions">localizable revision</a> model you'll see that this is how we support independently revisioned translations while avoiding data duplication. Here's what the two tables look like that will replace 
<code>event_table</code>:</p>
<pre>
<code>
event_generic_table = Table('event_generic', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('start_date', DateTime, nullable=False, default=datetime.now),
    Column('end_date', DateTime, nullable=True),
    Column('url_slug', String(75), nullable=False),
    Column('show_in_calendar', Boolean, nullable=False, default=True)
)

event_localized_table = Table('event_localized', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('title', Unicode(200), nullable=False),
    Column('description', TEXT, nullable=False, default="")
)
</code>
</pre>
<p>Since we want to support translations for the fields in 
<code>event_localized_table</code>, let's also add a 
<code>locale</code> field in order to tell the translations apart. 
<code>locale</code> will be a short identifier like "en-US", "fr", or "jp".</p>
<pre>
<code>
event_localized_table = Table('event_localized', metadata,
    Column('event_id', Integer, primary_key=True),
    Column('title', Unicode(200), nullable=False),
    Column('description', TEXT, nullable=False, default=""),
    
<strong>Column('locale', String(25), nullable=False)</strong>
)
</code>
</pre>
<p>The next step is to point both tables at Pagoda's revision table in order to support revisions. Since each event record points to a unique revision record, our primary key is now redundant, and can be changed to the revision's ID:</p>
<pre>
<code>
<strong>from pagoda.models import Revision</strong>

event_generic_table = Table('event_generic', metadata,
    
<strong>Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),</strong>
    Column('start_date', DateTime, nullable=False, default=datetime.now),
    Column('end_date', DateTime, nullable=True),
    Column('url_slug', String(75), nullable=False),
    Column('show_in_calendar', Boolean, nullable=False, default=True)
)

event_localized_table = Table('event_localized', metadata,
    
<strong>Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),</strong>
    Column('title', Unicode(200), nullable=False),
    Column('description', TEXT, nullable=False, default=""),
    Column('locale', String(25), nullable=False)
)
</code>
</pre>
<p>A column type of 
<code>None</code> here will cause SQLAlchemy to use the column type of the foreign key&#8212;almost always what you want. There's one more change to make. Since Pagoda helps manage your site's content hierarchy, it already has a table to hold the URL of every object on the site. So we can get rid of the 
<code>url_slug</code> field&#8212;Pagoda will include its own when we tell it about this content type. Our final tables:</p>
<pre>
<code>
from pagoda.models import Revision

event_generic_table = Table('event_generic', metadata,
    Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),
    Column('start_date', DateTime, nullable=False, default=datetime.now),
    Column('end_date', DateTime, nullable=True),
    Column('show_in_calendar', Boolean, nullable=False, default=True)
)

event_localized_table = Table('event_localized', metadata,
    Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),
    Column('title', Unicode(200), nullable=False),
    Column('description', TEXT, nullable=False, default=""),
    Column('locale', String(25), nullable=False)
)
</code>
</pre>
<p>Just a few more small changes! Since we have two different tables, and are now adding some more tables (like Revision) into the mix, we need to join them somehow for SQLAlchemy to map against the resulting join. Pagoda has a function called 
<code>revisioned_table</code> that will perform the necessary joins. Just tell it about your two tables and give it an alias:</p>
<pre>
<code>
from pagoda.models import Revision
<strong>, revisioned_table</strong>

...

<strong>event_table = revisioned_table('event', event_generic_table, event_localized_table)</strong>
</code>
</pre>
<p>
<code>event_table</code> is now a Selectable according to SQLAlchemy. Let's map against it! Pagoda uses a 
<a href="http://www.sqlalchemy.org/docs/adv_datamapping.html#advdatamapping_extending">mapper extension</a> to help with querying and modifying revisioned records. You can add 
<code>pagoda.models.RevisionableMapperExtension</code> to the mapper yourself, or you can use our helper called 
<code>revision_mapper</code> to do it. 
<code>revision_mapper</code> is a small wrapper around 
<code>assign_mapper</code> that makes sure the mapper extension is there, and gives the mapped class methods some more helpful docstrings.</p>
<pre>
<code>
from pagoda.models import Revision, revisioned_table
<strong>, revision_mapper</strong>

...

<strong>revision_mapper(session.context, Event, event_table)</strong>
</code>
</pre>
<p>Last change! Since Event is now revisioned, it would be nice to have some helpful methods for dealing with revisions, like querying for the latest published revision or creating a new revision based on a previous revision. Pagoda has a base class for your mapped class that will give it a few such methods. Just subclass your mapped class from 
<code>Revision</code>:</p>
<pre>
<code>
class Event(
<strong>Revision</strong>):
    def move_to_date(self, new_date):
        self.start_date = new_date
        if self.end_date:
           time_delta = new_date - self.start_date
           self.end_date += time_delta
</code>
</pre>
<p>And that's all it takes to support revisions. 
<code>Event</code> works just like before, except it now has some more methods and fields. A 
<code>url</code> column came from Pagoda's Node table, 
<code>content_id</code> and 
<code>content_type</code> came from Pagoda's Content table, and Revision's columns came along too. Note that no columns were added to either Event table&#8212;these additional fields came from joins. Using it looks much the same as before:</p>
<pre>
<code>
bday_party = Event(start_date=datetime(2007, 10, 30, 19, 30),
    title="Brian turns twenty-three!", 
<strong>url</strong>="brian_turns_23",
    description="The party will take place in my underwater hideout.",
    show_in_calendar=True
<strong>, locale='en', content_type='event',
    revision_author="brian"</strong>)

revised_bday_party = bday_party.new_revision(title="Brian gets older")
revised_bday_party.publish()

session.flush()

# revised_bday_party is now "active" - the latest published revision

calendar_events = Event.select_active_by(show_in_calendar=True)

from datetime import datetime, timedelta
yesterday = datetime.today() - timedelta(days=1)
events_as_they_were_yesterday = Event.filter_snapshot(
    yesterday
).select_by(show_in_calendar=True)

</code>
</pre>
<p>Here's the final code. It's just a couple more lines than the original model at the beginning of this article:</p>
<pre>
<code>
from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
from turbogears.database import metadata, session
from datetime import datetime
from pagoda.models import Revision, revisioned_table, revision_mapper

event_generic_table = Table('event_generic', metadata,
    Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),
    Column('start_date', DateTime, nullable=False, default=datetime.now),
    Column('end_date', DateTime, nullable=True),
    Column('show_in_calendar', Boolean, nullable=False, default=True)
)

event_localized_table = Table('event_localized', metadata,
    Column('revision_id', None, primary_key=True,
        ForeignKey(Revision.c.revision_id)),
    Column('title', Unicode(200), nullable=False),
    Column('description', TEXT, nullable=False, default=""),
    Column('locale', String(25), nullable=False)
)

event_table = revisioned_table('event', event_generic_table, event_localized_table)

class Event(Revision):
    def move_to_date(self, new_date):
        self.start_date = new_date
        if self.end_date:
           time_delta = new_date - self.start_date
           self.end_date += time_delta

revision_mapper(session.context, Event, event_table)
</code>
</pre>
<p>So, hopefully those changes to the original Event weren't too jarring. Sure we could make many of those changes automatically, but we're trying to avoid magic in favor of small helpers, each extending the model The SQLAlchemy Way. If you think all this is too much work, let us know! We want this to be fun to hack on for everyone, not just us.</p>
<p>Next time I'll talk about content type controllers.</p>
</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>SQLAlchemy Bundle for TextMate</title
><link href="http://blog.case.edu/bmb12/2007/06/sqlalchemy_bundle_for_textmate"
 /><id
>http://blog.case.edu/bmb12/2007/06/sqlalchemy_bundle_for_textmate</id
><published
>2007-06-30T06:36:58Z</published
><updated
>2007-06-30T20:56:27Z</updated
><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>We've been using a lot of 
<a href="http://www.sqlalchemy.org">SQLAlchemy</a> here in 
<a href="http://www.pagodacms.org">Pagoda-land</a>. Not long after I started using 
<a href="http://www.macromates.com">TextMate</a>, I started making all kinds of shortcuts for common SQLAlchemy constructs. Pretty soon models were flying out of our fingertips left and right. Anyway, now you can download my 
<a href="http://www.pagodacms.org/files/SQLAlchemy_Bundle.tar.gz">SQLAlchemy TextMate bundle</a>. Just extract that file and drag the resulting bundle onto TextMate to install it. There are currently 8 Snippets and 2 Templates, a few of which are demonstrated below. Here's a quick little screencast where I make a few related tables using a Template and some Snippets. As you can see I've still got revisions on the brain. There's no talking, just some music. It's a minute and a half long. You have a minute, right? 
<a href="http://blog.case.edu/bmb12/2007/06/30/SQLAlchemy_Bundle.mov">
<img alt="alchemy-thumbnail.png" src="http://blog.case.edu/bmb12/2007/06/30/alchemy-thumbnail.png" width="463" height="365" />
</a> Ideas for additions and improvements are always welcome.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Tux Droid Presentation</title
><link href="http://blog.case.edu/bmb12/2007/06/tux_droid_presentation"
 /><id
>http://blog.case.edu/bmb12/2007/06/tux_droid_presentation</id
><published
>2007-06-05T03:55:32Z</published
><updated
>2007-06-05T04:02:54Z</updated
><category term="Clepy" label="Clepy"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Tonight at 
<a href="http://clepy.org/">Clepy</a> I gave a presentation about 
<a href="http://www.tuxisalive.com">Tux Droid</a>. A few months ago I received an offer to test and keep a free Tux Droid if I promised to do cool stuff with it. A while after responding and talking about 
<a href="http://wiki.case.edu/Project_Club">Case's sweet hacker club</a> it arrived in the mail! The presentation isn't much without the demonstrations, but the slides are online at 
<a href="http://exogen.case.edu/tux/">exogen.case.edu/tux</a>. When I come up with some nice polished Tux programs you'll hear about them here. Tux is available in 
<a href="http://wiki.case.edu/Project_Club">Project Club</a> for anyone to play with.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Project and apartment updates</title
><link href="http://blog.case.edu/bmb12/2007/05/project_and_apartment_updates"
 /><id
>http://blog.case.edu/bmb12/2007/05/project_and_apartment_updates</id
><published
>2007-05-04T06:49:21Z</published
><updated
>2007-05-04T08:02:02Z</updated
><category term="Cleveland" label="Cleveland"
 /><category term="Pagoda" label="Pagoda"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Some interesting bits from the past few weeks... Next Tuesday I'll be having lunch with Mike Cermak, webmaster for the 
<a href="http://www.gcrta.org">Greater Cleveland Regional Transit Authority</a>. In 
<a href="http://blog.case.edu/bmb12/2007/04/quicker_cleveland_rta_schedule_with_django">my previous entry</a> I mentioned my 
<a href="http://exogen.case.edu/rta/">RTA Schedule</a> project which has been gaining popularity. There were only a few routes listed on there when I posted it, and the list has been growing as people have been using the route adder. Mike wants to work together to come up with ideas and improvements that will encourage projects like mine&#8212;a very cool response, and beneficial to RTA users as well. I'm looking forward to it! Remember 
<a href="http://blog.case.edu/bmb12/2007/04/multiple_sites_one_python_pagoda_import_tricks">those wacky import tricks I posted about</a> to get multiple database engines working nicely in 
<a href="http://www.pagodacms.org">Pagoda</a>? After coming up with that, Ian dug around to figure out what changes would be necessary to not have to do that. He narrowed it down to one single line of code in TurboGears! In 
<a href="http://svn.turbogears.org/branches/1.0/turbogears/database.py">
<tt>turbogears.database</tt>
</a>:
<pre>
<code>def create_session():
    "Creates a session with the appropriate engine"
    return sqlalchemy.create_session(bind_to=get_engine())</code>
</pre>That 
<tt>bind_to</tt> argument is totally unnecessary when using 
<tt>DynamicMetaData</tt>! Changing that to just use SQLAlchemy's 
<tt>create_session</tt> without arguments makes multiple database engines possible without any black magic. Unfortunately, we didn't notice 
<a href="http://www.blueskyonmars.com/2007/05/02/turbogears-102-released/">TurboGears 1.0.2</a> about to be released and didn't start any discussion about changing this in time. For now we use this little monkeypatch:
<pre>
<code>session_context = turbogears.database.session.context
session_context.registry.createfunc = sqlalchemy.create_session</code>
</pre>So I think it works more like 
<a href="http://cheeseshop.python.org/pypi/Alchemyware">Alchemyware</a> now, except we don't have to write models any differently and the engines are cached. The metadata is simply pointed to the appropriate engine in each thread. Speaking of 
<a href="http://www.pagodacms.org">Pagoda</a>, we're still at least a couple weeks away from a beta release. We're currently writing glue for all the little bits and pieces we've created over the past couple months. We've satisfied many of our original goals and learned more about (and sometimes changed) others. I'll share more about these satisfied and modified goals later. Pagoda's third contributor, 
<a href="http://www.cshesse.com">Chris</a>, moved back home to start hunting for jobs in the California area. Good luck, Chris! Chris is a fine electrical engineer and programmer and you should hire him. This was his plan since starting to help with Pagoda, so it doesn't really affect our development schedule. After receiving practically no feedback from the release of 
<a href="http://code.google.com/p/dmath/">dmath</a>, there has been a small surge of interest recently, with a couple contributions, so there will likely be a new release. I put up a 
<a href="http://cheeseshop.python.org/pypi/dmath">new egg</a> of the old version on the Cheese Shop after learning that the Python 2.5 version was busted. 
<a href="http://exogen.case.edu/projects/geopy/">geopy</a> continues to receive patches; recently the most-requested improvement was contributed by 
<a href="http://latteier.com/">Amos Latteier</a> and that is the removal of 
<tt>print</tt> chatter in favor of logging. I'll get 0.94 out this weekend with that and other improvements. Since Chris moved out, our friend Greg moved in with me and Sara. Greg went to school for art and likes to paint and draw, and might even prove his cooking talents at culinary school next semester. I'll be helping him make a website for his comics, which are very funny, but I can't decide if it's because I know Greg and imagine him coming up with them, which itself makes me laugh. You'll be the judge soon enough... There are two more new, smaller residents of our apartment as well... one's a 14-inch 
<a href="http://en.wikipedia.org/wiki/Oscar_%28fish%29">Oscar cichlid</a> and the other's a 15-inch 
<a href="http://en.wikipedia.org/wiki/Plecostomus">Plecostomus</a>. They're friendly and big! Now I have fantasies about getting them a bigger aquarium with all manner of luxuries. I picked them up from someone who's graduating and they came with their 45-gallon home and necessities for free! I'll post some pictures of these guys soon.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Multiple sites, one Python: Pagoda import tricks</title
><link href="http://blog.case.edu/bmb12/2007/04/multiple_sites_one_python_pagoda_import_tricks"
 /><id
>http://blog.case.edu/bmb12/2007/04/multiple_sites_one_python_pagoda_import_tricks</id
><published
>2007-04-17T23:57:24Z</published
><updated
>2007-04-18T00:30:43Z</updated
><category term="Pagoda" label="Pagoda"
 /><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>One of our early goals when designing 
<a href="http://www.pagodacms.org">Pagoda</a> was to allow a single Pagoda instance to support multiple sites. This was due to the way memory works for web servers running on Python and TurboGears. How exactly this adds up depends on your threading and web server configuration (mod_python), but traditionally hosting multiple sites means running at least one Python instance per site, each costing 10-20 MB. The more modules each instance loads, the higher the memory usage, and since Pagoda sites will likely use a bunch of modules, that adds up. The most limiting factor in many hosting services is the amount of memory your account is allowed to consume. Obviously if each Pagoda site is large and running custom code, it might be a good idea to run each in its own Python instance, so one site can't bring down all the others. But the common case, we think, is a bunch of moderately sized sites using just the built-in page management tools. So we devised some ways to allow multiple sites to run from one TurboGears project... The first and simplest plan involved a database model, where pages and other table rows point to whichever site they belong to. You probably already know why this is a bad idea. First of all, every single table in the database needed to have a site_id column, since nothing would be shared between sites. Unique things like usernames would need their constraints modified to only be unique per-site. That got old pretty fast. Secondly was security. How could we ensure that every piece of code touching the database, even the eventual third-party plugins, would use the correct site in their queries so as not to mess with the others? And finally, having each site's contents in one massive database would not be very convenient if the site owners wanted backups of their portion of the database. So we started looking at multi-database solutions, and quickly realized we were pretty much on our own for what we wanted to do. We don't just want some models in one database, and other models in a difference database; we want the same models in every database. Every site needs a pages table, for example. Since we're mapping tables with 
<a href="http://www.sqlalchemy.org">SQLAlchemy</a>, and each mapper is bound to 
<a href="http://www.sqlalchemy.org/docs/metadata.html">metadata</a>, an engine, and a session, it seems that we'd need to run the table and mapper definitions once per site; each time, the engine would point to the appropriate site's. And now the big trick: how do we do this without modifying any model code, so that plugin writers don't have to learn any silly new details, and without doing a bunch of extra work every time a controller needs to use a model? If our controllers import 
<tt>pagoda.models.pages</tt>, how will it know to get the 
<tt>Page</tt> class bound to the current site's engine, and not another site's? We looked to 
<a href="http://www.cherrypy.org">CherryPy</a> for inspiration. In a TurboGears controller, importing 
<tt>cherrypy.request</tt> and 
<tt>cherrypy.response</tt> will make the current thread's request and response objects available. How do these objects magically belong to the appropriate thread? They simply use a class called 
<tt>ThreadLocalProxy</tt>. As the name suggests, 
<tt>cherrypy.request</tt> and 
<tt>cherrypy.response</tt> are proxy objects that determine the current thread and point object access to the correct 
<tt>request</tt> and 
<tt>response</tt> instances. Similarly, we want something like 
<tt>SiteLocalProxy</tt>, which will make model classes available that are magically bound to the correct site's engine. Using 
<tt>ThreadLocalProxy</tt> as inspiration, we made a clever little object called 
<tt>site</tt>. When anything is imported from 
<tt>pagoda.site</tt>, it will rebind 
<tt>turbogears.database.metadata</tt> and 
<tt>turbogears.database.session</tt> after updating 
<tt>sqlalchemy.dburi</tt> in the config to point to the current site's. Then the requested module is imported and cached for next time (so the models aren't reinitialized every time). No model code was changed at all! The only necessary modification was importing from 
<tt>pagoda.site.models</tt> instead of 
<tt>pagoda.models</tt> in our controllers. Our first implementation looked very much like 
<tt>ThreadLocalProxy</tt>, but it made our import statements look funny since 
<tt>site</tt> wasn't a real module. So we started investigating the 
<tt>imp</tt>, 
<tt>ihooks</tt>, and 
<tt>imputils</tt> modules, eventually leading us to 
<a href="http://www.python.org/dev/peps/pep-0302/">PEP 302</a>. With help from 
<a href="http://peak.telecommunity.com/DevCenter/Importing">Importing</a> (to reduce the amount of code necessary), we now have a special pseudo-module called 
<tt>site</tt>, and Pagoda modules imported from that will take the current request's site into account instead of just being imported once for the entire process. Before writing up this entry, I came across 
<a href="http://cheeseshop.python.org/pypi/Alchemyware">Alchemyware</a>. At first it looked promising for what we want to do, but as far as I can tell it requires modifying the way you write models and reinstantiating them on every request. Also, I don't understand how the mapped class can be "shared by everyone" if it's being mapped to multiple databases. Anyway, after cleaning up our proof-of-concept I'll share the code behind our import trickery in case anyone is trying to do something similar, but mostly just because such tricks are interesting. In case you forgot, we missed the end-of-March deadline we set for our demo, due in part to being burned out after PyCon. We're shooting for the end of April now.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Better Python Editing in Kate</title
><link href="http://blog.case.edu/bmb12/2007/03/better_python_editing_in_kate"
 /><id
>http://blog.case.edu/bmb12/2007/03/better_python_editing_in_kate</id
><published
>2007-03-06T20:53:42Z</published
><updated
>2007-03-06T21:05:49Z</updated
><category term="KDE" label="KDE"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Perhaps this will damage my hacker credibility, but I use KDE's 
<a href="http://www.kate-editor.org">Kate Editor</a> as my development environment. For a while I used 
<a href="http://kdevelop.org">KDevelop</a>, but then I realized the only features I was using were Kate's (note to KDevelop developers: having to decide on a location and filename immediately when I hit New File is extremely annoying. Give me an Untitled!) The included python.xml (the file that tells Kate how to categorize tokens for highlighting) is a bit insufficient for Python programmers: no differentiation of class definitions, function definitions, or decorators. I've added these and some other minor improvements to my own python.xml and packaged it up with a custom color scheme in 
<a href="http://exogen.case.edu/kate_colors.tar.gz">kate_colors.tar.gz</a>. If you extract this to your home directory, it will replace a few Kate-specific files. Your configuration options will be kept, but I think any custom colors you've set will be replaced (Kate lacks a way to split these up to make sharing easy). If you select "exogen - Dark" as the default schema in Fonts &amp; Colors, your HTML, CSS, JavaScript, and Python files will look pretty: 
<a href="http://blog.case.edu/bmb12/2007/03/06/kate_colors.png">
<img alt="kate_colors.png" src="http://blog.case.edu/bmb12/2007/03/06/kate_colors-thumb.png" width="400" height="288" />
</a> Of course, you don't have to use the included schema. The enhanced syntax 
<em>highlighter</em> is the biggest improvement, giving you several more contexts under Text Highlighting Styles. 
<a href="http://blog.case.edu/bmb12/2007/03/06/kate_styles.png">
<img alt="kate_styles.png" src="http://blog.case.edu/bmb12/2007/03/06/kate_styles-thumb.png" width="400" height="369" />
</a> I shared these changes with 
<a href="http://holovaty.com">Adrian Holovaty</a> at PyCon, who I noticed also uses Kate. I guess he couldn't get used to the high contrast color scheme, but here's hoping the new syntax file is serving him well. ;) (I also recommend the Tab Bar Extension, the Word Completion Plugin, and learning to use those double arrows in the Filesystem Browser panel.)</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Pagoda CMS Notes</title
><link href="http://blog.case.edu/bmb12/2007/03/pagoda_cms_notes"
 /><id
>http://blog.case.edu/bmb12/2007/03/pagoda_cms_notes</id
><published
>2007-03-03T01:11:09Z</published
><updated
>2007-03-03T17:06:02Z</updated
><category term="Pagoda" label="Pagoda"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<div style="text-align: center">
<img alt="pagoda_logo.png" src="http://blog.case.edu/bmb12/2007/03/02/pagoda_logo.png" width="176" height="269" />
</div>If you were at 
<a href="http://us.pycon.org/TX2007/HomePage">PyCon 2007</a> or read 
<a href="http://blog.extracheese.org">Gary's blog</a> or read the 
<a href="http://groups.google.com/group/turbogears">TurboGears mailing list</a>, you may have seen 
<a href="http://www.pagodacms.org">Pagoda CMS</a> mentioned. Pagoda is an open source content management system I've been working on with 
<a href="http://blug.cshesse.com/">Chris</a> and 
<a href="http://www.iancharnas.com">Ian</a>. It's built on 
<a href="http://www.turbogears.org">TurboGears</a> and is focused on being simple yet extensible. We put up an 
<a href="http://www.pagodacms.org/screencast/swf/index.html">introductory screencast</a> at 
<a href="http://www.pagodacms.org">pagodacms.org</a> that we hurriedly made the night before PyCon. We've tried a bunch of content management systems, both open source and commercial, and developed for small shops, big corporations, government organizations, and of course 
<a href="http://eecs.case.edu">Case itself</a>. There are features that are consistently implemented poorly, hard to understand, or simply missing. Pagoda is a result of the observations we've made of how content management systems are really used in a production setting. These are just a few of the notes and design goals we've been using along the way.
<h3>Don't overengineer it</h3>Somewhere along the line someone decided that if you're going to make a content management system, you have to build everything on top of a dozen layers of abstraction. Some pretend that there's no difference between static page content (like a blog entry) and dynamically generated content (like a news feed). Some pretend that building complex workflows that are exactly suited to the way your organization is structured is a common need (we've found that people already have real-life workflows and rarely do they want this duplicated in a CMS). Experience has shown us that such complexity is rarely needed. We don't try to fit every feature into a "plugin" structure or an "actions" framework. We've streamlined the features based on our experience, and hopefully kept it fun to hack on (when you do need something extra) by avoiding meaningless abstractions.
<h3>Do one thing really, really well</h3>A lot of content management systems try to do everything involved in running a web site. Database management, email management, form design, you name it. We don't want a content management system that takes over every aspect of making a web site. We've made conscious decisions to leave a lot of features out. In addition to the above (which can all be found in Zope + Plone, for example), we've spent a lot of time deciding how far certain features should reach and what should be left up to the webmaster. One example is theme switching. When you're first building a web site, being able to download prepackaged themes might be nice. But for production sites, this simply does not happen. Imagine the 
<a href="http://www.cmnh.org">Cleveland Museum of Natural History</a> or a 
<a href="http://eecs.case.edu">university department</a> downloading new themes and swapping them out. Not gonna happen. Instead this is limiting, because prepackaged themes require predetermined markup. As a result, most 
<a href="http://www.plone.org">Plone</a> sites look the same and are structured the same way. They have the little tree on the left and those tiny tabs and a logo above that. And then you're scared to modify too much CSS because there's a bunch already dedicated to making those tabs pixel-perfect. We don't have a default theme or even default markup. Markup and design are meant for programmers and web designers, let's not pretend otherwise.
<h3>Use simple terminology</h3>As Jeffrey Veen mentioned in 
<a href="http://www.adaptivepath.com/publications/essays/archives/000365.php">Making A Better CMS</a>, 
<strong>stop it with the jargon already!</strong> "Mambots", "archetypes", "portlets", and I'll admit it, I'm not even a fan of the term "widgets". We've tried to use understandable terminology throughout Pagoda and not extend failed analogies. One example where we created a feature and spent some effort on choosing a name is Placeholders. This is a feature that we've actually needed on production sites but haven't found in other content management systems. The idea is that there is text that appears on multiple pages 
<em>within the content</em>, and it would be nice to only have to change in one place so we don't have to hunt down every page in the future. Phone numbers, store hours, admission prices, and press contact information are some examples. These aren't template variables because they have nothing to do with templates (to the user) and aren't arbitrary Python objects, and they're not code snippets because they have nothing to do with code. They're simply content placeholders. Here's the mockup we used while implementing this feature:
<div style="text-align: center;">
<img alt="placeholders.png" src="http://blog.case.edu/bmb12/2007/03/02/placeholders.png" width="574" height="588" />
</div>
<h3>Borrow features that work</h3>We've had a lot of inspiration along the way and used it to solve real problems. For example, if you need to have a downloadable file on your web site, a lot of content management systems will force you to ask "where do I put this?" and once you've decided on a place, require you to find your way there in the filesystem. We decided on pages having Attachments. Most downloads are associated with a particular page, so just upload them to that page and that will determine their location. We used 
<a href="http://www.campfirenow.com/tour/">37signal's Campfire</a> for inspiration, where people can upload files to the room they're in and they appear as attachments.
<h3>Reduce the number of clicks</h3>We're lucky enough to have started developing after AJAX became popular. The "Web 2.0" buzzword might be annoying, but this is really something we can use to make content management quicker and easier. Navigation and messing around with page options won't require dozens of clicks and page reloads anymore. Instead of having to retrofit our software to take advantage of AJAX, we can design with it in mind.
<h3>Built a content management system, not a new framework</h3>Similar to doing one thing really well, we're not building a web framework. That's what TurboGears is for. People can still use their existing TurboGears controllers, models, and templates. We're using 
<a href="http://www.sqlalchemy.org">SQLAlchemy</a> for Pagoda's models and 
<a href="http://genshi.edgewall.org">Genshi</a> for the templates. To install Pagoda for your existing TurboGears project, you'll just have to subclass from PagodaController instead of the default RootController, so Pagoda can dispatch requests to the appropriate page. So hopefully it sounds like an interesting project. We're still hacking on the core and hope to release a demo before the end of March, when we'll also invite people to help out and find weak spots. We have some mailing lists on Google Groups for discussion: 
<a href="http://groups.google.com/group/pagoda-talk">pagoda-talk</a> (general discussion), 
<a href="http://groups.google.com/group/pagoda-coders">pagoda-coders</a> (core development), and 
<a href="http://groups.google.com/group/pagoda-announcements">pagoda-announcements</a> (for releases and other notices). For the first few releases we'll also make announcements on the TurboGears list.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Pagoda CMS Notes</title
><link href="http://blog.case.edu/bmb12/2007/03/pagoda_cms_notes"
 /><id
>http://blog.case.edu/bmb12/2007/03/pagoda_cms_notes</id
><published
>2007-03-03T01:11:09Z</published
><updated
>2007-03-03T17:06:02Z</updated
><category term="Pagoda" label="Pagoda"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<div style="text-align: center">
<img alt="pagoda_logo.png" src="http://blog.case.edu/bmb12/2007/03/02/pagoda_logo.png" width="176" height="269" />
</div>If you were at 
<a href="http://us.pycon.org/TX2007/HomePage">PyCon 2007</a> or read 
<a href="http://blog.extracheese.org">Gary's blog</a> or read the 
<a href="http://groups.google.com/group/turbogears">TurboGears mailing list</a>, you may have seen 
<a href="http://www.pagodacms.org">Pagoda CMS</a> mentioned. Pagoda is an open source content management system I've been working on with 
<a href="http://blug.cshesse.com/">Chris</a> and 
<a href="http://www.iancharnas.com">Ian</a>. It's built on 
<a href="http://www.turbogears.org">TurboGears</a> and is focused on being simple yet extensible. We put up an 
<a href="http://www.pagodacms.org/screencast/swf/index.html">introductory screencast</a> at 
<a href="http://www.pagodacms.org">pagodacms.org</a> that we hurriedly made the night before PyCon. We've tried a bunch of content management systems, both open source and commercial, and developed for small shops, big corporations, government organizations, and of course 
<a href="http://eecs.case.edu">Case itself</a>. There are features that are consistently implemented poorly, hard to understand, or simply missing. Pagoda is a result of the observations we've made of how content management systems are really used in a production setting. These are just a few of the notes and design goals we've been using along the way.
<h3>Don't overengineer it</h3>Somewhere along the line someone decided that if you're going to make a content management system, you have to build everything on top of a dozen layers of abstraction. Some pretend that there's no difference between static page content (like a blog entry) and dynamically generated content (like a news feed). Some pretend that building complex workflows that are exactly suited to the way your organization is structured is a common need (we've found that people already have real-life workflows and rarely do they want this duplicated in a CMS). Experience has shown us that such complexity is rarely needed. We don't try to fit every feature into a "plugin" structure or an "actions" framework. We've streamlined the features based on our experience, and hopefully kept it fun to hack on (when you do need something extra) by avoiding meaningless abstractions.
<h3>Do one thing really, really well</h3>A lot of content management systems try to do everything involved in running a web site. Database management, email management, form design, you name it. We don't want a content management system that takes over every aspect of making a web site. We've made conscious decisions to leave a lot of features out. In addition to the above (which can all be found in Zope + Plone, for example), we've spent a lot of time deciding how far certain features should reach and what should be left up to the webmaster. One example is theme switching. When you're first building a web site, being able to download prepackaged themes might be nice. But for production sites, this simply does not happen. Imagine the 
<a href="http://www.cmnh.org">Cleveland Museum of Natural History</a> or a 
<a href="http://eecs.case.edu">university department</a> downloading new themes and swapping them out. Not gonna happen. Instead this is limiting, because prepackaged themes require predetermined markup. As a result, most 
<a href="http://www.plone.org">Plone</a> sites look the same and are structured the same way. They have the little tree on the left and those tiny tabs and a logo above that. And then you're scared to modify too much CSS because there's a bunch already dedicated to making those tabs pixel-perfect. We don't have a default theme or even default markup. Markup and design are meant for programmers and web designers, let's not pretend otherwise.
<h3>Use simple terminology</h3>As Jeffrey Veen mentioned in 
<a href="http://www.adaptivepath.com/publications/essays/archives/000365.php">Making A Better CMS</a>, 
<strong>stop it with the jargon already!</strong> "Mambots", "archetypes", "portlets", and I'll admit it, I'm not even a fan of the term "widgets". We've tried to use understandable terminology throughout Pagoda and not extend failed analogies. One example where we created a feature and spent some effort on choosing a name is Placeholders. This is a feature that we've actually needed on production sites but haven't found in other content management systems. The idea is that there is text that appears on multiple pages 
<em>within the content</em>, and it would be nice to only have to change in one place so we don't have to hunt down every page in the future. Phone numbers, store hours, admission prices, and press contact information are some examples. These aren't template variables because they have nothing to do with templates (to the user) and aren't arbitrary Python objects, and they're not code snippets because they have nothing to do with code. They're simply content placeholders. Here's the mockup we used while implementing this feature:
<div style="text-align: center;">
<img alt="placeholders.png" src="http://blog.case.edu/bmb12/2007/03/02/placeholders.png" width="574" height="588" />
</div>
<h3>Borrow features that work</h3>We've had a lot of inspiration along the way and used it to solve real problems. For example, if you need to have a downloadable file on your web site, a lot of content management systems will force you to ask "where do I put this?" and once you've decided on a place, require you to find your way there in the filesystem. We decided on pages having Attachments. Most downloads are associated with a particular page, so just upload them to that page and that will determine their location. We used 
<a href="http://www.campfirenow.com/tour/">37signal's Campfire</a> for inspiration, where people can upload files to the room they're in and they appear as attachments.
<h3>Reduce the number of clicks</h3>We're lucky enough to have started developing after AJAX became popular. The "Web 2.0" buzzword might be annoying, but this is really something we can use to make content management quicker and easier. Navigation and messing around with page options won't require dozens of clicks and page reloads anymore. Instead of having to retrofit our software to take advantage of AJAX, we can design with it in mind.
<h3>Built a content management system, not a new framework</h3>Similar to doing one thing really well, we're not building a web framework. That's what TurboGears is for. People can still use their existing TurboGears controllers, models, and templates. We're using 
<a href="http://www.sqlalchemy.org">SQLAlchemy</a> for Pagoda's models and 
<a href="http://genshi.edgewall.org">Genshi</a> for the templates. To install Pagoda for your existing TurboGears project, you'll just have to subclass from PagodaController instead of the default RootController, so Pagoda can dispatch requests to the appropriate page. So hopefully it sounds like an interesting project. We're still hacking on the core and hope to release a demo before the end of March, when we'll also invite people to help out and find weak spots. We have some mailing lists on Google Groups for discussion: 
<a href="http://groups.google.com/group/pagoda-talk">pagoda-talk</a> (general discussion), 
<a href="http://groups.google.com/group/pagoda-coders">pagoda-coders</a> (core development), and 
<a href="http://groups.google.com/group/pagoda-announcements">pagoda-announcements</a> (for releases and other notices). For the first few releases we'll also make announcements on the TurboGears list.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>A memorable PyCon moment</title
><link href="http://blog.case.edu/bmb12/2007/02/a_memorable_pycon_moment"
 /><id
>http://blog.case.edu/bmb12/2007/02/a_memorable_pycon_moment</id
><published
>2007-02-28T08:03:43Z</published
><updated
>2007-02-28T08:30:21Z</updated
><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>In one of the meeting rooms in the conference hotel, a dozen or so 
<a href="http://groups.google.com/group/django-developers">Djangonauts</a> are quietly and productively hacking away on projects such as 
<a href="http://groups.google.com/group/django-developers/browse_thread/thread/79adb844e481d8c0/#">geodjango</a>, a more 
<a href="http://groups.google.com/group/django-developers/browse_thread/thread/f002f89b656973c4/#">decoupled admin</a>, and 
<a href="http://www.djangosnippets.org/">Django snippets</a>. 
<a href="http://blog.ianbicking.org">Ian Bicking</a> pops his head in the door. In a loud whisper: "Hey is this the quiet room! Can I join the quiet room!" (It was, in fact, designated as The Quiet Room.) A crowd of other web folks burst into the room and it quickly becomes apparent that they were probably at the 
<a href="http://www.flickr.com/photos/mikepirnat/404132325/">EWT party</a> and not sprinting. There is some uproar as Ian demonstrates 
<a href="http://pythong.org">the next generation of Python packaging</a>. The crowd leaves shortly after, causing mischief elsewhere in the hotel. 
<a href="http://www.b-list.org/">James Bennett</a> leaves the room to get a drink, returning with the following prediction (paraphrased): "It seems that Django and Zope are the only web frameworks whose members aren't going to get arrested." (Gary sums up some other moments nicely in 
<a href="http://blog.extracheese.org/2007/02/pycon-2007-untold-stories.html">PyCon 2007: The Untold Stories</a>.)</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>CAS 1.0 Authentication for Django, Part 2</title
><link href="http://blog.case.edu/bmb12/2006/12/cas_for_django_part_2"
 /><id
>http://blog.case.edu/bmb12/2006/12/cas_for_django_part_2</id
><published
>2006-12-01T18:27:12Z</published
><updated
>2006-12-01T18:39:00Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<p>After using 
<a href="http://blog.case.edu/bmb12/2006/11/simple_cas_for_django">my Django CAS authentication module</a> for a while, I decided to make a couple improvements.</p>
<p>The biggest improvement is that instead of modifying code in the CAS module itself to set your CAS address and do things like custom User field population, all this stuff can now be configured in your settings file.</p>
<p>Another improvement is that CAS authentication now works for the 
<a href="http://www.djangoproject.com/documentation/tutorial2/">bundled admin interface</a>. Since the administration interface does not account for an authentication backend that doesn't know the user's password, this makes the login form useless. The CAS module will now intercept requests to the administration interface and do the proper authentication routine if necessary, never showing the login form (which doesn't make sense for CAS). Intercepting requests, you ask? Yes, that means the CAS module is now 
<a href="http://www.djangoproject.com/documentation/middleware/">middleware</a>. Actually it's middleware, a couple views, and an 
<a href="http://www.djangoproject.com/documentation/authentication/#other-authentication-sources">authentication backend</a>.</p>
<p>So here's how to use it now...</p>
<p>Get 
<a href="http://exogen.case.edu/cas_middleware.tar.gz">cas_middleware.tar.gz</a>.</p>
<p>Extract it in 
<code>django/contrib/</code>. The code will be located at 
<code>django/contrib/cas/</code>. Is this a valid place to install 
<em>third-party</em> middleware? It's not really clear. Just do it anyway.</p>
<p>Now add it to the middleware and authentication backends in your settings. Make sure you also have the authentication middleware installed. Here's what mine looks like:</p>
<pre>
<code>MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.cas.middleware.CASMiddleware',
    'django.middleware.doc.XViewMiddleware',
)

AUTHENTICATION_BACKENDS = (
    'django.contrib.cas.backend.CASBackend',
)</code>
</pre>
<p>You can now configure the CAS module in the same settings file. Here are the possible options, most of which can be safely ignored:</p>
<ul>
<li>
<code>CAS_SERVICE_URL</code>: This is the only setting you must explicitly define. Set it to the base URL of your CAS source.</li>
<li>
<code>CAS_POPULATE_USER</code>: A callable or the location of a callable. When a user logs in and is missing name and email attributes in the database, this will be called with their User model instance. Default is None (do nothing).</li>
<li>
<code>CAS_ADMIN_PREFIX</code>: The URL prefix of the Django administration site. If undefined, the CAS middleware will just check the view being rendered to see if it lives in 
<code>django.contrib.admin.views</code>. The method is a little evil, but it works.</li>
<li>
<code>CAS_LOGIN_URL</code>: The URL where you bound 
<code>django.contrib.cas.views.login</code>. If undefined, assume 
<code>/accounts/login/</code>.</li>
<li>
<code>CAS_LOGOUT_URL</code>: The URL where you bound 
<code>django.contrib.cas.views.logout</code>. If undefined, assume 
<code>/accounts/logout/</code>.</li>
<li>
<code>CAS_REDIRECT_URL</code>: Where to send a user after logging in or out if there is no referrer and no 
<code>next</code> page set. Default is 
<code>/</code>.</li>
<li>
<code>CAS_REDIRECT_FIELD_NAME</code>: The name of the GET parameter in which to store the page URL to send the user to after logging in. Default is 
<code>next</code>.</li>
</ul>
<p>Need an example? Here's what my CAS settings look like:</p>
<pre>
<code>CAS_SERVICE_URL = 'https://login.case.edu/cas/'
CAS_POPULATE_USER = 'present.utils.populate_user'
</code>
</pre>
<p>And the callable that lives at 
<code>present.utils.populate_user</code> (notice this code lives in my project instead of tinkering with the CAS module) looks like this:</p>
<pre>
<code>def populate_user(user):
    try:
        ldap = LDAP()
        person = ldap.filter_one_by(uid=user.username)
    except:
        if not user.email:
            user.email = "%s@case.edu" % user.username
    else:
        # If it succeeds, update their User entry
        user.email = person.mail[0]
        user.first_name = fix_case(person.givenName[0])
        user.last_name = fix_case(person.sn[0])
</code>
</pre>
<p>(
<code>LDAP</code> and 
<code>fix_case</code> also live in my 
<code>utils</code> module).</p>
<p>Finally, make sure your project knows how to log users in and out by adding these to your URLconf:</p>
<pre>
<code>(r'^accounts/login/$', 'django.contrib.cas.views.login'),
(r'^accounts/logout/$', 'django.contrib.cas.views.logout'),</code>
</pre>
<p>Users should now be able to log into your site, and staff into the administration interface, using CAS 1.0.</p>
</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Simple CAS 1.0 Authentication for Django</title
><link href="http://blog.case.edu/bmb12/2006/11/simple_cas_for_django"
 /><id
>http://blog.case.edu/bmb12/2006/11/simple_cas_for_django</id
><published
>2006-11-27T20:18:13Z</published
><updated
>2006-11-27T21:12:30Z</updated
><category term="Case" label="Case"
 /><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Back when I expressed interest in making the 
<a href="http://blog.case.edu/bmb12/2006/10/a_webbased_presentation_system_for_case">web presentation bounty based solely on client-side code</a>, Simon (bounty master and 
<a href="http://filer.case.edu">Filer</a> admin) expressed his wish to keep the two services decoupled (so I shouldn't rely on Filer for slideshow storage). While I still want to have a save-to-Filer feature, I decided that I should just go ahead and get the web presentation system up and running before worrying about a client-side-only version. So I started a 
<a href="http://www.djangoproject.com">Django</a> project. Anyway, the result is that I got 
<a href="http://wiki.case.edu/CAS">CAS</a> 1.0 working alongside the Django authentication system, which means I can take advantage of built-in features like 
<a href="http://www.djangoproject.com/documentation/authentication/">permissions and messages</a> with CAS-authenticated users. If anyone else is interested in using CAS authentication with Django, you can 
<a href="http://exogen.case.edu/django_cas.tar.gz">download the code I'm using</a>. Here's a brief usage guide:
<ul>
<li>Set 
<code>SERVICE_URL</code> in 
<code>cas/__init__.py</code> to the location of your CAS service. For example, Case's is 
<code>https://login.case.edu/cas/</code>.</li>
<li>Set 
<code>DEFAULT_REDIRECT_URL</code> in 
<code>cas/__init__.py</code>. Normally the user will be sent back to their 
<code>HTTP_REFERER</code> (the page that requested login) after authentication. But if the user requests 
<code>/accounts/login/</code> directly (or there is no 
<code>HTTP_REFERER</code>), they will be sent to 
<code>DEFAULT_REDIRECT_URL</code>.</li>
<li>Enable the 
<code>login</code> and 
<code>logout</code> views by adding these to your URLconf (customize the URLs if you want):
<pre>
<code>(r'^accounts/login/$', 'your_site.cas.views.login'),
(r'^accounts/logout/$', 'your_site.cas.views.logout'),
</code>
</pre></li>
<li>Add the backend in 
<code>settings.py</code>:
<pre>
<code>AUTHENTICATION_BACKENDS = (
    'your_site.cas.backends.CASBackend',
)
</code>
</pre></li>
<li>Make sure at least the following apps are installed:
<pre>
<code>INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.sessions',
    'your_site.cas',
)
</code>
</pre></li>
<li>Finally, if you have a way to populate the user's name and e-mail address fields from their username, put it in 
<code>cas/backends.py</code> (see the comments). For example, I have LDAP code there.</li>
</ul>P.S.: This just implements the minimum required for CAS authentication. Features like gateway, renew, and proxies are not supported. An alpha version of the presentation system should be online to play with later this week.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Workshop: Making Databases Fun with Python</title
><link href="http://blog.case.edu/bmb12/2006/11/workshop_making_databases_fun_with_python"
 /><id
>http://blog.case.edu/bmb12/2006/11/workshop_making_databases_fun_with_python</id
><published
>2006-11-20T17:13:41Z</published
><updated
>2006-11-20T17:14:11Z</updated
><category term="Case" label="Case"
 /><category term="Case" label="Case"
 /><category term="Programming" label="Programming"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<strong>Reminder!</strong> This is today! Did you ever notice how writing SQL is not very fun? This 
<strong>Monday (November 20th)</strong> on behalf of Case 
<a href="http://wiki.case.edu/Project_Club">Project Club</a>, I will be hosting a workshop for those interested in Python and databases. The talk will be at 
<strong>7:01 PM (sharp) until 8:30 PM in the Olin 303 classroom/computer lab</strong>. I'll have Python all set up for everyone to play with and follow along. Pizza and drinks will be provided! 
<a href="http://www.python.org">Python</a> is a powerful dynamic programming language suitable for many tasks, including data analysis for research, web programming, and just plain fun. Even if you don't know Python, there won't be any crazy wizardry going on during the worskhop, so you should be able to pick up the basics very quickly. Some contents of the talk will include:
<ul>
<li>Simple data/object persistence, for when SQL is overkill.</li>
<li>The dbapi, a standardized interface for talking to databases with Python.</li>
<li>An overview of object-relational mappers that will let you harness the power of relational databases without writing a single line of SQL (and easily swap out SQL backends).</li>
<li>Construction of a database application during the workshop everyone can play with, made with 
<a href="http://www.djangoproject.com">Django</a>'s object-relational mapper (or perhaps 
<a href="http://www.sqlalchemy.org">SQLAlchemy</a>).</li>
</ul>Again, no prior knowledge of Python or any of the related libraries is required. Hope to see you there! 
<a href="http://blog.case.edu/bmb12/2006/11/17/databases_72.png">
<img alt="databases_72.png" src="http://blog.case.edu/bmb12/2006/11/17/databases_72-thumb.png" width="320" height="452" />
</a></div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Automating Case Wiki Tasks</title
><link href="http://blog.case.edu/bmb12/2006/10/automating_case_wiki_tasks"
 /><id
>http://blog.case.edu/bmb12/2006/10/automating_case_wiki_tasks</id
><published
>2006-10-22T03:41:38Z</published
><updated
>2006-10-24T00:01:38Z</updated
><category term="Programming" label="Programming"
 /><category term="Python" label="Python"
 /><category term="Wiki" label="Wiki"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>
<p>A while ago 
<a href="http://blog.case.edu/csh11/">Chris</a> added a 
<code>login</code> method to the 
<a href="http://opensource.case.edu/projects/CaseClasses/browser/python/trunk/Case/case_cas.py">CAS module</a> in 
<a href="http://opensource.case.edu/projects/CaseClasses/">CaseClasses</a>. It returns a 
<a href="http://wwwsearch.sourceforge.net/mechanize/">mechanize</a> Browser object so that you can programmatically surf the web as if you had logged into CAS in a real web browser.</p>
<p>CaseClasses also has a 
<a href="http://opensource.case.edu/projects/CaseClasses/browser/python/trunk/Case/case_codes.py">Codes module</a> that has the abbreviated codes for majors, departments, and 
<strong>buildings</strong>. I combined these two features to tackle the 
<a href="http://wiki.case.edu/CaseWiki:Projects/Building_codes">Building codes project</a> on the 
<a href="http://wiki.case.edu">Case Wiki</a>.</p>
<p>
<strong>P.S.:</strong> There is a 
<a href="http://wiki.case.edu/api.php">MediaWiki API</a> that would normally be used to do this kind of stuff, but 
<a href="http://wiki.case.edu/User_talk:Brian.Beck">according to Greg</a>, editing is not fully functional yet.</p>
<p>Think you could add a lot to the wiki with some automated task? Here's how it was done.</p>
<p>First, you'll need 
<a href="http://wwwsearch.sourceforge.net/mechanize/">mechanize</a> and CaseClasses:</p>
<pre>
<code>$ sudo easy_install mechanize
$ sudo easy_install http://opensource.case.edu/svn/CaseClasses/python/trunk
</code>
</pre>
<p>Now log into CAS with mechanize:</p>
<pre>
<code>import Case
from getpass import getpass

username = 'bmb12'
password = getpass() # Enter a password without echoing

cas = Case.CAS()
browser = cas.login(username, password)
</code>
</pre>
<p>You can open any page with 
<code>browser</code> and interact with it as a logged in Case user. So let's go to the Case Wiki and log in:</p>
<pre>
<code>browser.set_handle_robots(False)
browser.open("http://wiki.case.edu")
browser.follow_link(text_regex='Log In')
</code>
</pre>
<p>Editing can be done like so:</p>
<pre>
<code>browser.open("http://wiki.case.edu/User:Brian.Beck")
<br />browser.follow_link(text='Edit this page')
browser.select_form(name='editform')
browser['wpTextbox1'] += " Also, this guy sucks!"
browser.submit()
</code>
</pre>
<p>Automating the building code edits was done like so:</p>
<pre>
<code>for code, name in Case.Codes.buildings.iteritems():
    url = "http://wiki.case.edu/%s" % name.replace(' ', '_')
    try:
        browser.open(url)
    except:
        print "Didn't find %r." % name
    else:
        browser.follow_link(text='Edit this page')
        browser.select_form(name='editform')
        source = browser['wpTextbox1']
        add_text = "The building code for %s is [[building code:=%s]].\r\n"
        add_text %= (name, code)
        if 'code:=' not in source:
            insert_at = source.find('{{Building')
            if insert_at != -1:
                new_source = source[:insert_at] + add_text + source[insert_at:]
            else:
                new_source = source + add_text
            browser['wpTextbox1'] = new_source
            browser.submit()
            print "Added building code for %r." % name
</code>
</pre>
<p>Happy automating!</p>
<strong>Update:</strong> The same has now been done for the 
<a href="http://wiki.case.edu/CaseWiki:Projects/Street_addresses">Street addresses project</a>. Check out 
<a href="http://wiki.case.edu/CaseWiki_talk:Projects/Street_addresses">the discussion</a> to see how.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>geopy 0.93 Released: distance, util, GeoNames</title
><link href="http://blog.case.edu/bmb12/2006/10/geopy_093_released_distance_util_geonames"
 /><id
>http://blog.case.edu/bmb12/2006/10/geopy_093_released_distance_util_geonames</id
><published
>2006-10-08T23:48:36Z</published
><updated
>2006-10-09T00:02:31Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Finally released 
<a href="http://cheeseshop.python.org/pypi/geopy/0.93">geopy 0.93</a>, which contains the 
<a href="http://blog.case.edu/bmb12/2006/09/geopy_gets_distance_and_util_modules">distance and util modules I previously mentioned</a>, a 
<a href="http://www.geonames.org/">GeoNames</a> geocoder, and improvements to the Google geocoder in other formats. Updating 
<a href="http://exogen.case.edu/projects/geopy/">the documentation</a> was all that was holding it back, really. You can now pass 
<code>domain</code> and 
<code>resource</code> arguments to the Google geocoder. To query the actual Google Maps interface (instead of their official HTTP geocoder), initialize like so:
<pre>
<code>g = geocoders.Google(resource='maps')</code>
</pre>The JavaScript results tend to be the best for this resource, so change that as well:
<pre>
<code>g = geocoders.Google(resource='maps', output_format='js')</code>
</pre>Finally, for geocoding addresses outside of the US, change the domain being queried:
<pre>
<code>g = geocoders.Google(domain='maps.google.co.uk', resource='maps', output_format='js')</code>
</pre>As 
<a href="http://groups-beta.google.com/group/geopy/browse_frm/thread/3ad6a1f131342a32">James Robinson brought up on the geopy mailing list</a>, work is under way for accuracy support. This will let you determine how precise the geocoded result is for the given location. For example, is it only guaranteed to be the correct city? Street? Is it the exact address? I decided to release this version of geopy without completing this, because not much work is done so far (and we also want to normalize values across geocoders), and the distance module was a pretty big addition. To upgrade:
<pre>
<code>sudo easy_install geopy</code>
</pre></div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Miscellaneous School, Blog, Python Stuff</title
><link href="http://blog.case.edu/bmb12/2006/09/miscellaneous_school_blog_python_stuff"
 /><id
>http://blog.case.edu/bmb12/2006/09/miscellaneous_school_blog_python_stuff</id
><published
>2006-09-27T05:15:28Z</published
><updated
>2006-09-27T07:03:37Z</updated
><category term="Blogging" label="Blogging"
 /><category term="Case" label="Case"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Today was perhaps the most easygoing day ever. My first class was cancelled, my second class ended 20 minutes early, and my last class only lasted for 20 minutes. Ah, education! After getting some free food from the 
<a href="http://acm.case.edu">ACM</a> / Women in EECS event, I finished my day by admiring this totally legit use of the 
<a href="http://wiki.case.edu/Expression_Wall">Expression Wall</a> (clink clink!): 
<a href="http://blog.case.edu/bmb12/2006/09/27/regexp.jpg">
<img alt="regexp.jpg" src="http://blog.case.edu/bmb12/2006/09/27/regexp-thumb.jpg" width="480" height="321" />
</a> Finally, an expression I can relate to! A while ago I decided to try out some fancy log analyzers like 
<a href="http://performancing.com/metrics">Performancing Metrics</a> and 
<a href="http://www.google.com/analytics">Google Analytics</a>. Google Analytics seems to be better for checking out data about your users, while Performancing Metrics seems to be better for checking referrers and (surprisingly) search terms (well, Google's might be better, but Performancing Metrics is way easier to navigate). One interesting thing these sites (and 
<a href="http://blog.case.edu/stats/bmb12">Blog@Case Stats</a>) tell me is that 
<a href="http://start.case.edu">start.case.edu</a> is consistently my top external referrer. So it seems to send a lot of traffic my way. Go 
<a href="http://start.case.edu">start</a>! 
<a href="http://blog.case.edu/csh11/">Chris</a> and I are working on the next version of 
<a href="http://blog.case.edu/bmb12/2006/09/dmath_math_routines_for_pythons_arbitraryprecision_decimal_type">dmath</a>, mostly for speed and to deal with 
<a href="http://docs.python.org/lib/decimal-decimal.html">custom contexts</a>. For example, the result of 
<a href="http://en.wikipedia.org/wiki/Atan2">atan2</a>(0, 0) should be indefinite, but in the 
<code>math</code> module it's 0 (presumably so that the function is continuous). But if someone wants it to be indefinite (by which I mean D('NaN')), they should be able to set that in their context. Oh yeah, one big improvement is that 
<code>pow</code> will allow Decimals to be raised to Decimal powers. We're still trying to wrap our heads around some of the context stuff. For example, should all of our functions accept an optional 
<code>context</code> argument, like the 
<code>sqrt</code>, 
<code>pow</code>, and other methods in Decimal? If so, does every Decimal constructed within that function need to also be passed the context, even D(1)? This is stuff that will probably be obvious after some more browsing of 
<a href="http://svn.python.org/view/*checkout*/python/trunk/Lib/decimal.py?content-type=text/plain">decimal.py</a>. We're also looking into doing things in 
<a href="http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/">pyrex</a> once everything is known to be in working order. Need for speed, baby! Did I ever mention that 
<a href="http://exogen.case.edu/projects/geopy">geopy</a> 
<a href="http://geopy.googlecode.com/svn/trunk/geopy/">trunk</a> now has support for 
<a href="http://www.geonames.org/">GeoNames</a>, and may soon support 
<a href="http://www.map24.com/">Map24</a>? Map24 has done a pretty good job of convoluting their JavaScript so that their free geocoder is only accessible via AJAX, but this is merely a speedbump and not a road block. It almost works (but not the version in trunk). Sadly, like 
<a href="http://developer.yahoo.com/maps/mapsTerms.html">Yahoo!'s</a>, their 
<a href="http://legal.map24.com/html/TOU_FREEAJAX/en-GB/">Terms of Use</a> state that their geocoding tools can only be used in combination with their Maps AJAX API. But hey, just because you can access their stuff from Python doesn't mean the developer isn't still using it legitimately (that is to say, to show locations on a Map24 map). That's all I got!</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>dmath: Math routines for Python's arbitrary-precision Decimal type</title
><link href="http://blog.case.edu/bmb12/2006/09/dmath_math_routines_for_pythons_arbitraryprecision_decimal_type"
 /><id
>http://blog.case.edu/bmb12/2006/09/dmath_math_routines_for_pythons_arbitraryprecision_decimal_type</id
><published
>2006-09-25T20:27:33Z</published
><updated
>2006-09-25T20:31:14Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Yesterday 
<a href="http://blog.case.edu/csh11/">Chris</a> and I spent all day writing math functions for 
<a href="http://docs.python.org/lib/module-decimal.html">Python's Decimal type</a>. The result is our new 
<a href="http://cheeseshop.python.org/pypi/dmath">dmath</a> library, 
<a href="http://code.google.com/p/dmath/">available on Google Code</a> and the 
<a href="http://cheeseshop.python.org/pypi/dmath">Cheese Shop</a> under the MIT/X11 license. Sparked by the routine for 
<code>atan</code> in 
<a href="http://blog.case.edu/bmb12/2006/09/geocoding_and_pythons_decimal_module">my last post</a>, I decided it wouldn't be too hard to go ahead and do the rest of the functions already offered by 
<code>math</code> and 
<code>cmath</code>. We now have 
<code>acos</code>, 
<code>asin</code>, 
<code>atan</code>, 
<code>atan2</code>, 
<code>ceil</code>, 
<code>cos</code>, 
<code>cosh</code>, 
<code>degrees</code>, 
<code>e</code>, 
<code>exp</code>, 
<code>floor</code>, 
<code>golden_ratio</code>, 
<code>hypot</code>, 
<code>log</code>, 
<code>log10</code>, 
<code>pi</code>, 
<code>pow</code>, 
<code>radians</code>, 
<code>sign</code>, 
<code>sin</code>, 
<code>sinh</code>, 
<code>sqrt</code>, 
<code>tan</code>, and 
<code>tanh</code>. Check it out:
<pre>
<code>&gt;&gt;&gt; from dmath import *
&gt;&gt;&gt; from decimal import Decimal as D, getcontext
&gt;&gt;&gt; getcontext().prec = 50
&gt;&gt;&gt; asin(D(1))
Decimal("1.5707963267948966192313216916397514420985846996876")
&gt;&gt;&gt; golden_ratio()
Decimal("1.6180339887498948482045868343656381177203091798058")
</code>
</pre>We're calling this release 0.9 because it just needs some testing and maybe some speed improvements, otherwise it's ready to use. There is currently some 
<a href="http://svn.python.org/projects/sandbox/trunk/decimal-c/">work being done</a> in Python sandbox/trunk to convert the decimal module to C, and maybe they'll include fast versions of all these routines. But hey, you can use these right now! Arbitrary precision is one of the coolest things in programming. We spent a lot of time in 
<a href="http://www.wolfram.com/">Mathematica</a>, where if you ask it to tell you the precision, it says 'Infinity'. During our testing, we actually stumbled across a bug in 
<a href="http://functions.wolfram.com/ElementaryFunctions/ArcTan2/">Mathematica's ArcTan function</a>! 
<a href="http://functions.wolfram.com/ElementaryFunctions/ArcTan2/03/01/01/">This page</a> correctly states that ArcTan[-Infinity, y] should always be Pi (with the sign of y). However, Mathematica always returns 0. I sent a message with my findings to the 
<a href="http://groups.google.com/group/comp.soft-sys.math.mathematica">Mathematica mailing list</a> and Daniel Lichtblau of Wolfram Research confirmed that it is indeed a simple bug. ArcTan users, beware! Anyway, enjoy 
<a href="http://cheeseshop.python.org/pypi/dmath">dmath</a>. Contributions are welcome, especially if you have any speed tips!</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>Geocoding and Python's decimal module</title
><link href="http://blog.case.edu/bmb12/2006/09/geocoding_and_pythons_decimal_module"
 /><id
>http://blog.case.edu/bmb12/2006/09/geocoding_and_pythons_decimal_module</id
><published
>2006-09-24T09:56:00Z</published
><updated
>2006-09-24T20:56:23Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="geopy" label="geopy"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>Python has an awesome 
<a href="http://docs.python.org/lib/module-decimal.html">decimal module</a> for decimal floating point arithmetic. It has configurable precision and keeps track of significant digits and does some other neat stuff. While I was adding the 
<a href="http://blog.case.edu/bmb12/2006/09/geopy_gets_distance_and_util_modules">geopy distance module</a>, I began to wonder if it would be worth the effort to switch everything in geopy over to use Decimals instead of floats. After checking out the decimal module (I had never used it before), I decided that I had nothing to lose, so I went for it... I quickly ran into some snags when I realized that I'd have to code my own trigonometric functions for use with Decimals, since those that come with Python are for complex or floating point numbers. The 
<a href="http://docs.python.org/lib/decimal-recipes.html">decimal recipes</a> page in the documentation has functions for 
<code>sin</code> and 
<code>cos</code>, but 
<code>distance</code> uses 
<code>asin</code>, 
<code>acos</code>, 
<code>atan</code>, and 
<code>atan2</code>. 
<a href="http://www.gdssw.com">Don Peterson</a> has a nice 
<a href="http://cheeseshop.python.org/pypi/decimalfuncs">decimalfuncs module</a> with most of these, but it's GPL (and would be an uncommon dependency) &#8212; geopy is 
<a href="http://www.opensource.org/licenses/mit-license.php">MIT/X11</a>. So I went ahead and started on these... I decided it would be easiest to define 
<code>asin</code> and 
<code>acos</code> in terms of 
<code>atan</code>, and it turns out there is a (relatively) quickly converging algorithm for that. Here's what I came up with for a Decimal-compatible 
<code>atan</code>:
<pre>
<code>def atan(x):
    if x == D('-Inf'):
        return pi() / -2
    elif x == 0:
        return D(0)
    elif x == D('Inf'):
        return pi() / 2
    
    if x &lt; -1:
        c = pi() / -2
        x = 1 / x
    elif x &gt; 1:
        c = pi() / 2
        x = 1 / x
    else:
        c = 0
    
    getcontext().prec += 2
    x_squared = x ** 2
    y = x_squared / (1 + x_squared)
    y_over_x = y / x
    i, lasts, s, coeff, num = D(0), 0, y_over_x, 1, y_over_x
    while s != lasts:
        lasts = s    
        i += 2
        coeff *= i / (i + 1)
        num *= y
        s += num * coeff
    if c:
        s = c - s
    getcontext().prec -= 2
    return +s
</code>
</pre>It depends on the 
<code>pi</code> function from the 
<a href="http://docs.python.org/lib/decimal-recipes.html">decimal recipes page</a>, which calculates pi to the currently configured precision. Upon finishing this, 
<a href="http://blog.case.edu/csh11/">Chris</a> came home and I told him what I was doing. Immediately, he tried to talk me out of it, asserting that floating point was good enough for geocoding. I tried to counter by explaining all the floating point calculations being performed in 
<code>distance</code>, but in the end he won. I no longer think it would be a very important change to convert everything in geopy to use the Decimal type. What finally convinced me was this quote from the 
<a href="http://www.movable-type.co.uk/scripts/LatLongVincenty.html">Vincenty distance page</a> I used for reference:
<blockquote>Vincenty&#226;&#8364;&#8482;s formula is accurate to within 0.5mm, or 0.000015&#226;&#8364;&#179; (!), on the ellipsoid being used.</blockquote>0.000015 arcseconds is about 4.16667e-9 degrees. Well, if floating point is good to about 10 decimal places, I guess Chris wins this time... Still, if anyone wants Decimal support in the future, maybe I'll just ask Don Peterson for permission to include decimalfuncs with geopy... 
<strong>Update:</strong> On second thought, maybe I will just continue implementing my own trig functions for Decimals. Chris and I just spent a while investigating the precision of my atan vs. decimalfunc's, and mine seems to be faster and more precise.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>geopy gets distance and util modules</title
><link href="http://blog.case.edu/bmb12/2006/09/geopy_gets_distance_and_util_modules"
 /><id
>http://blog.case.edu/bmb12/2006/09/geopy_gets_distance_and_util_modules</id
><published
>2006-09-24T09:20:50Z</published
><updated
>2006-10-08T23:02:07Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="geopy" label="geopy"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>If you check out 
<a href="http://geopy.googlecode.com/svn/trunk/">geopy trunk</a> right now you'll notice a few changes. I introduced two modules: 
<a href="http://geopy.googlecode.com/svn/trunk/geopy/util.py">
<code>util</code>
</a> and 
<a href="http://geopy.googlecode.com/svn/trunk/geopy/distance.py">
<code>distance</code>
</a>. 
<code>util</code> now contains the 
<code>parse_geo</code> and 
<code>arc_angle</code> functions, and will grow more in the future. 
<code>distance</code> is a bigger addition and contains helpful functions for calculating geodesic distances. I planned to add this eventually, but development was sparked by a request from 
<a href="http://polimetrix.com">Chris Mulligan</a>. There are two distance formulas: 
<a href="http://en.wikipedia.org/wiki/Great-circle_distance">Great-circle (aka haversine, aka spherical law of cosines) distance</a> and 
<a href="http://www.movable-type.co.uk/scripts/LatLongVincenty.html">Vincenty distance</a>. Great-circle distance uses a spherical model of the earth, using the average great-circle radius of 6372.795 kilometers (this is configurable). This results in an error of up to about 0.5%. Vincenty distance uses a more accurate ellipsoidal model of the earth. This is the default distance formula, and is thus aliased as 
<code>distance.distance</code> &#8212; so you can easily swap out distance formulas just by changing 
<code>distance.distance</code> at the top of your code. There are multiple popular ellipsoidal models, and which one will be the most accurate depends on where your points are located on the earth. geopy includes a few good models in the 
<code>distance.ELLIPSOIDS</code> dictionary:
<pre>
<code>#             model             major (km)   minor (km)     flattening
ELLIPSOIDS = {'WGS-84':        (6378.137,    6356.7523142,  1 / 298.257223563),
              'GRS-80':        (6378.137,    6356.7523141,  1 / 298.257222101),
              'Airy (1830)':   (6377.563396, 6356.256909,   1 / 299.3249646),
              'Intl 1924':     (6378.388,    6356.911946,   1 / 297.0),
              'Clarke (1880)': (6378.249145, 6356.51486955, 1 / 293.465),
              'GRS-67':        (6378.1600,   6356.774719,   1 / 298.25),
              }
</code>
</pre>Here's an example usage of 
<code>distance.distance</code>:
<pre>
<code>&gt;&gt;&gt; from geopy import distance
&gt;&gt;&gt; import Case
&gt;&gt;&gt; wiki = Case.Geocode.CaseWikiGeocoder()
&gt;&gt;&gt; _, a = wiki.geocode('Wade')
&gt;&gt;&gt; _, b = wiki.geocode('Fribley')
&gt;&gt;&gt; distance.distance(a, b).kilometers
1.342250272726943
&gt;&gt;&gt; distance.distance(a, b).miles
0.83403565192666562
</code>
</pre>Using Great-circle distance:
<pre>
<code>&gt;&gt;&gt; distance.distance = distance.GreatCircleDistance
&gt;&gt;&gt; distance.distance(a, b).miles
0.835175984734287
</code>
</pre>You can change the ellipsoid model used by the Vincenty formula like so:
<pre>
<code>&gt;&gt;&gt; distance.VincentyDistance.ELLIPSOID = 'Intl 1924'
</code>
</pre>The above model name will automatically be retrieved from the ELLIPSOIDS dictionary. Alternatively, you can specify the model values directly:
<pre>
<code>&gt;&gt;&gt; distance.VincentyDistance.ELLIPSOID = (6377., 6356., 1 / 297.)
</code>
</pre>Oh yeah, you can add distances too (for paths and such). Here's the distance from 
<a href="http://wiki.case.edu/Fribley">Fribley</a> to 
<a href="http://wiki.case.edu/Wade">Wade</a> to 
<a href="http://wiki.case.edu/Phi_Kappa_Theta">Phi Kappa Theta</a>:
<pre>
<code>&gt;&gt;&gt; _, c = wiki.geocode('Phi Kappa Theta')
&gt;&gt;&gt; (distance.distance(b, a) + distance.distance(a, c)).miles
1.0596624112817861
</code>
</pre>Also included in the 
<code>distance</code> module are functions for converting between length units (kilometers, miles, feet, nautical miles), and calculating a destination given a starting point, initial bearing, and distance. This stuff is still just in trunk, no egg or updated documentation yet...</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
><entry
><title
>geopy: Now on Google Code, More Geocoders</title
><link href="http://blog.case.edu/bmb12/2006/09/geopy_now_on_google_code_more_geocoders"
 /><id
>http://blog.case.edu/bmb12/2006/09/geopy_now_on_google_code_more_geocoders</id
><published
>2006-09-11T03:11:23Z</published
><updated
>2006-09-11T03:15:10Z</updated
><category term="Programming" label="Programming"
 /><category term="Projects" label="Projects"
 /><category term="Python" label="Python"
 /><category term="geopy" label="geopy"
 /><content type="xhtml"
><div xmlns="http://www.w3.org/1999/xhtml"
>I decided to try out the Google's 
<a href="http://code.google.com/hosting/">Project Hosting</a> feature for 
<a href="http://exogen.case.edu/projects/geopy">geopy</a>. You can find the hosted page at 
<a href="http://code.google.com/p/geopy/">code.google.com/p/geopy</a>. So far it seems pretty sweet and very easy to administer. I added a geocoder for Microsoft's 
<a href="http://local.live.com">Windows Live Local</a> (powered by 
<a href="http://virtualearth.net">Virtual Earth</a>) to the 
<a href="http://exogen.case.edu/projects/geopy/source/geopy.geocoders.html">geocoders module</a>. Sadly, they don't actually have a non-JavaScript geocoding API, so I had to reverse-engineer it. Norman Khine and I have been 
<a href="http://groups.google.com/group/geopy/browse_thread/thread/d316e0a782bdb73f/#">investigating issues geocoding UK addresses</a> with the Google Maps API. Due to contractual reasons, they can't offer geocoded addresses with their HTTP geocoder. So instead I again had to reverse-engineer their JavaScript to get it to work. The geocoded results aren't always accurate, but this is Google's problem and not geopy's. I also tried to add a geocoder for 
<a href="http://www.mapquest.com/features/main.adp?page=developer_tools_oapi">MapQuest's OpenAPI</a>. It is possible to get geocoded results over HTTP (although they don't tell you how, you have to look at their JavaScript or guess), but unfortunately they require you to parse the input location first. This is totally lame. You're telling me they can't parse the address into street, city, country for me? I didn't want to have to do this, but I now plan to add address parsing methods to geopy.</div
></content
><author
><name
>Brian Beck</name
><email
>brian.beck@case.edu</email
><uri
>http://blog.case.edu/bmb12</uri
></author
></entry
></feed
>
