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

<channel>
	<title>Ctrl-C &#187; Programming</title>
	<atom:link href="http://ctrl-c.us/blog/archives/category/computers/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://ctrl-c.us/blog</link>
	<description>a friendly interruption</description>
	<lastBuildDate>Fri, 28 May 2010 11:49:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Adding &#8216;addremove&#8217; to Git</title>
		<link>http://ctrl-c.us/blog/archives/849</link>
		<comments>http://ctrl-c.us/blog/archives/849#comments</comments>
		<pubDate>Fri, 09 Apr 2010 21:57:38 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[addremove]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[hg]]></category>
		<category><![CDATA[mercurial]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=849</guid>
		<description><![CDATA[I&#8217;m a happy git user, but mercurial has a nifty command with no git equivalent: hg addremove. This command removes deleted files from the repository and also adds new files in the current directory. For me, at least, it&#8217;s a useful command that I&#8217;d like to have. Of course, git is nothing if not flexible, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a happy git user, but mercurial has a nifty command with no git equivalent: <code>hg addremove</code>. This command removes deleted files from the repository and also adds new files in the current directory. For me, at least, it&#8217;s a useful command that I&#8217;d like to have. Of course, git is nothing if not flexible, so let&#8217;s add this command! I&#8217;m using a suggestion for a script that I found <a href="http://ionrails.com/2009/08/07/git-commands-adding-and-committing-cheatsheet/">here</a>. Add the following lines to your <code>$HOME/.gitconfig</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>alias<span style="">&#93;</span></span>
    <span style="color: #000099;">addremove</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #000066; font-weight:bold;"><span style="color: #660066;"> !git add . &amp;&amp; git ls-files --deleted | xargs --no-run-if-empty git rm</span></span></pre></div></div>

<p>Tada&mdash;<code>git addremove</code> puts new files in the staging area and also removes deleted files. Now you don&#8217;t have to pay attention to your hipster friends who try to tell you how great mercurial is.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/849/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using notify-send to Slack Off Efficiently</title>
		<link>http://ctrl-c.us/blog/archives/746</link>
		<comments>http://ctrl-c.us/blog/archives/746#comments</comments>
		<pubDate>Thu, 17 Dec 2009 12:25:16 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[libnotify]]></category>
		<category><![CDATA[notify-send]]></category>
		<category><![CDATA[procrastination]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=746</guid>
		<description><![CDATA[I&#8217;ve been working on a big project in MPI and the execution times on my code are annoyingly long (on the order of minutes). I like to keep my work in a separate workspace from my browser to minimize distractions, but then when I set my code running and head over to visit reddit or [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a big project in MPI and the execution times on my code are annoyingly long (on the order of minutes). I like to keep my work in a separate workspace from my browser to minimize distractions, but then when I set my code running and head over to visit <a href="http://reddit.com">reddit</a> or something I often don&#8217;t notice when my code finishes. Today it occurred to me that this would be a good use for libnotify. There is a command-line utility called <code>notify-send</code> (you should have it if you have a recent version of Ubuntu; not sure about other distros) which gives a simple interface to libnotify, so I whipped up a tiny script to call it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #007800;">EVAL_STRING</span>=<span style="color: #ff0000;">&quot;notify-send -u normal -t 5000 -i info <span style="color: #000099; font-weight: bold;">\&quot;</span>Task completed<span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #000099; font-weight: bold;">\&quot;</span>$@<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span>
<span style="color: #7a0874; font-weight: bold;">eval</span> $<span style="color: #000000; font-weight: bold;">@</span>; <span style="color: #7a0874; font-weight: bold;">eval</span> <span style="color: #007800;">$EVAL_STRING</span></pre></div></div>

<p>I put it in a folder which is in my path, and now I just do<br />
<pre>$ notify ./takes_forever</pre><br />
and I know as soon as it&#8217;s done. OK, <a href="http://xkcd.com/303/">back to work</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/746/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First Thoughts on Scala</title>
		<link>http://ctrl-c.us/blog/archives/589</link>
		<comments>http://ctrl-c.us/blog/archives/589#comments</comments>
		<pubDate>Tue, 14 Jul 2009 19:31:36 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/blog/?p=589</guid>
		<description><![CDATA[Over the past week or so I&#8217;ve been looking into the Scala programming language. If you aren&#8217;t familiar with it, Scala is one of a group of new-ish languages including Groovy, Clojure, and Nice (as well as new implementations of preexisting languages like Jython, JRuby, and Rhino) that run on the JVM (either interpreted or [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past week or so I&#8217;ve been looking into the <a href="http://www.scala-lang.org/">Scala</a> programming language. If you aren&#8217;t familiar with it, Scala is one of a group of new-ish languages including <a href="http://groovy.codehaus.org/">Groovy</a>, <a href="http://clojure.org/">Clojure</a>, and <a href="http://nice.sourceforge.net/">Nice</a> (as well as new implementations of preexisting languages like <a href="http://www.jython.org/">Jython</a>, <a href="http://jruby.codehaus.org/">JRuby</a>, and <a href="http://www.mozilla.org/rhino/">Rhino</a>) that run on the JVM (either interpreted or compiled to Java bytecode). Over perhaps the past decade the JVM has been increasingly seen as an attractive target platform for language development for several reasons:</p>
<ul>
<li> Implementation in Java instead of C</li>
<li> Features like garbage collection, portability, and a huge standard library come for free</li>
<li> Languages benefit from advances and optimizations in the JVM</li>
</ul>
<p>(This is part of a trend pointed out as Prediction #4 in <a href="http://steve.yegge.googlepages.com/ten-predictions">a list of 10 predictions</a> about software Steve Yegge made about 5 years ago.) In fact, targeting a virtual machine has almost become the <em>only</em> way to implement new, fancy languages with reasonable performance, portability, and implementation time.</p>
<p><span id="more-589"></span></p>
<p>Scala has an interesting mix of features. Some are taken from functional programming such as higher-order and anonymous functions, closures, partial function applications, and algebraic data types. It has pure OO (everything is an object, even functions) with mixins to compose functionality. Probably the most distinguishing feature of Scala is that it is statically typed (with some pretty nice type inference). For all this, it has a fairly small syntax (significantly smaller than Java, for instance).</p>
<p>As implied by the name, Scala tries to be as scalable as possible. Here are a couple of ways this is done (and I&#8217;m sure there are many others that I haven&#8217;t seen). Scala programs can be interpreted like scripts: if <code>hello.scala</code> contains</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;">println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;OHAI&quot;</span><span style="color: #F78811;">&#41;</span></pre></div></div>

<p>then this can be run by</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ scala hello.scala
OHAI</pre></div></div>

<p>They can also be compiled to class files. If <code>hello.scala</code> instead contains</p>

<div class="wp_syntax"><div class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> HelloApp <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>arg<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;OHAI&quot;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></div></div>

<p>then we can do</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ scalac hello.scala
$ scala HelloApp
OHAI</pre></div></div>

<p>What really makes Scala scalable, though, is how its small but powerful syntax allows creation of new constructs that almost look like new syntax itself. In <a href="http://ftp.heanet.ie/mirrors/fosdem-video/2009/maintracks/scala.xvid.avi">a great talk</a> by Martin Odersky, the creator of Scala, he presents three nice examples of features that can be concisely added to Scala due to its extensibility: the <code>using</code> keyword, <code>break</code> and <code>continue</code>, and Erlang-style actors (the latter two are included in the Scala library).</p>
<p>I&#8217;m just getting started with Scala, but I&#8217;ve been very happy with its thorough documentation and the large collection of documents available to help learn the language. For instance, I have found Martin Odersky&#8217;s <a href="http://www.scala-lang.org/docu/files/ScalaByExample.pdf">Scala By Example</a> to be a great introduction. It&#8217;s nice to see a language that makes it so easy for beginners to figure out what&#8217;s going on (I&#8217;m looking at you, <a href="http://cobra-language.com/">Cobra</a>). I&#8217;m also having a good time reading <a href="http://www.amazon.com/Programming-Scala-Comprehensive-Step-step/dp/0981531601/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1247598681&amp;sr=8-1">&#8220;Programming in Scala&#8221;</a>, the comprehensive and extremely well-written language guide. Not only does it explain Scala very well, it is easy to read and includes tidbits such as this (<code>var</code>s are mutable variables; <code>val</code>s are immutable):</p>
<blockquote><p>If you’re coming from an imperative background, such as Java, C++, or C#, you may think of <code>var</code> as a regular variable and <code>val</code> as a special kind of variable. On the other hand, if you’re coming from a functional background, such as Haskell, OCaml, or Erlang, you might think of <code>val</code> as a regular variable and <code>var</code> as akin to blasphemy.</p></blockquote>
<p>Scala has some traction in the software community. The most well-known example, of course, is Twitter; last year they switched their message queuing from Rails to Scala, in their continuing quest to turn millions of dollars of venture capital into the hippest code possible. (Also there was a bit of a case of their backend exploding when things started heating up; it seems a bit telling that they switched to from Ruby to a language that has the first two syllables of &#8217;scalable&#8217; in its name.) There is also a Rails-like web framework for Scala, <a href="http://liftweb.net/">Lift</a>.</p>
<p>Given my great initial impression, I can only hope that Scala becomes more widespread. I might post more thoughts on Scala quirks and features (as well as my improved vim indent file) later on.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/589/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://ftp.heanet.ie/mirrors/fosdem-video/2009/maintracks/scala.xvid.avi" length="185676474" type="video/x-msvideo" />
		</item>
		<item>
		<title>Interfacing C and Python 3 using SWIG</title>
		<link>http://ctrl-c.us/blog/archives/99</link>
		<comments>http://ctrl-c.us/blog/archives/99#comments</comments>
		<pubDate>Mon, 27 Apr 2009 09:06:17 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python 3]]></category>
		<category><![CDATA[swig]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/?p=99</guid>
		<description><![CDATA[I like Python, and I use it for almost all of my personal projects and whenever I can get away with it in school. Sometimes, however, (rarely, these days) you&#8217;re looking for raw speed. Python, alas, is not the fastest kid on the block, and at these times you might have to turn to a [...]]]></description>
			<content:encoded><![CDATA[<p>I like Python, and I use it for almost all of my personal projects and whenever I can get away with it in school. Sometimes, however, (rarely, these days) you&#8217;re looking for raw speed. Python, alas, is not the fastest kid on the block, and at these times you might have to turn to a different language to give you the necessary speed. (Another option you might consider first is <a title="Psyco" href="http://psyco.sourceforge.net/" target="_blank">Psyco</a>.) In these situations it is often the case that only particular operations or functions really have to be fast. Even if you want to write the whole piece of software in C, you might like to use Python to write the front end. There are a lot of different ways to do this, but my favorite is <a title="Simplified Wrapper and Interface Generator" href="http://www.swig.org/" target="_blank">SWIG</a>, because it&#8217;s easy and I&#8217;m lazy.</p>
<p><span id="more-99"></span>SWIG is a great tool that supports a host of high-level languages; I&#8217;ve only ever used it with Python. At the other end, you get your choice of C and C++—crappy languages aren&#8217;t really my thing, so this tutorial will be about C. I&#8217;m going to use Python 3 because all the cool kids live on the bleeding edge. This requires SWIG 1.3.37 or better; if you&#8217;re on Ubuntu like me, you&#8217;re going to have to go grab it from the website and compile it yourself (I&#8217;m using 1.3.39). Also, remember that you need the header files for python installed (python3-dev on Ubuntu). If you&#8217;re too lazy to copy-paste, you can download the files <a href='http://ctrl-c.us/wordpress/wp-content/uploads/2009/04/swig_intro.tar.gz'>here</a>.</p>
<p>We&#8217;ll start out with the obligatory hello world program just to test that SWIG is set up correctly. First make a C function that prints the text and name it <code>hello.c</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">void</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Hello, World!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we&#8217;ll get SWIG to generate a wrapper for us so that we can call it in Python. Make a file called hello.i and put this in it (my code highlighting isn&#8217;t feeling very confident about SWIG files, sorry):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>module hello
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">void</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span></pre></div></div>

<p>This file is the interface file, and it tells SWIG what functions and data structures are going to be shared between C and Python. Now we&#8217;ll have SWIG process the interface file to make a wrapper for a python module:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ swig <span style="color: #660033;">-python</span> hello.i</pre></div></div>

<p>This should generate two files: <code>hello.py</code> and <code>hello_wrap.c</code>. Compile the C files:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-c</span> <span style="color: #660033;">-fPIC</span> hello_wrap.c hello.c -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>include<span style="color: #000000; font-weight: bold;">/</span>python3.0 -I<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>python3.0</pre></div></div>

<p>Make sure that the direcotories are correct. Unfortunately, gcc generates a warning when I compile, which is bothersome as I&#8217;m a <code>-Werror</code> kind of guy. The <code>-fPIC</code> flag is necessary, because we&#8217;re making a shared library the python module is going to use:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ld</span> <span style="color: #660033;">-shared</span> hello.o hello_wrap.o <span style="color: #660033;">-o</span> _hello.so</pre></div></div>

<p>If that works, you are ready to fire up Python and try it. At a Python3 prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> hello <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> hello<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
Hello, World<span style="color: #66cc66;">!</span></pre></div></div>

<p>Easy, right? Now I&#8217;ll show you some of the features that I most frequently use. First, we&#8217;ll pass some data through by wrapping a C function to multiply integers. Make a file called <code>swig_intro.h</code>, and put in the following declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include&lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include&lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then in <code>swig_intro.c</code> we can write</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;swig_intro.h&quot;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> a <span style="color: #339933;">*</span> b<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, the interface file, <code>swig_intro.i</code>, will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>module swig_intro
<span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">#include &quot;swig_intro.h&quot;</span>
<span style="color: #339933;">%</span><span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> mult<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> a<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To make things easier, let&#8217;s write a makefile for this little project:</p>

<div class="wp_syntax"><div class="code"><pre class="make" style="font-family:monospace;">TARGET     <span style="color: #004400;">=</span> swig_intro
CC         <span style="color: #004400;">=</span> gcc
CFLAGS     <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>g <span style="color: #004400;">-</span>fPIC <span style="color: #004400;">-</span>Wall <span style="color: #004400;">-</span>Wextra
HEADERS    <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>h
SRCS       <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>c
INCLUDES   <span style="color: #004400;">=</span>
INTERFACE  <span style="color: #004400;">=</span> swig_intro<span style="color: #004400;">.</span>i
SWIG       <span style="color: #004400;">=</span> swig
&nbsp;
LDSHARED   <span style="color: #004400;">=</span> ld
CSHARED    <span style="color: #004400;">=</span> <span style="color: #004400;">-</span>shared
&nbsp;
OBJS       <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>SRCS<span style="color: #004400;">:.</span>c<span style="color: #004400;">=.</span>o<span style="color: #004400;">&#41;</span>
&nbsp;
IWRAP      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>INTERFACE<span style="color: #004400;">:.</span>i<span style="color: #004400;">=</span>_wrap<span style="color: #004400;">.</span>i<span style="color: #004400;">&#41;</span>
ISRCS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>IWRAP<span style="color: #004400;">:.</span>i<span style="color: #004400;">=.</span>c<span style="color: #004400;">&#41;</span>
IOBJS      <span style="color: #004400;">=</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span>IWRAP<span style="color: #004400;">:.</span>i<span style="color: #004400;">=.</span>o<span style="color: #004400;">&#41;</span>
&nbsp;
PYTHON_INCLUDE<span style="color: #004400;">=</span> <span style="color: #004400;">-</span>I<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span><span style="color: #666622; font-weight: bold;">include</span><span style="color: #004400;">/</span>python3<span style="color: #004400;">.</span>0 <span style="color: #004400;">-</span>I<span style="color: #004400;">/</span>usr<span style="color: #004400;">/</span>lib<span style="color: #004400;">/</span>python3<span style="color: #004400;">.</span>0
PYTHON_SO     <span style="color: #004400;">=</span> <span style="color: #004400;">.</span>so
&nbsp;
all<span style="color: #004400;">:</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SWIG</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>python <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INTERFACE</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CC</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>c <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CCSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CFLAGS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">ISRCS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">HEADERS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">SRCS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">INCLUDES</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PYTHON_INCLUDE</span><span style="color: #004400;">&#41;</span>
	<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">LDSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">CSHARED</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">OBJS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">IOBJS</span><span style="color: #004400;">&#41;</span> <span style="color: #004400;">-</span>o _<span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">TARGET</span><span style="color: #004400;">&#41;</span><span style="color: #004400;">$</span><span style="color: #004400;">&#40;</span><span style="color: #000088;">PYTHON_SO</span><span style="color: #004400;">&#41;</span>
&nbsp;
clean<span style="color: #004400;">:</span>
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*</span>_wrap<span style="color: #004400;">*</span> <span style="color: #004400;">*</span>~ <span style="color: #004400;">.</span>~<span style="color: #004400;">*</span> <span style="color: #004400;">*.</span>pyc
	rm <span style="color: #004400;">-</span>f <span style="color: #004400;">*.</span>o <span style="color: #004400;">*.</span>so <span style="color: #004400;">*.</span>so</pre></div></div>

<p>If that works, you can launch a Python3 shell (from now on I&#8217;ll assume you do the import first):</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> swig_intro <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> mult<span style="color: black;">&#40;</span>-<span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
-<span style="color: #ff4500;">50</span></pre></div></div>

<p>Behind the scenes, SWIG uses something called typemaps to specify how each type is from Python to C and vice versa. In many cases, such as this, we don&#8217;t need to worry about that at all, because SWIG has default typemaps for all the fundamental datatypes. For example, you might wonder what SWIG does with a C struct. Go ahead and add this definition to the header file and the interface file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> Person <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>first_name<span style="color: #339933;">;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>last_name<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> birth_year<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> Person<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">void</span> print_person<span style="color: #009900;">&#40;</span>Person <span style="color: #339933;">*</span>p<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and put the following function into the <code>.c</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">void</span> print_person<span style="color: #009900;">&#40;</span>Person <span style="color: #339933;">*</span>p<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: #339933;">!</span>p<span style="color: #339933;">-&gt;</span>first_name <span style="color: #339933;">||</span> <span style="color: #339933;">!</span>p<span style="color: #339933;">-&gt;</span>last_name<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;The person is not properly defined.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s %s was born in %d.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>first_name<span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>last_name<span style="color: #339933;">,</span> p<span style="color: #339933;">-&gt;</span>birth_year<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now recompile and try this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> p = Person<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">first_name</span> = <span style="color: #483d8b;">&quot;Paul&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">last_name</span> = <span style="color: #483d8b;">&quot;Black&quot;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> p.<span style="color: black;">birth_year</span> = <span style="color: #ff4500;">1977</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> print_person<span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
Paul Black was born <span style="color: #ff7700;font-weight:bold;">in</span> 1977.</pre></div></div>

<p>As you can see, the the struct Person was wrapped in a very simple Python class with the three public data fields <code>first_name</code>, <code>last_name</code>, and <code>birth_year</code>.</p>
<p>Next, let&#8217;s see how to use typemaps ourselves to pass a more complex structure from Python to C. We&#8217;re going to make a (rather contrived) C function that will return the product of a list of integers. We&#8217;ll do this by turning a Python list into a C struct that specifies an int array and a size. Add the following code to the header and interface files:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> IntList <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> IntList<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> product<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In the <code>.c</code> file, add the function:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> product<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>l<span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">&lt;=</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> ret_val <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ret_val <span style="color: #339933;">*=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ret_val<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we need to define the typemap in the <code>.i</code> file. This isn&#8217;t too difficult; we just make use of several functions from the Python C API:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>in<span style="color: #009900;">&#41;</span> IntList <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyList_Check<span style="color: #009900;">&#40;</span>$input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyErr_SetString<span style="color: #009900;">&#40;</span>PyExc_TypeError<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Not a list.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> size <span style="color: #339933;">=</span> PyList_Size<span style="color: #009900;">&#40;</span>$input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>IntList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list <span style="color: #339933;">=</span> list<span style="color: #339933;">;</span>
    $<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">=</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyObject <span style="color: #339933;">*</span>item <span style="color: #339933;">=</span> PyList_GetItem<span style="color: #009900;">&#40;</span>$input<span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyInt_Check<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            PyErr_SetString<span style="color: #009900;">&#40;</span>PyExc_TypeError<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;List item is not an integer.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            free<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            free<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">return</span> NULL<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> PyInt_AsLong<span style="color: #009900;">&#40;</span>item<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>freearg<span style="color: #009900;">&#41;</span> IntList <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This also illustrates how to tell SWIG to free your objects and how to handle errors. Now we can see it in action:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> <span style="color: #ff7700;font-weight:bold;">from</span> swig_intro <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: Not a <span style="color: #008000;">list</span>.
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;five&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;&lt;stdin&gt;&quot;</span>, line <span style="color: #ff4500;">1</span>, <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #66cc66;">&lt;</span>module<span style="color: #66cc66;">&gt;</span>
<span style="color: #008000;">TypeError</span>: List item <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> an integer.
<span style="color: #66cc66;">&gt;&gt;&gt;</span> product<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">3</span>,<span style="color: #ff4500;">5</span>,<span style="color: #ff4500;">7</span>,<span style="color: #ff4500;">9</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">945</span></pre></div></div>

<p>As you would expect, transferring data the other way is similar. For an example, let&#8217;s make a function that takes a list and returns a dictionary mapping the list elements to their squares. Add these definitions to the header and interface files:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">struct</span> IntMap <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> size<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list1<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>list2<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> IntMap<span style="color: #339933;">;</span>
&nbsp;
IntMap <span style="color: #339933;">*</span>square<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The actual C function will take a pointer to an <code>IntList</code> and return a pointer to an <code>IntMap</code>, allocating space for a second list as needed. The conversion to a <code>dict</code> will be specified with an out typemap. In the <code>.c</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">IntMap <span style="color: #339933;">*</span>square<span style="color: #009900;">&#40;</span>IntList <span style="color: #339933;">*</span>l<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    IntMap <span style="color: #339933;">*</span>ret_map <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>IntMap<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>size <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>size<span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>list1 <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #339933;">;</span>
    ret_map<span style="color: #339933;">-&gt;</span>list2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>malloc<span style="color: #009900;">&#40;</span><span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> l<span style="color: #339933;">-&gt;</span>size<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ret_map<span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">*</span> l<span style="color: #339933;">-&gt;</span>list<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> ret_map<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Add the typemap to the <code>.i</code> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span> IntMap <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    $result <span style="color: #339933;">=</span> PyDict_New<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i size<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        PyObject <span style="color: #339933;">*</span>key <span style="color: #339933;">=</span> PyInt_FromLong<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list1<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        PyObject <span style="color: #339933;">*</span>val <span style="color: #339933;">=</span> PyInt_FromLong<span style="color: #009900;">&#40;</span>$<span style="color: #0000dd;">1</span><span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        PyDict_SetItem<span style="color: #009900;">&#40;</span>$result<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> val<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">%</span>typemap<span style="color: #009900;">&#40;</span>freearg<span style="color: #009900;">&#41;</span> IntMap <span style="color: #339933;">*</span> <span style="color: #009900;">&#123;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>list2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    free<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>IntMap <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And we&#8217;re done!</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> square<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>,-<span style="color: #ff4500;">12</span>,<span style="color: #ff4500;">60000</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#123;</span><span style="color: #ff4500;">60000</span>: <span style="color: #ff4500;">3600000000</span>, <span style="color: #ff4500;">1</span>: <span style="color: #ff4500;">1</span>, -<span style="color: #ff4500;">12</span>: <span style="color: #ff4500;">144</span><span style="color: black;">&#125;</span></pre></div></div>

<p>I&#8217;ve only shown some of the more useful capabilities of SWIG that I find myself using a lot. For more information, consult the official <a href="http://www.swig.org/Doc1.3/Python.html">SWIG docs</a>. Unfortunately, they&#8217;re not entirely complete, so it is sometime easier to get answers by just browsing the built-in SWIG interface files to see how the typemaps work.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/99/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Comments, Please</title>
		<link>http://ctrl-c.us/blog/archives/14</link>
		<comments>http://ctrl-c.us/blog/archives/14#comments</comments>
		<pubDate>Fri, 27 Jun 2008 09:57:37 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/index.php?page=articles&amp;article=comments_please</guid>
		<description><![CDATA[I just put up the comments. This took a few hours of work to get right, and I suspect that there are still bugs. The formatting seems to be great, but I think that there might be problems with submission. I&#8217;ve tried to make it somewhat secure by stripping most of the nonsense you could [...]]]></description>
			<content:encoded><![CDATA[<p>I just put up the comments. This took a few hours of work to get right, and I suspect that there are still bugs. The formatting seems to be great, but I think that there might be problems with submission. I&#8217;ve tried to make it somewhat secure by stripping most of the nonsense you could put into one of the form strings to cause nasty things to happen. So (fingers crossed) it should be safe from SQL injection, XSS trickery, or html that messes up the page too much.<span id="more-14"></span></p>
<p>I&#8217;ve allowed some tags, and this introduces a problem: you can submit a comment that doesn&#8217;t have proper closing tags, such as<br />
<pre class="code">&amp;lt;del&amp;gt;I think I&#039;m funny</pre><br />
and mess with subsequent comments and text. I&#8217;ve yet to think of a good way to fix this, but I&#8217;m sure I&#8217;ll come up with something soon. I&#8217;m open to suggestions; why don&#8217;t you go ahead and leave one in the comments? :P</p>
<p>As I was working on the form validation, I came across a <a href="http://www.regular-expressions.info/email.html">tidbit of knowledge</a> I hadn&#8217;t been aware of. Apparently, the official specification for a valid email address is in <a href="http://tools.ietf.org/html/rfc2822#section-3.4.1">RFC 2822</a> and can be implemented with <a href="http://ctrl-c.us/regex.txt">this</a> monster of a regular expression. Lovely.</p>
<p><strong>Update: </strong>I&#8217;d love it if you&#8217;d like to try to break the comments, but please do it in the testing area of my site at <a href="http://ctrl-c.us/testing/">http://ctrl-c.us/testing/</a>. Also, please don&#8217;t try anything <em>too</em> malicious. If you notice something, leave a comment to let me know. Thanks.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/14/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSS Goodness</title>
		<link>http://ctrl-c.us/blog/archives/15</link>
		<comments>http://ctrl-c.us/blog/archives/15#comments</comments>
		<pubDate>Thu, 26 Jun 2008 11:35:44 +0000</pubDate>
		<dc:creator>Caleb Spare</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://ctrl-c.us/index.php?page=articles&amp;article=rss_goodness</guid>
		<description><![CDATA[I&#8217;ve set up a lovely RSS feed for the site. Please subscribe! I actually don&#8217;t use RSS feeds myself, although I set up Google Reader a long time ago with all of my favorite feeds. I know quite a few people who use Google Reader to aggregate all the content from sites they frequent. I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve set up a lovely RSS feed for the site. Please <a type="application/rss+xml" href="rss.php">subscribe</a>! I actually don&#8217;t use RSS feeds myself, although I set up <a href="http://www.google.com/reader">Google Reader</a> a long time ago with all of my favorite feeds. I know quite a few people who use Google Reader to aggregate all the content from sites they frequent. I still love going to the sites, though—having it all in one place makes me feel like I&#8217;m wasting time too efficiently.<span id="more-15"></span></p>
<p>In other news, when I most recently checked this site has climbed to somewhere on the second page of Google search results for both my name and &#8220;ctrl-c&#8221;. Hopefully this is going up quickly. I still have several features I&#8217;m working on which I have yet to roll out. I&#8217;m almost done with the comments code, but I haven&#8217;t done the submission form (along with all the proper stripping to get rid of nasty injections&#8230;ugh). Also, I&#8217;m going to be adding nice syntax highlighting for code samples, which should both look cool and necessitate the writing of an actual article about code :) Finally, I&#8217;ve yet to add admin pages, but this is pretty low priority.</p>
]]></content:encoded>
			<wfw:commentRss>http://ctrl-c.us/blog/archives/15/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
