<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Dabbler &#187; Programming</title>
	<atom:link href="http://sethjust.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://sethjust.com</link>
	<description>If it ain&#039;t broke, fix it!</description>
	<lastBuildDate>Wed, 25 Apr 2012 04:19:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>More Map Scripts</title>
		<link>http://sethjust.com/2012/02/11/more-map-scripts/</link>
		<comments>http://sethjust.com/2012/02/11/more-map-scripts/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 03:32:40 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=390</guid>
		<description><![CDATA[In the last installment‎ I wrote a script (gdal_slice.py) to cut up geotagged images and save them in a format that GPSdrive will read. I&#8217;ve uploaded that script into a new repository on GitHub. I&#8217;ve also uploaded a script I wrote called get_quad.py, which will download USGS topographic maps automatically. This script naturally complements gdal_slice.py; [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://sethjust.com/2011/07/13/map-tiles-with-python-gdal/ ">last installment</a>‎ I wrote a script (gdal_slice.py) to cut up geotagged images and save them in a format that GPSdrive will read. I&#8217;ve uploaded that script into a <a href="https://github.com/sethjust/map_scripts">new repository on GitHub</a>.</p>
<p>I&#8217;ve also uploaded a script I wrote called get_quad.py, which will download USGS topographic maps automatically. This script naturally complements gdal_slice.py; using the two together allows for almost-totally automatic population of GPSdrive&#8217;s map database with high quality topographic maps.</p>
<pre>usage: get_quad.py [-h] [-q] [-s STATE] (-g | -c COORD COORD)</pre>
<pre>Get DRG USGS quad for given latitude and longitude from LibreMaps.org</pre>
<pre>optional arguments:
 -h, --help show this help message and exit
 -q, --silent don't produce any output
 -s STATE, --state STATE
 Postal abbr. for the state in which the quad falls, so
 we don't need to look it up
 -g, --gps get coordinates from GPSd
 -c COORD COORD, --coords COORD COORD
 specify latitude N and longitude E (respectively) in
 decimal degrees; longitude should be negative, as all
 maps fall in the western hemisphere</pre>
<p>Unfortunately, because of the way USGS maps are organized, the script needs to know the state in which the coordinates fall. If the -s flag is not given, this is inferred from the coordinates, but this lookup requires internet access.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2012/02/11/more-map-scripts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>IndieWeb, or how to avoid Digital Sharecropping</title>
		<link>http://sethjust.com/2011/08/14/indieweb-or-how-to-avoid-digital-sharecropping/</link>
		<comments>http://sethjust.com/2011/08/14/indieweb-or-how-to-avoid-digital-sharecropping/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 18:54:57 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[addon]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[social]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=332</guid>
		<description><![CDATA[Thanks to the DorkbotPDX blog I found my way onto the website of this year&#8217;s IndieWebCamp, which took place in Portland June 25 &#38; 26. Although I was saddened that I wasn&#8217;t in town for it, the projects that ended up being developed by the camp immediately grabbed my interest. Although I had never been [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to the <a href="http://dorkbotpdx.org/">DorkbotPDX blog</a> I found my way onto the website of this year&#8217;s <a href="http://indiewebcamp.com/Main_Page">IndieWebCamp</a>, which took place in Portland June 25 &amp; 26. Although I was saddened that I wasn&#8217;t in town for it, the <a href="http://indiewebcamp.com/Demos">projects</a> that ended up being developed by the camp immediately grabbed my interest.</p>
<p>Although I had never been aware of the term &#8220;indie web&#8221; (which seems to have been around for almost 15 years, judging by <a href="http://www.uzine.net/article63.html">this article</a> from uzine.net), I&#8217;ve been building my own independent web presence for more than four years. In the words of the IndieWebCamp home page:</p>
<blockquote><p>Rather than posting content on many third-party silos of content, we should all begin owning the content we&#8217;re creating. Publish short status updates on your own domain, and syndicate to Twitter. Publish photos on your own domain, syndicate to Flickr, etc, etc.</p></blockquote>
<p>For some good summaries of the ideas and goals of the IndieWebCamp, <a href="http://gigaom.com/collaboration/the-indie-web-who-owns-your-identity/">this article</a> from GigaOm and <a href="http://markmhendrickson.com/homesteading-on-the-indie-web">this post</a> from Mark Hendrickson are a great place to start, while each of the camp&#8217;s <a href="http://indiewebcamp.com/Guest_List">guests</a> also have their own websites, each with a wealth of information.</p>
<p>While I&#8217;ve been using this domain for quite a while, it&#8217;s been in use alongside Facebook, Delicious (and Flickr, to a smaller degree) as a part of my web presence. However, since I ditched Facebook five or six months ago, I&#8217;ve been relying on this domain for more, and overall I&#8217;ve been pleased. However, as the IndieWebCamp is readily willing to admit, there is room for a lot of room for technical innovation in expanding the functionality of the indie web. Towards that end, I see three threads of work that I&#8217;m interested in pursuing:</p>
<ul>
<li>Using my site for Flickr-like photo sharing. While this might include syndication to Flickr at some point, I mostly want a light-weight DIY solution to photo sharing.</li>
<li>Similarly, using my site for bookmarking. I really like the service that delicious offers, but I want to own the data and let delicious simply keep a copy of it (see <a href="http://adactio.com/journal/4197/">http://adactio.com/journal/4197/</a> for some ideas).</li>
<li>Finally, I&#8217;m hugely enamored of the work on delivery-agnostic messaging that took place at IndieWebCamp, which you can find details of <a href="http://aaronparecki.com/IndieWeb_Messaging">here</a>. Not only is it a ridiculously simple protocol, but it starts to address some real issues of a distributed social web, in that it allows two-party communication based solely on domain names as unique identifiers, therefore eliminating the middleman in social-web-type communications.</li>
</ul>
<div>Hopefully I&#8217;ll have time to get one of these ideas worked out and running on my site soon (before school starts), so check back here soon, I&#8217;ll be sure to keep you posted.</div>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2011/08/14/indieweb-or-how-to-avoid-digital-sharecropping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Map Tiles with Python + GDAL</title>
		<link>http://sethjust.com/2011/07/13/map-tiles-with-python-gdal/</link>
		<comments>http://sethjust.com/2011/07/13/map-tiles-with-python-gdal/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 21:36:55 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[free]]></category>
		<category><![CDATA[gdal]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[maps]]></category>
		<category><![CDATA[open source]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=341</guid>
		<description><![CDATA[After getting GPSDrive running on OS X I started looking into different sources for maps. The GPSDrive wiki has a couple of interesting pages about this: one on creating maps and one on map sources. The second page pointed me to the LibreMap Project which has a complete collection of  USGS 1:24000 (large scale) topographic maps for the [...]]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://sethjust.com/2011/07/13/gpsdrive-on-os-x/">getting GPSDrive running on OS X</a> I started looking into different sources for maps. The GPSDrive wiki has a couple of interesting pages about this: one on <a href="http://sourceforge.net/apps/mediawiki/gpsdrive/index.php?title=Creating_maps">creating maps</a> and one on <a href="http://sourceforge.net/apps/mediawiki/gpsdrive/index.php?title=Data_sources">map sources</a>. The second page pointed me to <a href="http://libremap.org/">the LibreMap Project</a> which has a complete collection of  USGS 1:24000 (large scale) topographic maps for the entire US. These files are distributed as high-resolution GeoTIFF files, along with <a href="http://en.wikipedia.org/wiki/World_file">world files</a>, so the maps are fully geotagged. This means that they contain all of the information needed to generate GPSDrive-friendly map tiles, but GPSDrive will not read them directly. The <a href="http://sourceforge.net/apps/mediawiki/gpsdrive/index.php?title=Creating_maps">creating maps</a> page gives some (heavily out-of-date) advice on how to use the <span style="font-family: monospace;">gdal_slice.sh</span> script distributed with GPSDrive to create map tiles using the tools from <a href="http://gdal.org/">GDAL</a>. Sadly, this script is completely unusable on OS X, because of incompatibilities in the command line tools that are used by the shell script. After spending about five minutes trying to tune it to work with the tools that Apple ships, I completely gave up and looked into other approaches.</p>
<p>Luckily, I quickly found out that GDAL ships with Python bindings. I installed GDAL from <a href="https://trac.osgeo.org/gdal/wiki/DownloadSource">source</a> using the <a href="https://trac.osgeo.org/gdal/wiki/BuildingOnUnix">instructions</a> from their wiki. To make sure it built with Python modules I ran <span style="font-family: monospace;">./configure &#8211;with-python</span>. After that <span style="font-family: monospace;">make</span> and <span style="font-family: monospace;">sudo make install</span> ran without a hitch.</p>
<p>I then set about re-implementing <span style="font-family: monospace;">gdal_slice.sh</span> in Python. Once I got a handle on how to use the Python bindings it was fairly easy to write. You can find the script <a href="http://homepage.sethjust.com/files/gdal_slice.txt">here</a>.</p>
<p>Using <span style="font-family: monospace;">gdal_slice.py</span> is very similar to using <span style="font-family: monospace;">gdal_slice.sh</span>:</p>
<pre>$ ./gdal_slice.py -h
Usage: gdal_slice.py [options] FILENAME

Options:
  -h, --help            show this help message and exit
  -o OVERLAP, --overlap=OVERLAP
                        percentage tiles will overlap. should be at least 20%
  -a, --add             write map info to map_koord.txt in current working
                        directory
  -m, --map             use *_map folders for output; use if input image is
                        UTM. Default behavior
  -t, --topo            use *_top folders for output; use if input image is
                        not UTM
  -v, --verbose         enable debugging output</pre>
<p>The biggest difference is that <span style="font-family: monospace;">gdal_slice.py</span> does not perform any format conversion; it will write maps to TIFF files. Although the file size is decidedly larger than that of PNGs, my version of GDAL is unable to create PNGs, and disk space is (fairly) cheap. If I&#8217;m motivated I&#8217;ll get around to adding a format option, at least to allow conversion to PNG.</p>
<p>Running <span style="font-family: monospace;">gdal_slice.py filename.ext</span> will create a folder named filename_map (or _top, if you use the -t flag) that contains a set of 1280&#215;1024 TIFF files, as well as a map_koord_draft.txt file. Moving this folder into GPSDrive&#8217;s map directory, and merging filename_[map|top]/map_koord_draft.txt with map_koord.txt will make the new maps available to GPSDrive. Note that filename.ext must be in the current working directory, as the script handles filenames/paths somewhat naivëly.</p>
<p>So far I&#8217;ve only tested this with GeoTIFFs from LibreMaps, but it seems to work great, with the caveat that LibreMaps&#8217; files include the borders of map, and my script doesn&#8217;t do any cropping, so you see borders if you&#8217;re near the edges of quads. This doesn&#8217;t bother me terribly much, but you may need to turn mapsets on and off in the Map Control dialog to see the appropriate images.</p>
<p><b>Update 7-14-11:</b> I&#8217;ve modified the script with an additional flag (-a) that will automatically merge the generated map_koords file with map_koord.txt in the current working directory. This means that if you run the script from inside .gpsdrive/maps with the -a flag the generated tiles will be automatically added to GPSDrive&#8217;s database. The script avoids creating duplicate entries in map_koord.txt, as well as alphabetizing all entries.</p>
<p>I&#8217;ve modified the documentation here, as well as the links, to reference the latest version.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2011/07/13/map-tiles-with-python-gdal/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Sign your email like Randall Waterhouse</title>
		<link>http://sethjust.com/2011/07/11/sign-your-email-like-randall-waterhouse/</link>
		<comments>http://sethjust.com/2011/07/11/sign-your-email-like-randall-waterhouse/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 00:57:04 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[GPS]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[email]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[Mail.app]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[thunderbird]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=307</guid>
		<description><![CDATA[Neal Stephenson&#8217;s novel Cryptonomicon is a fascinating book; I&#8217;m re-reading it for the umpteenth time and loving it every bit as much as when I first read it in 7th grade. One thing that caught my attention on this reading (especially in light of my recent GPS post) is the way that one of the main [...]]]></description>
			<content:encoded><![CDATA[<p>Neal Stephenson&#8217;s novel <em><a href="http://www.amazon.com/gp/product/B004R96U4A/ref=as_li_ss_tl?ie=UTF8&#038;tag=dabbler-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=B004R96U4A">Cryptonomicon</a><img src="http://www.assoc-amazon.com/e/ir?t=&#038;l=as2&#038;o=1&#038;a=B004R96U4A&#038;camp=217145&#038;creative=399373" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></em> is a fascinating book; I&#8217;m re-reading it for the umpteenth time and loving it every bit as much as when I first read it in 7th grade. One thing that caught my attention on this reading (especially in light of my recent <a href="http://sethjust.com/2011/07/10/gpsd-under-os-x-revisited/">GPS post</a>) is the way that one of the main characters, Randall Lawrence Waterhouse, signs his email:</p>
<blockquote><p>Randall Lawrence Waterhouse</p>
<p>Current meatspace coordinates, hot from the GPS receiver card in my laptop:</p>
<p>8 degrees, 52.33 minutes N latitude 117 degrees, 42.75 minutes E longitude</p>
<p>Nearest geographical feature: Palawan, the Philippines</p></blockquote>
<p>(from <a href="http://www.euskalnet.net/larraorma/crypto/slide51.html">http://www.euskalnet.net/larraorma/crypto/slide51.html</a>)</p>
<p>Using GPSd&#8217;s python bindings, and the free, CC-licenced RESTful API from <a href="http://geonames.org">GeoNames.org</a>, I wrote a short python script that will produce a similar output:</p>
<blockquote><p>Seth Just</p>
<p>Current meatspace coordinates, hot from the GPS receiver card in my laptop:</p>
<p>41.751 N latitude, 111.807 W longitude</p>
<p>Nearest geographical feature: Logan Canyon, Utah, United States</p></blockquote>
<p>The core of the script is two functions &#8212; one that gets GPS coordinates from GPSd, and a second that does a lookup for geographical features (mountains, canyons, islands, etc) on <a href="http://geonames.org">GeoNames.org</a>. For more details, see the final section of this post.</p>
<h2>Using gpssig.py</h2>
<p>To use gpssig.py, first download it from <a href="http://homepage.sethjust.com/files/gpssig.txt">here</a>, and change the extension to .py.</p>
<p>In order to be useful as an email signature, I had to produce output in HTML format, which allows almost any mail client to use the output as a signature. I&#8217;ve gone through the effort of setting it up with the two clients I use the most: Mozilla Thunderbird and Apple&#8217;s Mail.app. Of these two, Thunderbird has documented support for HTML signatures (see <a href="http://opensourcearticles.com/thunderbird_15/english/part_03">here</a> for details), and should be the most like other email clients. This script has been tested on Mac OS X Snow Leopard, and should run on Linux without problems (although I haven&#8217;t tried it on versions of python higher than 2.6.1).</p>
<p>Using gpssig.py takes two steps: first configure your Mail client to take a signature from a file, and then configure gpssig.py to run automatically to update that file.</p>
<p>Thunderbird is relatively easy to set up with an HTML signature. Because it only supports one signature per account, you simply need to tell it to draw that signature from a file. Under each account&#8217;s settings there&#8217;s an option to &#8220;Attach the signature from a file&#8221; (see <a href="http://opensourcearticles.com/thunderbird_15/english/part_03">here</a> for a detailed howto). I chose to use ~/.signature.html, but you can use any file you&#8217;d like to, just remember the location you choose.</p>
<p>Mail.app is a more finicky beast &#8212; although the script supports it, I recommend using Thunderbird, mainly because Mail only refreshes its signatures from files on every launch, whereas Thunderbird will read the file each time you compose a message. However, if that limitation is acceptable (if you don&#8217;t move very much, perhaps), it will work just fine. The main issue with Mail.app is that it doesn&#8217;t store signatures in an HTML file, but instead in the proprietary <a href="http://en.wikipedia.org/wiki/Webarchive">.webarchive</a> format. These files can be found in ~/Library/Mail/Signatures. To use gpssig.py with Mail, you need to follow steps 4&amp;5 from <a href="http://bytes.com/topic/macosx/insights/825900-setup-html-signatures-apple-mail-mail-app">http://bytes.com/topic/macosx/insights/825900-setup-html-signatures-apple-mail-mail-app</a>. First create a new signature (under Preferences-&gt;Signatures). This will create a new file in the signature folder &#8212; take note of the file name. You will use this file name to configure gpssig.py.</p>
<p>gpssig.py is configured with command line options:</p>
<pre>Usage: gpssig.py [options] NAME FILENAME
Note: NAME should be enclosed in quotes, unless it contains no spaces.

Options:
  -h, --help   show this help message and exit
  --no-gps     don't attempt to get coordinates from GPS
  -a, --amail  generate files for Apple's Mail.app (.webarchive)
  --lat=LAT    default latitude, if GPS is unavailable
  --lon=LON    default longitude, if GPS is unavailable</pre>
<p>The simplest way to use it is manually &#8212; simply running it whenever you want to update your signature. For example, I use <span style="font-family:monospace;">./gpssig.py &#8211;lat=41.752 &#8211;lon=-111.793 &#8216;Seth Just&#8217; ~/.signature.html</span> to generate a signature for Thunderbird.</p>
<p>If you want to make this automatic, you&#8217;ll need to have the script run automatically. On OS X you&#8217;ll need to use launchd, while on linux you need cron. Both of these make setting up repeating actions very easy &#8212; see <a href="https://arunxjacob.wordpress.com/2008/08/28/mac-launchd-and-launchctl-the-osx-alternative-to-cron/">here</a> for information on launchd and <a href="https://arunxjacob.wordpress.com/2008/08/28/mac-launchd-and-launchctl-the-osx-alternative-to-cron/">here</a> for information on cron.</p>
<p>Sadly I haven&#8217;t yet figured out a way to run the script whenever you write an email &#8212; if I figure it out, I&#8217;ll be sure to post something.</p>
<h2>The Script</h2>
<p>gpssig.py is built around three major functions &#8212; one gets coordinates from GPSd, one gets geographical feature information from</p>
<p>The function I use to get GPS information is a bit hacky &#8212; there doesn&#8217;t seem to be terribly much documentation on GPSd&#8217;s python bindings, so I made do with what I could figure out:</p>

<div class="wp_codebox"><table><tr id="p3074"><td class="code" id="p307code4"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> NoGPSError <span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_lat_lon<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># GPSd Python bindings</span>
  <span style="color: #ff7700;font-weight:bold;">import</span> gps
&nbsp;
  <span style="color: #808080; font-style: italic;"># Create GPS object</span>
  session = gps.<span style="color: black;">gps</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Set GPS object as an iterator over reports from GPSd</span>
  session.<span style="color: black;">stream</span><span style="color: black;">&#40;</span>gps.<span style="color: black;">WATCH_ENABLE</span>|gps.<span style="color: black;">WATCH_NEWSTYLE</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Loop until we get a report with lat and lon. The limit of 5 loops should be more than enough -- my gps never takes more than three when it has a lock</span>
  i = <span style="color: #ff4500;">0</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      session.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
      lat, lon = session.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'lat'</span><span style="color: black;">&#93;</span>, session.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'lon'</span><span style="color: black;">&#93;</span>
      <span style="color: #ff7700;font-weight:bold;">break</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>i <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> NoGPSError 
      i += <span style="color: #ff4500;">1</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> lat, lon</pre></td></tr></table></div>

<p>The other function is more straightforward &#8212; it makes two http requests to <a href="http://geonames.org">GeoNames.org</a> to get the nearest geographical feature and locality. It then combines those responses in a way that should provide a good response almost anywhere in the world (provided that GeoNames has good, local data). I&#8217;ve tested it with a variety of coordinates (provided by <a href="http://www.getlatlon.com/">http://www.getlatlon.com/</a>), and the responses seem to be pretty good.</p>

<div class="wp_codebox"><table><tr id="p3075"><td class="code" id="p307code5"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> get_geo_string<span style="color: black;">&#40;</span>lat, lon<span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># Modules for REST/XML request from GeoNames</span>
  <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib</span>
  <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">xml</span>.<span style="color: black;">etree</span> <span style="color: #ff7700;font-weight:bold;">import</span> ElementTree <span style="color: #ff7700;font-weight:bold;">as</span> ET
&nbsp;
  <span style="color: #808080; font-style: italic;"># Request the name of the nearest geographical feature</span>
  placename = ET.<span style="color: black;">parse</span><span style="color: black;">&#40;</span>
      <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://api.geonames.org/findNearby?lat=&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lat<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;lng=&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lon<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;featureClass=T&amp;username=sethjust&quot;</span><span style="color: black;">&#41;</span>
      <span style="color: black;">&#41;</span>
  subdiv = ET.<span style="color: black;">parse</span><span style="color: black;">&#40;</span>
      <span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;http://api.geonames.org/countrySubdivision?lat=&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lat<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;lng=&quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>lon<span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;&amp;username=sethjust&quot;</span><span style="color: black;">&#41;</span>
      <span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Format and return place, region, country</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    country = subdiv.<span style="color: black;">getiterator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;countryName&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      country = placename.<span style="color: black;">getiterator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;countryName&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span>
    <span style="color: #ff7700;font-weight:bold;">except</span>:
      country = <span style="color: #483d8b;">''</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    region = subdiv.<span style="color: black;">getiterator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;adminName1&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    region = <span style="color: #483d8b;">''</span>
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    name = placename.<span style="color: black;">getiterator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;name&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">text</span>
  <span style="color: #ff7700;font-weight:bold;">except</span>:
    name = <span style="color: #483d8b;">''</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">if</span> country:
    <span style="color: #ff7700;font-weight:bold;">if</span> region:
      <span style="color: #ff7700;font-weight:bold;">if</span> name:
        result = <span style="color: #483d8b;">&quot;%s, %s, %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>name, region, country<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        result = <span style="color: #483d8b;">&quot;%s, %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>region, country<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      <span style="color: #ff7700;font-weight:bold;">if</span> name:
        result = <span style="color: #483d8b;">&quot;%s, %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>name, country<span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        result = country
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    result = <span style="color: #483d8b;">&quot;unknown&quot;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">return</span> result</pre></td></tr></table></div>

<p>Finally, the main method of the program parses arguments, determines what coordinates to use, gets the geographical feature string, and then generates the proper HTML and copies it into the correct place.</p>

<div class="wp_codebox"><table><tr id="p3076"><td class="code" id="p307code6"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>__name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span><span style="color: black;">&#41;</span>:
  <span style="color: #808080; font-style: italic;"># Parse command line options</span>
  <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser
  <span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Usage: %prog [options] NAME FILENAME<span style="color: #000099; font-weight: bold;">\n</span>Note: NAME should be enclosed in quotes, unless it contains no spaces.&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--no-gps&quot;</span>, action=<span style="color: #483d8b;">&quot;store_true&quot;</span>, dest=<span style="color: #483d8b;">&quot;nogps&quot;</span>, default=<span style="color: #008000;">False</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;don't attempt to get coordinates from GPS&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-a&quot;</span>, <span style="color: #483d8b;">&quot;--amail&quot;</span>, action=<span style="color: #483d8b;">&quot;store_true&quot;</span>, dest=<span style="color: #483d8b;">&quot;amail&quot;</span>, default=<span style="color: #008000;">False</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;generate files for Apple's Mail.app (.webarchive)&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--lat&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;float&quot;</span>, dest=<span style="color: #483d8b;">&quot;lat&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;default latitude, if GPS is unavailable&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;--lon&quot;</span>, action=<span style="color: #483d8b;">&quot;store&quot;</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;float&quot;</span>, dest=<span style="color: #483d8b;">&quot;lon&quot;</span>, <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;default longitude, if GPS is unavailable&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
  options, args = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">try</span>: <span style="color: #ff7700;font-weight:bold;">assert</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AssertionError</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You must provide NAME FILENAME<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">print_help</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    exit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">try</span>: <span style="color: #ff7700;font-weight:bold;">assert</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>options.<span style="color: black;">lat</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">lon</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> | <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>options.<span style="color: black;">lat</span>==<span style="color: #008000;">None</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">lon</span>==<span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AssertionError</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;You must provide both lat and lon arguments<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">print_help</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    exit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #808080; font-style: italic;"># Modules for creating signature file</span>
  <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">tempfile</span>, <span style="color: #dc143c;">os</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">try</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">nogps</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">raise</span> NoGPSError
    lat, lon = get_lat_lon<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Got %f, %f from gps&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>lat, lon<span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">except</span> NoGPSError:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> options.<span style="color: black;">nogps</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Failed to get coordinates from GPS&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
      <span style="color: #ff7700;font-weight:bold;">assert</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>options.<span style="color: black;">lat</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">lon</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">AssertionError</span>:
      <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;No default coordinates provided; exiting&quot;</span>
      exit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    lat, lon = options.<span style="color: black;">lat</span>, options.<span style="color: black;">lon</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Using %f, %f as coordinates&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>lat, lon<span style="color: black;">&#41;</span>
&nbsp;
  geostring = get_geo_string<span style="color: black;">&#40;</span>lat, lon<span style="color: black;">&#41;</span>
&nbsp;
  lac, loc = <span style="color: #483d8b;">'N'</span>, <span style="color: #483d8b;">'E'</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> lat <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:
    lat <span style="color: #66cc66;">*</span>= -<span style="color: #ff4500;">1</span>
    lac = <span style="color: #483d8b;">'S'</span>
  <span style="color: #ff7700;font-weight:bold;">if</span> lon <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">0</span>:
    lon <span style="color: #66cc66;">*</span>= -<span style="color: #ff4500;">1</span>
    loc = <span style="color: #483d8b;">'W'</span>
&nbsp;
  <span style="color: #008000;">file</span> = <span style="color: #dc143c;">tempfile</span>.<span style="color: black;">NamedTemporaryFile</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;html&gt;&lt;body&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;p&gt;&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;p&gt;&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Current meatspace coordinates, hot from the GPS receiver card in my laptop:&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;p&gt;&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%.3f %s latitude, %.3f %s longitude&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>lat, lac, lon, loc<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span>&lt;p&gt;&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Nearest geographical feature: &quot;</span> + geostring<span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
  <span style="color: #008000;">file</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&lt;/html&gt;&lt;/body&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #008000;">file</span>.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span>options.<span style="color: black;">amail</span><span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;textutil -convert webarchive -output &quot;</span> + args<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot; &quot;</span> + <span style="color: #008000;">file</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">else</span>:
    <span style="color: #dc143c;">os</span>.<span style="color: black;">system</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;cp &quot;</span> + <span style="color: #008000;">file</span>.<span style="color: black;">name</span> + <span style="color: #483d8b;">&quot; &quot;</span> + args<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2011/07/11/sign-your-email-like-randall-waterhouse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Web Laundry, Smart Cards, Python and OS X Smorgasbord</title>
		<link>http://sethjust.com/2011/06/14/laundry-smart-cards/</link>
		<comments>http://sethjust.com/2011/06/14/laundry-smart-cards/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 00:22:11 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[laundry]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[smart card]]></category>
		<category><![CDATA[stored value]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=285</guid>
		<description><![CDATA[Disclaimer: This article is for informational purposes, the author shall not be held liable for any actions taken on the basis of the information presented. I do not condone fraud, theft, or other malfeasance. In September 2010, Hans Viksler posted an article about the insecurity of the stored-value smart card system used by Web Laundry (now [...]]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;">Disclaimer: This article is for informational purposes, the author shall not be held liable for any actions taken on the basis of the information presented. I do not condone fraud, theft, or other malfeasance.</span></p>
<p>In September 2010, <a rel="author" href="http://www.blogger.com/profile/09056609859376859009">Hans Viksler</a> posted an <a href="http://ihackiam.blogspot.com/2010/09/web-laundry-insecurity.html">article</a> about the insecurity of the stored-value smart card system used by <a href="http://weblaundry.com/">Web Laundry</a> (now <a href="http://www.washlaundry.com/">Wash Laundry</a>). I&#8217;ll let that article stand on its own &#8212; it gives a good overview of the bone-headed system that the engineers there had in place. In short, they used a secure memory card made by Atmel (specifically, from the CryptoMemory line), the AT88SC0404C (datasheet <a href="http://www.atmel.com/dyn/resources/prod_documents/doc5211.pdf">here</a>), but neglected to change the default master password. This means that all you need to read or write the stored data is a ISO-7816-3 compliant smart card reader.</p>
<p>This is where my story starts &#8212; this summer I&#8217;m living in a dorm that uses a Web Laundry stored value system. That was enough of a push for me to buy a smart card reader, seeing as I&#8217;d always wanted to play with them, and this looked like a good opportunity. I picked up a smart card reader from Amazon.com, an <a id="static_txt_preview" href="http://www.amazon.com/gp/product/B0012K5P02/ref=as_li_ss_tl?ie=UTF8&amp;tag=dabbler-20&amp;linkCode=as2&amp;camp=217153&amp;creative=399349&amp;creativeASIN=B0012K5P02">SCM Microsystems SCR3310</a>. It was both cheap and, according to the manufacturer&#8217;s <a href="https://www.scmmicro.com/security/view_product_en.php?PID=4">product page</a>, offers full ISO 7816, CCID and PC/SC compatibility (i.e. all of the relevant standards), as well as drivers for OS X, which is my primary operating system for the time being.</p>
<p>Doing some research, it turns out that Apple ships a (modified) version of the open-source <a href="http://pcsclite.alioth.debian.org/">PCSC Lite</a> framework for smart card integration. This means that all I would need to get up and running was a driver for my device and a way to interface it. Once the reader arrived I installed the drivers, and ran pcsctest (from PCSC Lite) on the command line. Sadly, all I got was an error message:</p>
<pre>Testing SCardEstablishContext    : Service not available.</pre>
<p>Googling told me that the appropriate services are supposed to start whenever a smart card reader with appropriate drivers is plugged in, which made this error all the more confusing. Luckily, I found <a href="http://support.scmmicro.com/forum/viewtopic.php?f=10&amp;t=552">this thread</a> on SCM&#8217;s forums &#8212; it seems that the driver they have posted doesn&#8217;t include the USB identifiers for the version of the reader I had, but a helpful poster left instructions to modify a plist file in the driver (mirrored <a href="http://homepage.sethjust.com/files/SCM3310_OSX.html">here</a> for archival purposes). With that, I was up and running!</p>
<p>pcsctest spat out useful output this time around:</p>
<pre>$ pcsctest

MUSCLE PC/SC Lite Test Program

Testing SCardEstablishContext    : Command successful.
Testing SCardGetStatusChange
Please insert a working reader   : Command successful.
Testing SCardListReaders         : Command successful.
Reader 01: SCM SCR 3310 00 00
Enter the reader number          : 1
Waiting for card insertion
                                 : Command successful.
Testing SCardConnect             : Command successful.
Testing SCardStatus              : Command successful.
Current Reader Name              : SCM SCR 3310 00 00
Current Reader State             : 0x34
Current Reader Protocol          : 0x0
Current Reader ATR Size          : 8 (0x8)
Current Reader ATR Value         : 3B B2 11 00 10 80 00 04
Testing SCardDisconnect          : Command successful.
Testing SCardReleaseContext      : Command successful.</pre>
<p>That left me with the issue of interacting with the smart card somehow. In case you hadn&#8217;t guessed from the post title, that solution was Python. Specifically, the pyscard bindings for PCSC Lite, from <a href="http://pyscard.sourceforge.net/">pyscard.sourceforge.net</a>. There&#8217;s a nice array of examples, and so I wrote up a python script for interacting with <em>this particular </em>CryptoMemory card, based on the command set from the datasheet and the examples. It&#8217;s not as polished or feature-rich as the software Hans posted, but if you&#8217;re running OS X or Linux, it should be helpful. At the moment this script is extremely rough &#8212; it gets the job done, but in an ugly and unsafe way. I&#8217;m posting it <a href="http://homepage.sethjust.com/files/CryptoMemory.txt">here</a>, but you <strong>SHOULD NOT USE IT IF YOU DON&#8217;T KNOW WHAT YOU&#8217;RE DOING</strong>. If you aren&#8217;t careful, it will mess up your card. Hopefully I&#8217;ll have time to wrap it all in a library that will abstract away the messiness and provide nice features like exception handling; if I do, you&#8217;ll see it here.</p>
<p>Using my script I was able to verify Hans&#8217; findings &#8212; although the card is configured to require cryptographic authentication to access the user zones (where the goodies can be found), the password is left at the default value, which makes overwriting the access registers and conducting a replay attack (as demonstrated by the video in his post) a walk in the park. Following his example, I won&#8217;t post any information on how the cards store their values, but it&#8217;s surprising that this security hole still isn&#8217;t fixed, seeing as it would cost nothing to overwrite the master password when they first configure the cards.</p>
<p>Hopefully this post will give some of you who&#8217;ve wanted to work with smart cards a push in that direction &#8212; although there are a few hurdles along the way, it&#8217;s an interesting area to work on. I&#8217;m hoping to move on from poking at other people&#8217;s cards and start using my new hardware for authentication and encryption.</p>
<p>Happy hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2011/06/14/laundry-smart-cards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>RGB LED Sun with Rainbowduino</title>
		<link>http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/</link>
		<comments>http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 17:50:58 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[display]]></category>
		<category><![CDATA[diy]]></category>
		<category><![CDATA[LED]]></category>
		<category><![CDATA[project]]></category>
		<category><![CDATA[Rainbowduino]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=251</guid>
		<description><![CDATA[So my current project (outside of massive amounts of school work) is building a large, bright, RGB LED display for my college&#8217;s weekend-long end-of-year party. I&#8217;ve got three of these strips from DealExtreme, which have 30 RGB LEDs in separable strips of three LEDs. That makes for a total of 30 RGB dots, which is [...]]]></description>
			<content:encoded><![CDATA[<p>So my current project (outside of massive amounts of school work) is building a large, bright, RGB LED display for my college&#8217;s weekend-long end-of-year party. I&#8217;ve got three of <a href="http://www.dealextreme.com/p/rgb-multicolored-1-meter-30-led-6w-light-strip-dc-12v-14965">these</a> strips from <a href="http://www.dealextreme.com/">DealExtreme</a>, which have 30 RGB LEDs in separable strips of three LEDs. That makes for a total of 30 RGB dots, which is presents no problem for a <a href="http://garden.seeedstudio.com/index.php?title=Rainbowduino_LED_driver_platform_-_Atmega_328">Rainbowduino</a>.</p>
<p>My goal for this post is to record my process in putting this project together. I&#8217;m nowhere near done at the moment, but I&#8217;ll keep updating it as I go.</p>
<h2>Planning</h2>
<p>The plan for this project came together slowly &#8211; I knew that I wanted to make a large RGB LED display, and I had already bought one light strip to play with a year or so ago. I&#8217;m not sure how the Rainbowduino came across my radar, but it seemed perfect, because it could drive up to 64 RGB LEDs simultaneously, so could support up to 6 strips. However, the funding for the project chose to use 3, and I felt that that was more than enough to get the effect I was shooting for.</p>
<p>Because the strips run at 12 volts, and are each rated for 6 amps, I decided to use a spare PC power supply to provide power because it supplies a well-regulated voltage and plenty of current.</p>
<p>Finally, the Rainbowduino can operate in one of three ways: standalone mode, serial mode, or I2C mode. The second and third allow you to stream patterns to the Rainbowduino from another microcontroller or computer. However, I want this sun to stand on its own, so I&#8217;m planning on having it run in standalone mode. This means I&#8217;ll need a way of programming it with whatever patterns I want it to use. The Rainbowduino has a 6-pin ICSP plug, which will work perfectly with the AVR programmer I already have, Lady Ada&#8217;s <a href="http://www.ladyada.net/make/usbtinyisp/">UsbTinyISP</a>.</p>
<h2>Preparation</h2>
<p>To prepare for putting the whole display together, I wanted to test my LEDs, power supply and control board. I used an RGB driver based on a <a href="http://www.pjrc.com/teensy/">Teensy 2.0</a>, a few FETs and a wall-wart (which I know works well with these strips) as a simple tester for the LEDs, and found no problems. The power supply also checked out, I took apart a single 4-pin plug to get the +12V and GND wires and checked the voltage with a multimeter. The reading was good and steady, but attaching it to the Rainbowduino caused one of the power-filtering capacitors to explode (which was unexpected, given the board is rated for 12V), but removing the damaged cap fixed the problem, and soon I had it powered up and lighting LEDs. I also connected the UsbTinyISP and used <a href="http://savannah.nongnu.org/projects/avrdude">Avrdude</a> to ensure I could program the Rainbowduino. (As an aside, the Avrdude part name for the ATMEGA328P is &#8216;m328p&#8217;, which took me way too long to figure out).</p>
<p>At this point I felt comfortable that I had everything I needed to put the display together (except for some sort of mounting board. However, the end of the weekend was drawing to a close, so I had to wait to get more done.</p>
<h2>Steeling Myself</h2>
<p><a href="http://sethjust.com/wp-content/uploads/2011/04/layout.png"><img class="alignright size-medium wp-image-264" title="layout" src="http://sethjust.com/wp-content/uploads/2011/04/layout-300x220.png" alt="" width="300" height="220" /></a>Before I prepared to cut my 3 strips into 30 smaller ones, and soldered 4 wires to each one, I decided to get a picture of what the final wiring would look like. This was a job for Cadsoft&#8217;s <a href="http://www.cadsoftusa.com/">Eagle</a>, the defacto-standard for making schematics and PCB layouts. Even though I&#8217;m planning on laying out the display by hand, with wires, I felt that having a reference for wiring the matrix would be worthwhile. I built a <a href="http://sethjust.com/wp-content/uploads/2011/04/schematic.png">schematic</a> to show the control layout for the LEDs, as a matrix, which is what the Rainbowduino is designed to drive. I then built a <a href="http://sethjust.com/wp-content/uploads/2011/04/layout.png">layout</a> based on the schematic that corresponds (roughly, and not to scale) to how I&#8217;m planning to arrange the LED strips, although the footprints of all the parts are completely arbitrary. However, the pin layout of the headers on either side match the layout of those on the Rainbowduino, which will make it helpful when it comes time to connect all 120 wires.</p>
<h2>Wiring</h2>
<p><strong>Update, May 2:</strong> I&#8217;ve now finished wiring the matrix: I attached all of the strips to a piece of foamcore board using their adhesive backing and soldered all of the connections. Everything worked on the first try, and I flashed the code (rainbowduino_v1_0_4) from <a href="http://code.google.com/p/rainbowduino/downloads/list">here</a>. The demo patterns looked amazing, but sadly, shortly after powering it up (before Icould even get a video), something stopped working. Not only were the patterns not being displayed, but I couldn&#8217;t get my programmer to connect to the rainbowduino! Luckily, I emailed SeeedStudio, and one of their very helpful employe has promised me that a replacement is already on the way. Hopefully it arrives before the fifth, when I was hoping to set up the display on campus. Until it arrives, and I can replace the defective board on the display, enjoy the only two photos I took while it was working (before I flashed the new firmware onto it).</p>
<div class="aligncenter"><a href="http://sethjust.com/wp-content/uploads/2011/04/0501111603-00.jpg"><img class="size-medium wp-image-277" title="Rainbowduino Sun displaying test pattern" src="http://sethjust.com/wp-content/uploads/2011/04/0501111603-00-300x225.jpg" alt="" width="300" height="225" /></a><a href="http://sethjust.com/wp-content/uploads/2011/04/0501111603-01.jpg"><img class="size-medium wp-image-278" title="Rainbowduino with USBTinyISP attached" src="http://sethjust.com/wp-content/uploads/2011/04/0501111603-01-300x225.jpg" alt="" width="300" height="225" /></a></div>
<p><span style="font-size: 20px; font-weight: bold;">Final words</span></p>
<p>Despite the setback with my Rainbowduino, I went ahead with getting the display ready for this weekend, where (hopefully) it will be sitting in a central place, entertaining people with colorful patterns. To do this, I used a large zip-tie to strap the ATX power supply to the back of the piece of foamcore board, and used some more zip ties to position all of the wires out of sight, as well as to securely fasten the two wires running 12V to the display. All in all it looks good, and I&#8217;m excited to load some exciting patters onto it once my new control board arrives.</p>
<p>Also, this post is still in progress &#8212; check back soon for updates! Apologies for the lack of photos, they should be coming soon.
<a href='http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/schematic/' title='schematic'><img width="150" height="150" src="http://sethjust.com/wp-content/uploads/2011/04/schematic-150x150.png" class="attachment-thumbnail" alt="schematic" title="schematic" /></a>
<a href='http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/layout/' title='layout'><img width="150" height="150" src="http://sethjust.com/wp-content/uploads/2011/04/layout-150x150.png" class="attachment-thumbnail" alt="layout" title="layout" /></a>
<a href='http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/0501111603-00/' title='Rainbowduino Sun displaying test pattern'><img width="150" height="150" src="http://sethjust.com/wp-content/uploads/2011/04/0501111603-00-150x150.jpg" class="attachment-thumbnail" alt="Rainbowduino Sun displaying test pattern" title="Rainbowduino Sun displaying test pattern" /></a>
<a href='http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/0501111603-01/' title='Rainbowduino with USBTinyISP attached'><img width="150" height="150" src="http://sethjust.com/wp-content/uploads/2011/04/0501111603-01-150x150.jpg" class="attachment-thumbnail" alt="Rainbowduino with USBTinyISP attached" title="Rainbowduino with USBTinyISP attached" /></a>
</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2011/04/26/rgb-led-sun-with-rainbowduino/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>New Keyboard — Reviewed!</title>
		<link>http://sethjust.com/2010/11/05/new-keyboard-reviewed/</link>
		<comments>http://sethjust.com/2010/11/05/new-keyboard-reviewed/#comments</comments>
		<pubDate>Fri, 05 Nov 2010 20:29:04 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[cheap]]></category>
		<category><![CDATA[computer]]></category>
		<category><![CDATA[keyboard]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=187</guid>
		<description><![CDATA[Have you ever had one of those days when you bought a keyboard for no good reason? Well I did the other day, and ended up with an &#8220;Irocks Chocolate Key Style Slim Keyboard for Pc with Elegant Body&#8221;. It felt like a steal &#8212; I got it $12.99 with free two day shipping. It [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sethjust.com/wp-content/uploads/2010/11/1105101317-01.jpg"><img class="size-medium wp-image-190 alignright" title="Keyboard Comparison" src="http://sethjust.com/wp-content/uploads/2010/11/1105101317-01-300x225.jpg" alt="" width="300" height="225" /></a>Have you ever had one of those days when you bought a keyboard for no good reason? Well I did the other day, and ended up with an <a href="http://www.amazon.com/gp/product/B002NR2CJ0?ie=UTF8&amp;tag=dabbler-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=B002NR2CJ0">&#8220;Irocks Chocolate Key Style Slim Keyboard for Pc with Elegant Body&#8221;</a>. It felt like a steal &#8212; I got it $12.99 with free two day shipping.</p>
<p>It is now replacing an older, wireless, but more compact keyboard from the same company, which, despite serving me well, had been getting flaky in age.</p>
<p>Hit the jump for another picture and a review/comparison of the two keyboards</p>
<p><span id="more-187"></span>As I said, this keyboard is replacing my old wireless one, and offers several advantages over it. The first thing I noticed after taking it out of the box was the size: the new one is considerably bigger, allowing it room for a full, proper numpad and, arrow keys, and Home/End/PgUp/PgDn/Ins/Del keys that need a Fn key on the old keyboard.</p>
<p style="text-align: center;">The second major difference is the new key design, which they call &#8220;chocolate key&#8221;. It&#8217;s the same design as Macbook keyboards, with flat keys shaped like rounded rectangles. The combination of a new key design and layout mean that the new keyboard is a lot roomier &#8212; my hands don&#8217;t feel cramped and can move freely.<a href="http://sethjust.com/wp-content/uploads/2010/11/1105101317-03.jpg"><img class="size-medium wp-image-191 aligncenter" title="1105101317-03" src="http://sethjust.com/wp-content/uploads/2010/11/1105101317-03-300x225.jpg" alt="" width="300" height="225" /></a></p>
<p>The last thing I noticed quickly after getting the new keyboard set up is the feel of the keys. While the old, wireless keyboard has the mush feel of most laptop keyboards, the new key design has a much crisper feel and a more &#8220;clicky&#8221; feel, even though it still uses scissor-style key switches. While this makes typing slightly louder, it feels much nicer, and makes typing more enjoyable and responsive.</p>
<p>In all, the new keyboard is a welcome update. However, it won&#8217;t completely replace the old one. The biggest reason is the wireless keyboard&#8217;s range: because it will work anywhere within hearing range of the computer, I use it as a remote control for my music player.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2010/11/05/new-keyboard-reviewed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Sexy Alphabet Image Generator</title>
		<link>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/</link>
		<comments>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/#comments</comments>
		<pubDate>Mon, 31 May 2010 21:58:57 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[alphabet]]></category>
		<category><![CDATA[erotic]]></category>
		<category><![CDATA[font]]></category>
		<category><![CDATA[generator]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[sex]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=178</guid>
		<description><![CDATA[When my girlfriend came across this erotic alphabet, the first thing she wanted was to be able  to spell out various things with it. Add a pinch of python and php and the result is the Erotic Alphabet Image Generator. The source code of the script used to generate the images is available here.]]></description>
			<content:encoded><![CDATA[<p>When my girlfriend came across <a href="http://sexoteric.com/blog/index.php/__show_article/_a000018-001091.htm">this</a> erotic alphabet, the first thing she wanted was to be able  to spell out various things with it. Add a pinch of python and php and the result is the <a href="http://homepage.sethjust.com/erotic_alphabet/">Erotic Alphabet Image Generator</a>.</p>
<p>The source code of the script used to generate the images is available <a href="http://homepage.sethjust.com/files/erotic_alphabet_image_generator.txt">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2010/05/31/erotic-alphabet-image-generator/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>M-Set update</title>
		<link>http://sethjust.com/2010/05/27/m-set-update/</link>
		<comments>http://sethjust.com/2010/05/27/m-set-update/#comments</comments>
		<pubDate>Thu, 27 May 2010 17:16:29 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[mandelbrot]]></category>
		<category><![CDATA[program]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=167</guid>
		<description><![CDATA[So, I know that I&#8217;ve been harping on this a bit, but I&#8217;ve re-written my M-Set renderer in C, so it&#8217;s faster, and I&#8217;ve added a few more command line flags to make it easier to use. Get the code here.]]></description>
			<content:encoded><![CDATA[<p><a href="http://homepage.sethjust.com/files/mandel_py.png"></a><a href="http://sethjust.com/wp-content/uploads/2010/05/mandel_py.png"><img class="alignright size-medium wp-image-222" title="mandel_py" src="http://sethjust.com/wp-content/uploads/2010/05/mandel_py-300x229.png" alt="" width="300" height="229" /></a>So, I know that I&#8217;ve been harping on this a bit, but I&#8217;ve re-written my M-Set renderer in C, so it&#8217;s faster, and I&#8217;ve added a few more command line flags to make it easier to use.</p>
<p>Get the code <a href="http://sethjust.com/wp-content/uploads/2010/05/mandel.c">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2010/05/27/m-set-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>More Mandelbrot</title>
		<link>http://sethjust.com/2009/12/19/more-mandelbrot/</link>
		<comments>http://sethjust.com/2009/12/19/more-mandelbrot/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 00:27:19 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.com/?p=122</guid>
		<description><![CDATA[I just recently revisited the M-Set code from my Perl Snippets post. The code I had was pretty ugly, so I decided to rewrite it in Python. The result is not only a lot cleaner and easier to understand, but it&#8217;s also a lot faster: $ time python mandel.py &#62; \dev\null real 0m0.051s user 0m0.036s [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://sethjust.com/wp-content/uploads/2010/05/mandel_py.png"><img class="alignright size-medium wp-image-222" title="mandel_py" src="http://sethjust.com/wp-content/uploads/2010/05/mandel_py-300x229.png" alt="" width="300" height="229" /></a>I just recently revisited the M-Set code from my <a href="http://sethjust.com/2009/03/24/perl-snippets/">Perl Snippets</a> post. The code I had was pretty ugly, so I decided to rewrite it in Python. The result is not only a lot cleaner and easier to understand, but it&#8217;s also a lot faster:</p>
<pre>$ time python mandel.py &gt; \dev\null
real	0m0.051s
user	0m0.036s
sys	0m0.010s
$ time perl mandel.pl &gt; \dev\null
real	0m3.518s
user	0m3.463s
sys	0m0.029s</pre>
<p>You can find the code <a href="http://homepage.sethjust.com/files/mandel.txt">here</a>.</p>
<p>This script works well for zooms, as long as you stay below a few thousand iterations. The following picture was generated with x=-1.1887204, y=-0.3032472, width=0.01 and 150 iterations.</p>
<p style="text-align: center;"><a href="http://sethjust.com/wp-content/uploads/2009/12/mandel_py_zoom.png"><img class="size-medium wp-image-224 aligncenter" title="mandel_py_zoom" src="http://sethjust.com/wp-content/uploads/2009/12/mandel_py_zoom-300x229.png" alt="" width="300" height="229" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2009/12/19/more-mandelbrot/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Perl Snippets</title>
		<link>http://sethjust.com/2009/03/24/perl-snippets/</link>
		<comments>http://sethjust.com/2009/03/24/perl-snippets/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 02:39:05 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[japh]]></category>
		<category><![CDATA[m-set]]></category>
		<category><![CDATA[mandlebrot]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[obfuscation]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=65</guid>
		<description><![CDATA[I&#8217;ve been getting into a mood lately that makes me fiddle around with fun Perl stuff, but sadly school&#8217;s picking up to the point that writing anything up isn&#8217;t going to happen. However, I have a couple short scripts that I&#8217;m just dying to share. Just Another Perl Hacker I figured that it was about [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been getting into a mood lately that makes me fiddle around with fun Perl stuff, but sadly school&#8217;s picking up to the point that writing anything up isn&#8217;t going to happen. However, I have a couple short scripts that I&#8217;m just dying to share.</p>
<h2>Just Another Perl Hacker</h2>
<p>I figured that it was about time in my hacking career (read: I was bored enough) that I should make a <a href="http://en.wikipedia.org/wiki/Just_another_Perl_hacker">japh script</a>. After a couple attempts I came up with this:</p>

<div class="wp_codebox"><table><tr id="p658"><td class="code" id="p65code8"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #009999;">&lt;DATA&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
 	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/map.html"><span style="color: #000066;">map</span></a><span style="color: #009900;">&#123;</span><a href="http://perldoc.perl.org/functions/ord.html"><span style="color: #000066;">ord</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">33</span><span style="color: #009900;">&#125;</span><a href="http://perldoc.perl.org/functions/split.html"><span style="color: #000066;">split</span></a> <span style="color: #ff0000;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #0000ff;">$__</span><span style="color: #339933;">++;</span>
		<span style="color: #0000ff;">$_</span><span style="color: #339933;">||</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/chr.html"><span style="color: #000066;">chr</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$__</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">19</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;&amp;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$__</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">__END__</span>
                   <span style="color: #339933;">/|</span>                        <span style="color: #339933;">|</span>\
      <span style="color: #339933;">!</span>            <span style="color: #339933;">;</span> <span style="color: #339933;">:</span>                        <span style="color: #339933;">:</span> <span style="color: #339933;">:</span>
                  <span style="color: #339933;">|</span> Y<span style="color: #339933;">,</span>                      <span style="color: #339933;">,</span>P <span style="color: #339933;">|</span>
     <span style="color: #339933;">!</span>             <span style="color: #339933;">|</span>  Yb<span style="color: #339933;">.</span>        __        <span style="color: #339933;">,</span>dP  <span style="color: #339933;">|</span>
                  l\  YMMb<span style="color: #339933;">,</span>_ _<span style="color: #339933;">,/</span>  \<span style="color: #339933;">,</span>_ _<span style="color: #339933;">,</span>dMMP  <span style="color: #339933;">/</span>f
  <span style="color: #339933;">!</span>                 j<span style="color: #339933;">;</span>  <span style="color: #ff0000;">`YMMP'  `</span><span style="color: #339933;">--</span><span style="color: #ff0000;">'  `YMMP'</span>  <span style="color: #339933;">;</span>j
                   <span style="color: #339933;">:</span> \   YP<span style="color: #ff0000;">`-._    _.-'YP   / ;
  !            !      <span style="color: #000099; font-weight: bold;">\ </span>`</span>\<span style="color: #339933;">,</span>  _<span style="color: #339933;">,</span><span style="color: #0000ff;">\_</span>    _<span style="color: #339933;">/,</span>_  <span style="color: #339933;">,/</span><span style="color: #ff0000;">' /
                     `,_,   \`o&gt;  &lt;o'</span><span style="color: #339933;">/</span>   <span style="color: #339933;">,</span>_<span style="color: #339933;">,!</span><span style="color: #ff0000;">'
                         `\            /'</span>
                           <span style="color: #339933;">|</span>  _    _  <span style="color: #339933;">|</span>
      <span style="color: #339933;">!</span>                     <span style="color: #339933;">/</span> 88b  d88 \
                           \ <span style="color: #ff0000;">`8P  Y8' /`</span><span style="color: #ff0000;">`-.
             !               `</span>\<span style="color: #339933;">,</span>    <span style="color: #339933;">,/</span>      \
                             _<span style="color: #ff0000;">`----'        <span style="color: #000099; font-weight: bold;">\</span>
                  !        _.'/ '          `</span><span style="color: #339933;">,</span> \
                        <span style="color: #339933;">.</span><span style="color: #ff0000;">'_,'</span>_<span style="color: #339933;">...</span>_          \ <span style="color: #339933;">;</span>
       <span style="color: #339933;">!</span>               <span style="color: #339933;">.</span><span style="color: #ff0000;">',/.dMMMMMMb. |      /  \
                    .'</span><span style="color: #339933;">./.</span>MMMMMMMMMMM <span style="color: #339933;">;</span> <span style="color: #339933;">!</span>    <span style="color: #339933;">/</span>    <span style="color: #339933;">;</span>
                   <span style="color: #009966; font-style: italic;">/ /</span><span style="color: #339933;">.</span>MMMMMMMMMMMMM<span style="color: #339933;">/</span>     <span style="color: #339933;">/</span>     <span style="color: #339933;">;</span>
                  <span style="color: #339933;">|</span> <span style="color: #339933;">;</span>dMMMMMMMMMMM<span style="color: #339933;">!</span>P<span style="color: #339933;">/</span><span style="color: #ff0000;">'   ,/'</span>   <span style="color: #339933;">!</span>    <span style="color: #339933;">;</span>
                  <span style="color: #339933;">|/</span>dMMMMMMMMMP<span style="color: #ff0000;">'     .'</span>          <span style="color: #339933;">|</span>
   <span style="color: #339933;">!</span>               <span style="color: #339933;">;</span>dMMMMMMMMM<span style="color: #339933;">|</span>     _<span style="color: #339933;">/</span>            <span style="color: #339933;">|</span>
                 <span style="color: #339933;">;</span>dMMMMMMMMMMb_<span style="color: #339933;">|</span>_<span style="color: #339933;">!|</span> <span style="color: #339933;">/;</span>            <span style="color: #339933;">;</span>\<span style="color: #339933;">,</span>
                 dMMMMMMMMMMMM<span style="color: #ff0000;">`M`</span>M<span style="color: #ff0000;">`;.---..      '   `</span>\<span style="color: #339933;">,</span>
                <span style="color: #339933;">:</span>M<span style="color: #339933;">!</span>MMMMMMMMMMMMMMMP<span style="color: #339933;">/</span><span style="color: #ff0000;">'     `\            \
               /|MMMMMMMMMMMMMMP/          \    _!_      `.
  !            / |MMMMMMMMMMMMMM|            |.-'</span>  <span style="color: #ff0000;">`-._    <span style="color: #000099; font-weight: bold;">\ </span>        .'<span style="color: #000099; font-weight: bold;">\</span>
             ;!  ;MMMMMMMMMMMMMM|            :         `</span><span style="color: #339933;">-.</span>_ <span style="color: #ff0000;">`-.     /  _<span style="color: #000099; font-weight: bold;">\</span>
             |   ;!MMMMMMMMMMMMM;            |             `</span><span style="color: #339933;">-.</span> <span style="color: #ff0000;">`-.._.'/
              <span style="color: #000099; font-weight: bold;">\ </span>  `</span><span style="color: #0000ff;">\YMMMMM</span><span style="color: #339933;">!</span>MMMP<span style="color: #339933;">/</span><span style="color: #ff0000;">'\           |                `~~----'</span>
               <span style="color: #ff0000;">`-._, `</span><span style="color: #ff0000;">`YMMMP'_.-'<span style="color: #000099; font-weight: bold;">\ </span>        ! ;_
                 |      /         `</span><span style="color: #339933;">-.,</span>_<span style="color: #339933;">./</span>     <span style="color: #cc66cc;">7</span>
                _<span style="color: #339933;">/</span>    <span style="color: #ff0000;">`<span style="color: #000099; font-weight: bold;">\,</span>    !        /      _/
             ,-' ,  ,   /         _.'      /
            (_(_(__(__.'       !,-'        /
                              (__(__(_(_/'</span></pre></td></tr></table></div>

<p><a href="http://people.reed.edu/~justs/japh.pl">Download</a>.</p>
<p>I&#8217;ll let you go ahead and figure it out on your own. It&#8217;s not super-hard, but it&#8217;s fun.</p>
<h2>Mandelbrot</h2>
<p>In another fit of boredom I decided that it was finally time to create a mandelbrot set renderer. I originally tried to make one of these in basic, long before I had the math to do so. I was proud that I got the real axis to render, and figured it was time to complexify it. To keep things simple I decided to make it render an ASCII-art version of the set that would fit in a terminal window. The output looks like this:</p>
<pre>                        ...............................:::::oo@@@@o::::..........
                      ...............................::::::O@@@@@@@@o:::::.......
                     .............................::::::::oO@@@@@@@@o::::::::....
                   ...........................::::OOO8ooO@O88@@@@@@8@O8o::::Oo:..
                  .......................:::::::::o8@@@@@@@@@@@@@@@@@@@@OO@@@@::.
                 ...................:::::::::::::oOO@@@@@@@@@@@@@@@@@@@@@@@@@o:::
                ................:::::::::::::::o@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo:::
               ..............::::@oo::oOoo:::ooo@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8o:
               ...........:::::::oO@@@O@@8@OooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O::
              ..........:::::::::oO@@@@@@@@@@88@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@::
              ........:::::::8ooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o::
              .:::::::::::::oO@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o:::
              @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo::::
              .:::::::::::::oO@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o:::
              ........:::::::8ooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@o::
              ..........:::::::::oO@@@@@@@@@@88@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@::
               ...........:::::::oO@@@O@@8@OooO8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@O::
               ..............::::@oo::oOoo:::ooo@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8o:
                ................:::::::::::::::o@@@@@@@@@@@@@@@@@@@@@@@@@@@@Oo:::
                 ...................:::::::::::::oOO@@@@@@@@@@@@@@@@@@@@@@@@@o:::
                  .......................:::::::::o8@@@@@@@@@@@@@@@@@@@@OO@@@@::.
                   ...........................::::OOO8ooO@O88@@@@@@8@O8o::::Oo:..
                     .............................::::::::oO@@@@@@@@o::::::::....
                      ...............................::::::O@@@@@@@@o:::::.......
                        ...............................:::::oo@@@@o::::..........</pre>
<p>The code&#8217;s far from polished and not what I like to publish, but it&#8217;s a fun thing to look at and offers you some neat abilities to poke things around and fix some pesky problems that just need clear thinking applied to them. It&#8217;s available <a href="http://people.reed.edu/~justs/mandel.pl">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2009/03/24/perl-snippets/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Genetic Algorithms in Perl</title>
		<link>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/</link>
		<comments>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/#comments</comments>
		<pubDate>Thu, 11 Dec 2008 09:20:19 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[evolution]]></category>
		<category><![CDATA[genetic]]></category>
		<category><![CDATA[genetic algorithm]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=38</guid>
		<description><![CDATA[Inspired by recent genetic algorithms floating around, I decided to try my hand at implementing one in perl. I&#8217;d thought for a long time that it would be quite difficult, but really it&#8217;s quite easy. My biggest hangup was dealing with data structures, but once I did that, it turns out that all you really [...]]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://hackaday.com/2008/12/08/genetic-programming/">recent</a> <a href="http://rogeralsing.com/2008/12/07/genetic-programming-evolution-of-mona-lisa/">genetic</a> algorithms floating around, I decided to try my hand at implementing one in perl. I&#8217;d thought for a long time that it would be quite difficult, but really it&#8217;s quite easy. My biggest hangup was dealing with data structures, but once I did that, it turns out that all you really need is a few functions:</p>
<ul>
<li>A fitness function, that determines which individuals are most fit to reproduce</li>
<li>A mutate function, that will add random chance into each generation</li>
<li>A breed function that allows the best individuals to reproduce.</li>
</ul>
<p>I ended up implementing a very simple algorithm, but it&#8217;s fairly fast and very generic – it can be easily adapted to just about any task. Sadly, I have no fascinating application just yet, but if I stumble across one, I&#8217;ll be sure to post about it.</p>
<p>After the jump, I&#8217;ll put up some of the code I used and a link to the script, all for your viewing pleasure.<br />
<span id="more-38"></span></p>
<p>The heart of the algorithm is the mutate and breed functions:</p>

<div class="wp_codebox"><table><tr id="p3810"><td class="code" id="p38code10"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">sub</span> mutate<span style="color: #009900;">&#123;</span>     <span style="color: #666666; font-style: italic;"># Mutates each allele of an individual by a random, weighted amount,</span>
                <span style="color: #666666; font-style: italic;">#    controlled by certain parameters. It re-initializes if the allele</span>
                <span style="color: #666666; font-style: italic;">#    is zero, and takes abs()</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ind</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$ind</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$init_string</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$_</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/rand.html"><span style="color: #000066;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$mut_weight</span><span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000ff;">$mut_offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000ff;">$_</span> <span style="color: #b1b100;">unless</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> breed<span style="color: #009900;">&#123;</span>      <span style="color: #666666; font-style: italic;"># Breeds each individual with the top 20 percent of the population randomly</span>
                <span style="color: #666666; font-style: italic;">#    by averaging each allele of the individual with the corresponding allele</span>
                <span style="color: #666666; font-style: italic;">#    of a random individual in the top quintile. Note that this means that</span>
                <span style="color: #666666; font-style: italic;">#    each individual breeds with, at most (and ideally), as many individuals</span>
                <span style="color: #666666; font-style: italic;">#    as it has alleles, which breaks down the parent / child model slightly.</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$indvs</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/shift.html"><span style="color: #000066;">shift</span></a><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@list</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$minimize</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@list</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #009900;">&#123;</span>fitness<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #009900;">&#41;</span>  fitness<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@$indvs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;"># Sort asc. vs. desc.</span>
    <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$minimize</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@list</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/sort.html"><span style="color: #000066;">sort</span></a> <span style="color: #009900;">&#123;</span>fitness<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$a</span><span style="color: #009900;">&#41;</span>  fitness<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$b</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span> <span style="color: #0000ff;">@$indvs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #0000ff;">@list</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">@list</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">..</span><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/int.html"><span style="color: #000066;">int</span></a><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/scalar.html"><span style="color: #000066;">scalar</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$indvs</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>              <span style="color: #666666; font-style: italic;"># Iterate through individuals</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$ind_ref</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@$ind_ref</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>        <span style="color: #666666; font-style: italic;"># Iterate through alleles</span>
            <span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@list</span><span style="color: #009900;">&#91;</span><a href="http://perldoc.perl.org/functions/int.html"><span style="color: #000066;">int</span></a><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/rand.html"><span style="color: #000066;">rand</span></a><span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;"># Average given allele with</span>
                                                                          <span style="color: #666666; font-style: italic;">#    a random, fit,</span>
                                                                          <span style="color: #666666; font-style: italic;">#    corresponding allele</span>
            <span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/rand.html"><span style="color: #000066;">rand</span></a><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$reset_prob</span><span style="color: #339933;">?</span><a href="http://perldoc.perl.org/functions/eval.html"><span style="color: #000066;">eval</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$init_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">$_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;"># Re-init the allele some</span>
                                                                          <span style="color: #666666; font-style: italic;">#    percent of the time</span>
            <span style="color: #0000ff;">$i</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <a href="http://perldoc.perl.org/functions/return.html"><span style="color: #000066;">return</span></a> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Between the comments and the code, it should be pretty clear what&#8217;s going on in here, although some of the data structures are kinda hard to get from this section. As I explain in the <a href="http://people.reed.edu/~justs/genes.pl">code</a>:</p>
<blockquote><p>A randomized population is created. An array holds references to the specified number of &#8220;individuals&#8221;. Each individual is an array of alleles (scalar values) that are chosen by evaluating a specified string. These are currently positive real numbers.</p></blockquote>
<p>These references (or references to the population array) are passed as arguments to the functions each iteration.</p>
<p>If you&#8217;re interested, be sure to check out the <a href="http://people.reed.edu/~justs/genes.pl">code</a> and please give me some feedback!</p>
<p>UPDATE: Ivan in the comments made the recommendation of using an anonymous function to intialize individuals. I made that change he recommended and updated the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2008/12/11/genetic-algorithms-in-perl/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Fast and Simple Stock Quotes Using Perl</title>
		<link>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/</link>
		<comments>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 06:18:05 +0000</pubDate>
		<dc:creator>sethjust</dc:creator>
				<category><![CDATA[Perl]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[exchange]]></category>
		<category><![CDATA[finance]]></category>
		<category><![CDATA[program]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[stocks]]></category>
		<category><![CDATA[ticker]]></category>

		<guid isPermaLink="false">http://sethjust.wordpress.com/?p=21</guid>
		<description><![CDATA[One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that makes perl so powerful and fascinating is the huge number of modules that are available online, especially through the CPAN repository. Today I stumbled upon one called Finance::Quote, which does one thing, very simply: it retrieves stock (or mutual fund) quotes. You feed it a ticker symbol and it gives back a hash with all sorts of information, but most importantly, the price. I&#8217;m going to show how to use this to create a command line tool that will grab an up-to-the minute stock quote for any ticker symbol you give it.</p>
<p><span id="more-21"></span><br />
To start, installing Finance::Quote is a breeze. As long as you have CPAN installed, just type at the command line:</p>
<pre>$perl -MCPAN -e shell
&gt;install Finance::Quote</pre>
<p>From there it&#8217;s easy to get start, it&#8217;s just a matter of importing the module and creating and instance. This means the beginning of our script will look like:</p>

<div class="wp_codebox"><table><tr id="p2114"><td class="code" id="p21code14"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The instance that we created has one main method: fetch(). It takes a list of arguments, with the first being a the exchange to look for quotes on, and the remaining being a list or array of ticker symbols. The Finance::Quote documentation provides the following list of valid exchanges:</p>
<pre>australia           Australan Stock Exchange
dwsfunds            Deutsche Bank Gruppe funds
fidelity            Fidelity Investments
tiaacref            TIAA-CREF
troweprice          T. Rowe Price
europe              European Markets
canada              Canadian Markets
usa                 USA Markets
nyse                New York Stock Exchange
nasdaq              NASDAQ
uk_unit_trusts      UK Unit Trusts
vanguard            Vanguard Investments
vwd                 Vereinigte Wirtschaftsdienste GmbH</pre>
<p>For the purposes of this script, I&#8217;m going to stick to &#8220;usa&#8221; because it covers both NYSE and NASDAQ, and those are the stocks I&#8217;m interested in.<br />
Fetch() returns a two-dimensional hash of results. The first index (or dimension) is the ticker symbol of the stock and the second is the label for the specific piece of information. For now, the labels that we&#8217;re interested are the name of the company (&#8216;name&#8217;) and the price (&#8216;price&#8217;). A full listing of labels is available in the Finance::Quote documentation.<br />
Using this information we can now build a simple script to fetch a stock quote:</p>

<div class="wp_codebox"><table><tr id="p2115"><td class="code" id="p21code15"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># retrieve stock quote</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>fetch<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'usa'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'GOOG'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># print price</span>
<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'GOOG'</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'price'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This script, although it does what it&#8217;s supposed to, isn&#8217;t very pretty. By adding a little code to read ticker symbols from the command line we can check on multiple stocks at the same time, as well as remove the need to hard-code the ticker symbols. We can also make it produce much prettier results by adding more information about the company and add some basic error checking, which yields the following script:</p>

<div class="wp_codebox"><table><tr id="p2116"><td class="code" id="p21code16"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl -w</span>
<span style="color: #000000; font-weight: bold;">use</span> strict<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># import module</span>
<span style="color: #000000; font-weight: bold;">use</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># create object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$q</span> <span style="color: #339933;">=</span> Finance<span style="color: #339933;">::</span><span style="color: #006600;">Quote</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>new<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#print usage information</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Usage: $0 ticker1, ticker2, ... , tickerN<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<a href="http://perldoc.perl.org/functions/exit.html"><span style="color: #000066;">exit</span></a><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># get stock symbols from the command line and</span>
<span style="color: #666666; font-style: italic;"># format them correctly (uppercase)</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #0000ff;">$_</span> <span style="color: #339933;">=</span> <a href="http://perldoc.perl.org/functions/uc.html"><span style="color: #000066;">uc</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># retrieve stock quote</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">%data</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$q</span><span style="color: #339933;">-</span><span style="color: #0000ff;">&amp;gt</span><span style="color: #339933;">;</span>fetch<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'usa'</span><span style="color: #339933;">,</span> <span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># print result for each stock</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">@</span><span style="color: #000000; font-weight: bold;">ARGV</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'success'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		<span style="color: #666666; font-style: italic;"># if getting the quote succeeded</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'name'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;"># build a report</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$price</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$data</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">$_</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">'price'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$message</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">''</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$name</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">' ('</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">$_</span> <span style="color: #339933;">.</span> <span style="color: #ff0000;">')'</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">' '</span> x<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">25</span> <span style="color: #339933;">-</span> <a href="http://perldoc.perl.org/functions/length.html"><span style="color: #000066;">length</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #0000ff;">$message</span> <span style="color: #339933;">.=</span> <span style="color: #ff0000;">&quot;$$price<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #0000ff;">$message</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <a href="http://perldoc.perl.org/functions/print.html"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Failed to retrieve quote for $_: $data{$_, 'errormsg'}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This is very simple, but it does its job well:</p>
<pre>$ quote goog msft aapl dell java
GOOGLE (GOOG)            $352.32
MICROSOFT CP (MSFT)      $22.32
APPLE INC (AAPL)         $98.23
DELL INC (DELL)          $11.99
SUN MICROSYSTEMS (JAVA)  $4.54</pre>
<p><a href="http://people.reed.edu/~justs/quote.pl">Download the script here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://sethjust.com/2008/10/23/fast-and-simple-stock-quotes-using-perl/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

