<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Python Guy</title>
	<atom:link href="http://pythonguy.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://pythonguy.wordpress.com</link>
	<description>Just another Python Guy with opinions...</description>
	<lastBuildDate>Thu, 22 Dec 2011 16:00:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='pythonguy.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Python Guy</title>
		<link>http://pythonguy.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://pythonguy.wordpress.com/osd.xml" title="Python Guy" />
	<atom:link rel='hub' href='http://pythonguy.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Probabilities</title>
		<link>http://pythonguy.wordpress.com/2011/12/05/probabilities/</link>
		<comments>http://pythonguy.wordpress.com/2011/12/05/probabilities/#comments</comments>
		<pubDate>Mon, 05 Dec 2011 20:05:51 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Beginning Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=175</guid>
		<description><![CDATA[I used to play a game called Hamurabi, a very ancient game. The way it works is you have a population, an amount of land, and an amount of grain. Every year, you need to choose how much grain to plant, how much grain to feed the people, and how many acres of land to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=175&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I used to play a game called Hamurabi, a very ancient game.</p>
<p>The way it works is you have a population, an amount of land, and an amount of grain. Every year, you need to choose how much grain to plant, how much grain to feed the people, and how many acres of land to buy or sell.</p>
<p>Each year, a certain amount of grain would grow, people would starve if they didn&#8217;t get enough food, and rats would eat your surplus grain. Random events would cause disasters to make things interesting.</p>
<p>The game was remarkably simple but surprisingly addictive.</p>
<p>I figured it was time to write a new version for the modern age, when memory and CPU are no longer real constraints to most of the problems we faced programming 30 years ago. I chose the Hamurabi game as inspiration.</p>
<p>Of course, a real simulation of a primitive society could track each individual within the population. People would age, and then die from starvation, random events, or just old age.</p>
<p>Using the US Census data, I found some actuarial tables that gave a percentage chance someone would die at a particular age.</p>
<p>I stored the number of people in each age group, separated by sex, in a simple array. The index was the age.</p>
<p>Now all I needed to do was find a scalable way of calculating how many people die in each age group every year.</p>
<p>The simplest way to do this is to roll the dice for each person each year. Simply use the random modules&#8217; random() function to get a number between  0.0 and 1.0. If this is left than the probability they will die, then they die.</p>
<p>Of course, if you have a thousand people, you have to call random() a thousand times. A million people need it called a million times. This would slow the program down as your population grew.</p>
<p>I vaguely recalled some math from my college years that should help me work these things out more quickly. Reading some of my old textbooks, I discovered that that Binomial Distribution gives you the exact probability of seeing x events in population of n where each has a probability of p.</p>
<p>However, the binomial distribution relies on combinatorials, which rely on factorials, which are notoriously difficult to calculate precisely for large numbers.</p>
<p>Luckily, this is not a new problem. Approximating the Binomial Distribution leads to the Normal or Gauss Distribution. This relies on simple exponentials and Python&#8217;s random module already has a gauss() and a normalvariate() built in.</p>
<p>What do you use for mu and sigma, the mean and standard deviation? The mean is simply n*p, and the standard deviation is the square root of n*p*(1-p).</p>
<p>The normal distribution really doesn&#8217;t give you good approximations when n or n*p is small. If n is really small (there are only a handful of people), I can just roll random() that many times. But if n is large, and p is very small (generally, for younger ages), then my approximation breaks down.</p>
<p>For this, the Poisson Distribution is useful. The Poisson Distribution can give you a very good approximation of the Binomial Distribution for small numbers of deaths. What I do is roll the dice with random(), and walk from 0 deaths upward, subtracting the probability that that many deaths would occur given the Poisson Distribution. When I&#8217;ve exceeded the roll, then that&#8217;s the number of deaths that occur.</p>
<p>Using these three methods of calculating deaths lead to a very scalable algorithm for calculating the number of deaths in a population of 1, 10, hundreds of even millions of people. I ran my simulation for several hundred years until I saw hundreds of billions of people, with tens of billions dying each year.</p>
<p>I think this shows why I like Python. Never once did I have to think very hard about how to express my ideas in Python. I was completely free to consider the algorithm. Most of my time was spent reading math books and translating complicated formulas into simple algorithms, and testing that it did what I expected.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/175/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/175/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/175/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=175&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/12/05/probabilities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Robust Processes</title>
		<link>http://pythonguy.wordpress.com/2011/10/17/robust-processes/</link>
		<comments>http://pythonguy.wordpress.com/2011/10/17/robust-processes/#comments</comments>
		<pubDate>Mon, 17 Oct 2011 18:04:11 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=172</guid>
		<description><![CDATA[I often over-engineer my processes. I adhere, roughly, to the idea, &#8220;Be liberal in what you receive, conservative in what you send.&#8221; I ran into an issue today that demonstrated why this is a good philosophy. In the abstract view, I had a system with several individual component processes. Each process depended on the output [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=172&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I often over-engineer my processes. I adhere, roughly, to the idea, &#8220;Be liberal in what you receive, conservative in what you send.&#8221;</p>
<p>I ran into an issue today that demonstrated why this is a good philosophy.</p>
<p>In the abstract view, I had a system with several individual component processes. Each process depended on the output of the previous process, through a roundabout way. (One accepted internet connections, another accepted log messages in our log format, another analyzed those log messages, and another summarized the analyses.)</p>
<p>We ran into a problem. A less-than-honest user of our system fed some very bad data in through the internet connection. That process didn&#8217;t particularly care what the input looked like, and fed it on through the rest of the system. It wasn&#8217;t until much later than an exotic problem arose that shut the entire system down.</p>
<p>As we meddled with the backend process that barfed on the bad data, we decided that that kind of data wasn&#8217;t valuable in the first place, so we might as well shut it down from the beginning.</p>
<p>If we had only implemented that fix, then our system would still be broken. See, we still had bad data in our system that had to be processed. So the dual fix of (a) eliminated the acceptance of obviously bad data, and (b) building a system more tolerant of bad data were needed.</p>
<p>I am often amazed by the engineers in the physical world. These people build machines that have the same issues with bad input, except their bad input includes &#8220;dust in the air&#8221; and &#8220;metal filings that are created by wear and tear.&#8221; They have to build even the most sensitive components of their system to be tolerant of this kind of bad data, even though they install filters and covers and such to eliminate as much of it as possible. If they didn&#8217;t do so, then the entire machine would come screeching to a halt the first time something bad happened.</p>
<p>We need to build our software the same way. No matter how internal a component is, it needs to be ready to accept bad data.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/172/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/172/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/172/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=172&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/10/17/robust-processes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Python Sucks (&#8230; when you suck at writing code)</title>
		<link>http://pythonguy.wordpress.com/2011/08/26/python-sucks-when-you-suck-at-writing-code/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/26/python-sucks-when-you-suck-at-writing-code/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 20:15:21 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=170</guid>
		<description><![CDATA[One of the neat features of Python is how readable it is. For most of your code, you won&#8217;t have to document what the code means, just why you chose to do things a certain way. I am poking at a long function, more than my 1080p rotated monitor can fit on one screen at [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=170&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the neat features of Python is how readable it is. For most of your code, you won&#8217;t have to document what the code means, just why you chose to do things a certain way.</p>
<p>I am poking at a long function, more than my 1080p rotated monitor can fit on one screen at a readable font size for me. It flows off the edge of the screen, making it hard to see how different pieced fit together.</p>
<p>I don&#8217;t like long functions. When you reach a certain number of variables in a particular scope, or when you have bits and pieces of your code spread out like peanut butter, it becomes unreadable.</p>
<p>Of course, long functions are probably better than the alternatives, provided you can&#8217;t separate out different parts of it.</p>
<p>The author of the code I am reading decided it wasn&#8217;t a good idea to put relevant pieces of code together. As a result, the code is intertwined. </p>
<p>Variables are created in one spot and used in another. In between, they are not relevant at all.</p>
<pre>
  the_var = the_value

  ... some irrelevant bits of code ...

  use(the_var)
</pre>
<p>Obviously, it is much more readable if you do things this way:</p>
<pre>
  ... irrelevant bits of code ...

  the_var = the_value
  use(the_var)

  ... other irrelevant bits of code ...
</pre>
<p>Or even better, eliminate the variable altogether:</p>
<pre>
   use(the_value)
</pre>
<p>By minimizing the mixing of code, it becomes much easier to read the code and put things together in my head. This is no different than writing English in paragraphs, where each paragraph has sentences that relate to each other.</p>
<p>In addition, you can make your code more readable by sensibly keeping the variable count down. Sometimes, reducing the number of variables means you have a more complicated expression, so you have to think carefully about how to do this.</p>
<p>Comments, in addition to vertical whitespace, can be used to separate &#8220;paragraphs&#8221; of code. I use single-line comments when I&#8217;m talking about the next few lines, but multi-line comments when I&#8217;m talking about entire sections of code.</p>
<pre>
#
# This is a description of the following block of
# code. Note how it sticks out because of the white space.
#

# This is a comment about the next two lines of code.
do_something()
while you_do_something_else():
   append_this_to_that()

# This comment applies to the next few lines.
this = that + that_other_thing - 5
these = [that, that_other_thing]
</pre>
<p>I hope my point is clear. You can write wonderful code, but make it entirely illegible, even in Python. So don&#8217;t do that. Take a little bit of time to make your code readable. Your future self will thank you.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/170/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/170/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/170/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=170&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/26/python-sucks-when-you-suck-at-writing-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Counting Days in Python</title>
		<link>http://pythonguy.wordpress.com/2011/08/25/counting-days-in-python/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/25/counting-days-in-python/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 18:44:36 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Advanced Python]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=166</guid>
		<description><![CDATA[When generating reports and charts, it&#8217;s very common that you want to generate a sequence of days between a range. Before we begin, let&#8217;s remind ourselves that the Python pattern for sequences is to include the first item, but exclude the last. That&#8217;s how range() and all the other functions work. I&#8217;ve seen people often [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=166&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When generating reports and charts, it&#8217;s very common that you want to generate a sequence of days between a range.</p>
<p>Before we begin, let&#8217;s remind ourselves that the Python pattern for sequences is to include the first item, but exclude the last. That&#8217;s how range() and all the other functions work.</p>
<p>I&#8217;ve seen people often do something like this:</p>
<pre>
days = (some list-generating code)
for day in days:
   ... process that day ...
</pre>
<p>This may be how you were trained to do this in other languages. I strongly recommend not doing this way. In general, using lists when you don&#8217;t really need them means you&#8217;re passing up some wonderful features of Python that will make your code more readable, more robust, and faster.</p>
<p>Some languages encourage you to avoid lists and use the 3-clause for loop that is not found in Python. I prefer lists over 3-clause for loops, mostly because I&#8217;ve never met one that was clear and obvious in meaning compared to the iterative approach.</p>
<p>For finite sequences of days, I prefer the generator. It gives me the highest chance of getting things right from the beginning.</p>
<pre>
for day in (... generator for a sequence of days ...):
    ... process that day ...
</pre>
<p>What is appropriate for that generator? A generic generator function should do the trick:</p>
<pre>
import datetime
def generate_days(start_day, end_day):
    day = start_day
    while day &lt; end_day:
        yield day
        day += datetime.timedelta(days=1)
</pre>
<p>The above pattern is so common it&#8217;s a wonder why we don&#8217;t generalize xrange. The interface could be something like:</p>
<pre>
xrange(first=0, last, increment=1)
Returns first, first+increment, first+increment+increment, etc until but not including last. Requires that increment can be added to first and that the current item can be compared with last.
</pre>
<p>One can dream&#8230;</p>
<p>You&#8217;ll note that if we write our own general xrange generator, it is just this:</p>
<pre>
def our_xrange(first, last, step):
    cur = first
    while cur &lt; last:
        yield cur
        cur += step
</pre>
<p>Of course, using itertools, you can combine the count() and takewhile(), but count() seems to only care for numbers. It&#8217;s trivial to write your own count() generator, though.</p>
<p>You&#8217;ll notice that I&#8217;m not dealing with numbers and then converting those numbers to dates. I much prefer objects that know what month and day they are in to integers that require lots of math to massage their meaning out of them.</p>
<p>Finally, I want to mention one special case that arises with databases. Oftentimes, you want to query data from the database for a sequence of dates. While some databases provide advanced features to allow you to do this within the database, many (most, now that NoSQL databases seem to be common) do not. I wouldn&#8217;t rely on the database doing the hard work for you. Besides, as I&#8217;ll show below, it&#8217;s really not that hard in Python, probably much easier than the solution the database provides.</p>
<p>There are two ways to handle this. One, we could pre-generate a list of dates, and then query the database for each date with an &#8220;in&#8221; condition (SQL). Two, we could ask for all the data between two date ranges, sort them, and then walk through the data in parallel with our date counter in Python.</p>
<p>The first is not desirable. This is because you have to send a lot of data across the wire&#8212;one date for each point on your graph, or row in your table. Let&#8217;s ignore it and move on.</p>
<p>The second is more desirable, but the problem of iterating through two lists in parallel is not something that&#8217;s obvious to most programmers. There is a simple solution, but it takes a bit of explaining. I&#8217;ll try to summarize that here.</p>
<p>What you want to do is to first generate the iterator that will give you all the dates you are interested in, given only the start, end, and interval. Then you want to query your database for all points of data that align with this, giving only the start, end, and interval. You&#8217;ll need to sort the data coming from the database. This will end up in another iterator of some sort.</p>
<p>Next, you want to iterate across your date iterator, performing some action. You&#8217;ll want to grab the row from the database only if the next row matches that date.</p>
<p>Here&#8217;s some sample code.</p>
<pre>
def date_result_pairs(dates, results):
    """Generates pairs of (date, result), one for each date in dates. If there is no corresponding result, then the result will be None."""
    dates = iter(dates)
    results = iter(results)
    try:
        result = results.next()
        for day in dates:
            if result.date &gt; day:
                yield (day, none)
            else:
                yield (day, result)
                result = results.next()
    except StopIteration:
        for day in dates:
            yield (day, None)

dates = our_xrange(start, end, interval)
results = (query the DB for all data between start, end, matching interval, sorted by date.)
for date, result in date_result_pairs(dates, results):
    print "Result for date %s is %r" % (date, result)
</pre>
<p>This isn&#8217;t perfect. It assumes the database is returning dates that match exactly with the dates you have, which may or may not be correct given your ability to write the correct query.</p>
<p>I hope this has demonstrated some of the elegance of Python for these kinds of problems.</p>
<p>As always, questions and ideas are welcome.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/166/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=166&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/25/counting-days-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>SQLAlchemy Tips: Performance</title>
		<link>http://pythonguy.wordpress.com/2011/08/17/sqlalchemy-tips-performance/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/17/sqlalchemy-tips-performance/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 21:47:58 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Advanced Python]]></category>
		<category><![CDATA[SQLAlchemy]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=161</guid>
		<description><![CDATA[Sometimes, people use SQLAlchemy, see that things are slow, and blame SQLAlchemy. In my experience, SQLAlchemy is never to blame for performance problems. There are, in general, two areas of blame: Poor implementation of query code Poor implementation of database schema Granted, SQLAlchemy makes things look deceptively simple. It handles so many optimizations for you [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=161&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, people use SQLAlchemy, see that things are slow, and blame SQLAlchemy. In my experience, SQLAlchemy is never to blame for performance problems.</p>
<p>There are, in general, two areas of blame:</p>
<ul>
<li>Poor implementation of query code</li>
<li>Poor implementation of database schema</li>
</ul>
<p>Granted, SQLAlchemy makes things look deceptively simple. It handles so many optimizations for you that you generally should go with the first idea that pops into your head. You can get a lot done with bad code.</p>
<p>However, at some point, you need to think, really hard, about what you want to get down when and how.</p>
<p>I won&#8217;t even touch on the topics of optimizing your database. There are innumerable resources out there to discuss when and when not to use indexes and how to arrange your data for the best query response.</p>
<p>I will say, however, that you need to think about which machine is going to do the work that needs to be done. Is your database going to do all the number crunching, or are you going to send the data to your app and have it do the number crunching? There are arguments for both ways.</p>
<p>The database is going to have indexes and the most recent data for you. If you try to limit the number of queries you make and the amount of data you send back and forth between the database, you&#8217;re probably doing things right.</p>
<p>Occasionally, the operation is better handled by having the database send the data to the app and the app works it over. These cases are rarer than the former.</p>
<p>With that in mind, let&#8217;s look at some simple optimizations to push the work on to the database and outside of your app.</p>
<p><b>Optimization 1:</b> Are you making too many queries?</p>
<p>A good hint that you are making too many queries is when you put a query inside of a for loop iterating over the results of another query. In general, these queries can be combined. It will require, however, that you learn a little bit more about SQL and how to write those SQL statements in SQLAlchemy. You may want to visit the advanced topics of nested SELECT statements, and fully understand it, so that you can see how to optimize your nested queries.</p>
<p>Another problem I see from time are <i>hidden queries</i>. Look at your logs. Identify which queries match which lines of code. If you can&#8217;t point to where the query originated from, then you&#8217;ve got a hidden query. You need to understand more fully what exactly your SQLAlchemy model is and which attributes are spawning which queries.</p>
<p><b>Optimization 2:</b> Are you gathering too much data?</p>
<p>The obvious case is when you have big tables, or tables that join with many tables, and you are sucking down more data from the database than you want or need.</p>
<p>The way to resolve this is with a healthy dose of deferred() and grouping of those deferreds.</p>
<p>Spreading your data across multiple tables makes this a bit clearer. For instance, do not put your metrics data in the same table as the attributes of the object. Seldom do you want both the color of something and the number of times someone bought it at the same time.</p>
<p><b>Optimization 3:</b> Pagination.</p>
<p>This one is so obvious for an experienced SQLAlchemy user, but is unfamiliar to novices.</p>
<p>SQLAlchemy provides powerful mechanisms for pagination. When you want to do pagination, follow this simple formula.</p>
<ul>
<ol>Specify the query for the data you want, including only the columns you are interested in. Be sure to join with all the tables you need to join with in order to get the data you need. (NOTE: You can specify columns as the parameters to the query() method of your Session!) Be sure to include whatever sorting or filtering you need as well.</ol>
<ol>Run the count() on that query. This is the total number of rows. Certain databases do not like the count() call very much, so if you need to, store this someplace safe and temporary, such as a cookie or session or even in the HTML form.</ol>
<ol>Append the modifiers limit() and offset(). Some databases do not respect these, but most of them do.</ol>
</ul>
<p><b>Optimization 4:</b> Sanity.</p>
<p>As a final check, just poke around the logs of both your app and your database and see how long each of the queries take. Identify the slower, more common ones, and attack those.</p>
<p>Are the queries necessary? Do they gather too much information? Or should they be paginated?</p>
<p>Finally, you&#8217;ll want to revisit your schema and indexes to see if there is a better way for the database to handle that data.</p>
<p>Optimization is an ongoing process. Try not to get hung up on premature optimization, and try to not let your unfamiliarity with SQLAlchemy or your database limit you. Take the time to learn the more advanced features of both, so that you can apply the full power to your problems.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=161&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/17/sqlalchemy-tips-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Objects vs. Arrays</title>
		<link>http://pythonguy.wordpress.com/2011/08/16/objects-vs-arrays/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/16/objects-vs-arrays/#comments</comments>
		<pubDate>Tue, 16 Aug 2011 16:12:16 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Advanced Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=159</guid>
		<description><![CDATA[I&#8217;m playing around with some 3D data. I think this is a good learning experience for those who want to understand the flexibility Python allows. I started with the simplest case. I had three Numpy arrays. One was an array of vertices, another an array of normals, and the third an array of triangles which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=159&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m playing around with some 3D data. I think this is a good learning experience for those who want to understand the flexibility Python allows.</p>
<p>I started with the simplest case. I had three Numpy arrays. One was an array of vertices, another an array of normals, and the third an array of triangles which referenced three vertices and three normals.</p>
<p>Rendering the surface in OpenGL is trivial. Just glBegin(GL_TRIANGLES), then go through each triangle, calling glNormal3dv() and glVertex3dv() for each normal and vertex in the triangle.</p>
<p>Thinking of this in the computer science sense, it&#8217;s apparent that the list of vertices and the list of normals are not truly independent. If a vertex or normal isn&#8217;t mentioned as part of a triangle, there&#8217;s no reason to hold on to it in memory.</p>
<p>In a sense, the arrays of vertices and normals is an artificial data structure, created for convenience, not correctness. If I manipulated the surface so that triangles would change or disappear, there&#8217;s a good chance that the arrays of vertices and normals would be incorrect, holding information that is no longer needed.</p>
<p>I started down the OO path, creating a Triangle class, then a Vertex and Normal class. I realized, part way through this process, that I was moving away from the way things work in OpenGL land. Ultimately, I would load the vertices and normals into a buffer of some sort, and then I would issue render commands against the buffer of vertices. I haven&#8217;t quite seen how to do this yet, but it&#8217;s apparent that the array is the proper way to store these things, not as separate objects spread out all over creation.</p>
<p>The second problem was one of elegance. In the system I had devised earlier, it is quite natural that many triangles would share the same vertex, and these relationships are all but obvious. If I moved the vertex, then all of the connected triangles would likewise move, whether or not they were aware of the change. While I can implement a system where the same Vertex object is shared among many Triangle objects, I doubt it would be as elegant as the first solution I derived. Finding which triangles share the same vertex is not trivial in the OO approach, but in the indexed approach it is a simple filter for triangles that have the same index to the vertex.</p>
<p>I haven&#8217;t exactly resolved this dilemma in my mind. I suppose that I can create an object that uses invisible indexing into a massive array, and manages the memory within the array not unlike you&#8217;d have to do in C. I&#8217;m not convinced that such a solution is elegant or even beneficial.</p>
<p>In the long run, I think having a highly optimized set of data structures, each with their own unique access and modification rules, is probably the right way to go. I can &#8220;protect&#8221; these within a class, and then provide accessors and mutators to view and modify specific segments as needed. Perhaps having the elements defined as low as individual Triangles and Vertexes is simply too much detail.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/159/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/159/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/159/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=159&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/16/objects-vs-arrays/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Some Dict Patterns</title>
		<link>http://pythonguy.wordpress.com/2011/08/10/some-dict-patterns/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/10/some-dict-patterns/#comments</comments>
		<pubDate>Wed, 10 Aug 2011 17:36:53 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Advanced Python]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=152</guid>
		<description><![CDATA[Introduction I have seen a lot of Python code, as well as Java, Perl, and other languages. When dealing with dicts, I have identified a few patterns that I feel are optimal, depending on the situation. These are extraordinarily simple. NOTE: The dict data structure is known by many names: map, a hashmap, an associative [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=152&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>I have seen a lot of Python code, as well as Java, Perl, and other languages. When dealing with dicts, I have identified a few patterns that I feel are optimal, depending on the situation. These are extraordinarily simple.</p>
<p>NOTE: The dict data structure is known by many names: map, a hashmap, an associative array, a hash, or even a table.</p>
<h2>Use if Present</h2>
<p>For instance, how many times have you written the following pseudo-code in your language of choice?</p>
<pre>
# pseudo-code
if key is in map:
    lookup value in map with key
    use value
</pre>
<p>Python provides the &#8220;<tt>get()</tt>&#8221; method which returns <tt>None</tt> if it is not present.</p>
<pre>
# Python
value = map.get(key)
if value is not None:
    # use value
</pre>
<p>Of course, sometimes you need to distinguish between values that are None and that are not present in the dict. You can rely on exceptional behavior for this:</p>
<pre>
# Python
try:
    value = map[key]
except KeyError:
    pass
else:
    # use value
</pre>
<p>Or you can use the &#8220;<tt>in</tt>&#8221; test:</p>
<pre>
# Python
if key in map:
    value = map[key]
    # use value
</pre>
<p>The above, of course, does 2 dict lookups.</p>
<h2>Exceptions versus Lookups</h2>
<p>There is discussion about whether to use exceptions or lookups. The general rule of thumb is that exceptions are not as slow as you think, since Python is pretty slow to begin with. &#8220;Slow&#8221;, of course, is a relative term that is meaningful when you compare Python to C/C++. And nowadays with PyPy, &#8220;slow&#8221; isn&#8217;t a proper word for it anymore.</p>
<h2>Use Value or Default If Missing</h2>
<p>Sometimes you want to use a default value if the key is not present. This is simply:</p>
<pre>
# Python
value = map.get(key, 'default')
</pre>
<p>Note that whatever expression you use as the default value will be evaluated, whether or not it was used. If the expression is expensive to calculate, then you can use this form:</p>
<pre>
# Python
value = map.get(key)
if value is None:
    value = expensive_expression()
</pre>
<p>Notice that you&#8217;re back to the previous pattern if you need to distinguish between a value of None and a missing key.</p>
<h2>Use Value or Default and Store if Missing</h2>
<p>Sometimes you want to store the default value in the dict if it is missing. &#8220;<tt>setdefault()</tt>&#8221; is the ideal method for this.</p>
<pre>
# Python
value = map.setdefault(key, 'default')
</pre>
<p>Of course, the caveats for expensive default expressions applies.</p>
<h2>Conclusion and Summary</h2>
<p>Those of you who are unfamiliar with Python might note how similar all of the above patterns are. Indeed, if you simply learn what the following expressions mean, you don&#8217;t have to think very hard to understand what the code does or to choose the right code:</p>
<ul>
<li><tt>key in dict</tt></li>
<li><tt>dict[key]</tt></li>
<li><tt>dict.get(key)</tt></li>
<li><tt>dict.get(key, default)</tt></li>
<li><tt>dict.setdefault(key, default)</tt></li>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=152&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/10/some-dict-patterns/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Getting into PyOpenGL</title>
		<link>http://pythonguy.wordpress.com/2011/08/06/pyopengl-basics/</link>
		<comments>http://pythonguy.wordpress.com/2011/08/06/pyopengl-basics/#comments</comments>
		<pubDate>Sat, 06 Aug 2011 18:45:28 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=148</guid>
		<description><![CDATA[Python Guy is getting back into OpenGL. I remember doing some basic OpenGL back in &#8217;99/2000. Nowadays, so much has changed that I&#8217;m learning it all over again. If you want to follow along, I&#8217;m going to try and build a Minecraft clone. The general idea of the game is that you control a character [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=148&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Python Guy is getting back into OpenGL. I remember doing some basic OpenGL back in &#8217;99/2000. Nowadays, so much has changed that I&#8217;m learning it all over again.</p>
<p>If you want to follow along, I&#8217;m going to try and build a Minecraft clone.</p>
<p>The general idea of the game is that you control a character that can manipulate the world. I&#8217;m going for a medieval RPG style game play, maybe with some networking enabled in the future.</p>
<p>The problem of generating the cubes is harder than it looks. My big idea is to apply a smoothing algorithm so that the data looks more natural. That is, if two blocks only differ in elevation by 1 unit, then the two blocks should form a neat slope.</p>
<p>Another idea is just to start with raw data describing the landscape, and allow the player to manipulate the mesh.</p>
<p>Anyway, OpenGL is very well documented, both in the infamous Red Book and on the OpenGL wiki. I encourage you to check it out.</p>
<p>PyOpenGL, by the way, is the way to go.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=148&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/08/06/pyopengl-basics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Lua vs. Python, Embedding</title>
		<link>http://pythonguy.wordpress.com/2011/07/13/lua-vs-python-embedding/</link>
		<comments>http://pythonguy.wordpress.com/2011/07/13/lua-vs-python-embedding/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 05:10:21 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=146</guid>
		<description><![CDATA[People whine about embedding Python is a pain. I feel their pain, but not the pain of embedding Python. I feel their pain of being forced to write code in C. As Glyph Lefkowitz so clearly explains, the fact that Lua is easily embedded doesn&#8217;t make it a good choice. Embedding is the wrong engineering [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=146&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>People whine about embedding Python is a pain.</p>
<p>I feel their pain, but not the pain of embedding Python. I feel their pain of being forced to write code in C.</p>
<p>As <a href="http://www.twistedmatrix.com/users/glyph/rant/extendit.html">Glyph Lefkowitz so clearly explains</a>, the fact that Lua is easily embedded doesn&#8217;t make it a good choice. Embedding is the wrong engineering choice to begin with.</p>
<p>What you should be doing as a programmer is creating usable code fragments. If the Free Software movement has taught us anything, the survivability of code depends on its usefulness, and that usefulness goes into how other programs can tap into its power.</p>
<p>If you&#8217;re writing an environment from which you expect other programmers will confine themselves within, you are making a big mistake. Those programmers who fall into this trap will one day realize that they&#8217;ve wasted their lives in pursuit of the impossible. If you want to know where things will go, go read about Emacs and Lisp. That&#8217;s what you&#8217;re recreating.</p>
<p>If, instead, you&#8217;re writing components of programming intelligence, buried into .so&#8217;s or .dll&#8217;s, then other programmers can follow after you and incorporate your program into theirs.</p>
<p>Lua might be a nice embedded language, but embedded languages are not what you really want.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/146/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/146/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/146/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=146&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/07/13/lua-vs-python-embedding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
		<item>
		<title>Python v. Lua, Complexity and Simplicity</title>
		<link>http://pythonguy.wordpress.com/2011/07/13/python-v-lua-complexity-and-simplicity/</link>
		<comments>http://pythonguy.wordpress.com/2011/07/13/python-v-lua-complexity-and-simplicity/#comments</comments>
		<pubDate>Thu, 14 Jul 2011 04:52:24 +0000</pubDate>
		<dc:creator>PythonGuy</dc:creator>
				<category><![CDATA[Lua]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://pythonguy.wordpress.com/?p=144</guid>
		<description><![CDATA[One of the biggest arguments against Python and for Lua is simplicity. That is, the Lua fans claim that Lua is more simply than Python, because it&#8217;s language is much smaller. I agree and concede the point that Lua&#8217;s language is smaller. I also admit that Lua&#8217;s code base is much, much smaller. However, does [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=144&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the biggest arguments against Python and for Lua is simplicity. That is, the Lua fans claim that Lua is more simply than Python, because it&#8217;s language is much smaller.</p>
<p>I agree and concede the point that Lua&#8217;s language is smaller. I also admit that Lua&#8217;s code base is much, much smaller.</p>
<p>However, does this translate to simplicity for the programming task?</p>
<p>I think Lua is wonderfully engineered. The guts of Lua are beautifully assembled. It is a wonderful structure to behold, much like the Taj Mahal.</p>
<p>However, people don&#8217;t live in the Taj Mahal. They live in modern homes which are an engineer&#8217;s nightmare. Modern homes are much more comfortable because the modern conveniences that make living simple are readily available.</p>
<p>Behind the faucet is miles of piping. Behind the outlet is an electrical system that will confuse even the most experienced electrician. But we know that if we want to live, we would rather live in a modern home than the Taj Mahal.</p>
<p>Yes, the Taj Mahal is simpler, being constructed out of stones that fit perfectly together. Yes, we can appreciate how a few simple components have come together to make something greater than the whole. But that&#8217;s about all we can do.</p>
<p>A very, very good example is how Python approaches Object-Oriented Programming versus Lua&#8217;s approach to Object-Oriented Programming.</p>
<p>Python provides not insignificant language constructs, embedded deep within the syntax and interpretation, that make simple classes trivial to create, and hard classes easy.</p>
<p>Python&#8217;s OO system is easily learned. When you need special behavior, such as attribute look up and assignment magic, the way to do so is plain and easy.</p>
<p>Lua&#8217;s OO system, on the other hand, is extremely limited. You only have two or three syntactic elements to support OO programming, and these are not &#8220;magic&#8221;, nor can the magic be modified to fit the task.</p>
<p>Where is SQLAlchemy in Lua? It cannot exist. For Lua&#8217;s &#8220;simple&#8221; system, you will never have declarative ORM, and never understand what it means to have a class backed by a database. Sure, SQLAlchemy is much more complicated than anything you would ever dream of writing in Lua, but Python has provided the modern conveniences that not only make writing SQLAlchemy possible, but almost easy.</p>
<p>Why does Python have so many modules? Because it is so easy to create new modules that do interesting things, whereas in Lua, this is simply not done.</p>
<p>Yes, Lua, the language, is simpler. But I don&#8217;t want to use a language that is simpler. I want a language that makes my job, the task of programming, easier. I don&#8217;t care how complex that language is, anymore than I care whether how complicated the grammar in English is. I simply care how hard it is to do my job, and Lua makes my job harder, not easier.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/pythonguy.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/pythonguy.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/pythonguy.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=pythonguy.wordpress.com&amp;blog=52795&amp;post=144&amp;subd=pythonguy&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://pythonguy.wordpress.com/2011/07/13/python-v-lua-complexity-and-simplicity/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/dafe3076c43d9dd3a18817a1e6ede3b8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">pythonguy</media:title>
		</media:content>
	</item>
	</channel>
</rss>
