<?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>Alec's thoughts &#187; work</title>
	<atom:link href="http://www.flett.org/category/work/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flett.org</link>
	<description></description>
	<lastBuildDate>Thu, 15 Oct 2009 17:08:40 +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>demangling &#8216;property&#8217; values</title>
		<link>http://www.flett.org/2005/05/10/demangling-property-values/</link>
		<comments>http://www.flett.org/2005/05/10/demangling-property-values/#comments</comments>
		<pubDate>Tue, 10 May 2005 18:36:34 +0000</pubDate>
		<dc:creator>alecf</dc:creator>
				<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.flett.org/?p=44</guid>
		<description><![CDATA[I&#8217;m learning more about how properties work in Python. One thing I&#8217;m learning is that a property objects are only evaluated in the context of the parent object they&#8217;re attached to.

After  my last property trick, I now needed a way to manage groups of color tints. After thinking about it for a while, I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m learning more about how properties work in Python. One thing I&#8217;m learning is that a property objects are only evaluated in the context of the parent object they&#8217;re attached to.<br />
<span id="more-44"></span><br />
After  my last property trick, I now needed a way to manage groups of color tints. After thinking about it for a while, I ended up with:</p>
<pre class="code">
    gradientLeft = tintedColor(0.4)
    gradientRight = tintedColor(0.2)
    outlineColor = tintedColor(0.5)
    textColor = tintedColor(0.67, 0.6)
    defaultColors = (gradientLeft, gradientRight, outlineColor, textColor)
</pre>
<p>I thought, &#8220;I&#8217;m brilliant! I&#8217;m extending the dynamic nature of &#8216;property&#8217; to defaultColors&#8221;</p>
<p>But unfortunately, I ended up with something ugly instead. </p>
<p>What I got back was:</p>
<pre class="code">
>>> col.defaultColors
(&lt;property object at 0x009D9530&gt;, &lt;property object at 0x01351530&gt;,
&lt;property object at 0x013B5378&gt;, &lt;property object at 0x013B53A0&gt;)
</pre>
<p>That&#8217;s not helpful at all! If I tried to access col.defaultColors[0], I got a property object rather than an rgb tuple.</p>
<p>I think the problem is that when I said col.defaultColors[0], the property object didn&#8217;t know it was being accessed as a property of an object, so it didn&#8217;t unwrap itself. It looked like I was going to have to do that unwrapping myself.</p>
<p>Sure enough, if I look at the property object, I can see what to do:</p>
<pre class="code">
>>> dir(col.selectedColors[0])
['__class__', '__delattr__', '__delete__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__
', '__set__', '__setattr__', '__str__', 'fdel', 'fget', 'fset']
</pre>
<p>Ah! So all I have to do is call fget():</p>
<pre class="code">
>>> col.selectedColors[0].fget()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in ?
TypeError: getSaturatedColor() takes exactly 1 argument (0 given)
</pre>
<p>Of course, because it needs a &#8220;self&#8221; argument. I have one (&#8217;col&#8217;) but its not being used in the context of the call to fget. Lets try passing it in:</p>
<pre class="code">
>>> col.selectedColors[0].fget(col)
(216.75, 255.0, 255.0)
</pre>
<p>Ah! So now I need a way to unmangle each element in the list, at the time the list is accessed.. what could I possibly use build the list then? I know, property()!</p>
<p>and thus we have tupleProperty. like tintedColor, we need to essentially curry some state to the property call, so we need to wrap it with a function.</p>
<pre class="code">
    def tupleProperty(*args):
        def demangledTupleGetter(self):
            return [val.fget(self) for val in args]
        return property(demangledTupleGetter)
</pre>
<p>now, we can redefine defaultColors as appropriate:</p>
<pre class="code">
    defaultColors = tupleProperty(gradientLeft, gradientRight, outlineColor, textColor)
</pre>
<p>what&#8217;s particularly neat is that we can change the hue and the right properties will be called:</p>
<pre class="code">
>>> col.defaultColors
[(153.0, 255.0, 255.0), (204.0, 255.0, 255.0), (127.5, 255.0, 255.0),
(50.489999999999995, 153.0, 153.0)]
>>> col.eventHue = 0.0
>>> col.defaultColors
[(255.0, 153.0, 153.0), (255.0, 204.0, 204.0), (255.0, 127.5, 127.5),
(153.0, 50.489999999999995, 50.489999999999995)]
</pre>
<p>I think the only bummer here is that I had to dive into the internals of python in order to make use of fget() and untangle this duple/property dependency.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flett.org/2005/05/10/demangling-property-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cool python tricks</title>
		<link>http://www.flett.org/2005/04/26/cool-python-tricks/</link>
		<comments>http://www.flett.org/2005/04/26/cool-python-tricks/#comments</comments>
		<pubDate>Tue, 26 Apr 2005 19:38:09 +0000</pubDate>
		<dc:creator>alecf</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.flett.org/?p=42</guid>
		<description><![CDATA[Man I love Python. I came up with a neat trick yesterday that also couldn&#8217;t be done in any static language. Needless to say, I&#8217;m pretty pleased with myself. This trick isn&#8217;t slow or hard to understand, and actually makes a lot of my code very simple, and avoids a lot of boilerplate that I [...]]]></description>
			<content:encoded><![CDATA[<p>Man I love Python. I came up with a neat trick yesterday that also couldn&#8217;t be done in any static language. Needless to say, I&#8217;m pretty pleased with myself. This trick isn&#8217;t slow or hard to understand, and actually makes a lot of my code very simple, and avoids a lot of boilerplate that I would have had to write in another language</p>
<p>I needed a way to given a basic color to a class, and then have easy access to various tints of that color for painting different aspects of an object. The tints are based on HSV, not RGB, but all the callers need to deal with RGB.</p>
<p>The solution: wrap the property() descriptor with my own descriptor.<br />
<span id="more-42"></span><br />
I start by defining my class</p>
<pre class="code">
class TintedColors(object):
    def __init__(self, color):
        """
        color is an RGB triple
        """
        self.color = color
        self.hue = rgb_to_hsv(*color)[0]
</pre>
<p>Now I just need to define the property wrapper:</p>
<pre class="code">
    def tintedColor(saturation, value=1.0):
        def getColor(self):
            hsv = (self.hue, saturation, value)
            return hsv_to_rgb(*hsv)
        return property(getColor)
</pre>
<p>Finally, I can define all my tints as class attributes:</p>
<pre class="code">
    gradientLeft = tintedColor(0.4)
    gradientRight = tintedColor(0.2)
    outlineColor = tintedColor(0.5)
    textColor = tintedColor(0.67, 0.6)
</pre>
<p>What is so amazing about this is that because property is a runtime thing, and because classes are evaluated, not declared, the actual value of &#8220;f.gradientLeft&#8221; will be a descriptor as generated by property(). Each descriptor will call a special version of getColor() defined for each and every tint. The dynamic nature of this might make it look like it should be slower than a simple property() call, but in fact it is just as fast because the specialized versions of getColor() are very simple, and in memory act as though they were declared as:</p>
<pre class="code">
def getColor(self):
    hsv = (self.hue, 0.4, 1.0)
    return hsv_to_rgb(*hsv)
</pre>
<p>which is pretty darn fast.</p>
<p>Lets look at a similar option in C++:</p>
<pre class="code">
class TintedColors {
private:
    color mColor;
    float mHue;
&nbsp;
    color GetSaturatedColor(float saturation, float value=1.0) {
        hsv = make_hsv(this.mHue, saturation, value);
        return hsv_to_rgb(hsv);
    }
&nbsp;

public:
    TintedColors(const color&#038; c) const {
        mColor = c;
        this.mHue = rgb_to_hsv(c).hue
    }
&nbsp;

    color&#038; GetGradientLeft() const {
        return GetSaturatedColor(0.4);
    }
&nbsp;
    color&#038; GetOutlineColor() const {
        return GetSaturatedColor(0.2);
    }
&nbsp;
    color&#038; GetTextColor() const {
        return GetSaturatedColor(0.67, 0.6);
    }
}
</pre>
<p>There we go. I&#8217;ve tried to follow &#8220;good C++&#8221; guidelines with respect to consts, references, and so forth. And there we have the same pattern in C++ but with way more work: the 4 boilerplate calls meant I had to type GetSaturatedColor() 4 times, and the verbosity allows you to loose what&#8217;s really different between these functions &#8211; the color saturation and value. </p>
<p>And that&#8217;s all assuming that I&#8217;ve got a &#8220;color&#8221; type (which I&#8217;d have to define seperately &#8211; python just uses the built-in triples for a small value like that) and that hsv_to_rgb is part of that color system.. that may seem like a small assumption. It may not be part of the language, but there are so many extra easy routines just built in to Python that you can&#8217;t just write that off.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flett.org/2005/04/26/cool-python-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How python can be fast</title>
		<link>http://www.flett.org/2005/04/14/how-python-can-be-fast/</link>
		<comments>http://www.flett.org/2005/04/14/how-python-can-be-fast/#comments</comments>
		<pubDate>Fri, 15 Apr 2005 01:02:29 +0000</pubDate>
		<dc:creator>alecf</dc:creator>
				<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.flett.org/?p=41</guid>
		<description><![CDATA[So now that I&#8217;ve been working with python for a good 3-4 months, I&#8217;m starting to understand why people like it so much.. a bit part of it is that its just so damn simple. Its so easy to write code that works very quickly. The syntax is very elegant and easy to read. I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>So now that I&#8217;ve been working with python for a good 3-4 months, I&#8217;m starting to understand why people like it so much.. a bit part of it is that its just so damn simple. Its so easy to write code that works very quickly. The syntax is very elegant and easy to read. I&#8217;ve come to actually prefer the indent-based code grouping over most other language&#8217;s braces.</p>
<p>But one thing that has been plaguing me is how a dynamiclanguage could actually be faster than a static language like C++ or Java. I have faith in the theory, but its just that: faith. Until today, I didn&#8217;t have any real world examples. I needed something that you simply can&#8217;t do in a static language without bending over backwards. Conversely, I needed something that required the dynamicism of a language.</p>
<p>I just picked up the <i><a href="http://aspn.activestate.com/ASPN/Cookbook/Python">Python Cookbook</a></i> the other day and I&#8217;ve found a very concrete example about how a dynamic language can be fast.<br />
<span id="more-41"></span><br />
The example is from the beginning of the chapter on sorting and searching. They discuss the idea of decorate-sort-undecorate as a pattern for doing fast searching.</p>
<p>What I particularly like is the &#8216;key&#8217; function that you can now pass to the sort method in Python 2.4. You can simply pass a method that retrieves the &#8216;key&#8217; to a sort. Then when the sort is performed, the keys are more or less cached, and a fast internal comparison can be used.</p>
<p>For example:</p>
<pre>
MyList = [(2,3,4), (5,3,4), (1,2,3)]
def get_key(v):
  return v[2]

MyList.sort(key=get_key)
</pre>
<p>Now compare this to a traditional language, which might allow you to pass in a sort callback:</p>
<pre>
int *MyList[] = [ [2,3,4], [5,3,4], [1,2,3] ]

int MyCompare(void* a, void* b) {
  key1 = ((int*)a)[2]
  key2 = ((int*)b)[2]
  return key1 - key2
}

qsort(MyList, MyCompare)
</pre>
<p>Type definitions aside, aside what we see here is that MyCompare is going to be called much more than get_key. In this simple case, obviously it doesn&#8217;t matter much, but if you&#8217;re sorting something big and retrieving/calculating the key is slow, you&#8217;re going to retrieve and calculate the key way more often in C than in Python.</p>
<p>The reason this works only in a dynamic language is that a dynamic comparison function (MyCompare) in C++ is still not as dynamic as the key-retrieval function. The key retrieval function is a function who takes an arbitrary object as a parameter and returns an arbitrary object. Then the sort routine needs to arbitrarily compare two objects. You just can&#8217;t do that in C without a whole lot of work.</p>
<p>[As an aside, I know yes you could whack the hell out of a sort routine in C to do all sorts of crazy casting but nothing would be as general as the Python case and your code would be hopelessly unmaintainable.]</p>
<p>In the end, I think Python wins this one. It even gets extra points for implementing a readable get_key in one line.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flett.org/2005/04/14/how-python-can-be-fast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chandler</title>
		<link>http://www.flett.org/2005/04/13/chandler/</link>
		<comments>http://www.flett.org/2005/04/13/chandler/#comments</comments>
		<pubDate>Wed, 13 Apr 2005 21:40:50 +0000</pubDate>
		<dc:creator>alecf</dc:creator>
				<category><![CDATA[work]]></category>

		<guid isPermaLink="false">http://www.flett.org/?p=40</guid>
		<description><![CDATA[So where have I been? On January 10th of this year, I started working at the Open Source Applications Foundation. (http://www.osafoundation.org/) Its been quite an experience, and maybe I&#8217;ll actually start posting about THAT since I don&#8217;t seem to post about anything else here.
]]></description>
			<content:encoded><![CDATA[<p>So where have I been? On January 10th of this year, I started working at the Open Source Applications Foundation. (http://www.osafoundation.org/) Its been quite an experience, and maybe I&#8217;ll actually start posting about THAT since I don&#8217;t seem to post about anything else here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flett.org/2005/04/13/chandler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
