c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
esc
cancel

Latest Updates: python RSS

  • erik 8:02 pm on February 19, 2010 | 0 Permalink | Reply
    Tags: , , , python

    jsmacro 0.2.3

    The latest jsmacro (v0.2.3) adds support for “else” clauses to “if”, “ifdef”, and “ifndef” statements. Combine this with the command-line variable definition support and you can now do fun things like this:

    
    //@ifdef IE6_BUILD
     ...custom IE6 code here
    //@else
     ...code for other browsers here
    //@end
    

    Of course, this goes against the idea that your JavaScript would remain usable for development without needing to be processed, but it’s just an example. Longer term, I hope to have a different approach available that will allow conditional code substitution so that browser specific optimizations won’t get in the way of an easy development/test/debug process.

     
  • erik 7:38 pm on January 25, 2010 | 0 Permalink | Reply
    Tags: , , , python,

    jsmacro — an oddly named JavaScript preprocessor

    For awhile now I’ve wanted a JavaScript preprocessor to conditionally include debug and testing code when needed. It’s always registered as merely a “nice to have”, so I hadn’t sought one out. However, I had a little time over the weekend and wanted to play with the idea, so here it is: jsmacro (on GitHub.)

    [Note that before writing this I did seek out existing implementations, and found js-preprocess to be the most interesting; However, I needed something that would work as part of an existing build chain, so authoring the tool in Python instead of JavaScript made more sense.]

    Currently, jsmacro is poorly named, as I didn’t write the macro system that was in my head. Instead, it’s currently a basic preprocessor supporting only DEFINE and IF statements, which happened to be all I needed at the time. Usage works like this:

    Input JavaScript

    
      //@define DEBUG 0
    
      var foo = function() {
        //@if DEBUG
        alert('This.');
        alert('That.');
        //@end
    
        print "Hi";
      };
    

    Pass the above JavaScript through jsmacro from the command line like this: ./jsmacro.py -f infile.js > outfile.js (assuming the files are all in the same directory), and you get the following:

    Output JavaScript

    
      var foo = function() {
    
        print "Hi";
      };
    

    The tool has registered the variable ‘DEBUG’ as 0 (i.e., false), so the conditional include statements omit the alert() calls. If DEBUG had been set to 1 (i.e., true), the alert() statements would remain (though all jsmacro instructions would be removed either way.)

    One of the tricky things about doing macros or preprocessing in JavaScript is that I wanted the code to be valid JavaScript before the tool is run (which is why C-preprocessors won’t work.) The idea is that you develop as you normally would, but wrap your debug and testing code in conditional jsmacro statements so that they are automatically removed as part of your build process.

    There’s nothing fancy about the current implementation (it’s a crude state machine that scans line-by-line, top-to-bottom looking for regex patterns and deciding whether to output the line of not.) Crude as it may be though, it completely solved a problem for me, and hopefully it will help you out as well.

     
  • erik 9:47 pm on August 5, 2009 | 2 Permalink | Reply
    Tags: , projects, python, tinylinkfeed

    Announcing tinyLinkFeed — RSS feeds for links posted to Twitter

    I’ve come to enjoy the link-sharing that happens on Twitter, but a few months back I found myself with less time to regularly scan my Twitter stream.

    Since I don’t always need real-time links, devising a mechanism to time-shift, and read at my leisure, seemed appropriate. Furthermore, since I don’t mind following friend’s del.icio.us feeds with a feed reader, converting tweets-with-links into RSS feeds seemed like a natural fit. After some brainstorming with @karllong and a bit of hacking, tinyLinkFeed was born!

    tinyLinkFeed is designed to aggregate microblogging streams (like Twitter), and expose RSS feeds for posts containing URLs. To make the feeds a bit more useful, tinyLinkFeed also resolves shortened URLs, so you know what you’re linking to. (The URL un-shortening is done by another webservice I wrote at the same time, called tinyexpander; but I’ll save those details for another post.)

    Using tinyLinkFeed is pretty straight-forward. For example, my normal Twitter page can be found at http://twitter.com/smartt, so my tinyLinkFeed page lives at http://www.tinylinkfeed.com/twitter/smartt. To follow the links I post to Twitter, you’d point your RSS reader to http://www.tinylinkfeed.com/feed/twitter/smartt.xml. For something perhaps more useful, you might try Tim O’Reilly’s link feed: http://www.tinylinkfeed.com/feed/twitter/timoreilly.xml (which is actually one of the main reasons I wrote tinyLinkFeed.)

    The application came together fairly quick (it’s written in Python and runs on Google App Engine), though I haven’t had time to expand it since launch. I have a few ideas on what to add next, but I’m setting up getSatisfaction so you can leave ideas as well.

    Adding profiles to tinyLinkFeed’s aggregator isn’t openly available yet, so if you’d like a profile added, drop a message to @smartt or @tinylinkfeed. I hope you find it useful!

     
  • erik 5:15 pm on May 16, 2009 | 2 Permalink | Reply
    Tags: , , python

    How-To: Dynamic WWW-Authentication (.htaccess style) on Google App Engine

    Sometimes classic Basic Access Authentication is the right approach to password protecting a webpage. It’s not secure from sniffing, but functional if you’re just trying to ward off the casual surfer in the wrong spot. (For example, restricting access to your cat pictures, not your missile silo codes.)

    Basic authentication is often added to sites (or directories) using a .htaccess file and something like this:

    AuthUserFile /home/foo/.htpasswd
    AuthName "Private Area"
    AuthType Basic
    
    <Limit GET>
    require valid-user
    </Limit>
    

    …but you can also do basic authentication on-the-fly by reading/writing HTTP Headers. To ask the browser for a user/password, you can raise a 401 error, and write a “www-Authenticate” header containing something like ‘Basic realm=”Secure Area”‘. To read the user/password, look for an Authorization header, grab it’s value, Base 64 decode it, and you should have a string in the form of “user:password”.

    Here’s how you might handle it with Google App Engine. (Well, really you might use a decorator.. but this example is easier to explain.)

    class AuthTest(webapp.RequestHandler):
      def get(self):
    
        # Wrapping in a huge try/except isn't the best approach. This is just
        # an example for how you might do this.
        try:
          # Parse the header to extract a user/password combo.
          # We're expecting something like "Basic XZxgZRTpbjpvcGVuIHYlc4FkZQ=="
          auth_header = self.request.headers['Authorization']
    
          # Isolate the encoded user/passwd and decode it
          auth_parts = auth_header.split(' ')
          user_pass_parts = base64.b64decode(auth_parts[1]).split(':')
          user_arg = user_pass_parts[0]
          pass_arg = user_pass_parts[1]
    
          checkAuth(user_arg, pass_arg) # have this call raise an exception if it fails
    
          self.response.out.write(template.render('templates/foo.html', {}))
    
        except Exception, e:
          logging.debug("AuthTest Exception: %s" % (e))
    
          # Here's how you set the headers requesting the browser to prompt
          # for a user/password:
          self.response.set_status(401, message="Authorization Required")
          self.response.headers['WWW-Authenticate'] = 'Basic realm="Secure Area"'
    
          # Rendering a 401 Error page is a good way to go...
          self.response.out.write(template.render('templates/error/401.html', {}))
    

    That’s all there is to it.

    If you want to programatically write an Authorization header (as in, sending authentication credentials to another site, like the Twitter API’s, for example) you’ll do something like this:

    request = urllib2.Request(url)
    request.add_header('Authorization', "Basic %s" % (base64.b64encode("%s:%s" % (user, password))))
    

    Enjoy!

     
  • erik 11:31 am on February 25, 2009 | 0 Permalink | Reply
    Tags: , , python

    Conway’s Game of Life in Nodebox

    I was reading Ben Fry’s thesis, Organic Information Design yesterday, came across the section on Conway’s Game of Life, and thought it would make a nice NodeBox demo.

    Here it is: conway-life.py

    Nodebox screenshot

    There’s not much to it, but it does show a software pattern I’ve been using frequently with NodeBox. Many of the NodeBox examples make heavy use of non-namespaced, global variables. I suppose it makes simple code easy to read for those new to programming, but it’s a habit you’ll want to break before your code starts getting more complex.

    What I’ve found helpful is to create a World/Universe/Controller/Stage object that drives the rendering. Instead of using multiple globals in draw(), the controller object keeps the main parameters as local properties, and instantiates any needed objects in it’s __init__(). This approach prevents global variables names from clashing, and allows for creative reuse of rendering components.

    Enjoy!

     
  • erik 10:32 am on February 23, 2009 | 6 Permalink | Reply
    Tags: , python

    Using pymunk (physics engine) in NodeBox

    NodeBox makes a great environment for data visualizations and generative art. It’s easy to get started in, and you get basic drawing, type, and image manipulation. When you’re ready for more, it’s not too difficult to bring in external Python libraries to connect NodeBox to other systems, or add physics and particle simulation to spice up your visuals.

    For those unfamiliar with NodeBox,

    “NodeBox is a Mac OS X application that lets you create 2D visuals (static, animated or interactive) using Python programming code and export them as a PDF or a QuickTime movie.”

    It uses PyObjC to embed a Python runtime into an OS X native application, and fits into the same toolbox as Processing and openFrameworks. It’s a bit slower to run complex animations in, but you’re coding in Python, you get the gorgeous fonts and anti-aliasing you’d expect on OS X, and it provides easy access to some OS X native libraries, like Core Image. For a quick look at what it can do, check out the NodeBox gallery.

    NodeBox includes it’s own Python build, which is nice for portability and reliability, but it uses a custom sys.path that doesn’t look for Python packages you might already have installed on your system. There are a few ways to deal with this:

    1. You can install your packages into NodeBox’s path, ie., ~/Library/Application\ Support/NodeBox/ — meaning that you can use them from NodeBox, but not from other scripts…
    2. You can import sys in your NodeBox code and manually modify the sys.path value to add your existing packages…
    3. You can install packages into your system site-packages directory, and sym-link them from NodeBox’s directory…
    4. You can make NodeBox use your system packages instead of it’s own by sym-linking ~/Library/Application\ Support/NodeBox to your site-packages directory of choice (ex., /Library/Python/2.5/site-packages)

    For this exercise, I’ll be adding pymunk (Python bindings for the Chipmonk physics library) to NodeBox using option #3: Installing pymunk globally, and sym-linking from NodeBox’s package directory. This allows me to run the pymunk examples from the command-line (which use PyGame and Pyglet), but still use pymunk from NodeBox. This may not always be the best solution, so you’ll have to pick what’s right for your needs.

    Let’s get started.

    Pymunk (at the time of writing) includes it’s own copy of the Chipmunk source code, making this whole process rather easy. Once you’ve downloaded and uncompressed the pymunk source, cd into it’s directory and build chipmunk using:

    > python setup.py build_chipmunk

    Now you can build and install pymunk:

    > python setup.py install

    This will install an egg (which I normally hate dealing with, but that’s another story.) If you don’t want the egg, just copy the pymunk directory into your site-packages.

    Now we’ll add pymunk to NodeBox’s path. My pymunk is in /Library/Python/2.5/site-packages/, so I’ll:

    > cd ~/Library/Application\ Support/NodeBox

    > ln -s /Library/Python/2.5/site-packages/pymunk-0.8.1-py2.5.egg/pymunk .

    Finally, NodeBox needs access to libchipmunk. I used this approach:

    > cd /Applications/NodeBox/NodeBox.app/Contents/MacOS/

    > ln -s ~/Library/Application\ Support/NodeBox/pymunk/libchipmunk.dylib .

    We should be done! Fire up NodeBox and try an include pymunk to see if it loads. If you don’t see any error messages, you’re good to go.

    If you’re new to pymunk (as I was until this week), head over to the Slide and Pin Joint tutorial to see how it works. The example is written for PyGame, so you’ll be doing a little rewriting to bring it into NodeBox.

    The following screenshot shows the Slide and Pin Joint demo within NodeBox using my take on porting it. I’m having a little trouble with the slide joint, but you can check out my code if you’re curious: slide_and_pinjoint_example.py

    pymunk in nodebox screenshot

     
  • erik 4:54 pm on September 9, 2008 | 2 Permalink | Reply
    Tags: , python

    Presenting at Austin Python User Group tomorrow (9/10) 7pm

    FYI, some colleagues and I will be presenting at the Austin Python User Group (APUG) meeting tomorrow night (Wednesday, September 10th) at 7pm. We’ll be talking “behind-the-scenes tech” for one of the high-traffic, Django-based sites we’ve been building over the past year.

    For directions and more info, see the APUG wiki: http://wiki.python.org/moin/AustinPythonUserGroup

    Hope to see you there!

     
  • erik 8:08 pm on July 21, 2008 | 0 Permalink | Reply
    Tags: , , python, ,

    Book Review: “Practical Django Projects”

    Summary:

    • Targeted at developers wanting to learn Django by building example applications rather then (or in addition to) reading the docs and man pages
    • The reader builds three working applications by following along
    • The examples are based on up-to-date Django features (ie., a 2008 build)
    • Lesson’s focused on using Django (not on Django’s inner workings)
    • Doesn’t waste time explaining Python and HTML (nor does it dive deep explaining the how/why of what you’re doing in the examples)
    • Introduces the reader to powerful Django features — covering a wide range of capability
    • Examples focus on designing for code reuse (and leading by example, by integrating with existing reusable apps and Python libraries)
    • Offers an alternative approach to learning, focused on relevant, practical examples

    Background:

    Practical Django Projects (Apress book description) was written by James Bennett, release manager and contributor to the Django Web Framework. It was published by Apress in 2008. This was Bennett’s first book.

    Full disclosure: I was provided with a free, review-copy of the book by Apress.

    The Book:

    Practical Django Projects introduces the reader to the Django Web Framework by example. It takes the reader step-by-step through three example projects: a basic CMS, a blog application (called Coltrane, which powers the author’s personal blog), and a code-sharing/snippets site (called Cab, which powers http://www.djangosnippets.org/.) The examples cover real-world problems (and integration tasks) that developers are likely to be interested in, and leaves the reader with three working Django applications.

    The lessons are spread across eleven chapters:

    1. Welcome to Django — a wonderfully short introduction that wastes no space explaining prerequisites (it assumes the reader knows Python)
    2. Your First Django Site: A Simple CMS — an introduction to the Django Admin and Flatpages
    3. Customizing the Simple CMS — customizing the Admin interface (adding TinyMCE) and developing a simple, reusable search feature
    4. A Django-Powered Weblog — defining the basic models, and using django-tagging and Generic Views
    5. Expanding the Weblog — adding del.icio.us-synced links, and custom categories
    6. Templates for the Weblog — more extensive use of Generic Views, template inheritance, and custom template tags
    7. Finishing the Weblog — using django.contrib.comments and model signals to develop a moderation system with email notification and Akismet integration; Using django.contrib.syndication to add RSS/Atom feeds
    8. A Social Code-Sharing Site — building the initial models, integrating with the pygments syntax highlighter, and writing custom model managers
    9. Form Processing in the Code-Sharing Application — great examples of using newforms (much better then the The Definitive Guide to Django’s chapter on form processing)
    10. Finishing the Code-Sharing Application — more custom template tags, this time used with bookmarking and rating features
    11. Writing Reusable Django Applications — a summary of Bennett’s philosophy on decoupling application features into reusable components (with references to the UNIX saying, “do one thing, and do it well”)

    The examples focus on building applications the “Django way” — meaning that they heavily leverage Django features such as Generic Views, custom template tags, and the django.contrib package. Each section starts by outlining the features to be developed, then walking the reader through model definitions, URLs, template design, and the request-handler (view) code.

    While working through the three example applications, Bennett teaches the reader how to decouple applications from projects, how to think about (and look for) opportunities for code reuse, and how to integrate with other reusable Django applications. The lessons aren’t so much “how does Django work”, but rather “how do you, as a developer, structure your projects to get the most out of the framework.” Depending on your level of comfort using Django and Python, the lessons will either be a breeze, or ridiculously confusing. (ie., there’s a lot of magic going on in the examples, and the book assumes that either you get it, you’re comfortable not knowing, or that you’ll figure out the finer bits when you need them.)

    The Core Message

    Ultimately, the book isn’t so much about learning Django, as it is about learning how to use Django properly (where properly is defined as the way in which the Django developers use Django.) From this perspective, it’s quite successful. The reader is shown a number of patterns and concepts that can be applied to any Django project.

    Bennett wraps up the book with a chapter on design philosophy, but I think the overall lesson of the book is best summarized on page 124, with the following quote:

    …this is the hallmark of a well-built Django application. Installing it shouldn’t involve any more work than the following:

    1. Add it to INSTALLED_APPS and run syncdb.
    2. Add a new URL pattern to route to its default URLConf.
    3. Set up any needed templates.

    This is the zen of pluggable Django applications. It’s the path Bennett wants to help you start down. The value of going down this path will depend on how often you’ll use Django in the future.

    Conclusion:

    Overall, I think the book will be more valuable for someone just getting started with Django, then someone who’s been hacking lower-level with the framework for awhile. It’s a developer-focused, quick-start, “get you on the right foot” kind of book that I certainly would have appreciated more a few years ago. The big question then, is whether this book is for you. The answer depends on a couple things, with the most important being how you like to learn. Do you prefer learning by example, or learning by reading the docs and building things on your own? If you prefer to have an expert guide you step-by-step, then this book is for you. You’ll still need to poke around in the Django documentation to really grok how it all works, but this book will get you up to speed quickly.

    If you’ve read the docs, done the online tutorials, and are still interested in picking up some best-practices on decoupling your code from your specific application (ie., learning how Django supports code reuse), then this may still be a book for you. If you know you’ll be building a large application, the lessons in the book might help prevent you from writing a single, monolithic application, or at least give you some insight into how to organize and package your code. Down the road you’ll thank yourself.

    For me personally, I was actually looking forward to this book before it came out. I think the Django docs online (as great as they are) can sometimes lack in providing best practices. However, I’ve also been using the framework professionally for a number of years (to deploy personal, start-up, and enterprise-class web applications), and I’ve previously built and deployed a pluggable, multi-site, Django-based blog engine (with del.icio.us and Akismet integration, flexible moderation rules, etc.), so the idea of using a blog engine as the core example in the book was a bit disappointing. That said, I did enjoy seeing another developer’s approach on solving the same problem, and I picked up a few nice tips around some of the more recent Django features.

    If you’re looking to build a reusable code library (and you should be, if you’re going to build more then one Django project) and ensure that you’re using Django efficiently, this book will help point you down the right path and have you thinking about decoupling your architecture from the start.

     
  • erik 8:35 am on May 13, 2008 | 0 Permalink | Reply
    Tags: , , python

    Austin Python Users Group meeting tomorrow (May 14th) with guest speaker Greg Wilson

    This month’s APUG meeting will feature guest speaker Greg Wilson, author of Beautiful Code, Data Crunching, Parallel Programming Using C++, Practical Parallel Programming, etc.

    For more details, see: http://wiki.python.org/moin/AustinPythonUserGroup and http://python.meetup.com/188/.

    Hope to see you there!

     
  • erik 7:07 pm on December 31, 2007 | 0 Permalink | Reply
    Tags: , , python,

    Reading “The Definitive Guide to Django”; Verdict: A solid learning reference for a beginning/intermediate Django user

    Last week I received a review-copy of the new “The Definitive Guide to Django” book from Apress. I hadn’t planned on buying the book since it seemed a little too beginner-focused; but I agreed to give it an honest reading, so I happily dove in with an “it’s Python, of course I’m going to like it” attitude.

    Background

    The book was written by Adrian Holovaty and Jacob Kaplan-Moss, the creators and “Benevolent Dictators” of the Django Web Framework. It was Holovaty and Kaplan-Moss’ first book, and, I believe, meant to be the first Django book to market. The book was drafted online; open to peer-review and community feedback; and ultimately published under the GNU Free Documentation License.

    From the get-go, the print edition had a few inherent market challenges to face: First, the entire book is available online, for free, at: <http://www.djangobook.com/>. Second, in many ways the book is a re-hash of the docs available at <http://www.djangoproject.com/documentation/>, which are also free. Third, the book covers Django 0.96, not SVN. (0.96 is technically the latest-snapshot release, but a lot has changed since 0.96.) And finally, the $45 MSRP could be seen as a little steep for what is effectively a printed copy of a free, online book.

    The print experience

    Diving in, the book takes the reader through the basic installation process, provides a brief background on how the framework came to be (and why you want one), then steps through the major features (ie., the template system, ORM, URLconfs, generic views, etc.) It’s what you’d expect from a technical reference — no fluff, and straight to the details. There are plenty of code snippets to learn from, and the sidebar notes tend to be insightful.

    Since it wasn’t new material for me, the book was a fairly quick read; but the experience of reading Django documentation in book-form was actually quite fascinating. There’s something about settling into a comfortable chair with a book, pen, and highlighter that you just can’t get with online documentation. Perhaps it was just a little more noticeable given the material. When I read the Django docs online, I tend to skim over them while trying to solve a problem. I use them as a reference more then a learning tool, and it’s usually while actively coding, thus my brain is partially distracted with whatever it is I’m building.

    With a physical book, you can unplug, step away from the computer, and give the material your undivided attention. This isolation from distraction results in a much deeper understanding of the text. This is the real the value of the printed book — it’s an opportunity to digest online documentation in an environment more conducive to learning and retention.

    My general take-aways and observations

    • The book definitely has a beginner/intermediate feel to it, but only in the sense of a beginner Django user — not a beginner Web developer or Python programmer. I’m curious how well the book is received by folks who are beginners at Django and dynamic Web development since the text brings up a lot of complex topics in Web development that aren’t really explained. (Ex., database administration, server clustering, manipulating HTTP headers, etc.)
    • The breadth of the book is impressive, but in some ways, the book really feeds you through a firehose, so to speak. It throws a lot of new concepts at the reader and doesn’t always explain why you’d need to know them, or how you might use them in the real world. For someone deploying a site with Django, it will be good to know that all these features are available, but it might take awhile before they need to use them (if ever.)
    • The book does touch on some of the more advanced Django features (like extending the template system and writing custom middleware), which was nice, but some topics are reserved for the appendix and get limited coverage (ex., model managers and ‘Q’ queries.) Others, like the Sites Framework, are given good exposure, but not so much that the reader is left with a clear picture on when to use them and what their limitations are.
    • The forms processing chapter was a bit lighter then what I was hoping for — especially given that the current newforms documentation still trends toward “read the source code.” It provides enough to start using newforms if your form needs are pretty basic, but doesn’t address creating your own widgets, or any of the fun stuff you can do once you start dynamically generating and manipulating newforms objects.
    • It might have been nicer if the examples in the book were a little more tied together, perhaps all focused on building a single example project and showing how the various features are used in real-world applications. (The example of the book-publisher’s app was a reoccurring theme, but not so strongly that each chapter applied it’s new learnings to it.)
    • The Deploying Django: “Going Big” sub-section provides a nice infrastructure graphics for how high-traffic systems might be setup, but once you get to the point of being “big”, you need to architect for it, and that’s really outside of the scope of this book. For this section, it might have been nice to reference other resources on scaling infrastructure, and perhaps pointing out some of the ways that Django can be optimized for performance and horizontal scaling. (For example, one of the Django projects we put into production at work will happily support 1,200 requests/second, but the database layer and session middleware have been reworked a bit, and the content caching approach is a little different then the standard Django offering.)
    • On the more positive side, even as someone who’s been using Django for some time, I still learned a few new tricks, and I was reminded of a few features that I could be taking better advantage of. (And when you do this stuff professionally, every shortcut and productivity gain has monetary value — avoiding even a half-hour of debugging pays for the cost of this book.)
    • This book would make a fantastic read for a back-end developer joining a project that is already using Django. I normally tell new developers to go through the Python Tutorial at <http://python.org/doc/tut/> if they’re new to Python, then to complete the Django Tutorials at <http://www.djangoproject.com/documentation/> before trying to grok any in-progress Django project. Now I have a third reference (though I might still suggest that they walk through the tutorials first, so that they have some context when reading the book. Otherwise, there are just too many new concepts to do a straight read-through and still grasp it all.)

    Summary

    The market needed a good Django book, and this one delivered a solid reference for the framework. Arguably, it’s not really a “Beginner’s Guide to Django”, but hopefully it covers enough of the basics that future books can focus on best practices and more advanced techniques. (On a related note, there’s apparently an upcoming “Practical Django Projects” book, also from Apress, that will focus more on building “reusable Django applications from start to finish”. This might actually make for a better beginner’s book, depending on how it turns out. [Via The B-List: Speaking and writing].)

    The million-dollar question then, is “Should you buy this book?” My answer ended up being a bit more positive then I expected, but there are two parts: First, if you’re a front-end developer only, you don’t need this book. You can just read Chapter 4: The Django Template System online, and then use the “Django Templates: Guide for HTML authors” section of the online docs as a reference. For back-end developers, the story is different. If you’re going to just “read it while you hack”, then you might as well just read it online; but if you’re serious about building applications with Django (especially if you’re new to it), then you should consider the book and investing the time to step away from the computer and really let yourself get into it. Unless you are an active contributor to Django (which I’m not, just to be clear), the odds are pretty good that you’ll learn something new, even if you’re already using Django today.

     
  • erik 9:52 am on March 30, 2007 | 0 Permalink | Reply
    Tags: , , python, ,

    Django “lorem ipsum” generator (and a new contrib.webdesign module)

    Django “lorem ipsum” generator (and a new contrib.webdesign module)

    The Django Web Framework project just added a new contrib.webdesign module with an amazingly simple, but incredibly handy first feature: a lorem ipsum generator. The idea is that a project’s base templates can include generated lorem ipsum for testing layout and page flow, but inheriting templates can override the generated text once real content is available.

    The lorem tag is used like this (via the contrib.webdesign docs):

    • {% lorem %} will output the common “lorem ipsum” paragraph.
    • {% lorem 3 p %} will output the common “lorem ipsum” paragraph and two random paragraphs each wrapped in HTML <p> tags.
    • {% lorem 2 w random %} will output two random Latin words.

    In practice, you might do this:

    templates/template.html:

    
    <html>
      <head>
        <title>{% block article_title %}{% lorem 5 w %}{% endblock %}</title>
      </head>
      <body>
        <div class="article">
          <div class="article_title">{% block article_title %}{% lorem 5 w %}{% endblock %}</div>
          <div class="article_body">{% block article_body %}{% lorem 4 p %}{% endblock %}</div>
        </div>
      </body>
    </html>
    

    And then inherit when you’re ready:

    templates/article.html:

    
    {% extends "template.html" %}
    
    {% if article %}
      {% block article_title %}{{ article.title }}{% endblock %}
      {% block article_body %}{{ article.body }}{% endblock %}
    {% endif %}
    

    Previously, I used to just paste lorem ipsum text directly into the main template (wrapped in block tags for overridding), but this new tag will let you skip the copy/paste routine. Very nice!

     
  • erik 11:16 am on February 27, 2007 | 1 Permalink | Reply
    Tags: , python,

    PyCon 2007 wrap-up

    I’m back from PyCon 2007. It was a busy weekend, with 593 Pythonistas attending the conference. I took a fair amount of notes, but I’ve pulled out some highlights below:

    From Ivan Krstic’s keynote on the One Laptop Per Child project:

    • Python is the language of the One Laptop Per Child (OLPC). Everything that can, will be done in Python… and there’s a “view source” button on the keyboard (view layout) so you can view (and edit) the source of your current running application.
    • The filesystem (which supports versioning) is called Yellow, and will be released withing a week or so. The GUI is called Sugar, and is available on http://dev.laptop.org/ to play with. You can download the full image (or build the environment on Linux.)
    • The OLPC supports 802.11s mesh networking.
    • The hand crank was removed for case durability. The OLPC’s are designed to last five years, but the torque from the hand-crank would have stressed the plastic case too much for it to last that long.
    • The first OLPC’s will start shipping in August of this year!
    • The OLPC hardware was getting ~1100 pystones before optimization. They are now up to ~2300 pystones (on a 366 Mhz AMD Geode processor.) (Note: This means they have better Python performance then Python for S60 is seeing on current S60 phones.)

    From the Web Frameworks panel:

    • James Tauber, “Reinventing the wheel is great if your goal is to learn more about the wheel.”
    • Jonathan Ellis, “When you control the whole stack you can innovate faster.”

    From Adele Goldberg’s keynote:

    Public school education is so bad that real eLearning solutions can’t go to the schools — they need to be outside of schools so that you don’t have the traditional censorship that comes with public schools — and you don’t have the associates with the bad experiences kids have while at “school”.

    From Jacob Kaplan-Moss’ talk, “Becoming an open-source developer: Lessons from the Django project”:

    1. Use good tools. “Open source is better because it’s better.”
    2. Avoid dogma. Don’t get stuck on what language something is implemented in.
    3. Work with (and hire) smart people. The model in open source is that if you’re smart, people listen to you. That’s rough if you’re not smart… But also means that it’s worthwhile to mention when you’re an expert on a topic.
    4. “Methodologies” suck. Ex., MVC is cool, but Django abuses it because it doesn’t fit so well with the web.
    5. DRY — Don’t Repeat Yourself. The one methodology to use.
    6. The business case for open source. You have to make one (to your company.)
      • Money. You’ll get recognized and sell services because of it. (Ex., Ellignton wouldn’t be as successful without Django.)
      • Free labor. (Sad to think of this way, but true when you have an interesting project.)
      • Self-improvement. Knowing that peers will review your code makes you much more careful about the code you submit. This makes the code a lot better.
      • Geek cred — gaining credibility within the geek community makes it easier to hire great people.
      • Moral Argument — If you built a business on open source — it’s time to give something back.
      • Figure out where to draw the line — Django gave away the tools, but not all the apps.
    7. Selling open source to other companies. Microsoft’s FUD had been quite successful in some areas. Counter the “communist” argument with a “freedom” argument. Focus on the freedom of data — your data belongs to you; there is no vendor lock-in. Open vs. Lock-in is a better argument then Open vs. Closed.
    8. Create a community. This doesn’t just happen because you setup a mailing list. (Gave example of thanking people who post anti-Django blog posts and asking what they didn’t like.) Don’t say anything that would get you kicked out of a bar.
      • Avoid monsters (trolls, vampires, etc.) Detect them early, and ignore them.
      • Spam can’t be an afterthought. Collaborative tools require spam filtering from Day 1. You’ll get spam. Lots of it. Google Groups is pretty good about cutting out spam.
    9. listen to the community. But smartly. Sometimes the vocal majority doesn’t represent the wishes of the whole community. Django’s magic-removal was a big risk, driven by the community. You also have to be willing to ask for help. Sometimes you don’t feel comfortable delegating tasks that you think suck — but not everyone has the same definition of “what sucks” — sometimes there’s someone who actually WANTS to do this task!
    10. Handling community contributions. You need a defined method for how you take contributions. It helped the Django project when they adopted a system for differentiating between patches that are controversial, and those that aren’t. (ie., simple bug fixes vs. design decisions.) A ticket reviewer makes this decision.
    11. Learn to be comfortable saying ‘no’ — there are plenty of Python web frameworks, and maybe someone’s needs are better handled by another framework. “If everyone can check in features, you have PHP.”

    From “The absolute minimum an open source developer must know about intellectual property”:

    • It’s a lawyer’s job to figure out what will go wrong with your plan. They are professional pessimists.
    • Only the “claims” in a patent are covered, not the stuff in the “specification.”
    • A header file is a “purely functional” expression, thus NOT-copyrightable.
    • If you don’t protect your Trademark, you lose it. This is why companies have to send cease and desist. The “get a first life” situation was important because Lindon explicitly granted them a license to use the Second Life trademark in the parody, thus they were able to demonstrate that they were protecting their mark.
    • If you tell someone how to do the work (ie., “work for hire”), then you own it.
    • An independent contractor owns their work unless the contract specifically assigns the rights to the company.
    • The person who made a patch owns the patch. By giving it to you, you get an applied license to use it, but because it’s implied, it’s fuzzy as to what you can do with it.

    From Robert M. Lefkowitz’s keynote:

    • Only 2% of the population can read source code. (And free software doesn’t matter if no one can read it!)
    • Proprietary software values function. Free software people value the building of the “community of learning” around the software, even if it has fewer features.
    • The traditional view is that computer literacy is about one’s ability to use applications, rather then to program. If this is right, then what’s the point? Computers might as well be printing presses.
    • In literature, you read the greats (ex., Shakespeare), then try to write like them. So in computer literacy, who are the greats? If we were going to make every high school students memorize a program, what would it be?
    • Great programmers break the rules elegantly. Bad programmers break the rules without realizing it.
     
  • erik 3:10 pm on February 21, 2007 | 0 Permalink | Reply
    Tags: , python,

    Heading to PyCon 2007

    I’m off to PyCon 2007 (Dallas, TX) in the morning. I managaged to get into the Advanced Django tutorial (which I’m really looking forward to), so I’m heading up a day early. If you happen to be there, hopefully we’ll cross paths!

     
  • erik 1:23 pm on December 30, 2006 | 8 Permalink | Reply
    Tags: , , , python, ,

    Moving my blog from WordPress to Django; Part 2: Migrating the data

    In Part 1 of this series, I described some of the motivation, and the components being used to build a new blog for myself. In this (lengthy) post, I’ll address the solution I used to move my content archives from WordPress to the new app.

    Installing new blog software is generally easy, but if you have legacy content that you need to preserve, the ability to move content between systems becomes of utmost importance. Fortunately, it’s quite common for popular software to provide import/export features; Having good tools to migrate content reduces switching costs, making it easy to try new software without fear of content lock-in. Unfortunately, with a home-grow blog platform, these tools need to be written from scratch.

    For my soon-to-be-launched Django-based blog, importing content from my WordPress installation was an early priority — there’s only so much testing you can do with lorem ipsum posts. In tackling this content migration, I considered the following four options:

    1. Support the legacy database schema.
    2. Export and Import at the database level (ie., SQL dump, some text file munching, and SQL imports.)
    3. Write an adapter layer to pull from the existing database and insert into the new database.
    4. Export the content into a neutral format, and import from that format.

    Regardless of the approach taken, I also added one important requirement: The import solution had to be so easy (and easily repeatable) that I would never hesitate to make a change to the database models when needed. Naturally, it’s nice to freeze the model once you have a stable release, but during development, even the database model should be open to agile iteration. I’ve worked on systems where every model change meant writing accompanying SQL scripts to alter the tables, and while effective, it wastes time, and I wanted the option to simply export, wipe the database clean, and re-import whenever needed. (And preferably by simply running a single script.)

    I finally settled on option #4, to export into a neutral format (XML), and write an importer for that format; However, I did briefly consider each of the above options:

    1) Supporting the legacy (WordPress) database schema sounds nice on the surface. This would allow the two systems to share the same database (thus eliminating the need to migrate content at all), while making it extremely easy to run the systems side-by-side (perhaps even balancing traffic between the two to test the deployment.) The downside though, is that the custom application would need to maintain the data relationships that WordPress was relying on. It’s certainly doable, but on further investigation, I found that I didn’t actually like everything about the WordPress schema; There was a bit too much de-normalized data that I didn’t want to keep around.

    2) Exporting and Importing at the database level would essentially involve a mysqldump, some sed/grep/perl magic, and a SQL import into a new database. This would get the job done, but could very well lead to endless hours of tweaking regex patterns; and the end result would basically be throw-away code.

    3) Writing an adapter layer was actually the most tempting at first. I knew that Django contained a tool for generating model definitions based on an existing database schema. If this worked for the WordPress database, then all I would need to do is write a thin layer to fetch content from one model and stick it into another. Sure enough, the `inspectdb` tool did do a good job, and I got so far as having routines for pulling posts and comments before realizing that this also wasn’t as reusable a solution as I wanted. Complicating matters was the need to do all this magic in a single database, since the Multiple Database Support branch of Django is still in development/testing.

    With the above options scratched off the list, I went in search of a means to export directly from WordPress into a neutral format. With a little googling, I found some posts about an export/import feature that might be “in development” in the WordPress tree, but I found no documentation on the feature. Fortunately, a few more searches turned up the “WordPress XML Export” plugin, which sounded like an effort to backport the exporting feature to early versions of WordPress. After first installing the XML Export plugin, I found that it didn’t actually work with the version of WordPress on my server, but a quick look through the source code revealed a hardcoded version check that was easy enough to modify. With that change made, the plugin has run like a champ ever since.

    The XML Export plugin outputs the full contents of a WordPress blog into a WXR file (WordPress eXtended RSS), which is an RSS 2.0 file, extended with a wordpress export namespace so that it can include extra metadata and comments.

    With the content archives now in a massive RSS file, the next task was to write an importer. To parse the XML, I decided to use ElementTree for it’s simplicity in getting the job done. Pulling the file into ElementTree is a one-liner (when wordpress_xml_file is a File object):

    tree = ET.parse(wordpress_xml_file)

    The entries can be easily iterated:

    for item in tree.findall("channel/item"):

    Extracting the basic elements was also straight-forward (which I stuck into a Dictionary):


    results['link'] = item.find("link").text
    results['pubDate'] = item.find("pubDate").text
    results['summary'] = item.find("description").text
    results['body'] = item.find("{http://purl.org/rss/1.0/modules/content/}encoded").text
    results['post_date'] = item.find("{http://wordpress.org/export/1.0/}post_date").text
    results['post_date_gmt'] = item.find("{http://wordpress.org/export/1.0/}post_date_gmt").text

    Extracting the Categories/Tags was only slightly more work:

    
    results['categories'] = []
    
    categories = item.findall("category")
    
    for c in categories:
        results['categories'].append(c.text)
    

    Pulling the comments was the only messy part of the process. The list of comments is easy enough to fetch…

    comments = item.findall("{http://wordpress.org/export/1.0/}comment")

    …but extracting the actual comment text is a little more work because some comments may contain child nodes. For example, a comment containing a hyperlink, bold tag, or any other HTML will be truncated if you simply use the `.text` attribute. To crawl the comment text and child tags, I used the `getiterator()` method, while concatinating `.text` attributes to assemble the full comment text. While doing this, I also decided to filter out any HTML tags from the comments, which made the process fairly simple:

    
    tmp_comment_list = []
    
    comment_tag = comment.find("{http://wordpress.org/export/1.0/}comment_content")
    
    for comment_tag_child in comment_tag.getiterator():
        tmp_comment_text = comment_tag_child.text
        if tmp_comment_text: tmp_comment_list.append(tmp_comment_text)
    
    the_comment['body'] = ' '.join(tmp_comment_list)
    
    results['comments'].append(the_comment)
    

    By writing an importer for the WXR/RSS 2.0 format, this not only solves the problem at hand, but also sets the groundwork for a reusable RSS importer. IMO, this potential reuse adds additional value to the solution (as opposed to one-off SQL munching or custom adaption layers), which makes it worth any additional work that might have gone into it. With a little re-factoring, the same system could also be extended to support the Movable Type Import Format, making the software very easy to setup and evaluate.

    In Part 3, I’ll skip some of the development details and jump into the server issues, with a focus on why the new blog hasn’t launched yet. The answer lies heavily in the challenge of running a Python-based application server in shared hosting environments. The common lack of mod_python, the RAM hit, etc., all add to the complexity in adopting Django.

     
  • erik 6:41 pm on December 27, 2006 | 3 Permalink | Reply
    Tags: , , python,

    Moving my blog from WordPress to Django; Part 1: Assemble the wheel, don’t reinvent it

    I was hoping to write this post as an announcement for my new blogging solution, but instead (since I haven’t flipped the switch yet), I thought I’d start off with why I’m doing it, and what software I’ve pulled together to keep from reinventing the wheel. (In future posts I’ll address the development itself, the unique features, and the major obstacles in moving from a WordPress installation on a shared server, to a custom web app written using Django. This last bit, the actual hosting of a Django app, is a significant one, as it is the primary issue causing a delay in switching over).

    I moved my blog to WordPress software (from PyBlosxom, and a number of home-grown solutions before that) back in April 2005. I’ve been quite happy with WordPress, and would definitely recommend it for anyone who doesn’t enjoy coding (and maintaining) their own web apps. After writing a few custom plugins and a plain, but functional theme, my WordPress-based blog has been churning reliably for well over a year. However, after also using Django for over a year to build other web apps, it became too tempting not to use Django for my own site. (It really is a great framework to work with, particularly if you’re a fan of Python.)

    Building a custom app isn’t all roses and cherries. (I’m not sure what that means, but it sounds good.) With an established open source solution like WordPress, you have access to thousands of testers and hackers, all working to ensure that the software is reliable. You have access to good documentation, and plenty of bloggers who post solutions for custom integration problems. Furthermore, with PHP support being almost ubiquitous in shared hosting environments, you can have a WordPress installation up and running in a matter of minutes.

    With a home-grown system, you do ALL the heavy lifting in development, testing, and maintenance, and in that regard, you’re re-inventing the wheel in some areas, and leaving a community of support behind. Viewed in this light, it seems a little silly to build a custom solution when a proven, free system already exists. But custom apps can have their advantages if you can still leverage some open source communities while assembling a solution that is architected to address the specific needs you have. In my case, I tried to do as little custom, one-off engineering as possible (expect in the fun areas), while enabling a unique flexibility to re-think content interaction on my blog. I wanted the ability to prototype new feature ideas at the speed of Python + Django (which is to say, very fast), but not get bogged-down debugging ORM’s and template engines. (I’ve spent plenty of time doing that in the past.)

    Not wanting to write everything from scratch, my new solution is LAMP based (Linux [Ubuntu to be specific], Apache 2, MySQL, and Python), using the Django framework for it’s generated Admin CMS, object-relation mapping library, templating engine, URL mapping, etc. In other words, the only thing not leveraged from the open source community is the actual business logic of my app (which in a blog, can be quite simple.) I’m even leveraging external services like Akismet (for filtering comment spam), and del.icio.us, flickr, and Technorati for pulling in external content and metrics. I’m also using ElementTree (for the XML parsing in my content import system), Pygments (for syntax highlighting the code embedded in blog posts), simplejson (for generating JSON from Python objects), PyTechnorati (for accessing Technorati’s API’s), the Universal Feed Parser (for pulling in external RSS/ATOM feeds), and the Yahoo! Interface Library (for the CSS Fonts and Grids libraries.) During development, I’ve also relied heavily on Subversion and Bazaar for my revision control needs.

    With this arsenal of open source software, I was able to feature-match the bits I wanted from WordPress rather quickly, and then iterate on the presentation and interaction without the burden of implementing everything from scratch. Needless to say, I’m excited about the new site (it’s been running parallel to my WordPress blog for several months), and I’m eager to see what happens when I finally flip the switch and start routing traffic to it!

     
  • erik 12:16 pm on December 7, 2006 | 2 Permalink | Reply
    Tags: , python

    APUG: Austin (TX) Python Users Group meeting tonight, 7pm at Enthought

    Just a reminder, the Austin (TX) Python Users Group meeting is tonight, 7pm, at Enthought, in downtown Austin. Eugene Oden will be giving a presentation on using Pyro (Python Remote Objects.)

     
  • erik 10:58 am on November 20, 2006 | 2 Permalink | Reply
    Tags: , python

    PyS60 1.3.14 released today: OpenGL ES support included

    The PyS60 (Python for S60) project has released version 1.3.14 today. Notable new features include OpenGL ES and text-to-speech modules! See the SourceForge download page for installers and source code.

     
  • erik 9:02 am on October 25, 2006 | 0 Permalink | Reply
    Tags: , , python

    Collecting Python for S60 success stories

    Even though I find the title a little inappropriate (sorry, Kevin), there’s a question being posed over on the Forum Nokia blogs that’s worth responding to if you happen to use Python for S60:
    What’s Python good for, anyway?

    Kevin Sharp (the post’s author, and one of Nokia’s Technical Editors), is looking to help promote Python for S60, but first needs to collect some success stories.

    I know there’s a few of you who drop by this blog from time to time that are doing amazing things with PyS60. If you can spare a few minutes to write a response, it would be a great help to the project. Thanks!

     
  • erik 11:01 am on October 12, 2006 | 3 Permalink | Reply
    Tags: , python, ,

    Resetting a Django environment

    For one of my Django-based projects, I decided to setup an automated functional-testing system using Selenium to add content to the Admin tool and verify that it works in the site. In order to use this in a “continuous-integration”-like manner, I needed a way to automate the tear-down, initialization, and setup of a fresh installation of the app.

    I use a few more tricks to get this all working, but I wanted to share a couple scripts I wrote to handle the database re-initialization. I gather from some of the Django discussions that similar functionality may be working it’s way into the mainline already, but for the time being, here’s what I’m doing.

    I broke the process into two scripts, not because it’s the best thing to do, but because doing the first part as a shell script made sense, and doing the second part in Python was easier.

    This first script take a brute-force approach at pulling the database settings from the project’s settings.py file, and using them to delete the existing database and create a new one by driving the command-line ‘mysqladmin’ tool. (There’s also some voodoo done elsewhere which results in the script using a different database name if it’s in the testing environment, but that’s for another post.)

    
    #!/bin/bash
    
    # Extract the user/passwd from the settings file
    username=`grep DATABASE_USER settings.py | awk -F\' '{print $2}'`
    password=`grep DATABASE_PASSWORD settings.py | awk -F\' '{print $2}'`
    database=`grep DATABASE_NAME settings.py | awk -F\' '{print $2}'`
    
    echo 'Clearing the database...'
    echo 'y' | mysqladmin --host=localhost --user=$username --password=$password drop $database
    mysqladmin --host=localhost --user=$username --password=$password create $database
    
    echo 'Setting up the database and test account...'
    ./dbinit.py
    
    echo 'Done.'
    

    This second script (called ‘dbinit.py’, and called from the script above) uses pexpect (an Expect-like module for Python) to drive the ’syncdb’ function of Django’s manage.py tool. When using pexpect, the thing to remember is that you have to “expect” the full, and exact string that the child process outputs. I got hung up on this at first, which is why you’ll see me using the more crude “.*” pattern below:

    
    #!/usr/bin/python
    
    import sys
    import pexpect
    
    child = pexpect.spawn('python manage.py syncdb')
    child.logfile = sys.stdout
    
    #child.expect('Would you like to create one now.*:')
    child.expect('.*:')
    child.sendline('yes')
    
    child.expect('Username.*:')
    child.sendline('SOMEUSERNAME')
    
    child.expect('E-mail address:')
    child.sendline('SOMEUSERNAME@foo.com')
    
    child.expect('Password:')
    child.sendline('NOTSOSECRETPASSWORD')
    
    child.expect('Password.*:')
    child.sendline('NOTSOSECRETPASSWORD')
    
    child.expect(pexpect.EOF)
    

    With these scripts in place, not only have I been able to setup an automated testing solution, but I also use them in early development when I’m still flushing out a data-model. This approach allows me to quickly reinitialize an environment — although you should use with caution since it also deletes all content from the database.

     
  • erik 10:20 am on October 12, 2006 | 0 Permalink | Reply
    Tags: , , python

    Open position at Nokia in Austin, TX for a Python/Java developer

    For all the Pythonistas, there’s a publicly-listed position open at Nokia’s Austin, TX office:

    Software Engineer:

    The Software Engineering position will be responsible for development of software analysis tools within an Integrated Development Environment. The candidate will be required to design/develop static analysis models within Python that provide expression parsing and design pattern matching of Symbian OS applications and various UI environments, including UIQ and Series 60. Integration of the analysis tools within a Java based integrated development environment will be required through the development of application plugins. The candidate will be working in a fast paced software development environment that is very customer focused and is able to adapt features to meet customer requirements within planned product release cycles.

    A couple thoughts to help interpret the above description:

    • You won’t be starting from scratch.
    • “A Java based integrated development environment” means Eclipse.
    • If you’re not familiar with what the Austin office does, check out the “Creating Carbide.c++ Blog.”

    In case the link to the job posting doesn’t work, try starting from the Nokia Careers page, which is either: <http://careers.nokia.com/> or <http://www.nokia.com/careers>.

    Take note: If you’re interested in the position, contacting me isn’t the way to go. Follow the link to the job description and click the “Apply” button. Even if you know people in the office, you need to apply online to officially become a candidate.