<?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>Refactored scope &#187; hacks</title>
	<atom:link href="http://ominian.com/tag/hacks/feed/" rel="self" type="application/rss+xml" />
	<link>http://ominian.com</link>
	<description>PyMethius project notes</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:56:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>metaclass service bus decorator concept</title>
		<link>http://ominian.com/2011/08/25/metaclass-service-bus-decorator-concept/</link>
		<comments>http://ominian.com/2011/08/25/metaclass-service-bus-decorator-concept/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 01:28:57 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[metaclass]]></category>
		<category><![CDATA[oh_snap]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=140</guid>
		<description><![CDATA[While playing around with some idea&#8217;s for making PyProxy completely overridable ( and also potentially undebuggable ) I started playing around with metaclasses. Below is from my toxic directory [ gist url ] from collections import defaultdict from functools import wraps Just some preliminary basic utilities A simple bus implementation, decoratedCalls is a dictionary of <a href='http://ominian.com/2011/08/25/metaclass-service-bus-decorator-concept/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>While playing around with some idea&#8217;s for making PyProxy completely overridable ( and also potentially undebuggable ) I started playing around with metaclasses.</p>
<p>Below is from my toxic directory [ <a href="https://gist.github.com/1172478">gist url</a> ]</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">collections</span> <span style="color: #ff7700;font-weight:bold;">import</span> defaultdict
<span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> wraps</pre></div></div>

<p>Just some preliminary basic utilities</p>
<p>A simple bus implementation, decoratedCalls is a dictionary of service calls, with a list<br />
of callbacks to each service call hook.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">decoratedCalls = defaultdict<span style="color: black;">&#40;</span><span style="color: #008000;">list</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> call<span style="color: black;">&#40;</span>name, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> name, args, kwargs
    <span style="color: #ff7700;font-weight:bold;">if</span> name <span style="color: #ff7700;font-weight:bold;">in</span> decoratedCalls:
        <span style="color: #ff7700;font-weight:bold;">for</span> cb <span style="color: #ff7700;font-weight:bold;">in</span> decoratedCalls<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span>:
            cb<span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span></pre></div></div>

<p>Syntactic suger to add clarity later as to what functions are being bound to what.  Adding in<br />
debug/logging hooks here could allow for easier tracing of what is called for what methods.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Subscribe<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = name
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, f<span style="color: black;">&#41;</span>:
        decoratedCalls<span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> f</pre></div></div>

<p>Here&#8217;s the actual bus decorator, very simple just wraps the decorated function with a pre and post bus calls.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BusDecorator<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = name
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, f<span style="color: black;">&#41;</span>:
&nbsp;
        @wraps<span style="color: black;">&#40;</span>f<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">def</span> decorator<span style="color: black;">&#40;</span>inst, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
            call<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s-pre&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">name</span>, inst, args, kwargs<span style="color: black;">&#41;</span>            
            retval = f<span style="color: black;">&#40;</span>inst, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
            call<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s-post&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">self</span>.<span style="color: black;">name</span>, inst, retval<span style="color: black;">&#41;</span>            
            <span style="color: #ff7700;font-weight:bold;">return</span> retval
        <span style="color: #ff7700;font-weight:bold;">return</span> decorator</pre></div></div>

<p>And here&#8217;s my Bus Metaclass that combines most of the above.</p>
<p><em>The ease of wrapping the target class is accomplished by cdict which is a dictionary of<br />
every defined attribute of the target class.  As you can see it&#8217;s trivial to spin<br />
through and decorate every callable with the BusDecorator</em></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> BusWrap<span style="color: black;">&#40;</span><span style="color: #008000;">type</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__new__</span><span style="color: black;">&#40;</span>mcs, clsname, bases, cdict<span style="color: black;">&#41;</span>:
&nbsp;
        modName = cdict.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;__module__&quot;</span>, <span style="color: #483d8b;">&quot;unknownclass&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> cdict.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            prefix = <span style="color: #483d8b;">&quot;%s.%s.%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span> modName, clsname, name<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>cdict<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:                
                cdict<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span> = BusDecorator<span style="color: black;">&#40;</span>prefix<span style="color: black;">&#41;</span><span style="color: black;">&#40;</span>cdict<span style="color: black;">&#91;</span>name<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">type</span>.<span style="color: #0000cd;">__new__</span><span style="color: black;">&#40;</span>mcs, name, bases, cdict<span style="color: black;">&#41;</span></pre></div></div>

<p>Now give a dirt simple class like</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Foo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #0000cd;">__metaclass__</span> = BusWrap
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;init'd&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> bar<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;bar&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> blah<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;blah&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> ich<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ich&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> ego<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;lego&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> say<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Saying &quot;</span>, args</pre></div></div>

<p>And two service handlers to pre Foo.bar being called and after Foo.ego is called</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
@Subscribe<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;__main__.Foo.bar-pre&quot;</span><span style="color: black;">&#41;</span>        
<span style="color: #ff7700;font-weight:bold;">def</span> preBar<span style="color: black;">&#40;</span>inst, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>inst, <span style="color: #483d8b;">&quot;ext_info&quot;</span><span style="color: black;">&#41;</span>:
        inst.<span style="color: black;">ext_info</span> = <span style="color: #483d8b;">&quot;Here&quot;</span>
&nbsp;
@Subscribe<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;__main__.Foo.ego-post&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> postEgo<span style="color: black;">&#40;</span>inst, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>inst, <span style="color: #483d8b;">&quot;ext_info&quot;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Extended info is &quot;</span>, inst.<span style="color: black;">ext_info</span></pre></div></div>

<p>Our test shows&#8230;.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = Foo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
x.<span style="color: black;">bar</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
x.<span style="color: black;">blah</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
x.<span style="color: black;">ich</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
x.<span style="color: black;">ego</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
x.<span style="color: black;">say</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;abba&quot;</span>, <span style="color: #483d8b;">&quot;dabba&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>this as output</p>
<pre>
__main__.Foo.__init__-pre (<__main__.__init__ object at 0x02637890>, (), {}) {}
init'd
__main__.Foo.__init__-post (<__main__.__init__ object at 0x02637890>, None) {}
__main__.Foo.bar-pre (<__main__.__init__ object at 0x02637890>, (), {}) {}
bar
__main__.Foo.bar-post (<__main__.__init__ object at 0x02637890>, None) {}
__main__.Foo.blah-pre (<__main__.__init__ object at 0x02637890>, (), {}) {}
blah
__main__.Foo.blah-post (<__main__.__init__ object at 0x02637890>, None) {}
__main__.Foo.ich-pre (<__main__.__init__ object at 0x02637890>, (), {}) {}
ich
__main__.Foo.ich-post (<__main__.__init__ object at 0x02637890>, None) {}
__main__.Foo.ego-pre (<__main__.__init__ object at 0x02637890>, (), {}) {}
lego
__main__.Foo.ego-post (<__main__.__init__ object at 0x02637890>, None) {}
Extended info is  Here
__main__.Foo.say-pre (<__main__.__init__ object at 0x02637890>, ('abba', 'dabba'), {}) {}
Saying  ('abba', 'dabba')
__main__.Foo.say-post (<__main__.__init__ object at 0x02637890>, None) {}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/08/25/metaclass-service-bus-decorator-concept/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plugin&#8217;s for python</title>
		<link>http://ominian.com/2011/08/24/plugins-for-python/</link>
		<comments>http://ominian.com/2011/08/24/plugins-for-python/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 08:23:16 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=139</guid>
		<description><![CDATA[As the #1 google result for &#8220;python plugin&#8221;, the linked to blog post is extremely valuable for jump starting research into implementing your own plugin system ( like me ) or finding one that is viable for implementation in your project ( possibly like me ). These resources highlight one reason why there is not <a href='http://ominian.com/2011/08/24/plugins-for-python/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>As the #1 google result for &#8220;python plugin&#8221;, the linked to blog post is extremely valuable for jump starting research into implementing your own plugin system ( like me ) or finding one that is viable for implementation in your project ( possibly like me ).</p>
<blockquote><p>These resources highlight one reason why there is not a standard Python plug-in framework: there are a variety of different capabilities that a user may want, and the complexity of the framework generally increases as these new capabilities are added </p></blockquote>
<p><a href="http://wehart.blogspot.com/2009/01/python-plugin-frameworks.html">http://wehart.blogspot.com/2009/01/python-plugin-frameworks.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/08/24/plugins-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A friendlier asynchronous twisted web, the ghetto monkey patch way</title>
		<link>http://ominian.com/2011/07/27/a-friendlier-asynchronous-twisted-web-the-ghetto-monkey-patch-way/</link>
		<comments>http://ominian.com/2011/07/27/a-friendlier-asynchronous-twisted-web-the-ghetto-monkey-patch-way/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 02:11:26 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[oh_snap]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=135</guid>
		<description><![CDATA[UPDATE to the UPDATE &#8211; A cleaned up and more coherent example of txweb is here UPDATE &#8211; Github repo here I like twisted, and I like Cherrypy, unfortunately just like my militant atheist friends and my more spiritual friends neither seems to get along with the other. What to do? MONKEY PATCH + GHETTO <a href='http://ominian.com/2011/07/27/a-friendlier-asynchronous-twisted-web-the-ghetto-monkey-patch-way/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>UPDATE to the UPDATE &#8211; A cleaned up and more coherent example of txweb is<a href="http://ominian.com/2011/07/29/txweb-alpha-a-different-spin-on-twisted-web/"> here</a><br />
UPDATE &#8211; Github repo <a href="https://github.com/devdave/txWeb">here</a></p>
<p>I like twisted, and I like Cherrypy, unfortunately just like my militant atheist friends and my more spiritual friends neither seems to get along with the other.</p>
<p>What to do?  <strong>MONKEY PATCH</strong> + <strong>GHETTO HACKING</strong> to the rescue!</p>
<p>Note, this is just a mockup of CherryPy&#8217;s routing system and not a bridge or interface to CherryPy.  There is no CherryPy to be had here, just ghetto py.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">web</span> <span style="color: #ff7700;font-weight:bold;">import</span> server, <span style="color: #dc143c;">resource</span>
<span style="color: #ff7700;font-weight:bold;">from</span> twisted.<span style="color: black;">internet</span> <span style="color: #ff7700;font-weight:bold;">import</span> reactor
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> expose<span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span>:
    func.<span style="color: black;">exposed</span> = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> func
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> PageOne<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> foo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Hello From PageOne Foo!&quot;</span>        
    foo.<span style="color: black;">exposed</span> = <span style="color: #008000;">True</span>
&nbsp;
    @expose
    <span style="color: #ff7700;font-weight:bold;">def</span> delayed<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">def</span> delayedResponse<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            request.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;I was delayed :( &quot;</span><span style="color: black;">&#41;</span>
            request.<span style="color: black;">finish</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        reactor.<span style="color: black;">callLater</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>, delayedResponse<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> server.<span style="color: black;">NOT_DONE_YET</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> PageTwo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    @expose
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Hello From PageTwo index!&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Root<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
&nbsp;
    @expose
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Hello From Index!&quot;</span>
&nbsp;
    @expose
    <span style="color: #ff7700;font-weight:bold;">def</span> __default__<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;I Caught %s &quot;</span> <span style="color: #66cc66;">%</span> request.<span style="color: black;">path</span>
&nbsp;
    pageone = PageOne<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    pagetwo = PageTwo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> OneTimeResource<span style="color: black;">&#40;</span><span style="color: #dc143c;">resource</span>.<span style="color: black;">Resource</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
        Monkey patch to avoid rewriting more of twisted's lower web
        layer which does a fantastic job dealing with the minute details
        of receiving and sending HTTP traffic.
&nbsp;
        func is a callable and exposed property in the Root OO tree
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> render<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;">#Here would be a fantastic place for a pre-filter</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">func</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;">#ditto here for a post filter</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> OverrideSite<span style="color: black;">&#40;</span>server.<span style="color: black;">Site</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
        A monkey patch that short circuits the normal
        resource resolution logic @ the getResourceFor point
&nbsp;
    &quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> checkAction<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, controller, name<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
            On success, returns a bound method from the provided controller instance
            else it return None
        &quot;&quot;&quot;</span>
        action = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>controller, name<span style="color: black;">&#41;</span>:
                action = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>controller, name<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>action<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>action, <span style="color: #483d8b;">&quot;exposed&quot;</span><span style="color: black;">&#41;</span>:
                    action = <span style="color: #008000;">None</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> action
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> routeRequest<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        action = <span style="color: #008000;">None</span>
        response = <span style="color: #008000;">None</span>
&nbsp;
        root = parent = <span style="color: #008000;">self</span>.<span style="color: #dc143c;">resource</span>
        defaultAction = <span style="color: #008000;">self</span>.<span style="color: black;">checkAction</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;__default__&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        path = request.<span style="color: black;">path</span>.<span style="color: black;">strip</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            element = path<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>
&nbsp;
            parent = root
            root = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>root, element, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
            request.<span style="color: black;">prepath</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>element<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> root <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:                
                <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">checkAction</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;__default__&quot;</span><span style="color: black;">&#41;</span>:
                <span style="color: #808080; font-style: italic;">#Check for a catchall default action</span>
                defaultAction = <span style="color: #008000;">self</span>.<span style="color: black;">checkAction</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;__default__&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> element.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;_&quot;</span><span style="color: black;">&#41;</span>:
                <span style="color: #808080; font-style: italic;">#500 simplistic security check</span>
                action = <span style="color: #ff7700;font-weight:bold;">lambda</span> request: <span style="color: #483d8b;">&quot;500 URI segments cannot start with an underscore&quot;</span>
                <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">callable</span><span style="color: black;">&#40;</span>root<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;exposed&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> root.<span style="color: black;">exposed</span> == <span style="color: #008000;">True</span>:
                action = root
                request.<span style="color: black;">postpath</span> = path<span style="color: black;">&#91;</span>i:<span style="color: black;">&#93;</span> 
                <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> action <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> root <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">self</span>.<span style="color: black;">checkAction</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;index&quot;</span><span style="color: black;">&#41;</span>:
                    action = <span style="color: #008000;">self</span>.<span style="color: black;">checkAction</span><span style="color: black;">&#40;</span>root, <span style="color: #483d8b;">&quot;index&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #808080; font-style: italic;">#action = OneTimeResource(action) if action is not None else OneTimeResource(lambda request:&quot;500 Routing error :(&quot;)</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> action <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> defaultAction:
                action = defaultAction
            <span style="color: #ff7700;font-weight:bold;">else</span>:            
                action = <span style="color: #ff7700;font-weight:bold;">lambda</span> request:<span style="color: #483d8b;">&quot;404 :(&quot;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">return</span> OneTimeResource<span style="color: black;">&#40;</span>action<span style="color: black;">&#41;</span>         
&nbsp;
&nbsp;
&nbsp;
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> getResourceFor<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, request<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">routeRequest</span><span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #483d8b;">&quot;&quot;&quot;
    Twisted thankfully doesn't do any type checking, so a
    dumb OO graph is A-Okay here.  It will be assigned to
    site.resource
&quot;&quot;&quot;</span>
dumb = OverrideSite<span style="color: black;">&#40;</span>Root<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
reactor.<span style="color: black;">listenTCP</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">80</span>, dumb <span style="color: black;">&#41;</span>
reactor.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>   Slapped this together in about 30 minutes&#8230; so there is a HIGH probability that it is almost entirely edge cased!  Still it does work ( for me ) and it doesn&#8217;t hijack too much of twisted&#8217;s core, so it could be viable with a lot of unit-testing love, some additional sanity checking logics, and maybe some well thought out refactoring.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/07/27/a-friendlier-asynchronous-twisted-web-the-ghetto-monkey-patch-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The magic JS console thing</title>
		<link>http://ominian.com/2011/07/27/the-magic-js-console-thing/</link>
		<comments>http://ominian.com/2011/07/27/the-magic-js-console-thing/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 17:13:48 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[simpleCodeTricks]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=133</guid>
		<description><![CDATA[Source here &#8211; https://github.com/devdave/DevDave-s-miscellanious-bucket/tree/master/jsConsole Just wanted to see what would be involved in making a terminal like UI for the web. Honestly there&#8217;s a few REALLY annoying bits, but with a bit of hackery it was pretty straight forward. I&#8217;ve found splitting up the keyCode/which between key press and key down events was a lot <a href='http://ominian.com/2011/07/27/the-magic-js-console-thing/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Source here &#8211; <a href="https://github.com/devdave/DevDave-s-miscellanious-bucket/tree/master/jsConsole">https://github.com/devdave/DevDave-s-miscellanious-bucket/tree/master/jsConsole</a></p>
<p>Just wanted to see what would be involved in making a terminal like UI for the web.  Honestly there&#8217;s a few REALLY annoying bits, but with a bit of hackery it was pretty straight forward.  I&#8217;ve found splitting up the keyCode/which between key press and key down events was a lot simpler then making some sort of jury rigged code to character map when trying to compute shifted keys.</p>
<p>Also, I put in a brutally ugly left to right parser that does auto-complete / tab completion for good measure.  It&#8217;s got a very limited vocabulary but I could easily snap it into an asyncronous Ajax handler to bridge it with a much more powerful server side module.</p>
<p>Like all of my short term projects, no warranty or guarantee that it works is provided or implied.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/07/27/the-magic-js-console-thing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyProxy &#8211; Aka the development Helper proxy</title>
		<link>http://ominian.com/2011/07/01/pyproxy-aka-the-development-helper-proxy/</link>
		<comments>http://ominian.com/2011/07/01/pyproxy-aka-the-development-helper-proxy/#comments</comments>
		<pubDate>Fri, 01 Jul 2011 16:57:10 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[utilities]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=125</guid>
		<description><![CDATA[Coming out of nothing and into supah doopa Alpha is finally a working proof of concept of my python web proxy. I don&#8217;t really want to talk about the asinine alternatives I&#8217;ve tried until I finally said &#8220;fuck it, time to go completely twisted!&#8221; Low and behold the actual proxy part is 4 lines of <a href='http://ominian.com/2011/07/01/pyproxy-aka-the-development-helper-proxy/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Coming out of nothing and into <a href="http://www.youtube.com/watch?v=m6rqXHX3O48">supah doopa</a> Alpha is finally a working proof of concept of my python web proxy.  I don&#8217;t really want to talk about the asinine alternatives I&#8217;ve tried until I finally said &#8220;fuck it, time to go completely twisted!&#8221;   Low and behold the actual proxy part is 4 lines of code, which is then expanded to maybe 20-30 to allow for overloading some lower level classes.</p>
<p>Originally a public announcement for this project would have been in August at the earliest, give me time to clean things up and go from proof of concept to working concept but apparently a lot of other people have similar thoughts and I figured it&#8217;s better to collaborate then compete.</p>
<p>So some quick notes:<br />
The ultimate goal for PyProxy ( or whatever it ends up being named ) is to sit between a developer and a development server.  The first and immediate idea for this was to automagically parse out Python mechanize scripts to replicate the traffic.  These mechanize scripts could then be collected into a suite, marking other scripts as requirements ( example login  process ).  That alone would make it pretty easy to create full system under test unit-tests.   The next idea was to add in regex or pattern based hooks that could allow a developer to dial in to a specific domain, or even a specific set of webpages.</p>
<p>After that, the idea was to just continually tack on support plugins and scripts, maybe tell PyProxy the name of the target application&#8217;s database, and if it&#8217;s MySQL, switch on the general log.  This could allow for combining both mechanize scripts AND a SQLObject or SQLAlchemy powered unit-test suite to assert that the correct data was changed.</p>
<p>The final future idea was to make a Firefox/Chrome extension that would allow a developer to control some parts of the proxy from their browser and also see additional information.  For Python and PHP web apps, imagine have a finalization plugin that appended a response header listing all File&#8217;s used to perform a request&#8230;. then imagine having a &#8220;click to edit&#8221; button that, if the dev. instance is workstation local, would have your favorite IDE open the specified file for editing.</p>
<p>All in all, I think these are really subtle idea&#8217;s that if combined together, would cut down some mudane parts of developing a web app.</p>
<p>GitHub repo (https://github.com/devdave/PyProxy) <a href="https://github.com/devdave/PyProxy">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/07/01/pyproxy-aka-the-development-helper-proxy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A web-console implemented in nodeJS</title>
		<link>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/</link>
		<comments>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/#comments</comments>
		<pubDate>Tue, 17 May 2011 16:44:47 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[NodeJS]]></category>
		<category><![CDATA[hacks]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=107</guid>
		<description><![CDATA[Reading over this ( http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/ ) , I felt this was a pretty good and relatively real world example of nodeJS&#8217;s capabilities. I did find one line near the end very poignant and reflective of my own opinions at the end of the show &#038; tell post I don’t know if node.js is the future <a href='http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Reading over <a href="http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/">this</a> ( http://gonzalo123.wordpress.com/2011/05/16/web-console-with-node-js/ ) , I felt this was a pretty good and relatively real world example of nodeJS&#8217;s capabilities.  I did find one line near the end very poignant and reflective of my own opinions at the end of the show &#038; tell post </p>
<blockquote><p>I don’t know if node.js is the future or is just another hype, but it’s easy. And cool. Really cool.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/05/17/a-web-console-implemented-in-nodejs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canvas pixel collision detection</title>
		<link>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/</link>
		<comments>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 22:28:52 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[hacks]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=82</guid>
		<description><![CDATA[So&#8230; a fair number of people arrive here looking for how to detect when a pixel/object/thing has collided with something else. I&#8217;ll add more information in the future but in the interim, the quick and dirty advice I can provide: Given p = x, y where this is the point your trying to determine if <a href='http://ominian.com/2011/03/09/canvas-pixel-collision-detection/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>So&#8230; a fair number of people arrive here looking for how to detect when a pixel/object/thing has collided with something else.</p>
<p>I&#8217;ll add more information in the future but in the interim, the quick and dirty advice I can provide:</p>
<p>Given p = x, y where this is the point your trying to determine if it intersects another&#8230; the best place is to start with bounding box logic.  Regardless of what shape your object is, if wrap it in an invisible box boundry, it&#8217;s relatively trivial to do:</p>
<p>box upper left origin is bX, bY off size sizeX, sizeY.   If p.x greater then bX and p.x less then bX + sizeX AND p.y greater then bY and p.y less then bY + sizeY   you&#8217;re inside the bounding box and it&#8217;s highly likely your point has or will soon be colliding with an object.</p>
<p>That&#8217;s pretty much it and is perfect if you don&#8217;t mine lagging the crap out of whatever is running this logic.    To make these collision checks performant, you need to make the computer do less work.   For two dimensional space, my personnel research has led me to the Quad tree structure ( <a title="Quad Trees" href="http://en.wikipedia.org/wiki/Quadtree" target="_blank">wikipedia article</a> ).  I&#8217;ve got a brute force / naive implementation prototyped  @ <a href="https://github.com/devdave/Ping---canvas-helper-library/blob/master/mapping.js">here</a> &amp; <a href="https://github.com/devdave/Ping---canvas-helper-library/blob/master/mapping.html">here</a> but as of 2011 March 9th it is not finished and needs some more work.</p>
<p>Basically Quad tree&#8217;s provides a super quick way for you to determine if there is any other objects in relative space to an entity.  Doing a quick lookup, if there&#8217;s nothing nearby then you can skip the bounding box checks and then the stupidly expensive perimeter intersection checks.</p>
<p>Full library is <a href="https://github.com/devdave/Ping---canvas-helper-library">here</a> and like most of my pet projects is heavily undocumented and completely absent of unit-testing.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/09/canvas-pixel-collision-detection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Stellar simulator thingy</title>
		<link>http://ominian.com/2011/03/06/stellar-simulator-thingy/</link>
		<comments>http://ominian.com/2011/03/06/stellar-simulator-thingy/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 04:26:48 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[canvas]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[about]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[oh_snap]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=80</guid>
		<description><![CDATA[Inspired by this IF YOU CAN SEE THIS, YOUR BROWSER SUCKS! Source code @ Github here So&#8230; this basically covers elliptical paths ( which I want to play with more ), a rendering chain I&#8217;m happy with, and a few others things. What isn&#8217;t exactly right: speed, planetary/stellar surface animation is missing, and I&#8217;d like <a href='http://ominian.com/2011/03/06/stellar-simulator-thingy/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>Inspired by <a href="http://www.youtube.com/watch?v=JWVshkVF0SY" target="_blank">this</a><br />
<canvas id="myCan" width="450" height="350">IF YOU CAN SEE THIS, YOUR BROWSER SUCKS!</canvas><br />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script></p>
<p>        <script src="/examples/js/pinglib/libs/ping.main.js"></script><br />
        <script src="/examples/js/pinglib/libs/ping.shapes.js"></script><br />
        <script src="/examples/js/pinglib/libs/ping.input.js"></script><br />
        <script src="/examples/js/pinglib/libs/ping.canvas.js"></script><br />
<script src="/examples/js/pinglib/solar.js"></script></p>
<p>Source code @ Github <a href="https://github.com/devdave/Ping---canvas-helper-library" target="_blank">here</a></p>
<p>So&#8230; this basically covers elliptical paths ( which I want to play with more ), a rendering chain I&#8217;m happy with, and a few others things.  What isn&#8217;t exactly right:  speed, planetary/stellar surface animation is missing, and I&#8217;d like to have a fly by mechanism that shifts the Y ratio progressively over time.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2011/03/06/stellar-simulator-thingy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A DRY abomination</title>
		<link>http://ominian.com/2010/01/30/a-dry-abomination/</link>
		<comments>http://ominian.com/2010/01/30/a-dry-abomination/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 06:48:36 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hacks]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://ominian.com/?p=6</guid>
		<description><![CDATA[One of my habits when presented with a new framework/platform is to implement a problem I know like the back of my hand. Usually the user interface isn&#8217;t of concern so I haven&#8217;t done much to improve on the logic there. I decided to try something different with this implementation of Tic-Tac-Toe: Given the following <a href='http://ominian.com/2010/01/30/a-dry-abomination/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>   One of my habits when presented with a new framework/platform is to implement a problem I know like the back of my hand.   Usually the user interface isn&#8217;t of concern so I haven&#8217;t done much to improve on the logic there.   I decided to try something different with this implementation of Tic-Tac-Toe:</p>
<p>Given the following</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">table</span> <span style="color: #000066;">border</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;height:100%; width:100%&quot;</span> &gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x0&quot;</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y0&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y1&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y2&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x1&quot;</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y0&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y1&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y2&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">tr</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;x2&quot;</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y0&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y1&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
                <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">td</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;y2&quot;</span>&gt;</span><span style="color: #ddbb00;">&amp;nbsp;</span><span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">td</span>&gt;</span>
            <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">tr</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">table</span>&gt;</span></pre></div></div>

<p>I used the following logic to determine the coordinates from a user click</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> clickHandler <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">//Recieves a plan/jQuery managed event object                    </span>
       <span style="color: #003366; font-weight: bold;">var</span> element <span style="color: #339933;">=</span> e.<span style="color: #660066;">target</span><span style="color: #339933;">;</span>
       <span style="color: #003366; font-weight: bold;">var</span> parent  <span style="color: #339933;">=</span> element.<span style="color: #660066;">parentNode</span><span style="color: #339933;">;</span>                    
       <span style="color: #003366; font-weight: bold;">var</span> x<span style="color: #339933;">,</span>y<span style="color: #339933;">;</span>
       <span style="color: #006600; font-style: italic;">//Below is fine for 0-9 scenario's but will need to be refactored for more</span>
       <span style="color: #006600; font-style: italic;">//advanced/larger grids.</span>
       <span style="color: #000066; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            y <span style="color: #339933;">=</span> element.<span style="color: #660066;">classNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">detect</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/y\d/</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            x <span style="color: #339933;">=</span> parent.<span style="color: #660066;">classNames</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">detect</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #009966; font-style: italic;">/x\d/</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>cls<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
         console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Coordinate &quot;</span> <span style="color: #339933;">+</span> x <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>    The cool thing about this, is that with jQuery I can do jQuery(&#8220;#myTable tr.x2 td.y0&#8243;) and get the exact cell I am looking for&#8230; or even crazier stuff like:<br />
&#8220;#myTable td.y0&#8243; to select vertically down a row<br />
OR<br />
&#8220;#myTable tr.x0&#8243; to select horizontally.</p>
]]></content:encoded>
			<wfw:commentRss>http://ominian.com/2010/01/30/a-dry-abomination/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

