<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Stories For Sad Robots</title>
	<atom:link href="http://erezsh.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://erezsh.wordpress.com</link>
	<description>Programming, Design, Art and AI</description>
	<lastBuildDate>Thu, 08 Dec 2011 19:18:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='erezsh.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Stories For Sad Robots</title>
		<link>http://erezsh.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://erezsh.wordpress.com/osd.xml" title="Stories For Sad Robots" />
	<atom:link rel='hub' href='http://erezsh.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Contracts and protocols as a substitute to types and interfaces</title>
		<link>http://erezsh.wordpress.com/2011/12/08/contracts-and-protocols-as-a-substitute-to-types-and-interfaces/</link>
		<comments>http://erezsh.wordpress.com/2011/12/08/contracts-and-protocols-as-a-substitute-to-types-and-interfaces/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 11:02:03 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[contracts]]></category>
		<category><![CDATA[design by contract]]></category>
		<category><![CDATA[interfaces]]></category>
		<category><![CDATA[type systems]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=180</guid>
		<description><![CDATA[I am a big fan of assertions. Whenever I reach a point in my code where I say &#8220;that pointer can&#8217;t possibly be null&#8221;, I immediately write &#8211; assert( p != NULL ); - and whenever I say &#8220;this list can&#8217;t possibly be longer than 256&#8243; I write assert len(l) &#60;= 256. If you wonder why [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=180&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I am a big fan of assertions. Whenever I reach a point in my code where I say &#8220;that pointer can&#8217;t possibly be null&#8221;, I immediately write &#8211; assert( p != NULL ); - and whenever I say &#8220;this list can&#8217;t possibly be longer than 256&#8243; I write <em>assert len(l) &lt;= 256</em>. If you wonder why I keep doing this, it&#8217;s because very often I&#8217;m wrong. It&#8217;s not that I&#8217;m a particularly bad programmer, but sometimes I make mistakes, and even when I don&#8217;t, sometimes I get very unexpected input, and even when I don&#8217;t, sometimes other pieces of code conspire against me. Assertions save me from mythical bug hunts on a regular basis.</p>
<p>So, it&#8217;s not a big surprise that I&#8217;m a big fan of contracts too. If you don&#8217;t know what contracts are, they&#8217;re essentially asserts that run at the beginning and end of each function, and check that the parameters and the return values meet certain expectations. In a way, function type declarations, as can be found in C or Java, are a special case of contracts. (<a href="http://en.wikipedia.org/wiki/Design_by_contract" target="_blank">Would you like to know more?</a>)</p>
<h3>Why not just use duck-typing?</h3>
<p>Duck typing is great, but in my experience it becomes a burden as the system grows in size and complexity. Sometimes objects aren&#8217;t fully used right away; they are stored as an instance variable, pickled for later use, or sent to another process or another computer. When you finally get the AttributeError, it&#8217;s in another execution stack, or in another thread, or in another computer, and debugging it becomes very unpleasant! And what happens when you get the correct object, but it&#8217;s in the wrong state? You won&#8217;t even get an exception until something somewhere gets corrupted.</p>
<p>In my experience, using an assertion system is the best way to find the subtle bugs and incongruities of big and complex systems.</p>
<h3>Why do we need something new?</h3>
<p>Types are very confining, even in &#8220;typeless&#8221; dynamic languages. Take Python: If your API has to verify that it&#8217;s getting a file object, the only way is to call <em>isinstance(x, file). </em>That forces the caller to inherit from <em>file</em>, even if he&#8217;s writing a mock object (say, as an RPC proxy) that makes no disk access. In any static-type language the I know, it&#8217;s impossible to say that you accept either int <em>or</em> float, and you&#8217;re forced to either write the same function twice, or use a template and just define it twice.</p>
<p>Today&#8217;s interfaces are ridiculous. In C#, an interface with a method that returns a<em> IList&lt;int&gt;</em> will be very upset if you try to implement it as returning<em> List&lt;int&gt;</em>! And don&#8217;t even try to return a <em>List&lt;int&gt;</em> when you&#8217;re expected to return <em>List</em>. Note that C# will gladly cast between these types in the code, but when dealing with interfaces and function signatures it just goes nuts. It gets very annoying when you&#8217;re implementing an ITree inteface and can&#8217;t use your own class as <em>nodes</em>&#8216; type because the signatures collide, and instead you have to explicitly cast from ITree at every method. But I digress.</p>
<p>Even if today&#8217;s implementations were better, types are just not enough. They tell you very little about the input or the output. You want to be able to test its values, lengths, states, and maybe to even interact with it to some degree. What we have just doesn&#8217;t cut it.</p>
<h3>What should we do instead?</h3>
<p>Contracts are already pretty good: they have a lot of flexibility and power, they&#8217;re self-documenting, and they can be reasoned upon by the compiler/interpreter (&#8220;Oh it only accepts a list[int&lt;256]? Time to use my optimized string functions instead!&#8221;). But they only serve as a band-aid to existing type systems. They don&#8217;t give you the wholesome experience of abstract classes and methods. But, they can.</p>
<p>To me, contracts are much bigger than just assertions. I see them as stepping-stones to a completely new paradigm, that will replace our current system of interfaces, abstract methods, and needless inheritance, with &#8220;Contract Protocols&#8221;.</p>
<div>How? These are the steps that we need to take to get there:</div>
<ol>
<li><strong> Be able to state your assertions about a function, in a declarative manner. Treat these assertions as an entity called a &#8220;contract&#8221;. </strong> We&#8217;re in the middle of this step, and some contract implementations (such as the wonderful <a href="http://andreacensi.github.com/contracts/" target="_blank">PyContracts</a> for python) have already taken the declarative entity route, which is essential for the next step.</li>
<li><strong>Be able to compare contracts.</strong> Basically, I want to be able to tell if a contract is contained within another contract, so if <em>C1⊂C2</em> and <em>x∊C1</em> then <em>x∊C2</em>. I suspect it&#8217;s easier said then done, but I believe that the following (much easier) steps render it as worth doing.</li>
<li><strong>Be able to bundle contracts in a &#8220;contract protocol&#8221;, and use it to test a class.</strong> A protocol is basically just a mapping of {method-name: contract}, and applying it to a class tests that each method exists in the class, and that its contract is a subset of the protocol&#8217;s corresponding contract. If these terms are met, it can be said that the class implements the protocol. A class can implement several protocols, obviously.</li>
<li><strong>Be able to compare protocols.</strong> Similarly to contracts, we want to check if a protocol is a subset of another protocol. Arguably, it&#8217;s the same as step 3.</li>
<li><strong>Contracts can also check if an instance implements a protocol.</strong> Making a full circle, we can now use protocols to check for protocols and so on, allowing layers of complexity. We can now write infinitely detailed demands about what a variable should be, but very concisely.</li>
</ol>
<p style="direction:ltr;">When we finish point 5, we have a complete and very powerful system in our hands. We don&#8217;t need to ever discuss types, except for the most basic ones. Inheritance is now only needed to gain functionality, not identity. We can use it for debug-only purposes, but also for run-time decisions in production (For example, in a Strategy pattern).</p>
<h3>Example</h3>
<p>As a last attempt to get my point across, here is vaguely how I imagine the file protocol to look in pseudo-code.</p>
<p>It doesn&#8217;t do the idea any justice, but hopefully it&#8217;s enough to get you started.</p>
<p><pre class="brush: python;">
protocol Closeable:
&lt;pre&gt;    close()

protocol _File &lt; Closeable:
    [str] name
    [int,&gt;0] tell()
    seek( [int,in (0,1,2)] )

protocol ReadOnlyFile &lt; _File:
    [str,!=''] read( [int,&gt;0 or None]? )
    [Iterable[str]] readlines( )
    [Iterable[str]] __iter__( )

protocol WriteOnlyfile &lt; _File:
    [int,&gt;0] write( [str,!=''] )
    writelines( [Iterable[str]] )
    flush()

protocol RWFile &lt; ReadOnlyFile | WriteOnlyFile:
    pass

&gt;&gt;&gt; print ReadOnlyFile &lt; RWFile
True
&gt;&gt;&gt; print implements( open('bla'), ReadOnlyFile )
True
&gt;&gt;&gt; print implements( open('bla'), Iterable )  # has __iter__ function,
True
&gt;&gt;&gt; print implements( open('bla'), Iterable[int] )
False
&gt;&gt;&gt; print implements( open('bla'), WriteOnlyFile )  # default is 'r'
False
&gt;&gt;&gt; print implements( open('bla'), RWFile )
False
&gt;&gt;&gt; print implements( open('bla', 'w+'), RWFile )
True
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/180/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/180/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/180/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=180&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2011/12/08/contracts-and-protocols-as-a-substitute-to-types-and-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>Baker &#8211; Expose Python to the Shell</title>
		<link>http://erezsh.wordpress.com/2010/02/17/baker-expose-python-to-the-shell/</link>
		<comments>http://erezsh.wordpress.com/2010/02/17/baker-expose-python-to-the-shell/#comments</comments>
		<pubDate>Wed, 17 Feb 2010 17:24:05 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[baker]]></category>
		<category><![CDATA[command-line]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=171</guid>
		<description><![CDATA[It&#8217;s been a long time since my last post, and it would be appropriate that I post about whatever it is that I&#8217;ve been working on. But I won&#8217;t. I&#8217;m writing this post only to tell you about an interesting new python library I stumbled upon. Baker, in their own words, &#8220;lets you easily add [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=171&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a long time since my last post, and it would be appropriate that I post about whatever it is that I&#8217;ve been working on. But I won&#8217;t. I&#8217;m writing this post only to tell you about an interesting new python library I stumbled upon.</p>
<p><a href="http://bitbucket.org/mchaput/baker/wiki/Home">Baker</a>, in their own words, &#8220;lets you easily add a command line interface&#8221;.</p>
<p>In other words, it lets you expose your python utility functions to your favorite shell.</p>
<p>The only requirements are that:</p>
<ul>
<li> Your function must accept string arguments (an exception: it accepts ints/floats if you provide a default argument)</li>
<li> Your function must print its output to stdout</li>
</ul>
<p>Okay, so these are a little limiting. But the interesting part about Baker is not its implementation (which is still a bit clunky and basic), but rather its concept. Here&#8217;s a small piece of code I wrote:</p>
<p><pre class="brush: python;">
import baker

@baker.command
def substr(text, start, end, step=1):
    print text[int(start):int(end):step]

if __name__ == '__main__':
    baker.run()
</pre></p>
<p>And here&#8217;s how I use it from the command line:<br />
<code><br />
&gt; baketest.py substr "Hello World!" 1 10<br />
ello Worl</code></p>
<p><code> </code></p>
<p><code>&gt; baketest.py substr "Hello World!" 1 10 --step 2<br />
el ol<br />
</code></p>
<p>The simplicity and intuitiveness of this interface really appealed to me. Hopefully this will catch on, and we&#8217;ll see more python scripts providing command-line interface, just because it&#8217;s very easy.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/171/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/171/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/171/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=171&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2010/02/17/baker-expose-python-to-the-shell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>Lazier Copy-On-Write</title>
		<link>http://erezsh.wordpress.com/2009/06/21/lazier-copy-on-write/</link>
		<comments>http://erezsh.wordpress.com/2009/06/21/lazier-copy-on-write/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 18:13:10 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[copy on write]]></category>
		<category><![CDATA[cow]]></category>
		<category><![CDATA[coward]]></category>
		<category><![CDATA[fragmentation]]></category>
		<category><![CDATA[lazy]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=161</guid>
		<description><![CDATA[Copy-on-write (COW) is a popular mechanism of lazy evaluation, that helps improve running speed and reduce memory requirements by transparently delaying the copying of data. Essentially, this is how it works: When you try to copy an object, you are instead given a fake object. Attempts to read that new object will instead read from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=161&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Copy-on-write" target="_blank">Copy-on-write</a> (COW) is a popular mechanism of <a href="http://en.wikipedia.org/wiki/Lazy_evaluation" target="_blank">lazy evaluation</a>, that helps improve running speed and reduce memory requirements by transparently delaying the copying of data. Essentially, this is how it works: When you try to copy an object, you are instead given a fake object. Attempts to read that new object will instead read from the original object. Writing to the object will create a new copy (as originally requested) and refer to it in the future for reads and writes.</p>
<p>It allows to write simpler and safer programs. For instance, a programmer can pass &#8220;copies&#8221; of his data with minimal performance impact, and not have to worry about others changing his original data.</p>
<p>It&#8217;s great, but can it be more?</p>
<p>I present here two proposals for extending this idea for even greater optimization. They are far from my areas of expertise, so I hope they still make sense.</p>
<p><strong>1. Copy-On-Write + Fragmentation</strong></p>
<p style="direction:ltr;">Fragmentation of data is a mechanism that allows different parts (blocks) of data to reside in different locations in memory while appearing intact (that is, sequential).  This mechanism is often accused of  slowing the computer down. However, its a critical feature of <a href="http://en.wikipedia.org/wiki/Virtual_memory" target="_blank">virtual memory</a>, which is a basis to all modern operating systems.</p>
<p style="direction:ltr;">Introducing fragmentation into your data structures has many benefits, but let&#8217;s discuss the benefits regarding COW, which may be obvious by now: On write, you don&#8217;t have to copy all of the data, just the blocks which are being changed. This can be a big difference, if you&#8217;re changing a few bytes in a 50mb string.</p>
<p style="direction:ltr;">You still have to copy the meta-data (such as, where are the blocks, what is the next block, etc.), but that&#8217;s a small price to pay, and a reasonable design requirement.</p>
<p style="direction:ltr;">Now instead of copying the entire data, you copy only a fragment of it. How big is that fragment? Perhaps a fixed size, such as 64k. But assuming you have no real restriction on the size of these data blocks, the next logical step, in my eyes, is to ask: Why not make it as small as possible? That is, why not intentionally fragment the block into three smaller blocks: Before the area that is to be written, the area that is to be written, and after the area to be written. At this point we continue as we originally planned: We copy only the block which is to be written, which is, of course, exactly as small as it can be.</p>
<p style="direction:ltr;">Eventually, we have a model in which writing <em>n</em> bytes into a COWed data of <em>m</em> bytes takes <em>O(n) </em>time and memory, instead of the original <em>O(m+n) </em>time and <em>O(m)</em> memory. I argue that in the common case, <em>n</em> is significantly smaller than <em>m</em>, and so the win is big<em>.</em></p>
<p style="direction:ltr;">Of course, fragmentation has a habit of slowing down reading times. When fragmentation is &#8220;too high&#8221;, it is possible to defragment the memory (an occasional O(<em>m</em>) process). The optimal balance of fragmentation depends heavily on the frequency of reads vs of writes, but I argue that even a sub-optimal, common-case balance, will produce an improvement in performance.</p>
<p style="direction:ltr;">Edit: I&#8217;ve been unclear about how it affects look-ups. Fragmentation to blocks of fixed size remains O(1) for getting and setting items. However, for variable-size blocks it&#8217;s not so simple. A search tree can achieve a look-up of  O(log<em>n</em>) where <em>n</em> is number of fragments, which is a lot slower than the original array peformance. It is probably only a good idea if you have access to the operating system&#8217;s memory management, or if the use of look-ups is rare (and then an occasional defragmentation would still be necessary). Still, fixed-size fragments are <em>good enough</em>, and they can be dynamically resized with little cost, as long as the resize is uniform.</p>
<p style="direction:ltr;">
<p style="direction:ltr;"><strong>2. Copy-On-Write-And-ReaD</strong></p>
<p style="direction:ltr;">Or in short, COWARD, is a mechanism to even further delay copying, to only after the written data is also read. That is, when the programmer requests to write data, this mechanism will instead journal the data, producing sort of a &#8220;diff&#8221;. Only when the programmer attempts to read the result, the original data is copied and the diff is applied. A diff structure is provided by any implementation of lazy evaluation, by definition, but perhaps there are other more suitable diff structures for this purpose.</p>
<p style="direction:ltr;">This starts to make more sense with fragmentation: Then the diff can be applied only to the block that is read. And so, a block will be copied only if it is both written and read. In some cases, there may be very little intersection between the two (and so, very little copying).</p>
<p style="direction:ltr;">So basically, COWARD is just a (non-)fancy name for an array of <a href="http://en.wikipedia.org/wiki/Futures_and_promises" target="_blank">promises </a>(not to be confused with a field of dreams). The (possible) novelty is in the way this array is created and used: transparently, and relatively efficiently. Note that, like the previous proposal, it provides little value in situations where the all of the data is altered or read. However, I argue it will significantly improve performance in cases where only part of the data is read and written.</p>
<p>It can, for instance, be useful in cases where an algorithm works on COWed data (which happens quite often) and provides more processing than the user requires. Using this method, only blocks that the user requests are copied &#8212; and if the calculations themselves are lazy &#8212; processed. And all of it transparent to both the user and the implementer of the algorithm .</p>
<p>Here&#8217;s to lazier COWs!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/161/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=161&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/06/21/lazier-copy-on-write/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>PySnippets &#8211; improving code reuse</title>
		<link>http://erezsh.wordpress.com/2009/06/02/pysnippets-improving-code-reuse/</link>
		<comments>http://erezsh.wordpress.com/2009/06/02/pysnippets-improving-code-reuse/#comments</comments>
		<pubDate>Tue, 02 Jun 2009 12:06:41 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[import]]></category>
		<category><![CDATA[reuse]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[utility]]></category>
		<category><![CDATA[wikidot]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=149</guid>
		<description><![CDATA[For a long time now, I&#8217;ve been hindered by the issue of utilities, or snippets. These are convenience functions and classes that are too small or too incomplete to justify a library, yet are useful enough to be used. I&#8217;ve posted a few on my blog: Namespaces, X and now FileDict. Others I didn&#8217;t post, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=149&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>For a long time now, I&#8217;ve been hindered by the issue of utilities, or snippets. These are convenience functions and classes that are too small or too incomplete to justify a library, yet are useful enough to be used.<br />
I&#8217;ve posted a few on my blog: Namespaces, X and now FileDict. Others I didn&#8217;t post, and include a priority queue, an A* implementation, a lazy-list, an LRU memoizer, etc.  You probably have a few of those. I know because I see them on snippet sites.</p>
<p>However, I rarely actually use these in my code. I really want to. But once your code spans more than one file, you usually need to make a proper installation, or at least trouble your &#8220;users&#8221; a bit more. Usually saving a few lines just isn&#8217;t worth the trouble. Copy-pasting the snippet into the file is sometimes the solution, but it really pains me that I&#8217;ll have to re-paste it every time I improve my snippet.</p>
<p>I&#8217;m sure some of you looked at my snippets, or other people&#8217;s, thought &#8220;cool&#8221;, but never used them, simply because it was too much trouble.</p>
<p>Paradoxically, this is especially true when writing snippets. They are just one small file, and using another snippet would probably make them too hard to distribute. This is a magic-circle, for-ever limiting our snippets to a low level of sophistication, and discouraging re-use.</p>
<p>I want to break that circle. I want to create an economy of snippets, increasingly building on each other, eventually creating a &#8220;standard library&#8221; of their own. But how would one do that? I have one suggestion, along with a proof-of-concept, which I will present here.</p>
<h2><strong>PySnippets</strong></h2>
<p><a title="PySnippets" href="http://pysnippets.wikidot.com" target="_blank">PySnippets</a> is my attempt of making snippets usable. It&#8217;s comprised of two solutions &#8211; a server and a client.</p>
<ol>
<li>Server - <strong>A website for uploading snippets</strong>. Simple enough. You can rate them, tag them, discuss them, offer some documentation and of-course post newer versions.</li>
<li>Client - <strong>A python module that automagically imports snippets from the web</strong>. Essentially, it downloads the snippets you request to a cache, imports them if they&#8217;re already there, and periodically searches for updates.</li>
</ol>
<p>The server is structured in a predictable way, so that the client knows how to fetch a snippet just by its name.</p>
<p><strong>The Client</strong></p>
<p>Here&#8217;s a usage example with my current client implementation, I creatively call &#8220;snippets&#8221;:</p>
<p><pre class="brush: python;">
import snippets
antigravity = snippets.get('antigravity')  # &quot;snippet-import&quot;
antigravity.start(mode='xkcd')
</pre></p>
<p>Easy as that!</p>
<p>The <em>snippets.get</em> function looks for the module in the local snippets-cache. If it&#8217;s there, <em>get</em> just imports it and returns the module. If it&#8217;s not, it queries the server for a snippet called &#8220;antigravity&#8221; (names are unique), stores it in the cache, and the imports it. What the user notices is a 2-second pause the first time he ever imports that snippet, and nothing else from then on.</p>
<p>You can specify to download a specific version, like this:</p>
<p><pre class="brush: python;">
filedict = snippets.get('filedict', version=0.1)
</pre></p>
<p><strong>Auto-Updating Snippets</strong></p>
<p style="direction:ltr;">The current implementation also includes an &#8220;auto-update&#8221; feature: Periodically, before importing a module, the client surveys the server for a newer version of it. If a newer version exists, it downloads it to the cache and continues with the import.</p>
<p style="direction:ltr;">Auto-updates can be disabled in a parameter to <em>get</em>.</p>
<p style="direction:ltr;"><strong>The Server</strong></p>
<p style="direction:ltr;">The server is yet another service to upload snippets, however it has a slightly unusual design (which no other snippet site I know of has):</p>
<ul>
<li> A URL to a snippet is easy to deduce given its name.</li>
<li>There is a conscious (though simple) support for versions.</li>
<li>To increase reliability and trust (more on that later), <span style="text-decoration:underline;">uploaded snippets cannot be altered</span> (but a new version can be issued)</li>
</ul>
<p>Since I know very little about administration and server-maintenance, I chose wikidot.com to host my POC web-site. They have an elaborate support for permissions and most of the features I need, such as the ability to rate, tag and discuss snippets.</p>
<p><strong>Trust</strong></p>
<p>Perhaps the biggest issue with such a system is trust. Since you&#8217;re running code which resides online, you have to trust me not to maliciously alter the snippets, and also you have to trust the author of the snippet not to do so.</p>
<div style="direction:ltr;">As a partial solution, uploaded files cannot be altered: Not edited, nor renamed, nor deleted, etc. So if specify a particular snippet version, it is guaranteed that it will never change (I may commit changes by request, but I will audit them myself).</div>
<div style="direction:ltr;">If you decide to use the latest version of a snippet (that is, not specify a version), please make sure you trust its author.</div>
<p>Perhaps higher-ups in the Python community would like to take some sponsorship of the project, removing the remaining trust-issues with the administrator (that&#8217;s me).</p>
<h2><strong>Implications</strong></h2>
<ul>
<li>To distribute your snippets, all you need is for the reciever to have an internet connection, and the snippets client.</li>
<li>If you&#8217;re sending someone code, you can attach the client (it&#8217;s rather small, too), and just import away. The reciever will benefit from improvements and bugfixes to your snippets.</li>
<li>You can use other people&#8217;s snippets just as easily, as long as you trust them.</li>
<li>Snippets can now build on each other without worrying too much.</li>
</ul>
<p><strong>What if my user is offline?</strong></p>
<p>Then probably PySnippets isn&#8217;t for him.</p>
<p>However, I do have some ideas, and might implement them if there is sufficient demand.</p>
<h2><strong>Afterword</strong></h2>
<p>PySnippets is my humble attempt at solving the utility/snippet reuse problems. I hope you like it and find it useful.</p>
<p>Please <a title="PySnippets" href="http://pysnippets.wikidot.com/" target="_blank">try it</a>!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/149/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/149/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/149/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=149&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/06/02/pysnippets-improving-code-reuse/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>FileDict &#8211; bug-fixes and updates</title>
		<link>http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/</link>
		<comments>http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/#comments</comments>
		<pubDate>Sun, 31 May 2009 16:15:41 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[bugfix]]></category>
		<category><![CDATA[dict]]></category>
		<category><![CDATA[FileDict]]></category>
		<category><![CDATA[persistent]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=145</guid>
		<description><![CDATA[In my previous post I introduced FileDict. I did my best to get it right the first time, but as we all know, this is impossible for any non-trivial piece of code. I want to thank everyone for their comments and remarks. It&#8217;s been very helpful. The Unreliable Pickle A special thanks goes to the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=145&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://erezsh.wordpress.com/2009/05/24/filedict-a-persistent-dictionary-in-python/">previous post</a> I introduced FileDict. I did my best to get it right the first time, but as we all know, this is impossible for any non-trivial piece of code.<br />
I want to thank everyone for their comments and remarks. It&#8217;s been very helpful.</p>
<p><strong>The Unreliable Pickle</strong></p>
<p>A special thanks goes to the mysterious commenter &#8220;R&#8221;, for pointing out that pickling identical objects may produce different strings (!), which are therefor inadequate to be used as keys. And my FileDict indeed suffered from this bug, as this example shows:</p>
<p><pre class="brush: python;">
&gt;&gt;&gt; key = (1, u'foo')
&gt;&gt;&gt; d[(1, u'foo')] = 4
&gt;&gt;&gt; d[(1, u'foo')]
4
&gt;&gt;&gt; d[key]
Traceback (most recent call last):
  File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
  File &quot;filedict.py&quot;, line 64, in __getitem__
    raise KeyError(key)
KeyError: (1, u'foo')
</pre></p>
<p>And if that&#8217;s not bad enough:</p>
<p><pre class="brush: python;">
&gt;&gt;&gt; d[key] = 5
&gt;&gt;&gt; list(d.items())
[['a', 3], [(1, 2), 3], [(1, u'foo'), 4], [(1, u'foo'), 5]]
</pre></p>
<p>Ouch.<br />
I&#8217;ve rewritten the entire storing mechanism to poll only on hash and compare keys after unpickling. This may be a bit slower, but I don&#8217;t (and shouldn&#8217;t) expect many colliding hashes anyway.<br />
Bug is fixed.</p>
<p><strong>DictMixin</strong></p>
<p>Under popular demand, I&#8217;m now inheriting from DictMixin. It&#8217;s made my code a bit shorter, and was not at all painful.</p>
<p><strong>Copy and Close</strong></p>
<p>I no longer close the database on __del__, and instead I rely on the garbage collector. It seems to close the database on time, and it allows to one copy the dictionary (which, of course, will all be always have the same keys, but doesn&#8217;t have to have the same behavior or attributes).</p>
<p><strong>New Source Code</strong></p>
<p>Is available <a href="http://erez.wikidot.com/filedict-0-2-code">here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/145/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/145/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/145/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=145&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>FileDict &#8211; a Persistent Dictionary in Python</title>
		<link>http://erezsh.wordpress.com/2009/05/24/filedict-a-persistent-dictionary-in-python/</link>
		<comments>http://erezsh.wordpress.com/2009/05/24/filedict-a-persistent-dictionary-in-python/#comments</comments>
		<pubDate>Sun, 24 May 2009 14:59:55 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[dict]]></category>
		<category><![CDATA[persistent]]></category>
		<category><![CDATA[sqlite]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=137</guid>
		<description><![CDATA[The result is a dictionary which at all-times exists as a file, has virtually no size limit, and can be accessed by several processes concurrently.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=137&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Python&#8217;s dictionary is possibly the most useful construct in the language.  And I argue that for some purposes, mapping it to a file (in real-time) can be even more useful.</p>
<p><strong>*** Update ***</strong></p>
<p>There&#8217;s a newer and better version of FileDict, containing bugfixes and corrections, many of which are due to comments on this page.<br />
You can read about it (with explanations) in <a href="http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/">http://erezsh.wordpress.com/2009/05/31/filedict-bug-fixes-and-updates/</a></p>
<p><strong>Why?</strong></p>
<p>The dictionary resides in memory, and so has three main &#8220;faults&#8221;:</p>
<ol>
<li>It only lasts as long as your program does.</li>
<li>It occupies memory that might be useful for other, more commonly accessed, data.</li>
<li>It is limited to how much memory your machine has.</li>
</ol>
<p>The first can be solved by pickling and unpickling the dictionary, but will not survive an unexpected shutdown (even putting the pickling in a try-finally block won&#8217;t protect it against all errors).</p>
<p><strong>FileDict</strong></p>
<p>FileDict is a dictionary interface I wrote, that saves and loads its data from a file using keys. Current version uses Sqlite3 to provide consistency, and as a by-product, <a title="ACID" href="http://en.wikipedia.org/wiki/ACID" target="_blank">acidity</a>.</p>
<p>The result is a dictionary which at all-times exists as a file, has virtually no size limit, and can be accessed by several processes concurrently.</p>
<p>It is meant as a quick-and-simple general-purpose solution. It is rarely the best solution, but it is usually good enough.</p>
<p>Performance obviously cannot compare to the builtin dictionary, but it is reasonable and of low complexity (refer to sqlite for more details on <em>that</em>).</p>
<p><strong>Uses</strong></p>
<p>FileDict can be used for many purposes, including:</p>
<ul>
<li>Saving important data in a convinient manner</li>
<li>Managing large amounts of data in dictionary form, without the mess of implementing paging or other complex solutions</li>
<li>Communication between processes (sqlite supports multiple connections and implements ACID)</li>
</ul>
<p><strong>Examples</strong></p>
<p><pre class="brush: python;">
$ python
&gt;&gt;&gt; import filedict
&gt;&gt;&gt; d=filedict.FileDict(filename=&quot;example.dict&quot;)
&gt;&gt;&gt; d['bla'] = 10
&gt;&gt;&gt; d[(2,1)] = ['hello', (1,2) ]
-- exit --
$ python
&gt;&gt;&gt; import filedict
&gt;&gt;&gt; d=filedict.FileDict(filename=&quot;example.dict&quot;)
&gt;&gt;&gt; print d['bla']
10
&gt;&gt;&gt; print d.items()
[['bla', 10], [(2, 1), ['hello', (1, 2)]]]
&gt;&gt;&gt; print dict(d)
{'bla': 10, (2, 1): ['hello', (1, 2)]}
</pre></p>
<p><pre class="brush: python;">
&gt;&gt;&gt; d=filedict.FileDict(filename=&quot;try.dict&quot;)
&gt;&gt;&gt; with d.batch:  # using .batch suspend commits, making a batch of changes quicker
&gt;&gt;&gt;    for i in range(100000):
&gt;&gt;&gt;            d[i] = i**2
(takes about 8 seconds on my comp)
&gt;&gt;&gt; print len(d)
100000
&gt;&gt;&gt; del d[103]
&gt;&gt;&gt; print len(d)
99999
</pre></p>
<p><strong>Limitations</strong></p>
<ul>
<li>All data (keys and values) must be pickle-able</li>
<li>Keys must be hashable (<em>perhaps this should be removed by hashing the pickled key</em>)</li>
<li>Keys and values are stored as a copy, so changing them after assignment will not update the dictionary.</li>
</ul>
<p><strong>Source Code</strong></p>
<p>Is availible <del datetime="2010-04-13T12:43:19+00:00"><a href="http://erez.wikidot.com/filedict-0-1-code">in here</a></del> <a href="http://erez.wikidot.com/filedict-0-1-code">in here</a></p>
<p><strong>Future</strong></p>
<p>Additions in the future may include:</p>
<ul>
<li>An LRU-cache for fetching entries</li>
<li>A storage strategy different than Sqlite</li>
</ul>
<p>Other suggestions?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/137/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/137/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/137/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=137&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/05/24/filedict-a-persistent-dictionary-in-python/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>Ribbon</title>
		<link>http://erezsh.wordpress.com/2009/03/12/ribbon/</link>
		<comments>http://erezsh.wordpress.com/2009/03/12/ribbon/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 14:46:46 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Humor]]></category>
		<category><![CDATA[Ribbon]]></category>
		<category><![CDATA[Word]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=131</guid>
		<description><![CDATA[Most of you may think that Microsoft&#8217;s Ribbon is fairly new. However, the truth is it was already an optional feature in Word 5.5, as the following screenshot plainly demonstrates: I salute Microsoft for their foresight.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=131&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Most of you may think that Microsoft&#8217;s Ribbon is fairly new.<br />
However, the truth is it was already an optional feature in Word 5.5, as the following screenshot plainly demonstrates:</p>
<p><img class="size-full wp-image-130" title="ribbon" src="http://erezsh.files.wordpress.com/2009/03/ribbon.jpg?w=450"></p>
<p>I salute Microsoft for their foresight.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/131/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/131/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/131/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=131&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/03/12/ribbon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>

		<media:content url="http://erezsh.files.wordpress.com/2009/03/ribbon.jpg" medium="image">
			<media:title type="html">ribbon</media:title>
		</media:content>
	</item>
		<item>
		<title>Raphael and a Prophecy</title>
		<link>http://erezsh.wordpress.com/2009/03/07/raphael-and-a-prophecy/</link>
		<comments>http://erezsh.wordpress.com/2009/03/07/raphael-and-a-prophecy/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 09:59:28 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Web Programming]]></category>
		<category><![CDATA[Raphael]]></category>
		<category><![CDATA[vector graphics]]></category>
		<category><![CDATA[web graphics]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=127</guid>
		<description><![CDATA[you can create complex vector graphics that work in almost every browser<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=127&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>More than once I have wasted my time in attempt to introduce graphics into the webpage, some of these attempts are recorded in this very blog: First were my <a href="/2008/07/31/drawing-diagonal-lines-with-css/">lines in CSS</a>, followed by <a href="/2008/11/25/rotating-cube-in-javascript/">rotating rectangles</a>. I say &#8220;wasted&#8221; because I have now found <a href="http://raphaeljs.com/">Raphaël</a>, and I&#8217;m in love.</p>
<p><strong>Raphaël</strong></p>
<p>What is it? Their site is too modest, claiming &#8220;Raphaël is a small JavaScript library that should simplify your work with vector graphics on the web&#8221;.<br />
While true, it is also a most ambitious project to change the way the web looks, whether they mean to or not.<br />
All (popular) browsers support vector graphics: IE supports VML, and all other browsers support SVG. By implementing both languages and switching between them, you can create complex vector graphics that work in almost every browser.<br />
This idea was new to me, at least, and I think it&#8217;s great. Now Raphaël comes along and does it for you. Nice.</p>
<p><strong>My Prophecy</strong><br />
Vector graphics (VG) will become more popular. As VG libraries mature (and Raphaël is already in good shape), VG on the web would become so easy that anyone could use them, and many would. Soon enough you will have beautiful interactive GUIs. At fiirst snippets and demos, then complete libraries for everyone to use. What more can it do? Maybe interactive animations, maybe just add some spice to websites.<br />
It probably won&#8217;t bury Flash, but I expect it would be a competition.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/127/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/127/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/127/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=127&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/03/07/raphael-and-a-prophecy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>The Evils Of Positional Arguments</title>
		<link>http://erezsh.wordpress.com/2009/01/16/the-evils-of-positional-arguments/</link>
		<comments>http://erezsh.wordpress.com/2009/01/16/the-evils-of-positional-arguments/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 14:49:53 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=123</guid>
		<description><![CDATA[A few days ago I found myself writing pure ansi C code, after over two years of not touching it at all. In fact, these two years consisted mostly of Python, and a little of C#, both very far from it. While coding away, trying to get re-accustumed to mallocs and frees, return values instead [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=123&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A few days ago I found myself writing pure ansi C code, after over two years of not touching it at all. In fact, these two years consisted mostly of Python, and a little of C#, both very far from it. While coding away, trying to get re-accustumed to mallocs and frees, return values instead of exceptions and pointer arithmatic, I had to perform a memcpy. Very easy, of course, but I could not for the life of me remember how to call it. I remembered rather vividly that it had 3 arguments, named &#8216;dst&#8217;, &#8216;src&#8217; and &#8216;len&#8217;, the first two being void* and the latter.. perhaps plain int? But in which order they came, I could not recall. Now, that is not so evil, of course, because a quick look at the C reference refreshed my memory (until next time). This repeated with other functions (such as memset). But only when it started to happen with my own functions, I realized how unnatural it is for me, to remember things by order and not by name. Names allow us to establish a context, to find reasoning. Also, we are (relatively) good at remembering names.</p>
<p>By using position as a way for transferring parameters, we are essentially calling them &#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221; and etc., which are still names, but are devoid of context or meaning, and are the same for every function. It is not only hard to remember the right order; we can endure it (as reality proves), but it makes our code less readable, less natural.</p>
<p>Also, it complicates changing existing APIs. Move an argument from its position, and you have to change the position everywhere in the code. This can easily lead to subtle bugs (consider switching dst and src, it may be very hard to find where this happened!). So if you append parameters, they must come in the end, which somtimes does not make much sense.</p>
<p>While all programming languages (that I know of) sin in this evil of positionality , one device takes it one step ahead: Regular expressions. Specfically, RE Substitution. Not only do you address its matches using numbers, but actually figuring out which number goes to which match can be confounding. I cannot count how many bugs this must have produced.</p>
<p><strong>In conclusion:</strong></p>
<p>Design your systems differently.<br />
Let computers count. Let humans use names!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=123&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2009/01/16/the-evils-of-positional-arguments/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
		<item>
		<title>Rotating Cube in JavaScript</title>
		<link>http://erezsh.wordpress.com/2008/11/25/rotating-cube-in-javascript/</link>
		<comments>http://erezsh.wordpress.com/2008/11/25/rotating-cube-in-javascript/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 19:58:38 +0000</pubDate>
		<dc:creator>erezsh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[cube]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JS]]></category>

		<guid isPermaLink="false">http://erezsh.wordpress.com/?p=121</guid>
		<description><![CDATA[My CSS-Parrot gave me an appetite for more in-browser graphics. This, led to my newest creation: A rotating &#8220;3D&#8221; cube, written with JavaScript. Check it out here: http://kanelynchsucks.com/sfsr/rot_cube.html (glory to Tzahi for hosting it) Edit: The page is currently unavailible. If you&#8217;re interested, email me and I&#8217;ll send you a copy. Please note, this cube [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=121&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My CSS-Parrot gave me an appetite for more in-browser graphics. This, led to my newest creation: A rotating &#8220;3D&#8221; cube, written with JavaScript.</p>
<p><span style="text-decoration:line-through;">Check it out here: <a title="Rotating Cube" href="http://kanelynchsucks.com/sfsr/rot_cube.html" target="_blank">http://kanelynchsucks.com/sfsr/rot_cube.html</a></span></p>
<p><span style="text-decoration:line-through;">(glory to Tzahi for hosting it)</span></p>
<p><strong>Edit: The page is currently unavailible. If you&#8217;re interested, email me and I&#8217;ll send you a copy.</strong></p>
<p>Please note, this cube is <em>not really 3D</em>.  I just hacked it together using a couple of tricks, which I think are rather clever.</p>
<p>My next post (or more) will explain thoroughly how I made the cube and the tricks that I used. But I highly recommend you, the readers of this blog, to try and figure it out yourself. It can be a fun exercise. The actual code is very readable, and the only mystery is &#8211; how did I do it?</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/erezsh.wordpress.com/121/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/erezsh.wordpress.com/121/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/erezsh.wordpress.com/121/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=erezsh.wordpress.com&amp;blog=4049275&amp;post=121&amp;subd=erezsh&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://erezsh.wordpress.com/2008/11/25/rotating-cube-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">erezsh</media:title>
		</media:content>
	</item>
	</channel>
</rss>
