<?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>Algorithm.co.il &#187; Programming</title>
	<atom:link href="http://www.algorithm.co.il/blogs/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.algorithm.co.il/blogs</link>
	<description>Algorithms, for the heck of it</description>
	<lastBuildDate>Tue, 21 Jun 2011 20:37:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Wikipedia Images</title>
		<link>http://www.algorithm.co.il/blogs/programming/wikipedia-images/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/wikipedia-images/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 20:37:28 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[photo]]></category>
		<category><![CDATA[resizing]]></category>
		<category><![CDATA[wikipedia]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=701</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/wikipedia-images/' addthis:title='Wikipedia Images'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>A few days ago a friend (x) of a friend (y) showed me and my friend (y) a small app he was developing, that had photos from flickr and picasa. We suggested adding photos from Wikipedia as well, but he &#8230; <a href="http://www.algorithm.co.il/blogs/programming/wikipedia-images/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/wikipedia-images/' addthis:title='Wikipedia Images' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>A few days ago a friend (x) of a friend (y) showed me and my friend (y) a small app he was developing, that had photos from flickr and picasa. We suggested adding photos from Wikipedia as well, but he (x) said that the photos were too big, and it was too much trouble resizing them.<br />
Luckily for him I knew of Wikipedia&#8217;s &#8220;hidden&#8221; image resizing feature, and as it was useful to me and to someone else, I thought I&#8217;d share it here.</p>
<p>Let&#8217;s say you are looking to resize the following image of the Eiffel Tower: <a href="http://en.wikipedia.org/wiki/File:Tour_Eiffel_Wikimedia_Commons.jpg">http://en.wikipedia.org/wiki/File:Tour_Eiffel_Wikimedia_Commons.jpg</a>. Then the url to the image itself is:</p>
<blockquote><p>http://upload.wikimedia.org/wikipedia/commons/a/a8/Tour_Eiffel_Wikimedia_Commons.jpg</p></blockquote>
<p>To get the url to a resized image, just add &#8216;thumb/&#8217; after &#8216;commons/&#8217; and then add &#8216;/[%d]px-[filename]&#8216; at the end, were %d is the new width. So for our image, the new url would be:</p>
<blockquote><p>http://upload.wikimedia.org/wikipedia/commons/<strong>thumb/</strong>a/a8/Tour_Eiffel_Wikimedia_Commons.jpg<strong>/300px-Tour_Eiffel_Wikimedia_Commons.jpg</strong>
</p></blockquote>
<p>That&#8217;s it, simple and quick. Have fun adding Wikipedia to your content!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/wikipedia-images/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Small programming challenge No. 6 &#8211; nblocks</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/#comments</comments>
		<pubDate>Mon, 24 Jan 2011 23:22:44 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Statistics]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[nblocks]]></category>
		<category><![CDATA[percentile]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=622</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/' addthis:title='Small programming challenge No. 6 &#8211; nblocks'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I came up with this challenge when I had to write a function to divide a sequence to percentiles. I needed this to calculate some statistics over trips for plnnr.com. &#8220;This sounds trivial&#8221; I thought, and reached for my simple &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/' addthis:title='Small programming challenge No. 6 &#8211; nblocks' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I came up with this challenge when I had to write a function to divide a sequence to percentiles. I needed this to calculate some statistics over trips for <a href="http://plnnr.com/">plnnr.com</a>. &#8220;This sounds trivial&#8221; I thought, and reached for my simple blocks function:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> blocks<span style="color: black;">&#40;</span>seq, block_len<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;blocks(range(5),2) -&amp;gt; [[0, 1], [2, 3], [4]]&quot;&quot;&quot;</span>
    seq_len = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> seq_len<span style="color: #66cc66;">%</span>block_len == <span style="color: #ff4500;">0</span>:
        num_blocks = seq_len/block_len
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        num_blocks = <span style="color: #ff4500;">1</span> + <span style="color: black;">&#40;</span>seq_len/block_len<span style="color: black;">&#41;</span>
&nbsp;
    result = <span style="color: black;">&#91;</span><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;">xrange</span><span style="color: black;">&#40;</span>num_blocks<span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> idx, obj <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>seq<span style="color: black;">&#41;</span>:
        result<span style="color: black;">&#91;</span>idx/block_len<span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></div></div>

<p>So I set block_len to len(seq)/10 and called blocks(seq, block_len). Unfortunately, according to the docs of blocks (which I wrote&#8230;), when there is extra data, a &#8220;partial&#8221; block is added &#8211; which is exactly what we don&#8217;t want when calculating percentiles.<br />
<strong>Instead, the behavior we want is nblocks(seq, number_of_blocks), for example: nblocks(range(10), 3) -&gt; [0, 1, 2], [3, 4, 5], [6, 7, 8, 9].</strong></p>
<p>This is a surprisingly hard to write function, or rather, harder than you&#8217;d expect. I&#8217;ll be especially glad if someone writes it elegantly. My solution works well enough, but isn&#8217;t the prettiest.</p>
<p>So, you have the definition &#8211; let&#8217;s see if you can do better than me. Once enough solutions are presented, I will present my own.</p>
<p><u>IMPORTANT EDIT: the required signature is <strong>nblocks(seq, num_blocks).</strong></u> So for seq(range(10), 3), the return value should be 3 blocks, with the last one having an extra item. As a general rule, the extra items should be spread as evenly as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-6-nblocks/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Visualizing Data Using the Hilbert Curve</title>
		<link>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/</link>
		<comments>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 06:57:30 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Fractals]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[hilbert curve]]></category>
		<category><![CDATA[PIL]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=525</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he &#8230; <a href="http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/' addthis:title='Visualizing Data Using the Hilbert Curve' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago, a coworker asked me to help him visualize some data. He had a very long series (many millions) of data points, and he thought that plotting a pixel for each one would visualize it well, so he asked for my help.<br />
I installed Python &#038; PIL on his machine, and not too long after, he had the image plotted. The script looked something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data_points = get_data_points<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
n =  <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span>
&nbsp;
image = Image<span style="color: black;">&#40;</span><span style="color: #483d8b;">'1'</span>, <span style="color: black;">&#40;</span>n, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> idx, pt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">enumerate</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>:
	image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>idx/n, idx<span style="color: #66cc66;">%</span>n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
image.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bla.png'</span>, <span style="color: #483d8b;">'png'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Easy enough to do. Well, easy enough if you have enough memory to handle very large data sets. Luckily enough, we had just enough memory for this script &#038; data series, and we were happy. The image was generated, and everything worked fine.</p>
<p>Still, we wanted to improve on that. One problem with this visualization is that two horizontally adjacent pixels don’t have anything to do with each other. Remembering xkcd’s &#8220;<a href="http://xkcd.com/195/">Map of the Internet</a>&#8220;, I decided to use the <a href="http://en.wikipedia.org/wiki/Hilbert_curve">Hilbert Curve</a>. I started with wikipedia&#8217;s version of the code for the Python turtle and changed it to generate a string of instructions of where to put pixels. On the way I improved the time complexity by changing it to have only two recursion calls instead of four. (It can probably be taken down to one by the way, I leave that as a challenge to the reader :)</p>
<p>Unfortunately, at this point we didn’t have enough memory to hold all of those instructions, so I changed it into a generator. Now it was too slow. I cached the lower levels of the recursion, and now it worked in reasonable time (about 3-5 minutes), with reasonable memory requirements (no OutOfMemory exceptions). Of course, I&#8217;m skipping a bit of exceptions and debugging along the way. Still, it was relatively straightforward.</p>
<p>Writing the generator wasn&#8217;t enough &#8211; there were still pixels to draw! It took a few more minutes to write a simple &#8220;turtle&#8221;, that walks the generated hilbert curve.<br />
Now, we were ready:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">hilbert = Hilbert<span style="color: black;">&#40;</span><span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">math</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data_points<span style="color: black;">&#41;</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span> + <span style="color: #ff4500;">0.5</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> pt <span style="color: #ff7700;font-weight:bold;">in</span> data_points:
    x,y = hilbert.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    image.<span style="color: black;">putpixel</span><span style="color: black;">&#40;</span>pt, <span style="color: black;">&#40;</span>x,y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>A few minutes later, the image was generated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/math/visualizing-data-using-the-hilbert-curve/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Open Redirects</title>
		<link>http://www.algorithm.co.il/blogs/programming/open-redirects/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/open-redirects/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 19:59:18 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[web-design]]></category>
		<category><![CDATA[cross site scripting]]></category>
		<category><![CDATA[csrf]]></category>
		<category><![CDATA[open redirect]]></category>
		<category><![CDATA[robots.txt]]></category>
		<category><![CDATA[web applications]]></category>
		<category><![CDATA[xss]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=560</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/open-redirects/' addthis:title='Open Redirects'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>In this post I&#8217;ll discuss an issue I tackled a short while ago &#8211; open redirects. But first, the story of how I got to it. Feel free to skip ahead to the technical discussion. Background Our analytics for plnnr.com &#8230; <a href="http://www.algorithm.co.il/blogs/programming/open-redirects/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/open-redirects/' addthis:title='Open Redirects' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll discuss an issue I tackled a short while ago &#8211; open redirects. But first, the story of how I got to it. Feel free to skip ahead to the technical discussion.</p>
<h4>Background</h4>
<p>Our analytics for <a href="http://plnnr.com/">plnnr.com &#8211; the website for trip planning</a> wasn&#8217;t working as well as we wanted. We&#8217;re using Google Analytics, and it&#8217;s hard generating the specific report we want, and when we did get it, it seemed to show inaccurate numbers. To partially alleviate the issue, I was required to add tracking pixels for facebook &#038; adwords, so we can better track conversions.<br />
For us, an &#8220;internal&#8221; conversion is when a user clicks on a link to a booking url (for a hotel, or any other &#8220;bookable&#8221; attraction).<br />
After reviewing it, I decided that the best course of action would be to create an intermediate page, on which the tracking pixels would appear. Such a page would receive as a parameter the url to redirect to, and will contain the appropriate tracking pixels.</p>
<h4>Description of problem</h4>
<p>Let&#8217;s say we build the url for the page like so:</p>

<div class="wp_syntax"><div class="code"><pre class="code" style="font-family:monospace;">/redirect/?url=X</pre></div></div>

<p>This page will load the appropriate tracking pixels, and then redirect to the given url (the X).<br />
The problems are:<br />
1. We are potentially exposing ourselves to <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">Cross Site Scripting (XSS)</a>, if we don&#8217;t filter the redirect url correctly. A malicious website could create links to our page that will run scripts in our context.</p>
<p>2. A malicious webmaster could steal search engine authority. Let&#8217;s say he has two domains: a.com and b.com, of which he cares about b.com. He creates links on a.com to:</p>

<div class="wp_syntax"><div class="code"><pre class="code" style="font-family:monospace;">ourdomain.com/redirect/?url=b.com</pre></div></div>

<p>A search engine crawls his page, sees the links to his domain, and gives ourdomain.com authority to b.com. Not nice.</p>
<p>3. A malicious website could create links to ourdomain.com that redirect to some malware site, this way harming the reputation of ourdomain.com, or creating better phishing links for ourdomain.com.</p>
<h4>Possible solutions</h4>
<p>Before we handle the open-redirect issues it&#8217;s important to block cross site scripting attacks. Since the attack might be possible by inject code into the url string, this is doable by correctly filtering the urls, and by using existing solutions for XSS.</p>
<p>As for the open redirect:</p>
<p>1. Non solution: cookies. We can pass the url we want in a cookie. Since cookies may only be set by our domain, other websites would not be able to set the redirect url. This doesn&#8217;t work well if you want more than one redirect link, or with multiple pages open, etc.</p>
<p>2. Checking the <a href="http://en.wikipedia.org/wiki/HTTP_referrer">referrer (&#8220;referer&#8221;)</a>, and allowing redirects to come only from our domain. This will break for all users who use a browser that hides referrer information, for example, those using zone-alarm. Google also suggests that if the referrer information is available, block if it&#8217;s external. That way we are permissive for clients that hide it.</p>
<p>3. Whitelisting redirect urls. This solutions actually comes in two flavors &#8211; one is keeping a list of all possible urls, and then checking urls against it. The other is keeping a list of allowed specific url parts, for example, domains. While keeping track of all allowed urls may be impractical, keeping track of allowed domains is quite doable. Downside is that you have to update that piece of the code as well each time you want to allow another domain.</p>
<p>4. Signing urls. Let the server keep a secret, and generate a (sha1) hash for each url of &#8220;url + secret&#8221;. In the redirect page, require the hash, and if it doesn&#8217;t match the desired hash, don&#8217;t redirect to that url. This solution is quite elegant, but it means that the client code (the javascript) can&#8217;t generate redirect URLs. In my case this incurs a design change, a bandwidth cost, and a general complication of the design.</p>
<p>5. <a href="http://en.wikipedia.org/wiki/Robots_exclusion_standard">Robots.txt</a>. Use the robots.txt file to prevent search engines from indexing the redirect page, thereby mitigating at least risk number 2.</p>
<p>6. Generating a token for the entire session, much like <a href="http://en.wikipedia.org/wiki/Cross-site_request_forgery">CSRF</a> protection. The session token is added to all links, and is later checked by the redirect page (on the server side).  This is especially easy to implement if you already have an existing anti-csrf mechanism in place.</p>
<p>7. A combination of the above.</p>
<h4>Discussion and my thoughts</h4>
<p>It seems to me, that blocking real users is unacceptable. Therefor, only filtering according to referrer information is unacceptable if you block users with no referrer information.<br />
At first I started to implement the url signing mechanism, but then I saw the cost associated with it, and reassessed the risks. Given that cross-site-scripting is solved, the biggest risk is stealing search-engine-authority. Right now I don&#8217;t consider the last risk (harming our reputation) as important enough, but this will become more acute in the future.</p>
<p>Handling this in a robots.txt file is very easy, and that was the solution I chose. I will probably add more defense mechanisms in the future. When I do add another defense mechanism, it seems that using permissive referrer filtering, and the existing anti-csrf code will be the easiest to implement. A whitelist of domains might also be acceptable in the future.</p>
<p>If you think I missed a possible risk, or a possible solution, or you have differing opinions regarding my assessments, I&#8217;ll be happy to hear about it.</p>
<p>My thanks go to Rafel, who discussed this issue with me.</p>
<h4><a name="further_reading">Further reading</a></h4>
<p>* <a href="http://www.owasp.org/index.php/Open_redirect">http://www.owasp.org/index.php/Open_redirect</a><br />
* <a href="http://www.google.com/support/webmasters/bin/answer.py?hl=en&#038;answer=171297">http://www.google.com/support/webmasters/bin/answer.py?hl=en&#038;answer=171297</a><br />
* <a href="http://muffinresearch.co.uk/archives/2009/09/30/open-redirects-and-phishing-vectors/">Open Redirects and Phishing Vectors/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/open-redirects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pyweb-il Presentation on Optimization Slides</title>
		<link>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 18:31:50 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[presentation slides]]></category>
		<category><![CDATA[pyweb-il]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=570</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/' addthis:title='Pyweb-il Presentation on Optimization Slides'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Last Monday I gave a presentation in pywebil on optimization, that&#8217;s loosely based on my blog post on the same subject. Here are the slides for that presentation.<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/' addthis:title='Pyweb-il Presentation on Optimization Slides' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Last Monday I gave a presentation in <a href="http://groups.google.com/group/pyweb-il">pywebil</a> on optimization, that&#8217;s loosely based on my <a href="http://www.algorithm.co.il/blogs/index.php/programming/python/10-python-optimization-tips-and-issues/">blog post on the same subject</a>. Here are the <a href="http://www.algorithm.co.il/sitecode/optimization.pdf">slides for that presentation</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/pyweb-il-oresentation-on-optimization-slides/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript Element Creator</title>
		<link>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 14:29:22 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[startup]]></category>
		<category><![CDATA[web-design]]></category>
		<category><![CDATA[element creator]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=542</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/javascript-element-creator/' addthis:title='Javascript Element Creator'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago I was working on optimizing the client side code of my website, plnnr.com, an online trip planner. This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out &#8230; <a href="http://www.algorithm.co.il/blogs/programming/javascript-element-creator/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/javascript-element-creator/' addthis:title='Javascript Element Creator' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago I was working on optimizing the client side code of my website, <a href="http://plnnr.com/">plnnr.com, an online trip planner</a>.<br />
This website does automatic trip planning, and the problem was that recalculating trips was slow. After profiling, I found out that most of the time wasn&#8217;t actually taken up by the algorithm, but by the UI. Rendering the trip to html was the costly part. The process was like so:</p>
<p>Client-side Javascript code generates new trip prefs -> application calculates new trip -> Client-side Javascript gets the new trip, and creates new html.</p>
<p>It&#8217;s important to note that the app is &#8220;ajax based&#8221;, so the actual trip html was generated by the Javascript code, and not the server. At the time I was using Mochikit to generate the new html. Mochikit has a pretty nifty API for generating html, but it&#8217;s quite a bit on the slow side. Basically, this API is a wrapper around createElement.</p>
<p>Well, first I did a little test, and found out that generating html with cloneNode and innerHTML is much faster than createElement. Still, there was a problem &#8211; I needed to generate many similar elements &#8211; similar but not identical. Consider entries on a trip itinerary &#8211; they all look the same, yet each one has a different name, a different time string, and a different onclick event.</p>
<p>What I needed was a Javascript based html template library. My requirements:<br />
1. Speed. Html had to be generated quickly.<br />
2.  Expressiveness. It had to be able to create pretty arbitrary html with a given context. For example, an anchor element (&lt;a> tag) with a given href property, and a given text content.<br />
3. References to inner elements: Many elements inside the generated html need various events attached to them, or various code processing. This should be easy to achieve.<br />
4. The library has to allow the template html to be written as html, and not only as javascript strings.</p>
<p>So, I sat down with <a href="http://twitter.com/daonb">Benny</a>, and we wrote the Javascript Element Creator, which we are now releasing under the BSD license. I originally wrote it to work with Mochikit and the Sizzle library, and Benny changed his version to worked with jquery.</p>
<p>After adding the code to my project, I got two things: first, everything worked much, much faster. Second, it was much easier changing the generated html when it was generated according to a template, and not directly in code.</p>
<h4>Instructions</h4>
<p>1. Write your html somewhere visible to the javascript code. Add the &#8220;template&#8221; class to the upper node, and the id will be the name of the template. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;some_div&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>2. Similarly to other template engines, add double brackets to signify where text should be inserted:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;some_div&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;[[link_url]]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>[[link_text]]<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>3. Create a creator object. It will &#8220;collect&#8221; your template, and will make it available to your code.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> creator <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> ElementCreator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>4. Generate your DOM object, and add it to the document;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> creator.<span style="color: #660066;">generate</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;some_div&quot;</span><span style="color: #339933;">,</span>
                           <span style="color: #009900;">&#123;</span>link_url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'/url/'</span><span style="color: #339933;">,</span>
                            link_text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'hello world'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
appendChildNodes<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h4>The code</h4>
<p>We decided to publish for now only the jquery version. I might publish the mochikit version as well at a later date. Since Benny wrote the jquery version, he also wrote the tests for that version.</p>
<p>All in all, the final code is pretty short, and could probably be even shorter. Still, it&#8217;s good enough, and gave me a very real performance boost.</p>
<p>Here is the <a href="http://www.algorithm.co.il/sitecode/elementcreator.zip">code</a>, have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/javascript-element-creator/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ethics in Programming</title>
		<link>http://www.algorithm.co.il/blogs/programming/ethics-in-programming/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/ethics-in-programming/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 19:23:00 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Philosophy]]></category>
		<category><![CDATA[Programming ethics]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=511</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/ethics-in-programming/' addthis:title='Ethics in Programming'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>Some time ago I was bothered by the issue of ethics in programming. I heard the question best raised during a &#8220;game unconference&#8221; I attended. There was a panel about monetary systems for games, and people talked about the issues &#8230; <a href="http://www.algorithm.co.il/blogs/programming/ethics-in-programming/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/ethics-in-programming/' addthis:title='Ethics in Programming' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>Some time ago I was bothered by the issue of ethics in programming.<br />
I heard the question best raised during a &#8220;game unconference&#8221; I attended. There was a panel about monetary systems for games, and people talked about the issues faced when adding money to an online game.<br />
At one point someone from the audience said about ingame monetary systems (such as in WoW) &#8220;it&#8217;s like gambling and drugs!&#8221;, to which one panelist jokingly replied &#8220;so we have a proven business model&#8221;, and another said &#8220;except it&#8217;s legal&#8221;.</p>
<p>This was all in good spirit, but it got me thinking:</p>
<h4>What are the programming jobs I will not take?</h4>
<p><span id="more-511"></span></p>
<p>To answer this question I gave the subject some more thought, and discussed it with my friends. To make the discussion more concrete, here is a short (partial) list of jobs of which at least one is probably problematical for you:</p>
<ul>
<li>Advertising</li>
<li>SEO</li>
<li>Pornography</li>
<li>Gambling</li>
<li>Spam and spam related
<ul>
<li>regular advertising</li>
<li>Botnet based spam</li>
<li>scams</li>
<li>harvesting email addresses</li>
</ul>
</li>
<li>Hacking
<ul>
<li>en masse</li>
<li>commercial espionage</li>
<li>targeted “cons”</li>
</ul>
</li>
<li>Costly addictive games</li>
<li>Affiliate marketing</li>
<li>DRM</li>
<li>Weapon R&#038;D</li>
<li>Lawful Interception</li>
<li>guerilla marketing, specifically <a href="http://en.wikipedia.org/wiki/Astroturfing">astroturfing</a></li>
</ul>
<p>A critical issue that came up in discussions is the &#8220;victim&#8221;. &#8220;Victimless&#8221; jobs were perceived as ethically better than ones with a victim. Also some people considered gambling ok, because the player agreed to play. Some people considered spam victimless.</p>
<p>Another argument was practicality. Someone argued that while spam is marginally ethical, he still wouldn&#8217;t do it, as the returns on doing spam are not worth it. Similarly, many people said that while they don&#8217;t see working on pornography as ethically wrong, they would still not do it because of the stigma attached to it.</p>
<p>Still, all the people I talked to pointed out jobs they will not do.<br />
When I tried to reason what jobs are not for me, I came up with the following hypothetical questions to ask:</p>
<ul>
<li>Would you use the product yourself?</li>
<li>If appropriate, will you let your children use it?</li>
<li>Would you let your spouse use it and pay for it?</li>
<li>Would you partner with someone who has that work experience?</li>
</ul>
<p>Using this guide, it&#8217;s easier to think about which jobs I&#8217;d rather avoid.</p>
<p>One last note: many times, morality is a luxury that not everyone has. In dire times, I believe many gray-area jobs would be considered less ambiguous. After all, <a href="http://www.imdb.com/title/tt0427944/">everyone has to pay the mortgage</a>.</p>
<h4>Further reading:</h4>
<p><a href="http://www.acm.org/about/code-of-ethics">ACM&#8217;s Code of Ethics</a><br />
<a href="http://www.techcrunch.com/2009/10/31/scamville-the-social-gaming-ecosystem-of-hell/">Scamville</a><br />
<a href="http://www.sjtrek.com/trek/rules/">The Rules of Acquisition</a><br />
<a href="http://www.codinghorror.com/blog/archives/001253.html">Jeff Atwood&#8217;s take on the subject</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/ethics-in-programming/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Simple SQLObject DB Migration how-to</title>
		<link>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/</link>
		<comments>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 05:50:58 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[database migration]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[sqlobject]]></category>
		<category><![CDATA[turbogears]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=507</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/' addthis:title='Simple SQLObject DB Migration how-to'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I&#8217;ve been using sqlobject for plnnr.com for quite some time now. So far my experience with it has been positive. Although I&#8217;ll probably change ORM when I move to django, for now it stays. While it stays, I need to &#8230; <a href="http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/' addthis:title='Simple SQLObject DB Migration how-to' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using sqlobject for <a href="http://plnnr.com">plnnr.com</a> for quite some time now. So far my experience with it has been positive. Although I&#8217;ll probably change ORM when I move to django, for now it stays. While it stays, I need to be able to upgrade my schema to add features.<br />
SQLObject already has a tool for the job, sqlobject-admin. There are instructions on how to use it, but I found them unsatisfactory.<br />
(By the way, both django&#8217;s ORM and sqlalchemy also have tools for that, django-south and sqlalchemy-migrate respectively.)</p>
<p>So here is how I use sqlobject-admin to do migrations. Note that if you&#8217;re using turbogears 1.0, you would probably be using tg-admin. In that case, bear in mind that tg-admin just simplifies the job for you by adding various standard parameters, but apart from that, the idea stays the same.<br />
Notes:<br />
* I wrote these instructions on a windows machine. On linux machines it should be almost the same, but might require tweaking.<br />
* I used a specific db URI in the examples. You can change it to whatever you want.<br />
* I once had to tweak the main sqlobject-admin file to add the current dir to sys.path. YMMV.</p>
<p><strong>1. Example project:</strong><br />
Let&#8217;s setup a project that uses sqlobject. We&#8217;ll create a single file, &#8216;main.py&#8217; with the following content:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlobject
&nbsp;
sqlobject.<span style="color: black;">sqlhub</span>.<span style="color: black;">processConnection</span> = sqlobject.<span style="color: black;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: black;">SQLObject</span><span style="color: black;">&#41;</span>:
    bla = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is about as simple as I could get it with sqlobject.</p>
<p><strong>2. Starting to use sqlobject-admin</strong><br />
Sqlobject-admin has quite a bit of bureaucracy to go through before you get everything to work right. For a simple project, I cheat (i.e. fake an egg :), and do the following:<br />
a. Create a directory in your project called sqlobject-history<br />
b. If your project name is sotest, create a directory inside your project called sotest.egg-info<br />
c. Inside that dir create a file called sqlobject.txt<br />
d. Inside that file write:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">db_module=main
history_dir=$base/sqlobject-history</pre></div></div>

<p>(note that the main here is the name of the module we created earlier).</p>
<p><strong>3. Start using sqlobject-admin</strong><br />
This will be the workflow with sqlobject-admin:<br />
1. Have the creation sql for the current code version.<br />
2. Update your code<br />
3. Generate the creation sql for the new code version, *without updating the db*<br />
4. Create an upgrade script using the diff between the versions<br />
5. Use the upgrade script.</p>
<p>More specifically:<br />
1. First time &#8211; do:</p>
<blockquote><p>sqlobject-admin record &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>2. To see that everything works, do:</p>
<blockquote><p>sqlobject-admin list &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>and:</p>
<blockquote><p>sqlobject-admin status &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>3. Update your database definition (in the Python file). For example, change the contents of main.py to:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> sqlobject
&nbsp;
sqlobject.<span style="color: black;">sqlhub</span>.<span style="color: black;">processConnection</span> = sqlobject.<span style="color: black;">connectionForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:/D|/work/sotest/sotest.sqlite'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyThing<span style="color: black;">&#40;</span>sqlobject.<span style="color: black;">SQLObject</span><span style="color: black;">&#41;</span>:
    bla = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bla2 = sqlobject.<span style="color: black;">StringCol</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>4. Here is the critical part. Do</p>
<blockquote><p>sqlobject-admin record &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite &#8211;no-db-record</p></blockquote>
<p>In the sqlobject-history directory there should be now two subdirectories, for each version. Let&#8217;s call the old version X and the new version Y. In the old version directory create a file:<br />
upgrade_sqlite_Y.sql (where Y is the new version&#8217;s name).<br />
In this file, write down the sql to add the bla2 column to the MyThing table. You can use the creation sql commands in the respective versions&#8217; directories to write it.</p>
<p>(note: if we used &#8211;edit we would get an editor opened, and if the edited file has any content when you close it, it will be saved as the upgrade script. I don&#8217;t like using this method. Note that if you&#8217;re on windows you&#8217;ll have to fix sqlobject-admin to open your editor, as the command it uses works only on linux machines.)</p>
<p>5. run<br />
<blockquote>sqlobject-admin upgrade &#8211;egg=sotest -c sqlite:/D|/work/sotest/sotest.sqlite</p></blockquote>
<p>6. Make sure everything is OK with sqlobject-admin status.</p>
<p><strong>3. After using the upgrade script</strong><br />
You can use the same upgrade script for other instances of your project. Just make sure that you have the versions numbers correct, and the first version recorded in the database.</p>
<p>I hope this will be useful for someone using sqlobject, I know I needed this kind of how-to. If you have any questions, feel free to ask them in comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/programming/simple-sqlobject-db-migration-how-to/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The mathematics behind the solution for Challenge No. 5</title>
		<link>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 23:49:15 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[base systems]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[Knuth]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=500</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/' addthis:title='The mathematics behind the solution for Challenge No. 5'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>If you take a look at the various solutions people proposed for the last challenge of generating a specific permutation, you&#8217;ll see that they are very similar. Most of them are based on some form of div-mod usage. The reason &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/' addthis:title='The mathematics behind the solution for Challenge No. 5' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>If you take a look at the various solutions people proposed for the <a href="http://www.algorithm.co.il/blogs/index.php/programming/small-programming-challenge-no-5-generating-a-permutation/">last challenge of generating a specific permutation</a>, you&#8217;ll see that they are very similar. Most of them are based on some form of div-mod usage. The reason this is so, is because all of these solutions are using the <a href="http://en.wikipedia.org/wiki/Factorial_base">Factorial Base</a>.</p>
<p>What does that mean?<br />
Note that we usually encounter div-mods when we want to find the representation of a number in a certain base. That should already pique your interest. Now consider that a base&#8217;s digits need not have the same weight. For example, consider how we count the number of seconds since the start of the week:</p>
<p>seconds of the last minute, A (at most 60-1)<br />
minutes of the last hour, B (at most 60-1)<br />
hours of the last day, C (at most (24-1)<br />
days of the last week, D (at most 7-1)</p>
<p>So given A, B, C, D, we would say that the number of seconds is:<br />
A + 60*B + 24*C + 7*D. This certainly looks like a base transformation. To go back, we would use divmod.</p>
<p>The factorial base is just the same, with the numbers n, n-1, &#8230; 1. Note that in the factorial base, you can only represent a finite number of numbers &#8211; n!. This should not be surprising &#8211; this is what we set out to do in the first place!<br />
The thing that I found really amazing about this is that all the people to whom I posed this challenge came up with almost the same &#8220;way&#8221; of solving it.</p>
<p>Other interesting curiosities regarding bases can be found in Knuth&#8217;s book, &#8220;The Art of Computer Programming&#8221;, volume 2, Section 4.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/the-mathematics-behind-the-solution-for-challenge-no-5/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Small Programming Challenge no. 5 &#8211; Generating a Permutation</title>
		<link>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/</link>
		<comments>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:43:45 +0000</pubDate>
		<dc:creator>lorg</dc:creator>
				<category><![CDATA[Challenges]]></category>
		<category><![CDATA[computer science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[challenge]]></category>

		<guid isPermaLink="false">http://www.algorithm.co.il/blogs/?p=493</guid>
		<description><![CDATA[<div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/' addthis:title='Small Programming Challenge no. 5 &#8211; Generating a Permutation'  ><a class="addthis_button_facebook_like" fb:like:layout="button_count"></a><a class="addthis_button_tweet"></a><a class="addthis_counter addthis_pill_style"></a></div>I thought of this one quite a long time ago, and I believe that the idea behind it is pretty nice mathematically. I got the idea for it from Knuth&#8217;s &#8220;The Art of Computer Programming&#8221;. The challenge is simple: write &#8230; <a href="http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/">Continue reading <span class="meta-nav">&#8594;</span></a><div class="addthis_toolbox addthis_default_style " addthis:url='http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/' addthis:title='Small Programming Challenge no. 5 &#8211; Generating a Permutation' ><a href="http://addthis.com/bookmark.php?v=250&#38;username=xa-4d2b47597ad291fb" class="addthis_button_compact">Share</a><span class="addthis_separator">&#124;</span><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I thought of this one quite a long time ago, and I believe that the idea behind it is pretty nice mathematically. I got the idea for it from Knuth&#8217;s &#8220;The Art of Computer Programming&#8221;.</p>
<p>The challenge is simple:<br />
write a function that receives as arguments two numbers, n, and num such that 0 <= num < n!. This function needs to return an array (list) representing a permutation of the numbers 0..n-1. For each possible num, the function needs to return a different permutation, such that over all values of num, all possible permutations are generated. The order of permutations is up to you.</p>
<p>The function you write should do this in at most O(n) time &#038; space (Various O(nlogn) are also acceptable).<br />
Write your solutions in the comments, in [ LANG ] [/ LANG ] blocks (without the spaces) where LANG is preferably Python :). I will post my solution in a few days. As usual, the most efficient &#038; elegant solution wins.</p>
<p>Go!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.algorithm.co.il/blogs/challenges/small-programming-challenge-no-5-generating-a-permutation/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

