<?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>The GITS Blog &#187; code</title>
	<atom:link href="http://ginstrom.com/scribbles/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginstrom.com/scribbles</link>
	<description>Random scribbling about programming, translation, and Japan</description>
	<lastBuildDate>Fri, 11 May 2012 05:10:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Inno Setup: Determining the data directory at install time</title>
		<link>http://ginstrom.com/scribbles/2009/04/08/inno-setup-determining-the-data-directory-at-install-time/</link>
		<comments>http://ginstrom.com/scribbles/2009/04/08/inno-setup-determining-the-data-directory-at-install-time/#comments</comments>
		<pubDate>Wed, 08 Apr 2009 12:28:14 +0000</pubDate>
		<dc:creator>Ryan Ginstrom</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[app data]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[dynamic]]></category>
		<category><![CDATA[inno]]></category>

		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=961</guid>
		<description><![CDATA[Inno Setup is a fantastic and free application for creating Windows setup programs. It takes so much of the hassle out of the process that it's almost fun. I recently needed to extend Inno Setup with a custom code section: I needed to install the program's data to the Common App Data folder or the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://jrsoftware.org/isinfo.php">Inno Setup</a> is a fantastic and free application for creating Windows setup programs. It takes so much of the hassle out of the process that it's <em>almost</em> fun.</p>
<p>I recently needed to extend Inno Setup with a custom code section: I needed to install the program's data to the Common App Data folder or the Local App Data folder, depending on a selection made during the installation process (install for all users, or just me). Here's the script I came up with:</p>
<div class="dean_ch" style="white-space: wrap;">
; AppDataTest.<span class="me1">iss</span><br />
<span class="br0">&#91;</span><span class="kw1">Setup</span><span class="br0">&#93;</span><br />
<span class="kw3">AppName</span>=AppData Test<br />
<span class="kw3">AppVerName</span>=AppData Test <span class="nu0">0.1</span><br />
<span class="kw3">DefaultDirName</span>=<span class="br0">&#123;</span><span class="kw1">pf</span><span class="br0">&#125;</span>\AppDataTest<br />
<span class="kw3">DefaultGroupName</span>=AppDataTest<br />
<span class="kw3">OutputDir</span>=<span class="kw1">Setup</span></p>
<p><span class="br0">&#91;</span><span class="kw1">Files</span><span class="br0">&#93;</span><br />
<span class="kw3">Source</span>: AppDataTest.<span class="me1">iss</span>; &nbsp;<span class="kw3">DestDir</span>: <span class="br0">&#123;</span><span class="kw1">app</span><span class="br0">&#125;</span>\;<br />
<span class="kw3">Source</span>: AppDataTest.<span class="me1">iss</span>; &nbsp;<span class="kw3">DestDir</span>: <span class="br0">&#123;</span>code:DefAppDataFolder<span class="br0">&#125;</span>\Test\;</p>
<p><span class="br0">&#91;</span><span class="kw1">Tasks</span><span class="br0">&#93;</span><br />
<span class="kw3">Name</span>: common; <span class="kw3">Description</span>: <span class="st0">&quot;&amp;All users&quot;</span>; <span class="kw3">GroupDescription</span>: <span class="st0">&quot;Install For:&quot;</span>; <span class="kw3">Flags</span>: <span class="kw2">exclusive</span> <span class="kw2">unchecked</span><br />
<span class="kw3">Name</span>: local; &nbsp;<span class="kw3">Description</span>: <span class="st0">&quot;Just &amp;me&quot;</span>; <span class="kw3">GroupDescription</span>: <span class="st0">&quot;Install For:&quot;</span>; <span class="kw3">Flags</span>: <span class="kw2">exclusive</span></p>
<p><span class="br0">&#91;</span><span class="kw1">Icons</span><span class="br0">&#93;</span><br />
<span class="kw3">Name</span>: <span class="br0">&#123;</span><span class="kw1">group</span><span class="br0">&#125;</span>\Uninstall AppDataTest; <span class="kw3">Filename</span>: <span class="br0">&#123;</span>uninstallexe<span class="br0">&#125;</span></p>
<p><span class="br0">&#91;</span>Code<span class="br0">&#93;</span><br />
<span class="kw2">function</span> DefAppDataFolder<span class="br0">&#40;</span>Param: <span class="kw4">String</span><span class="br0">&#41;</span>: <span class="kw4">String</span>;<br />
<span class="kw2">begin</span><br />
&nbsp; <span class="kw2">if</span> IsTaskSelected<span class="br0">&#40;</span><span class="st0">'common'</span><span class="br0">&#41;</span> <span class="kw2">then</span><br />
&nbsp; &nbsp; Result := ExpandConstant<span class="br0">&#40;</span><span class="st0">'{commonappdata}'</span><span class="br0">&#41;</span><br />
&nbsp; <span class="kw2">else</span><br />
&nbsp; &nbsp; Result := ExpandConstant<span class="br0">&#40;</span><span class="st0">'{localappdata}'</span><span class="br0">&#41;</span><br />
<span class="kw2">end</span>;</div>
<p>I created a radio button for the [Tasks] page, asking whether the user wants to install the application for all users, or just herself.</p>
<p>Then in the [Code] section, I created a function to expand "DefAppDataFolder" to <code>commonappdata</code> or <code>localappdata</code>, depending on whether "common" or "local" is selected in in the [Tasks] section (<code>IsTaskSelected('common')</code>).</p>
<p>This value is then expanded in the [Files] section, at this line:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw3">Source</span>: AppDataTest.<span class="me1">iss</span>; &nbsp;<span class="kw3">DestDir</span>: <span class="br0">&#123;</span>code:DefAppDataFolder<span class="br0">&#125;</span>\Test\;</div>
<p>Here's the <a href="/code/AppDataTest/AppDataTest.iss" title="AppDataTest.iss">setup script (AppDataTest.iss)</a>, and the resulting <a href="/code/AppDataTest/setup.exe" title="setup.exe">setup program (setup.exe)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ginstrom.com/scribbles/2009/04/08/inno-setup-determining-the-data-directory-at-install-time/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Iterating over a window in Python</title>
		<link>http://ginstrom.com/scribbles/2008/12/07/iterating-over-a-window-in-python/</link>
		<comments>http://ginstrom.com/scribbles/2008/12/07/iterating-over-a-window-in-python/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 05:36:37 +0000</pubDate>
		<dc:creator>Ryan Ginstrom</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[deque]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[itertools]]></category>
		<category><![CDATA[window]]></category>

		<guid isPermaLink="false">http://ginstrom.com/scribbles/?p=685</guid>
		<description><![CDATA[This weekend, I had a fairly common problem: given a sequence of elements and an element in that sequence, I needed to get the next element; if the element was the last in the sequence, then I needed the first element. My first stab at a solution was pretty straightforward: def item_after&#40;elements, item&#41;: &#160; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend, I had a fairly common problem: given a sequence of elements and an element in that sequence, I needed to get the next element; if the element was the last in the sequence, then I needed the first element.</p>
<p>My first stab at a solution was pretty straightforward:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">def</span> item_after<span class="br0">&#40;</span>elements, item<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">for</span> i <span class="kw1">in</span> <span class="kw2">range</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>elements<span class="br0">&#41;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> elements<span class="br0">&#91;</span>i<span class="br0">&#93;</span> == item:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">try</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> elements<span class="br0">&#91;</span>i<span class="nu0">+1</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">except</span> <span class="kw2">IndexError</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> elements<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span></div>
<p>This works for the intended purpose, although the C-style looping doesn't sit too well with me. But this code has a major flaw: it doesn't work for generators.</p>
<p>After a little more thought, I decided that what I wanted was to iterate over a window of two items in the sequence, and if the first item matched, return the second one.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">from</span> <span class="kw3">itertools</span> <span class="kw1">import</span> chain</p>
<p><span class="kw1">def</span> by_pairs<span class="br0">&#40;</span>iterable<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; sequence = <span class="kw2">iter</span><span class="br0">&#40;</span>iterable<span class="br0">&#41;</span><br />
&nbsp; &nbsp; previous = sequence.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="kw2">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; current = sequence.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">yield</span> previous, current<br />
&nbsp; &nbsp; &nbsp; &nbsp; previous = current</div>
<p>To illustrate how this works:</p>
<div class="dean_ch" style="white-space: wrap;">
&gt;&gt;&gt; <span class="kw1">for</span> pair <span class="kw1">in</span> by_pairs<span class="br0">&#40;</span><span class="kw2">range</span><span class="br0">&#40;</span><span class="nu0">6</span><span class="br0">&#41;</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; <span class="kw1">print</span> pair</p>
<p>
<span class="br0">&#40;</span><span class="nu0">0</span>, <span class="nu0">1</span><span class="br0">&#41;</span><br />
<span class="br0">&#40;</span><span class="nu0">1</span>, <span class="nu0">2</span><span class="br0">&#41;</span><br />
<span class="br0">&#40;</span><span class="nu0">2</span>, <span class="nu0">3</span><span class="br0">&#41;</span><br />
<span class="br0">&#40;</span><span class="nu0">3</span>, <span class="nu0">4</span><span class="br0">&#41;</span><br />
<span class="br0">&#40;</span><span class="nu0">4</span>, <span class="nu0">5</span><span class="br0">&#41;</span></div>
<p>The new item_after function:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">def</span> item_after2<span class="br0">&#40;</span>elements, item<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; pairs = by_pairs<span class="br0">&#40;</span>elements<span class="br0">&#41;</span><br />
&nbsp; &nbsp; first, second = pairs.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> a, b <span class="kw1">in</span> chain<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#40;</span>first, second<span class="br0">&#41;</span><span class="br0">&#93;</span>, pairs<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> a == item:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> b<br />
&nbsp; &nbsp; <span class="kw1">return</span> first</div>
<p>In the new <code>item_after</code> function, I peel off the first pair so I can cache the first element, then glob it back on the front of the sequence using <code>itertools.chain</code>.</p>
<p>After a little more thought, I realized that the <code>by_pairs</code> function can be made generic to handle a window of any size.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">from</span> <span class="kw3">collections</span> <span class="kw1">import</span> deque</p>
<p><span class="kw1">def</span> window_iter<span class="br0">&#40;</span>elements, n<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; sequence = <span class="kw2">iter</span><span class="br0">&#40;</span>elements<span class="br0">&#41;</span><br />
&nbsp; &nbsp; window = deque<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> <span class="kw2">True</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; element = sequence.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; window.<span class="me1">append</span><span class="br0">&#40;</span>element<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span>window<span class="br0">&#41;</span> == n:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">yield</span> window<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.<span class="me1">popleft</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
<p>The item_after function now looks like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">def</span> item_after3<span class="br0">&#40;</span>elements, item<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; pairs = window_iter<span class="br0">&#40;</span>elements, <span class="nu0">2</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; first, second = pairs.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">for</span> a, b <span class="kw1">in</span> chain<span class="br0">&#40;</span><span class="br0">&#91;</span><span class="br0">&#40;</span>first, second<span class="br0">&#41;</span><span class="br0">&#93;</span>, pairs<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> a == item:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> b<br />
&nbsp; &nbsp; <span class="kw1">return</span> first</div>
<p>And with a little data: one of my pets gets a treat every day. I know who got the treat yesterday; who gets it next?</p>
<div class="dean_ch" style="white-space: wrap;">
pets = <span class="br0">&#91;</span>pet <span class="kw1">for</span> pet <span class="kw1">in</span> <span class="st0">&quot;Bear Lady Mikey Jerry Kiri&quot;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw1">print</span> <span class="st0">&quot;Pet after Kiri is&quot;</span>, item_after3<span class="br0">&#40;</span>pets, <span class="st0">&quot;Kiri&quot;</span><span class="br0">&#41;</span><br />
<span class="kw1">print</span> <span class="st0">&quot;Pet after Lady is&quot;</span>, item_after3<span class="br0">&#40;</span>pets, <span class="st0">&quot;Lady&quot;</span><span class="br0">&#41;</span></div>
<p>The output:</p>
<div class="dean_ch" style="white-space: wrap;">
Pet after Kiri is Bear<br />
Pet after Lady is Mikey</div>
<p>I can think of a lot of potential uses for this window iterator. One is in natural language processing, when calculating the "stickiness" of words in a corpus. You could iterate over a window of three words, calculating the collocations of words before and after the middle word.</p>
<p>Here's a rather contrived example: calculating the average temperature on days when the previous two days were 80 degrees or higher.</p>
<div class="dean_ch" style="white-space: wrap;">
<span class="kw1">def</span> ave_temp_after<span class="br0">&#40;</span>temp, temps<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; after_temps = <span class="br0">&#91;</span>z <span class="kw1">for</span> x, y, z <span class="kw1">in</span> window_iter<span class="br0">&#40;</span>temps, <span class="nu0">3</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">if</span> x &gt; temp &lt; y<span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">sum</span><span class="br0">&#40;</span>after_temps<span class="br0">&#41;</span> / <span class="kw2">float</span><span class="br0">&#40;</span><span class="kw2">len</span><span class="br0">&#40;</span>after_temps<span class="br0">&#41;</span><span class="br0">&#41;</span></p>
<p>tempdata = <span class="st0">&quot;99 101 85 91 71 64 67 90 81 101&quot;</span>.<span class="me1">split</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
temps = <span class="br0">&#40;</span><span class="kw2">float</span><span class="br0">&#40;</span>x<span class="br0">&#41;</span> <span class="kw1">for</span> x <span class="kw1">in</span> tempdata<span class="br0">&#41;</span><br />
temp = ave_temp_after<span class="br0">&#40;</span><span class="nu0">80</span>, temps<span class="br0">&#41;</span></p>
<p><span class="kw1">print</span> <span class="st0">&quot;Average temperature after two days of 80+:&quot;</span>, temp</div>
<p>Output:
</pre>
<div class="dean_ch" style="white-space: wrap;">
Average temperature after two days of 80+: 87.0</div>
<p>It strikes me that this would be great for those baseball statistics guys:</p>
<blockquote><p>This is just the third time since 1963 that a left-handed pinch hitter has broken a bat on three consecutive Wednesdays.</p></blockquote>
<p>I'm waiting for a call from The Show any day now.</p>
]]></content:encoded>
			<wfw:commentRss>http://ginstrom.com/scribbles/2008/12/07/iterating-over-a-window-in-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

