<?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/"
	>

<channel>
	<title>Zeroth Code &#187; programming</title>
	<atom:link href="http://www.oddco.ca/zeroth/zblog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.oddco.ca/zeroth/zblog</link>
	<description>Game design, development, technology, programming, and python</description>
	<lastBuildDate>Thu, 09 Sep 2010 14:18:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Movie Module Progress: Week 6</title>
		<link>http://www.oddco.ca/zeroth/zblog/2009/06/29/movie-module-progress-week-6/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2009/06/29/movie-module-progress-week-6/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 01:58:49 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gsoc2009]]></category>
		<category><![CDATA[pygame]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=232</guid>
		<description><![CDATA[So, week six. I devoted this entire week to exhaustive testing and bug fixing. Its just good software development to take some time, and test and fix bugs. No new features, nothing. I also took the time in the process to refactor a section of code I felt was extremely hacky and brittle. The benefit [...]]]></description>
			<content:encoded><![CDATA[<p>So, week six. I devoted this entire week to exhaustive testing and bug fixing. Its just good software development to take some time, and test and fix bugs. No new features, nothing. I also took the time in the process to refactor a section of code I felt was extremely hacky and brittle. The benefit is that I ended up creating a couple of special-purpose functions which can be reused in multiple areas in the code for the future, possibly. As I found small bugs or issues, like deadlocks, I fixed them. There were a few that remained by Friday:</p>
<ul>
<li>Unpausing doesn&#8217;t work right. The video would jerk and stutter after unpausing.</li>
<li>Audio and Video would drift out of sync within about 8 minutes on my test video.</li>
<li>Sound would not restart on a replay</li>
</ul>
<p>As of today(Monday), I fixed the sound restart issue, thanks to Rene&#8217;s advice, and the unpausing issue. Ironically enough, the unpausing issue was an extremely simple fix. The sound restart issue required me to simply play sound buffers of no sound while the sound was stopped. For the audio and video drift issue, I know why its happening, which is because I unlinked video timing from being synced to the audio. I knew I would have to fix this eventually, but it was an issue for later on. I implemented a first kind of draft solution today, which caused the video+audio to jerk and stutter. Very strange. </p>
<p>Once I have these issues fixed, and a lot of the slightly jury-rigged code from various attempts to fix or examine the various issues, I&#8217;ll begin adding new features.<br />
I will add for the Beta release:</p>
<ul>
<li>Subtitle support. This way you can have your in-game video with subtitles for the deaf and hard of hearing. Trust me on this, its necessary. I&#8217;m hard of hearing myself, and I&#8217;m seeing more and more games that don&#8217;t have subtitles as an option.</li>
<li>Rect assignation support. You can assign a rect, along with the surface of choice, to dictate which portion of the video image will be written to the surface you give.</li>
<li>Seeking support. Now, you should be able to seek to (somewhat depending on the format) arbritary points in the video file</li>
<li>Fix a couple of minor issues, like delayed resizing of the image</li>
<li>Begin working on a sound api, for developers that choose to redirect the sound samples to the sound system of their choice.</li>
<li>Adding more backends(like mplayer), and updating the backends we currently have to work better and faster</li>
<li>With the mplayer backend, the plan is to have the video streamed as frames over pipes, so the smart programmer can do&#8230;whatever they want with those.</li>
<li>Try to clean up the code, make it more readable, add a lot more updated documentation, including flow-charts!</li>
<li>And other features as requested by the community members. If there&#8217;s a feature you want, email me at either zeroth AT oddco.ca or trinioler AT gmail.com. They both go to the same mailbox. <img src='http://www.oddco.ca/zeroth/zblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>And checking with the schedule I gave the mentors, it appears I&#8217;m actually a week ahead of schedule! Granted, I&#8217;ve had to change my plans and ideas for the module as I learned more, but thats how the world works. <img src='http://www.oddco.ca/zeroth/zblog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I will predict Beta release in 3-4 weeks, including this week, which will give me time to update and fix bugs in all the other backends, as well as testing and bug fixing, after the beta release. In addition, after that I will begin the work of making the code work and compile on Windows and with Python 3.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2009/06/29/movie-module-progress-week-6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing types in Python</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/12/10/changing-types-in-python/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/12/10/changing-types-in-python/#comments</comments>
		<pubDate>Wed, 10 Dec 2008 23:54:09 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python tricks]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=161</guid>
		<description><![CDATA[Okay, so its another entry in my long-abandoned python tricks series. First, lets take a look at a couple of classes: class B&#40;object&#41;: def foo&#40;self&#41;: return '10' &#160; class A&#40;object&#41;: def __init__&#40;self&#41;: self.a='a' def foo&#40;self&#41;: return '1' The important part here is the (object) subclassing. This trick only works if you do that. Lets take [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so its another entry in my long-abandoned python tricks series.</p>
<p>First, lets take a look at a couple of classes:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> B<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'10'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> A<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">a</span>=<span style="color: #483d8b;">'a'</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'1'</span></pre></div></div>

<p>The important part here is the (object) subclassing. This trick only works if you do that. Lets take alook at it in operation:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> a=A<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a
<span style="color: #66cc66;">&lt;</span>changetype.<span style="color: black;">A</span> <span style="color: #008000;">object</span> at 0x7f0f18dbf9d0<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.__class__
<span style="color: #66cc66;">&lt;</span>class <span style="color: #483d8b;">'A'</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">a</span>
<span style="color: #483d8b;">'a'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">foo</span>
<span style="color: #66cc66;">&lt;</span>bound method A.<span style="color: black;">foo</span> of <span style="color: #66cc66;">&lt;</span>changetype.<span style="color: black;">A</span> <span style="color: #008000;">object</span> at 0x7f0f18dbf9d0<span style="color: #66cc66;">&gt;&gt;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">foo</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'1'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.__class__=B
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">a</span>
<span style="color: #483d8b;">'a'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">foo</span>
<span style="color: #66cc66;">&lt;</span>bound method B.<span style="color: black;">foo</span> of <span style="color: #66cc66;">&lt;</span>changetype.<span style="color: black;">B</span> <span style="color: #008000;">object</span> at 0x7f0f18dbf9d0<span style="color: #66cc66;">&gt;&gt;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> a.<span style="color: black;">foo</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">'10'</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>a, B<span style="color: black;">&#41;</span>
<span style="color: #008000;">True</span></pre></div></div>

<p>As you can see, I was able to override the foo() method with the one in the Beta class. As well, this only works on instanced objects. Now, what use is this you may ask? It is basically meta-programming. For 90% of programming tasks, meta-programming is not needed at all. But for that remaining 10%, you can do some seriously cool stuff, like changing the operation of a class during runtime, without modifying the class variables.</p>
<p>As a real-life example, I just finished writing a simplistic query parser. I realized I had forgetten to take into account price queries, so, I created a PriceToken class, that on instantiation, changes its own class to that of a TermToken. Otherwise, I would have had to rewrite about 100 lines of code to allow the use of a PriceToken. Pretty much useful because I&#8217;m lazy. But, yes, in Python, you can change the type of an object on the fly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/12/10/changing-types-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is Computer Science?</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/08/26/what-is-computer-science/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/08/26/what-is-computer-science/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 04:45:30 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[computer science]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=127</guid>
		<description><![CDATA[There is a great debate going on over at the compsci.ca blog, in the comments, about just what is, or isn&#8217;t computer science. I provided a pretty good response, but I thought I&#8217;d go into a bit more depth. Trying to define what CS is, is just like trying to define what Mathematics is. Math [...]]]></description>
			<content:encoded><![CDATA[<p>There is a great debate going on over at the <a href="http://compsci.ca/blog/the-most-important-programming-language/">compsci.ca blog</a>, in the comments, about just what is, or isn&#8217;t computer science.</p>
<p>I provided a pretty good response, but I thought I&#8217;d go into a bit more depth.</p>
<p>Trying to define what CS is, is just like trying to define what Mathematics is. Math gives us the tools to deal with relatively simplistic problems, like algebra, calculus, etc. These tools have allowed us to make enormous strides, in economics, statistics, physics, chemistry, biology, and so on. The presence, and usefulness of Math is undeniable in every field of Science.</p>
<p>Math is an asbtract field, dealing with numbers and logic at their very basic. Working with these, we have made some fantastic tools, but several fields are beginning to see the limitation of the equation.(Hmm, that rhymes.)</p>
<p>What Computer Science is, is the study of solving problems. It is not logic, nor math, nor boolean algebra. It is not linear optimization, database design, or programming. It is a superset, of all those concepts, though being derived from those basics, the sum really is more than the whole.</p>
<p>Thanks to CS, we have the most basic unit of problem solving, the algorithm. As we examine different kinds of algorithms, we create the tools to use and apply those algorithms. To me, this is much like the basic unit of math, the equation. Everything else we have in CS are really just an extension on algorithms. If you&#8217;ve studied high level CS, you will have seen that the problems being solved&#8230; are extremely abstracted.</p>
<p>The reason for this abstraction is simple. It builds our CS vocabulary and toolbox, giving us tools for screwing screws and hammering nails. Instead of needing a specific hammer for a specific kind of nail, we have an abstraction, that lets us deal in general with all kinds of nails.</p>
<p>Let me tell you a story. I worked for a week, in the cyber cafe for a congress of oceanographers and meteorologists. I had to help them upload their presentations, and in the process, I got to see the latest and greatest of oceanography and meteorology. And here&#8217;s the thing: they&#8217;re beginning to use tools and concepts that were developed in CS 30 years ago! I asked several scientists on this matter, and they admitted that the equation as a tool doesn&#8217;t work for the complexity they deal with, and the algorithm works very well.</p>
<p>We, in CS, are developing the tools and vocabulary needed to describe complex situations and balances. We are learning how to solve problems. We are&#8230; at the same point that the grecians were with numbers and mathematics. CS is more than Science or Math, though it contains elements from both. CS is itself a completely new field, and until we recognize that, we&#8217;ll be held back.</p>
<p>Oh, and Programming is simply the actual usage of these tools, much like accountacy, algebra, physics are all applications of Math.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/08/26/what-is-computer-science/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Art of Programming</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/08/21/art-of-programming/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/08/21/art-of-programming/#comments</comments>
		<pubDate>Thu, 21 Aug 2008 16:45:53 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[creation]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[art]]></category>
		<category><![CDATA[artists]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=119</guid>
		<description><![CDATA[I just recently finished reading a book, no, rather, a comic, by Scott McCloud, called Understanding Comics: The Invisible Art. It was a fantastic read, very illuminating in many aspects. A lot of things I didn&#8217;t understand about art in general are finally beginning to be less murky. One of the most striking concepts was [...]]]></description>
			<content:encoded><![CDATA[<p>I just recently finished reading a book, no, rather, a comic, by Scott McCloud, called <a href="http://www.amazon.com/Understanding-Comics-Invisible-Scott-Mccloud/dp/006097625X">Understanding Comics: The Invisible Art. </a>It was a fantastic read, very illuminating in many aspects. A lot of things I didn&#8217;t understand about art in general are finally beginning to be less murky.</p>
<p>One of the most striking concepts was the six stages of art. And it resonated. I realized that those six stages correspond as well to the development and progress of a programmer.</p>
<p>In his comic, the six stages are as follows: Idea/Purpose, Form, Idiom, Structure, Craft, Surface.</p>
<div class="wp-caption alignnone" style="width: 396px"><img title="Understanding Comics: The Six Steps" src="http://oddco.ca/uc2.jpg" alt="The Six Steps of Art" width="386" height="385" /><p class="wp-caption-text">The Six Steps of Art</p></div>
<p>Let&#8217;s start at the top: <strong><span style="text-decoration: underline;">Surface.</span></strong></p>
<p>Surface applies to the most basic of programming. Simple statements, flow control, basic functions. Every programmer begins their growth at this stage. They learn how basic math works, properties, etc. They learn how to order statements, if conditionals, and loop guards.</p>
<p>Next is <strong><span style="text-decoration: underline;">Craft.</span></strong></p>
<p>Craft is where they take the basics, the Surface, and begin constructing bigger things. Functions, classes, basic data structures. Here, they learn how to construct a complex piece of smaller simpler pieces, but they are only just barely beginning to learn why and how it works that way. Any data structures they build are based off of basic data structures, and they don&#8217;t yet understand deeply the fundamental ideas of efficiency, logic, and computability. The good programmer also begins to learn about source control, using their tools, building, and deploying their work.</p>
<p><strong><span style="text-decoration: underline;">Structure</span></strong> is next in their growth path.</p>
<p>Here, they begin to learn how to organize their complex pieces into a working piece. How to access and parse text files, basic database interactions. They&#8217;ll do things the slow, wrong way, but its necessary to reach the stage of development. Here, they begin to worry about efficiency, the right way of doing things. Object Oriented programming starts to make sense as a conceptual model(which is all it really is). The now competent programmer is capable of learning other basic conceptual models.</p>
<p><strong><span style="text-decoration: underline;">Idiom</span></strong> is the biggest part of the growth.</p>
<p>Here, they begin to learn about Design Patterns, and begin to apply them. They apply them at every stage, from the basic Surface, to the overall Structure of the program. They also begin to learn about the ideas and concepts behind data protocols, to use them where needed. This is when programmers become cynical about Silver Bullets. This is when they finally realize there is no substitute for skill, experience, and effort. Many programmers do extremely well staying at this stage. However, to become a top-notch programmer, one needs to see and explore the limits of programming.</p>
<p>That is where <strong><span style="text-decoration: underline;">Form</span></strong> comes in. Programmers at this stage explore the efficacy of Design Patterns, breaking them where and when needed, even developing their own versions. They begin to develop solid ideas about how API&#8217;s should work, about what is computable, about efficiency, and about Logic. These are the programmers that take already established tools and concepts, and bring us exciting new ones. Notable examples are: Bittorrent, Linux, and the Mouse. They looked at what was being done, and brought something new, and world-changing to the world of computing.</p>
<p>The final, highest level of development is <strong><span style="text-decoration: underline;">Ideas/Purpose</span></strong>. This is where programming Gods like Knuth and Stallman rest. This is where Computer Scientists do their heady, ivory tower research that is oh-so-important to the world at large. This is where concepts of algorithms, data structures, efficiency, and logic are explored in their abstract. This is where Computer Science is rooted, and where the birth of the computer industry started. This is where Boole developed his ideas of Boolean logic, where Cray designed his supercomputers, and the very basis of all digital electronics.</p>
<p>Now, this is the normal path most programmers take in their growth. Even though, in College and University, we&#8217;re taught the ideas and purpose of programming from almost the start&#8230; when we don&#8217;t have the basic building blocks of understanding of logic and program construction.</p>
<p>As my friend Khumba just said: &#8220;You can&#8217;t play Protoss until you&#8217;ve played a few games with Terran.&#8221; You need to understand the basic building blocks of logic, statements and flow-control and the experience with those to understand the higher level concepts of functions, routines, threads, classes, modules, executables, algorithms.</p>
<p>The thing is though, we can start at any stage we like, from Ideas/Purpose, to the Idioms of programming. But the greatest, best programmers amongst us will explore all of these stages in depth, learning what they need from it to improve themselves and their art. Because thats what Programming is. It is an art, and we are the artists, working with the pure stuffs of thought. We translate the abstract to binary logic, to 1&#8242;s and 0&#8242;s, and for that&#8230; we are artists.</p>
<p>If you disagree, or agree, let me know. Theres a handy comment box right down there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/08/21/art-of-programming/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Dynamic pathfinding(Cutting edge research!)</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/07/29/dynamic-pathfindingcutting-edge-research/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/07/29/dynamic-pathfindingcutting-edge-research/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 16:59:09 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[game design]]></category>
		<category><![CDATA[npcs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[game ai]]></category>
		<category><![CDATA[pathfinding]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=105</guid>
		<description><![CDATA[Today, I went to an awesome presentation by Vadim Bulitko about his most recent research. Here&#8217;s the presentation, but I&#8217;ll summarize what the presentation was about for general use. (I just found that the pdf didn&#8217;t come through the pdf export very well, apparently.) This was a presentation at UBC-Okanagan, in Kelowna, BC for reference. [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I went to an awesome presentation by Vadim Bulitko about his most recent research. <a href="http://ircl.cs.ualberta.ca/files/webfm/lrts/pres/2008-07-18-DLRTA.pdf">Here&#8217;s the presentation</a>, but I&#8217;ll summarize what the presentation was about for general use. (I just found that the pdf didn&#8217;t come through the pdf export very well, apparently.) This was a presentation at UBC-Okanagan, in Kelowna, BC for reference. Vadim is thinking about taking his sabbatical here at UBC, yay!</p>
<p>Its a rather innovative technique, and delivers great results for only a bit of computer power.</p>
<p><span id="more-105"></span></p>
<h1><span style="text-decoration: underline;"><strong>The Big Granddaddy Algorithms</strong></span></h1>
<p>First, a bit of background as to what has been done before. The Big Granddaddy of pathfinding algorithms is the A*(called the A-star) algorithm. It makes the complete plan, every step needed, to get from one point to another, then executes it. Unfortunately, the amount of time this takes scales exponentially with the size of the map. It, however, does provide an optimal path.</p>
<p>The other algorithm is the LRTA* algorithm, which looks ahead, and moves based only on that information. For example, it looks ahead, maybe 3 steps, and sees what sequence of 3 steps will bring it closer, in a straight-line measurement, to the goal. This has the benefit of always executing within a certain amount of time. Unfortunately, walls between the agent and the goal can and will prove difficult to surmount.</p>
<p>An addition to the LRTA* raises the &#8220;distance&#8221; weighting of a node every time the agent goes over it, thus eventually forcing the agent to go around the wall. But it still takes bumping into the wall three or four times to finally find the correct path.</p>
<h1><span style="text-decoration: underline;"><strong>Its all real(time that is).</strong></span></h1>
<p>There are a few constraints that pathfinding needs to work under in games, and even in robotics(Phoenix Mars Lander anyone?). You need to have something that can operate and find its way with only a bit of computation, and predictably work well. It would be a disaster for a $70 million dollar robot to keep bumping into a rock.</p>
<p>As well, with ever more powerful cpus and gpus, gamers demand more and more units, and bigger and better maps. The irony is, that with increasing hardware power&#8230; comes even less time to compute pathfinding and general AI. Computer power has only quadrupled a couple of times since Starcraft, yet, we demand 100 times more units, with smarter and more intuitive AI, coupled with four or five times bigger maps.</p>
<h1><span style="text-decoration: underline;">Dynamic Lookahead</span></h1>
<p>So, the way that D LRTA* works is first, they have dynamic lookaheads. Some squares on a map require more searching to find a near optimal solution, particularly around corners. One way to determine the lookahead value for a square, is to naively compute the required minimum lookahead depth to any other possible square. Then, store the pairs of coordinates and that lookahead depth, and just look it up! Simple, eh?</p>
<p>Not really. On a 512&#215;512 map, that results in ~68 billion pairs to store. On a normal desktop, that would take about 2 years to compute(probably a lot less on a compute farm&#8230; google could likely do it in one minute, because its an extremely parallelizable problem.) This is quite unacceptable, both due to time constraints and space constraints. This is where the research group&#8217;s second innovation comes into play.</p>
<h1><span style="text-decoration: underline;">A house divided&#8230;</span></h1>
<p>They found that splitting up the map into spaces, and then computing the optimal lookahead depth from one space to another worked very well. In the worst case, it devolved into LRTA*. However, there must be a better way to divide things up&#8230;</p>
<h1><span style="text-decoration: underline;">Clique Abstraction</span></h1>
<p>[Sturtevant, Buro 2005]</p>
<p>Clique abstraction was developed in the above cited paper. It is the concept of taking adjacent tiles, and lumping them into one larger tile. For example, four adjacent tiles that are all interconnected, could be replaced by one larger tile. And so on, with larger and larger abstraction at each level. (Take a look at page 38 from the PDF)</p>
<p>It becomes a lot easier to pre-calculate the optimal lookahead depths when you have a sufficient level of abstraction. Granted, you lose a bit of that oh so sweet optimalness, but its worth it for getting things to work within a certain amount of time.  Of course, walls and pockets on maps still give the budding algorithm a bit of trouble. Which leads us to the next addition to D LRTA*:</p>
<h1><span style="text-decoration: underline;">Setting goals</span></h1>
<p>The team found that by setting intermediate goals, they take advantage of a small property of lookahead algorithms: they&#8217;re more accurate closer to the goal. By setting goals in each of the subdivided spaces, its plainly obvious to the algorithm that going that-away is better than going this-away.</p>
<p>As well, by not placing goals in spaces that are obviously farther away from the ultimate goal than the starting point, it makes the algorithm ignore those spaces.</p>
<p>Right now, there are two ways to do dynamic lookahead: pre-calculated with clique abstractions, or calculated on the fly with sub-divided spaces and intermediate goals. They both come with their pros and cons, and if the game company can afford it, it appears that precomputed is much more reliable.</p>
<p>Optimal lookahead:</p>
<div id="attachment_107" class="wp-caption alignnone" style="width: 310px"><a href="http://www.oddco.ca/zeroth/zblog/wp-content/uploads/2008/07/optimal.png"><img class="size-medium wp-image-107" title="optimal" src="http://www.oddco.ca/zeroth/zblog/wp-content/uploads/2008/07/optimal-300x192.png" alt="Optimal lookahead depths for a Baldur's gate map" width="300" height="192" /></a><p class="wp-caption-text">Optimal lookahead depths for a Baldur&#39;s gate map</p></div>
<p>Stored Lookahead</p>
<div id="attachment_108" class="wp-caption alignnone" style="width: 310px"><a href="http://www.oddco.ca/zeroth/zblog/wp-content/uploads/2008/07/stored.png"><img class="size-medium wp-image-108" title="stored" src="http://www.oddco.ca/zeroth/zblog/wp-content/uploads/2008/07/stored-300x192.png" alt="Stored lookahead depths for a Baldur's gate map" width="300" height="192" /></a><p class="wp-caption-text">Stored lookahead depths for a Baldur&#39;s gate map</p></div>
<p>I do have a few questions about the research, that unfortunately I didn&#8217;t ask at the time, for example, the outliers on the graph on page 70. One of the results for the little dots was very efficient, but the other two dots are optimal, yet expanded lots and lots of moves. As well, I&#8217;d like to know exactly what the different algorithms that were used for the graphs, specifically. I will ask the presenter today.</p>
<h1><span style="text-decoration: underline;">What else?</span></h1>
<p>What needs to be done to expand this research, is to look at how to handle dynamically changing maps, and moving targets. As well, they haven&#8217;t looked at memory storage techniques or compression of precomputed files.(According to Information Theory, the data files could likely be extremely compressed, on the order of 75% compression or so.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/07/29/dynamic-pathfindingcutting-edge-research/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Functional for the win</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/06/19/functional-for-the-win/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/06/19/functional-for-the-win/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 20:53:31 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python tricks]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=81</guid>
		<description><![CDATA[As part of my new job, as a research student, I have to process a lot of data. On the order of several hundreds of thousands of records. So, I turned to my favourite language, Python. Its what caused my earlier issues with memory management. A few more lessons I&#8217;ve learned: sometimes theres a faster [...]]]></description>
			<content:encoded><![CDATA[<p>As part of my new job, as a research student, I have to process a lot of data. On the order of several hundreds of thousands of records. So, I turned to my favourite language, Python. Its what caused my earlier issues with memory management.</p>
<p>A few more lessons I&#8217;ve learned:</p>
<ul>
<li>sometimes theres a faster way to do stuff.</li>
<li>make sure you aren&#8217;t putting repetitive data into a database</li>
<li>verify the data format before you download 2-3 GB of it</li>
</ul>
<p>Okay, so, faster way to do stuff. Python has both a performance advantage and disadvantage. The advantage is that it only takes one or two hours to code up something to process data. The disadvantage is that in an effort to be clear and simple, one can end up coding a task that takes 20-100x longer than it should. Like I did.</p>
<p>I had a lot of nested loops, python speed loops, involving lots of duplicate handling, inserting to a database, etc. It took five hours to go through  only 14 sets of queries to clear out. I had a lot of duplicate rows that needed to be erased. For a query looking for rows with an id of 15, there are 25,607 records. Removing the duplicates yields&#8230; 807. Brilliant.</p>
<p>Today, I decided to take most of the loops, and replace them with their functional counterparts, map, lambda, and Google&#8217;s functional tools in python,  goopy. In only two hours, it has gone through now, 160 ids. In less than half the time. Pretty amazing, eh?</p>
<p>Here&#8217;s a sample:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">all</span>:
     curs=curs.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into elements2 values(?, ?, ?, ?, ?)'</span>, <span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, row<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>replaced with:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">map</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> row: <span style="color: #66cc66;">&lt;</span>code<span style="color: #66cc66;">&gt;</span>conn.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'insert into elements2 values(?, ?, ?, ?, ?)'</span>, <span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, row<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">&lt;</span>/code<span style="color: #66cc66;">&gt;</span>, <span style="color: #008000;">all</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A little bit more difficult to read, yes, but, the speed improvements are well over 50 times faster&#8230;</p>
<p>All this work to clear out duplicate entries, and how intensive it can be to fix, is a perfect example of why the &#8220;Look Before You leap&#8221; programming paradigm works better than, &#8220;Ask for Forgiveness Not Permission&#8221;.</p>
<p>Now, once the processing is done, I&#8217;ll be able to go through all the data&#8230; and add a piece of information I forgot to process in the first place to each entry. Yay me!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/06/19/functional-for-the-win/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory management in Python</title>
		<link>http://www.oddco.ca/zeroth/zblog/2008/06/17/memory-management-in-python/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2008/06/17/memory-management-in-python/#comments</comments>
		<pubDate>Tue, 17 Jun 2008 21:43:12 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[design]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python tricks]]></category>

		<guid isPermaLink="false">http://www.oddco.ca/zeroth/zblog/?p=80</guid>
		<description><![CDATA[Thats right, the dreaded two m&#8217;s. If you use C or C++, its quite dreaded. However, for about the first time ever, I ran into memory management issues in Python! Well, partially due to my own mistakes, and partially due to the way Python handles memory. Memory handling schemes go awry Well, not quite. More [...]]]></description>
			<content:encoded><![CDATA[<p>Thats right, the dreaded two m&#8217;s. If you use C or C++, its quite dreaded. However, for about the first time ever, I ran into memory management issues in Python! Well, partially due to my own mistakes, and partially due to the way Python handles memory.</p>
<h2>Memory handling schemes go awry</h2>
<p>Well, not quite. More like, a leaky abstraction, compounded by my own idiot mistakes in the code. When memory is allocated in a loop, it is normally a smart optimization to hold onto that memory. What this means is that there are some cool things that you can do, like, refer to variables that were allocated inside a loop, and they will be in the local scope. They will just have the latest value thats been assigned to that variable.</p>
<p>However, in my case it backfired. The memory was held onto&#8230; and never reused. Essentially, I had a memory leak.</p>
<h2>My own stupid mistakes</h2>
<p>What happened, is that I was loading in a variable from a pickled file. However, what I had done was:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">elements = <span style="color: #dc143c;">pickle</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'file'</span>, <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Normally, this would be okay, however, due to the size of the files, and the fact I was doing this in a loop, over different files, is that there was a now opened multi-megabyte file floating around, that has not been closed or reallocated. Actually, several. I came home after two hours to find that almost all of my RAM and 70% of my swap space had been used. Thats 70% of 3.8 GB. Yeah, great job Zeroth.</p>
<p>The lesson here is, that what works well for a small loop, or small files, does not neccesarilly work well for large files or large loops. Always make sure you close every file you open, and if worst comes to worst, allocate memory by using the <code>del</code> keyword in Python. I&#8217;m using this, and there is very moderate memory growth, as compared to the quite massive almost 6 GB of memory the code was taking up before.</p>
<p>So the proper way to do this is:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'file'</span>, <span style="color: #483d8b;">'rb'</span><span style="color: black;">&#41;</span>
elements = <span style="color: #dc143c;">pickle</span>.<span style="color: black;">load</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">del</span> f</pre></div></div>

<p>I do need to make this clear. This fault was not due to Python, or any problems or bugs in it, but rather with my own sloppy code. Even the best of us make mistakes, and I&#8217;m definitely not the best of us.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2008/06/17/memory-management-in-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cool Python Tricks part II</title>
		<link>http://www.oddco.ca/zeroth/zblog/2007/12/05/cool-python-tricks-part-ii/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2007/12/05/cool-python-tricks-part-ii/#comments</comments>
		<pubDate>Wed, 05 Dec 2007 15:10:04 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python tricks]]></category>
		<category><![CDATA[list comprehension]]></category>

		<guid isPermaLink="false">http://oddco.ca/zerothsblog/2007/12/05/cool-python-tricks-part-ii/</guid>
		<description><![CDATA[A new installment in my ever-popular series. Todays installment will concern one of the best performance enhancing tricks in python: list comprehensions. Most beginning pythonistas will produce code like this: lst=&#91;&#93; for i in range&#40;0, 10&#41;: lst.append&#40;i**2&#41; &#160; Now, thats pretty okay. It runs&#8230; okay. It is the kind of code I would expect from [...]]]></description>
			<content:encoded><![CDATA[<p><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /><title></title><meta name="GENERATOR" content="OpenOffice.org 2.3  (Unix)" /></p>
<style type="text/css"> 	<!-- 		@page { size: 21.59cm 27.94cm; margin: 2cm } 		P { margin-bottom: 0.21cm } 	--> 	</style>
<p>A new installment in my ever-popular <a href="http://oddco.ca/zerothsblog/category/python-tricks/" title="Cool python tricks! Tricks that will make you a better Python Guru">series.</a></p>
<p>Todays installment will concern one of the best performance enhancing tricks in python: list comprehensions.</p>
<p style="margin-bottom: 0cm">Most beginning pythonistas will produce code like this:</p>
<p style="margin-bottom: 0cm">

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">lst=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>:
     lst.<span style="color: black;">append</span><span style="color: black;">&#40;</span>i<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span></pre></div></div>

<p><span id="more-32"></span></p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Now, thats pretty okay. It runs&#8230; okay. It is the kind of code I would expect from a newcomer to Python, however, one of Python&#8217;s weaknesses is its slower running speed. This, by the way, is a weakness for pretty much any interpreted language, like Java or Ruby.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Now, imagine you have a much larger data set to work with. On the order of thousands, hundreds of thousands. Processing all of it at Python speed will drag your program down.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">But, you are in luck: there is a faster way.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Unfortunately, this faster method has its own set of issues, and the first and most obvious one, is legibility. Done wrong this technique can and will obscure the meaning of your program, and make it harder to maintain.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">This technique is list comprehensions. The upper piece of code would be, in a list comprehension,</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">lst=<span style="color: black;">&#91;</span>i<span style="color: #66cc66;">**</span><span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></div></div>

</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">This line of code runs in C speed, not Python speed. For a large dataset, this can be an improvement of 10-100 times faster!</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Now, lets break it down: there are three parts to a list comprehension. The first portion is an expression. The second portion is a <em>for </em>clause. And the third portion is one or more <em>for</em> or <em>if </em>clauses.</p>
<p style="margin-bottom: 0cm">The first for clause in the list comprehension declares a variable, and the expression must do something with that variable.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Or, for those language lawyers:</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm"><a title="tok-test" name="tok-test"></a>test ::= <a href="http://docs.python.org/ref/Booleans.html#tok-or_test">or_test</a> | <a href="http://docs.python.org/ref/lambdas.html#tok-lambda_form">lambda_form</a></p>
<p style="margin-bottom: 0cm"><a title="tok-testlist" name="tok-testlist"></a>testlist ::= <a href="http://docs.python.org/ref/lists.html#tok-test">test</a> ( &#8220;,&#8221; <a href="http://docs.python.org/ref/lists.html#tok-test">test</a> )* [ "," ]</p>
<p style="margin-bottom: 0cm"><a title="tok-list_display" name="tok-list_display"></a>list_display ::= &#8220;[" [<a href="http://docs.python.org/ref/lists.html#tok-listmaker">listmaker</a>] &#8220;]&#8221;</p>
<p style="margin-bottom: 0cm"><a title="tok-listmaker" name="tok-listmaker"></a>listmaker ::= <a href="http://docs.python.org/ref/Booleans.html#tok-expression">expression</a> ( <a href="http://docs.python.org/ref/lists.html#tok-list_for">list_for</a> | ( &#8220;,&#8221; <a href="http://docs.python.org/ref/Booleans.html#tok-expression">expression</a> )* [","] )</p>
<p style="margin-bottom: 0cm"><a title="tok-list_iter" name="tok-list_iter"></a>list_iter ::= <a href="http://docs.python.org/ref/lists.html#tok-list_for">list_for</a> | <a href="http://docs.python.org/ref/lists.html#tok-list_if">list_if</a></p>
<p style="margin-bottom: 0cm"><a title="tok-list_for" name="tok-list_for"></a>list_for ::= &#8220;for&#8221; <a href="http://docs.python.org/ref/exprlists.html#tok-expression_list">expression_list</a> &#8220;in&#8221; <a href="http://docs.python.org/ref/lists.html#tok-testlist">testlist</a> [<a href="http://docs.python.org/ref/lists.html#tok-list_iter">list_iter</a>]</p>
<p style="margin-bottom: 0cm"><a title="tok-list_if" name="tok-list_if"></a>list_if ::= &#8220;if&#8221; <a href="http://docs.python.org/ref/lists.html#tok-test">test</a> [<a href="http://docs.python.org/ref/lists.html#tok-list_iter">list_iter</a>]</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Basically, a list comprehension consists of:</p>
<p style="margin-bottom: 0cm">

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>expression using <span style="color: #483d8b;">'x'</span> variable <span style="color: #ff7700;font-weight:bold;">for</span> clause declaring <span style="color: #483d8b;">'x'</span> <span style="color: black;">&#40;</span>optionally: <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'x'</span> meets some condition<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span></pre></div></div>

</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">Again, one of the cardinal rules of optimization are that you only optimize when you know where the bottlenecks are. So, when you first code a for loop or list initialization, use the regular for loop. Then, after the program works, you can optimize it out to a list comprehension, and test the resulting speed.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">This will, in most cases, make your program faster, and make you a Python guru. So, till next time, keep on hissing!</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2007/12/05/cool-python-tricks-part-ii/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cool Python Tricks part I</title>
		<link>http://www.oddco.ca/zeroth/zblog/2007/11/30/cool-python-tricks-part-i/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2007/11/30/cool-python-tricks-part-i/#comments</comments>
		<pubDate>Fri, 30 Nov 2007 03:25:27 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[design patterns]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python tricks]]></category>
		<category><![CDATA[borg design patterns]]></category>

		<guid isPermaLink="false">http://oddco.ca/zerothsblog/2007/11/30/cool-python-tricks-part-i/</guid>
		<description><![CDATA[I&#8217;ve spent almost five years programming as a hobby, and my first language was Python. Its still one of my favourites for many reasons; simplicity, power, rapid development, and its fun. Due to its nature, Python is very flexible and very versatile, and over the years, I&#8217;ve learned some very very cool and useful python [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve spent almost five years programming as a hobby, and my first language was Python. Its still one of my favourites for many reasons; simplicity, power, rapid development, and its fun.</p>
<p>Due to its nature, Python is very flexible and very versatile, and over the years, I&#8217;ve learned some very very cool and useful python idioms and tricks.</p>
<p>What I will show today is a Design Pattern that fits the ideology of Python well, and delivers a great amount of flexibility and usefulness. Its called the Borg Design Pattern.<span id="more-28"></span></p>
<p>I&#8217;m sure we&#8217;re all familiar with the Singleton design pattern. The Borg DP is almost precisely the opposite. Instead of one instantiated object, and just one, the Borg DP allows you to instantiate any number of the objects, and have them all share the exact same traits, methods, variables.<br />
(assume there is proper formatting for this function)<br />
<code><br />
1| class Borg:<br />
2| _share_state={}<br />
3|    def __init__(self, *args, **kwargs):<br />
4|        self.__dict__=self._shared_state<br />
</code></p>
<p>Here&#8217;s the beauty of the Borg DP: on any of the instantiated objects, if you modify a variable, <strong>all</strong> of the instantiated objects reflect that modification. Every single one of them. Hence the name, Borg Design Pattern.</p>
<p>The important line here is line two: &#8220;__dict__={}&#8221; before you declare any methods of variables. When you do this in Python, all instantiated classes have a reference to that single variable and object. And __dict__ is an internal dict of the class, that stores all methods and all  variables!</p>
<p>The power of this trick is such that, you can use it for many of the same places you once used a Singleton, but without needing to worry about references, thread safe access, even checking how many objects have been instantiated. It cleans up all associated code, and gives you the confidence to know exactly what is going on with the object.</p>
<p>And thats part one! Stay tuned for more Cool Python Tricks(tm), hopefully one a week!</p>
<p>EDIT: Fixed borg design pattern, small bug.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2007/11/30/cool-python-tricks-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mission Statement</title>
		<link>http://www.oddco.ca/zeroth/zblog/2007/11/29/mission-statement/</link>
		<comments>http://www.oddco.ca/zeroth/zblog/2007/11/29/mission-statement/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 05:53:53 +0000</pubDate>
		<dc:creator>Zeroth</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[mission statement]]></category>

		<guid isPermaLink="false">http://oddco.ca/zerothsblog/2007/11/29/mission-statement/</guid>
		<description><![CDATA[I&#8217;ve made enough posts here without making a mission statement of any sort, so here goes. &#160; I want to help others like myself, curious and wanting to learn. I wish I&#8217;d had someone to share things with me several years ago, things I learned through my own trial and error, through blood and sweat [...]]]></description>
			<content:encoded><![CDATA[<p><meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8" /><title></title><meta name="GENERATOR" content="OpenOffice.org 2.3  (Unix)" /></p>
<style type="text/css"> 	<!-- 		@page { size: 21.59cm 27.94cm; margin: 2cm } 		P { margin-bottom: 0.21cm } 	--> 	</style>
<p style="margin-bottom: 0cm">I&#8217;ve made enough posts here without making a mission statement of any sort, so here goes.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">I want to help others like myself, curious and wanting to learn. I wish I&#8217;d had someone to share things with me several years ago, things I learned through my own trial and error, through blood and sweat dearly earned.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">I chose to become a programmer simply because their is nothing quite like it. All you need is intellect, logic, and creativity. And with those base ingredients, you can construct anything. Anything at all. It appealed to me, the power a programmer has over this inscrutable machine called a computer. I found it easy, and interesting, and thus my life goal was set.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">However, what kind of programmer was I to be? Will I be an egghead academic, working on complex and unknowable topics? Will I become a Unix hacker, complete with scruffy beard and free software on the tongue? Will I become one of those web 2.0 darlings, perfectly awkward in the limelight, and caring about things like &#8216;spiders&#8217; and &#8216;eyeballs&#8217; like they were some witche&#8217;s brew? I don&#8217;t know yet, but I do know, I have more fun making and designing games than anything else, and that is where I will head.</p>
<p style="margin-bottom: 0cm">&nbsp;</p>
<p style="margin-bottom: 0cm">But no, I don&#8217;t want to join Blizzard, or Bioware. No, I&#8217;m starting my own company! One where code monkeys can swing in the trees and go home at the end of the day. One where Artists of every kind aspire to be hired. A place where brilliance seeps out of the walls, and is breathed in like so many spores. I am making OddCo, with my best friend, Ruby. Join me for the ride.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.oddco.ca/zeroth/zblog/2007/11/29/mission-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

