<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
      xml:base="http://reinout.vanrees.org/" xml:lang="en">
  <link rel="self"
        href="http://reinout.vanrees.org/weblog/atom.xml" />
  <link href="http://reinout.vanrees.org/weblog/"
        rel="alternate" type="text/html" />

  <div xmlns="http://www.w3.org/1999/xhtml">
    <a href="http://www.atomenabled.org/feedvalidator/check.cgi?url=http://reinout.vanrees.org/weblog/atom.xml">
      <img title="Validate my Atom feed" width="88"
           height="31"
           src="http://www.atomenabled.org/feedvalidator/images/valid-atom.png"
           alt="[Valid Atom]" border="0px" />
    </a>
    <p>
      <span>
        This is an Atom formatted XML site feed. It is intended to be viewed in
        a Newsreader or syndicated to another site. Please visit
      </span>
      <a href="http://www.atomenabled.org/">Atom Enabled</a>
      <span>
        for more info.
      </span>
    </p>
  </div>

  <title type="html">Reinout van Rees' weblog</title>
  <subtitle>Python, grok, books, history, faith, etc.</subtitle>
  <updated>2009-04-04T21:44:00+01:00</updated>
  <id>urn:syndication:a55644db8591c020bd38852775819a9a</id>

  
  <entry>
    <title>Circular import errors</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/15/circular-imports.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/15/circular-imports.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-15T00:00:00+01:00</published>
      <updated>2012-05-15T01:56:00+01:00</updated>

      
      <category term="django" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>In rare cases, you can import a file that imports the file you're
importing from. This might sound a bit recursive, and it is. In
Python it is called a <em>circular import error</em>.</p>
<p>The best way to recognize a circular import that goes wrong is to look at your
results. You will get an <tt class="docutils literal">ImportError</tt> message from Django, like <tt class="docutils literal">cannot
import some_view from your_project.views</tt>. When you look in
<tt class="docutils literal">your_project/views.py</tt> you see that <tt class="docutils literal">some_view</tt> really exists in that
very same file. Huh?  That wasn't expected.</p>
<p>This <strong>huh???</strong> is exactly what a circular import looks like. Something that
exists (you checked it five times at least) doesn't seem to exist. A computer
doesn't lie, but you start to wonder.</p>
<p>The problem is, Python cannot complete the import, so it complains about the
point where it goes wrong, even though the actual error is the circular import
loop as a whole.</p>
<p>The solution is to break the loop somewhere. Perhaps the thing you want to
import is best placed somewhere else? Most often, a circular import error
indicates an organization problem in your code.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>AOP in Python API design - Douwe van der Meij</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/aop.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/aop.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T13:37:00+01:00</updated>

      
      <category term="python" />
      
      <category term="django" />
      
      <category term="pygrunn" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>AOP is <a class="reference external" href="https://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect Oriented Programming</a>. It is not often
used in Python, you see it more often in for instance Java.</p>
<p>His program does calculations on biogas installations. And the calculation
needs to be exposed through a web API (using Django).</p>
<p>He thought: <em>what about aspects</em>? For instance the security aspect?
Statistics/logging aspect? The serialization aspect? All several aspects of
the API. He showed a bit of sample code where all these aspects were all
inside the same single Django view method. Looked like quite a mixed-up mess
(as was his intention).</p>
<p>The resulting code is both scattered and tangled. Scattered because one aspect
is scattered all over the place in your code. Tangled because different
aspects are often interweaved/mixed.</p>
<p>You can use AOP to fix this. Now, <strong>how do we implement this in pure
Python?</strong>. The number one candidate is <a class="reference external" href="https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Decorators">decorators</a>.</p>
<p>AOP deals with pointcuts, join points and advices. Advices can be pre, post
and around (&quot;a wrapper&quot;). You can use three decorators for these three
cases. In the end his example looked a bit like this:</p>
<pre class="literal-block">
&#64;secure
&#64;serialize
&#64;statistics
&#64;dispatch
def api_call(...):
    ...
</pre>
<p>AOP offers some brilliant concepts for software engineering. Separate your
concerns and aspects; <strong>avoid tangling and scattering</strong>!</p>
<p>In response to a question: no, this is not a framework, these are just regular
decorators. But he used the ideas of AOP to design his decorators.</p>
<a href="http://photos.reinout.vanrees.org/Trains/Trains-2010/12292714_5GzLqW#!i=1841069431&k=DStFwt7&lb=1&s=A" title="Station Groningen"><img src="http://photos.reinout.vanrees.org/Trains/Trains-2010/i-DStFwt7/0/M/Station-Groningen-M.jpg" title="Station Groningen" alt="Station Groningen"></a></div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Development laptop setup: explicit automation</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/pygrunn-development-automation.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/pygrunn-development-automation.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T11:53:00+01:00</updated>

      
      <category term="python" />
      
      <category term="pygrunn" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>I also gave a talk at <a class="reference external" href="http://pygrunn.nl">http://pygrunn.nl</a> on <em>development laptop setup: explicit
automation</em>. I don't have a summary of my own talk, but I have two other
things for you:</p>
<ul class="simple">
<li>The basic idea is the same as a presentation I gave earlier in
Amsterdam. And I do have <a class="reference external" href="http://reinout.vanrees.org/weblog/2012/02/15/laptop-automation.html">a summary of that one</a>.</li>
<li>My presentation is online at
<a class="reference external" href="http://speakerdeck.com/u/reinout/p/explicit-developer-laptop-automation">http://speakerdeck.com/u/reinout/p/explicit-developer-laptop-automation</a></li>
<li><a class="reference external" href="https://github.com/reinout/tools/">https://github.com/reinout/tools/</a> has some of the content. Look at the
readme files in the subdirectories.</li>
</ul>
<p>Later on I'll try to document my dotfiles, but you can already find a lot of
ideas and tips online for bash settings, for instance.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Distributed job scheduling - Niels Hageman</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/distributed-job-scheduling.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/distributed-job-scheduling.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T10:27:00+01:00</updated>

      
      <category term="pygrunn" />
      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>Sisyphus, which they made at <a class="reference external" href="https://paylogic.nl">paylogic</a>, is a
semi-distributed job scheduling system.</p>
<p>Originally they used cronjobs. It is <em>very</em> robust and reliable. But it has
limitations:</p>
<ul class="simple">
<li>The configuration is strictly on one machine only. You cannot have a pool of
machines where you choose one of them to execute it. So you're limited
regarding redundancy: a cronjob only runs on one machine.</li>
<li>A cronjob really looks at the time. If a task is scheduled for 7:00 and the
server is down at that time, the cronjob does not run. Even if the server
starts up again at 7:01.</li>
<li>You have limited monitoring. Cron doesn't care if something crashes. You
really have to monitor the logging.</li>
</ul>
<p>So... they wanted something new! Sisyphus. The features:</p>
<ul class="simple">
<li>A central configuration, so you can schedule on a pool of machines
(any/all).</li>
<li>There is a configurable &quot;time to live&quot; for jobs: you can run the 7:00 at
7:02 if you think that's still &quot;fresh&quot; enough.</li>
<li>Job results are saved, so you can monitor it way better.</li>
<li>There are configuration options for for instance maximum execution time (&quot;if
it takes more than 5 minutes, something is wrong&quot;). And you can terminate
remotely or suspend a job. And retry-on-failure is also an option.</li>
<li>There are a couple of interaction possibilities: SQL, commandline client,
Python API.</li>
</ul>
<p>The system depends on a central SQL database, so that part is not
redundant. But that central database is the most critical part of Paylogic, so
if the main database goes down, jobs that don't run is hardly a priority :-)
So this dependency is fine.</p>
<p>Sisyphus is divided into separate parts, like scheduler, dispatcher,
configurator, etc.</p>
<p>Nice detail: sisyphus is actually run as a cronjob on all the active hosts
(polling for jobs). If a job is available, a &quot;guardian&quot; claimes a process
using a database lock. The job is run in a subprocess. The guardian keeps
track of the job and takes care of the bookkeeping.</p>
<p>The current state: they are using it in production at paylogic. There are
still some rough edges that need attention, however. It is not open source
yet, but it will be.</p>
<a href="http://photos.reinout.vanrees.org/Trains/Trains-2010/12292714_5GzLqW#!i=1841068503&k=HSdwjh5&lb=1&s=A" title="Photo & Video Sharing by SmugMug"><img src="http://photos.reinout.vanrees.org/Trains/Trains-2010/i-HSdwjh5/0/M/Station-Groningen-M.jpg" title="Photo & Video Sharing by SmugMug" alt="Photo & Video Sharing by SmugMug"></a></div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Django-crispy-forms - Miguel Araujo</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/django-crispy-forms.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/django-crispy-forms.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T12:55:00+01:00</updated>

      
      <category term="django" />
      
      <category term="pygrunn" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p><a class="reference external" href="https://github.com/maraujop/django-crispy-forms">django-crispy-forms</a> is a
Django application, but <a class="reference external" href="https://twitter.com/maraujop">Miguel</a> thinks the
talk will help you also with designing other systems and applications.</p>
<p>Django has three ways to render forms: <tt class="docutils literal">as_ul</tt>, <tt class="docutils literal">as_p</tt>, <tt class="docutils literal">as_table</tt>. They
do the same, but render themselves in a different way. Common questions by
people new to Django is &quot;what about divs?&quot; and &quot;how to reorder fields?&quot;. For
the last one, you need to switch the order of the form fields in your python
form code. There are some other tricks like overriding the
<tt class="docutils literal">self.fields.keyOrder</tt> attribute. If you have many fields, regular list
methods like delete() and pop() and insert() might help you.</p>
<p>But... ModelForms for the admin interface are different again: the
abovementioned form tricks don't work there. And those tricks sound a bit
dirty anyway. So: <strong>how to customize the form output?</strong></p>
<p>You can do a lot in by customizing the Django form in the template, but most
of it will be hardcoded and hand-tweaked that way. And if you customize a
form, you'll often forget <tt class="docutils literal">form.errors</tt> and <tt class="docutils literal">form.non_field_errors</tt>, for
instance.</p>
<p><a class="reference external" href="https://github.com/maraujop/django-crispy-forms">Django-crispy-forms</a> was
formerly known as django-uni-forms. It was created by pydanny in 2008, Miguel
is now one of the main committers.</p>
<p>Crispy forms work on forms, modelforms and on formsets. A <tt class="docutils literal">|crispy</tt> filter
in the template renders your form as handy divs with better classes and IDs
which helps a lot if you want to customize your form with css. Neat!</p>
<p>Crispy also has a <tt class="docutils literal">{% crispy %}</tt> template tag. You can pass it a &quot;form
helper&quot;. A form helper is a global helper: it is decoupled from forms. So it
normally works with any form. There are some attributes on the form helper
that you can set, like method (get/post), <tt class="docutils literal">form_id</tt>, things like that.</p>
<p>You can customize such a helper specifically for one form and set the order of
the fields, for instance. There are <strong>so many</strong> things you might want to
customize; crispy supports/allows most/all of them with a <tt class="docutils literal">Layout</tt> class and
other layout classes like <tt class="docutils literal">Div</tt>. You can get really deep into the machinery
by letting crispy inject Django template code directly into the template...</p>
<p>For the ultimate in customizability, you can write your own layout class that
renders itself in whatever way you want. Layouts can be nested, so there is a
lot of flexibility here. You can also customize crispy's own templates that it
uses for fields and forms using the regular Django overwrite-a-template
mechanism.</p>
<p>Handy: crispy forms has specific support for <a class="reference external" href="http://twitter.github.com/bootstrap/">twitter bootstrap</a>. This helps you get a nice looking
form.</p>
<a href="http://photos.reinout.vanrees.org/Trains/Trains-2010/12292714_5GzLqW#!i=1841069084&k=NWZcDfF&lb=1&s=A" title="Station Groningen"><img src="http://photos.reinout.vanrees.org/Trains/Trains-2010/i-NWZcDfF/0/M/Station-Groningen-M.jpg" title="Station Groningen" alt="Station Groningen"></a></div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Geodjango - Ivor Bosloper</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/geodjango.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/geodjango.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T12:00:00+01:00</updated>

      
      <category term="pygrunn" />
      
      <category term="django" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>In the GIS world, everything used to be proprietary (ESRI, Oracle), but there is a lot of
commoditization in the last years. Lots of open source. One of those open
source pieces is geodjango.</p>
<p><a class="reference external" href="https://docs.djangoproject.com/en/dev/ref/contrib/gis/">Geodjango</a> is
bundled with Django, but out of the box you miss a couple of pieces. You need
to install a couple of extra libraries (gdal, geos, proj) and you need a
geospatial database (postgis, oracle, mysql, spatialite).</p>
<p>Ivor guided us through a sample application. Things like setting a gis
database instead of a standard one. Adding <tt class="docutils literal">django.contrib.gis</tt> to the
<tt class="docutils literal">INSTALLED_APPS</tt> setting. And special geometry fields for points, lines,
polygons. Using a specific <tt class="docutils literal">OSMGeoAdmin</tt> for showing a map in the admin
interface for those geo fields.</p>
<p>A limitation in geodjango is that it doesn't give you regular form fields for
the geo fields. They work in the admin, but not in regular forms. Luckily
<a class="reference external" href="http://django-floppyforms.readthedocs.org">django-floppyforms</a> <strong>does</strong>
provide them, so he used floppyforms to get nice forms including a map in his
regular web interface. Creating geojson from database content and show that in
the map.</p>
<p>(Note to self: look at <a class="reference external" href="http://trac.osgeo.org/proj4js/">proj4js</a>).</p>
<p>Geodjango is well-integrated into Django, but you <em>do</em> need to use the
geodjango variants of fields, databases, admins. &quot;You need to prefix the
stuff&quot;. You get a lot out of the box, but there's quite a learning curve. You
also need to learn quite some javascript for the user interface.</p>
<a href="http://photos.reinout.vanrees.org/Trains/Trains-2010/12292714_5GzLqW#!i=1841068789&k=VFNJ7Ff&lb=1&s=A" title="Photo & Video Sharing by SmugMug"><img src="http://photos.reinout.vanrees.org/Trains/Trains-2010/i-VFNJ7Ff/0/M/Station-Groningen-M.jpg" title="Photo & Video Sharing by SmugMug" alt="Photo & Video Sharing by SmugMug"></a></div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>I am doing HTTP wrong - Armin Ronacher</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/doing-http-wrong.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/doing-http-wrong.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T09:59:00+01:00</updated>

      
      <category term="python" />
      
      <category term="django" />
      
      <category term="pygrunn" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>According to <a class="reference external" href="https://twitter.com/#!/mitsuhiko">Armin Ronacher</a> most
(Python) web frameworks use a request/response style of handling HTTP. At his
company, they're treating HTTP a litle bit different. (So the talk is first
about some HTTP-usage-in-Python observations and second a look at the
alternative way they're treating HTTP).</p>
<p><strong>Note</strong>: my brother has <a class="reference external" href="http://maurits.vanrees.org/weblog/archive/2012/05/armin-ronacher-i-am-doing-http-wrong">a clearer summary</a>, btw.</p>
<p>The most low-level way is to write directly to the response. Write the
response headers, write the actual response content. In Python, you often have
some response object; often some sort of middleware gets the chance to do
something to the response on the way out.</p>
<p>The nice things we like about HTTP:</p>
<ul class="simple">
<li>It is text based. You can easily debug it.</li>
<li>REST is handy for APIs.</li>
<li>Content negotiation.</li>
<li>Caching.</li>
<li>Very very well supported :-)</li>
</ul>
<p>A basic question you should ask yourself is <strong>why does my application look
like HTTP?</strong> A common Django application gets a request, does something and
returns a response. Works well. But why is it set up that way? Why is it so
focused on HTTP? (It <em>is</em> logical that it focuses on this use case, but you
can still ask the question).</p>
<p>HTTP can be a stream or buffered. Sending stuff from the server to your
browser is a <em>stream</em>. But often an incoming request in a Python web framework
is first <em>buffered</em> internally (memory or disk). In the same way a request is
a bit of a strange mix:</p>
<ul class="simple">
<li>request.headers: buffered</li>
<li>request.form</li>
<li>request.files: buffered to disk</li>
<li>request.body: streamed!</li>
</ul>
<p>On the client (like your webbrowser) you cannot do anything to an incoming
request, once it started, is to close the connection. You cannot interact
anymore once you received your first incoming byte.</p>
<p>A consequence of the buffering and the way HTTP is handled is that you can
have problems accepting data. How big a file should you accept? How big an
incoming form? Buffer it in memory? Or on disk? And how do you handle
streaming? You might be streaming in one part of your code, but how do the
other layers handle it?</p>
<p>Internally in his company, he's trying to handle HTTP differently. There's no
direct HTTP contact in most of the code base. Everything that eventually ends
up in the HTTP layer is implemented as some sort of &quot;type object&quot;. This
allowed them to really flexible in the HTTP layer. Support for different
input/output format. Easier to test. Documentation can be auto-generated. Lots
of common errors can be catched early.</p>
<p>A basic rule is to be <em>strict in what you send, but generous in what you
receive</em>. But web Python code is often generous by &quot;just&quot; accepting a lot
without much checking. That might be a security risk. In Armin's system, you
<em>know</em> what type should be coming in, so you can do proper checking.</p>
<p>How does this deal with the big-upload problem? Incoming streaming data? Well,
because of the type system, you actually know which types need a streaming
API. This makes it easy to set up your API correctly. You can even selectively
use a different protocol than HTTP.</p>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Large number of markers on google maps - Oleg Pidsadnyi</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/many-markers-on-google-maps.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/many-markers-on-google-maps.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T14:32:00+01:00</updated>

      
      <category term="pygrunn" />
      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>He had to show lots of markers on a google map, which often led to bad
performance. And he wanted real-time updates. And... the client needs to be
responsive!</p>
<p>A marker is a DOM element, but also a shadow, a click area, an icon image and
so on. Which takes a lot of memory and rendering it takes lots of CPU.</p>
<p>How to optimize? One option is to reuse an existing marker image if they are
the same. If they are different, using an image sprite helps a lot. You have
to set offsets and sizes on the markers to get the sprite to work.</p>
<p>The lazy way to draw lots of markers is to just draw all of them, regardless
of the viewpoint. Most of them will probably not even be visible... Looping
over so many DOM elements is <strong>slow</strong>.</p>
<p>There are some weapons you can use to speed it up: the <em>idle</em> (zooming/panning
is done) and <em>zoom_changed</em> google map events. For this, <a class="reference external" href="http://underscorejs.org/">underscore.js</a> helps with functions like
<em>defer</em> and with handy collection types. Similarly <a class="reference external" href="http://documentcloud.github.com/backbone/">backbone.js</a> helps maintaining collections
and binding them to events and to an API on the server.</p>
<p><strong>Backbone trick</strong>. Backbone has no real handy way to remove items from a
collection that are old, you can only add or reset a collection completely. He
did it by adding a version number to the data he fetched from the server. If,
after an update, items have a lower version number, they're old and you can
delete them. This way he could refresh a backbone collection without needing
to zap it first and rebuilding it from scratch.</p>
<p>Now, what about the webserver? It should be simple, fast and scalable for his
usecase. He went for <a class="reference external" href="http://www.tornadoweb.org/">tornado</a>. Tornado is
quick. And it simplifies asynchonous code.</p>
<p>The database should be JSON friendly, support geo queries and it should have
an async Python binding. <a class="reference external" href="http://www.mongodb.org/">MongoDB</a> has it. You can
do queries like <tt class="docutils literal">{'loc': <span class="pre">{'$within':</span> <span class="pre">{'$box':</span> box}, }}</tt>. For the Python
binding he used <a class="reference external" href="http://pypi.python.org/pypi/pymongo/">pymongo</a>.</p>
<p>He used google's fusion tables for zoomed out levels: they group markers
together so that you only see an indication of the number of items instead of
showing all the markers. (<em>Reinout: but somehow he didn't do this, at least I
didn't see it in his demo. I probably misunderstood something.</em>)</p>
<a href="http://photos.reinout.vanrees.org/Vacation/2010-hunebedroute-Drente-NL/13314851_xd4D8L#!i=968600820&k=PyiDx&lb=1&s=A" title="Hunebed 16/53: G1 bij Noordlaren (enigste Groningse hunebed)"><img src="http://photos.reinout.vanrees.org/Vacation/2010-hunebedroute-Drente-NL/IMG8174/968600820_PyiDx-M.jpg" title="Hunebed 16/53: G1 bij Noordlaren (enigste Groningse hunebed)" alt="Hunebed 16/53: G1 bij Noordlaren (enigste Groningse hunebed)"></a></div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>Space invaders with SQLAlchemy - Michael Bayer</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/11/sqlalchemy.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/11/sqlalchemy.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-11T00:00:00+01:00</published>
      <updated>2012-05-11T15:37:00+01:00</updated>

      
      <category term="pygrunn" />
      
      <category term="python" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>His talk is titled &quot;space invaders, relational modeling and domain models, a
mashup&quot; instead of just the plain <a class="reference external" href="http://www.sqlalchemy.org/">sqlalchemy</a>
that I expected :-)</p>
<div class="section" id="space-invaders">
<h1>Space invaders</h1>
<p>He started his computer career with a <a class="reference external" href="https://en.wikipedia.org/wiki/TRS-80">TRS-80</a> in 1980. The killer app at that time
(5th grade school) was printing out a picture of Snoopy on the matrix
printer.</p>
<p>Next up an <a class="reference external" href="https://en.wikipedia.org/wiki/Atari_800">Atari 800</a>. There was a
great 'vertical scroller' game and he wanted to build his own. But you needed
to write assembly to pull it off which was way over his head. <em>Machine level
programming is depressing</em>.</p>
</div>
<div class="section" id="relational-modeling">
<h1>Relational modeling</h1>
<p>Somewhere in the 90 was, for him, the time of <em>architecture astronauts</em> and
<em>enterprise java beans</em>. 1200 database tables for reading a big SGML document,
that sort of nightmare. He was made to use the Pure waterfall method. Spending
a month only drawing Rational Rose UML diagrams. Also depressing.</p>
<p>But he <em>did</em> finally understand objects and liked them. A domain model is important.</p>
<p>He showed wordpress' comment edit code. PHP code; hacked often. Hardcoded
ad-hoc SQL code to delete comments, embedded right in the PHP code. Comments are
only an integer ID, not real comment objects, of course. In the end, he showed
the entire PHP page in 3px font size: HTML, SQL and PHP all mixed up in one
file. Horrid.</p>
<p>You basically give an integer ID through a couple of calls all the way through
the application. Only valid for small scripts, not really for a system the
size of wordpress. Can we call it the <em>water slide</em> model? One straight flow
down from an ID to some action.</p>
</div>
<div class="section" id="domain-models">
<h1>Domain models</h1>
<p>A domain model is &quot;a conceptual model of a domain of interest&quot;, according to
wikipedia.  You model the problem, agnostic of implementation details. And we
create explicit adaptions between the outside world and the model (adapters,
serializers, views, etc: separation of concerns).  And ideally we only use
neat normalized data.</p>
<p>To him, dealing with such a domain model feels a bit like a horizontal
pipeline. (<em>Reinout: not sure what he means with this</em>).</p>
<p>At the core of relational databases is the <a class="reference external" href="https://en.wikipedia.org/wiki/Relational_model">relational model</a>. Information represented as
collections of rows, consisting of columns. Mostly <a class="reference external" href="https://en.wikipedia.org/wiki/ACID">ACID</a>.</p>
<p>Essential to relational databases are joins: intersecting or joining
data. SQL, the language, is a <em>declarative</em> language (as opposed to
<em>imperative</em> or <em>functional</em>, see <a class="reference external" href="http://stackoverflow.com/questions/602444/what-is-functional-declarative-and-imperative-programming">stackoverflow</a>).</p>
<p>How do we reconcile the SQL/relational model with the domain model? He things
an ORM, <a class="reference external" href="https://en.wikipedia.org/wiki/Object-relational_mapping">object relational mapper</a>, is a good
idea. An ORM needs to map objects and collections to the tables, rows and
columns of the database, including relations.</p>
<p>Keeping the &quot;R is for relation&quot; intact is a real challenge. There are several
ORMs that don't expose enough of the available relations. Sqlalchemy does
expose it, you can keep thinking in terms of your database and its tables and
relations that you know.</p>
<p>Another challenge is to limit the amount of queries. For instance, when you
iterate over some set of database objects (rows) in Python, there is a big
risk that you do a follow-up query for every row. So you need to be able to
give the ORM some instructions for loading that extra related data
directly. Sqlalchemy helps you with this: this way you get the luxury of a
nice ORM, but you still have well-optimized SQL.</p>
</div>
<div class="section" id="mashup">
<h1>Mashup</h1>
<p>Mashup? Yep, he wrote a space invaders clone that stores all the game data in an
SQL database (sqlite, in-memory)! Idiotic example, but loads of fun.</p>
<p>The domain model includes missile, player, splat, enemy, army and saucer
<a class="reference external" href="https://en.wikipedia.org/wiki/Glyph">glyphs</a>. Every glyph has a coordinate.</p>
<p>The code has a main loop with <tt class="docutils literal">update_state()</tt> that moves the missiles,
player, etcetera and <tt class="docutils literal">draw()</tt> that draws everything. Both query the
database! Are there coordination overlaps between missiles and a saucer? Boom!
You can finally get an explosion out of a database query. And drawing means
querying for all coordiates and rendering what's there. Wow.</p>
<p>Losing is also determined with an SQL query: an enemy coordinate matches the
player coordinate. Or an enemy coordinate matches the bottom row
coordinate :-)</p>
<p><strong>Then he started playing the game in one screen with a log file showing all the
SQL queries live in another screen.</strong> Fun!</p>
<a href="http://photos.reinout.vanrees.org/Grok-zope-plone-python/Python-usergroup-Nederland-PUN/5825188_zDb4vj#!i=1841189707&k=zhC4Q7S&lb=1&s=A" title="Pygrunn lunch"><img src="http://photos.reinout.vanrees.org/Grok-zope-plone-python/Python-usergroup-Nederland-PUN/i-zhC4Q7S/0/M/Pygrunn-lunch-M.jpg" title="Pygrunn lunch" alt="Pygrunn lunch"></a></div>
</div>

        </div>
      </content>

    </entry>
    
  <entry>
    <title>"Day against DRM": pragmatic programmer books are always DRM free</title>
    <link rel="alternate" type="text/html"
          href="http://reinout.vanrees.org/./weblog/2012/05/04/pragprog-is-drm-free.html" />
      <id>http://reinout.vanrees.org/./weblog/2012/05/04/pragprog-is-drm-free.html</id>
      <author>
        <name>Reinout van Rees</name>
      </author>
      <published>2012-05-04T00:00:00+01:00</published>
      <updated>2012-05-04T14:57:00+01:00</updated>

      
      <category term="book" />
      
      <category term="django" />
      

      <content type="xhtml"
               xml:base="http://reinout.vanrees.org/"
               xml:lang="en-US"
               xml:space="preserve">
        <div xmlns="http://www.w3.org/1999/xhtml">
          <div class="document">
<p>Today, 4 May, is <a class="reference external" href="http://www.fsf.org/news/may-4-day-against-drm">day against DRM</a>.</p>
<p>The publisher I'm writing my Django book for, the <a class="reference external" href="http://pragprog.com/">pragmatic bookshelf</a>, is fully DRM free. Lovely. You get your books in a
number of formats (I use epub and pdf) without any restriction. Well, they
generate the book with your name tucked away somewhere in the header so you
cannot post it on some FTP site and get away with it :-)</p>
<p>A nice feature they added a couple of months ago: <a class="reference external" href="http://pragprog.com/frequently-asked-questions/ebooks/read-on-desktop-laptop#dropbox">dropbox integration</a>. Any
book you buy is automatically added to your dropbox account! No need to keep
things in sync yourself.</p>
<p>So... I don't need to worry that my eBook purchases disappear into some
DRM-locked no-longer-supported device. Make sure your books are DRM free, too!</p>
</div>

        </div>
      </content>

    </entry>
    

</feed>

