<?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>Bryan Helmig &#187; Uncategorized</title>
	<atom:link href="http://bryanhelmig.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://bryanhelmig.com</link>
	<description>...does nerdy things.</description>
	<lastBuildDate>Thu, 29 Mar 2012 16:18:25 +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>Simple Finite State Machine Decorator for Python</title>
		<link>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/</link>
		<comments>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 16:18:25 +0000</pubDate>
		<dc:creator>Bryan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://bryanhelmig.com/?p=423</guid>
		<description><![CDATA[Working on a fun side project at HackComo, I needed a finite state machine. Well, this is a simple decorator for limiting when a method can be ran: def fsm&#40;target, whence='*', attr='state'&#41;: &#34;&#34;&#34; `target` -&#62; (str): REQUIRED should be the target state. &#160; `whence` -&#62; (list): '*' should be a *list* of states that you [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a fun side project at HackComo, I needed a finite state machine. Well, this is a simple decorator for limiting when a method can be ran:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> fsm<span style="color: black;">&#40;</span>target, whence=<span style="color: #483d8b;">'*'</span>, attr=<span style="color: #483d8b;">'state'</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    `target` -&gt; (str): REQUIRED
        should be the target state.
&nbsp;
    `whence` -&gt; (list): '*'
        should be a *list* of states that you can change from.
&nbsp;
    `attr` -&gt; (str): 'state'
        is the that of the attribute that represents state.
&nbsp;
&nbsp;
    Example:
&nbsp;
        class Car(object):
            state = 'off'
&nbsp;
            @fsm('on', whence=['off'])
            def turn_on(self):
                pass
&nbsp;
            @fsm('off', whence=['on'])
            def turn_off(self):
                pass
&nbsp;
        car = Car()
        # state is 'off'
&nbsp;
        car.turn_off()              # raise Exception
        car.turn_off(silent=True)   # fails silently
        # state is still 'off'
&nbsp;
        car.turn_on()
        # state is 'on
&nbsp;
    &quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> dec<span style="color: black;">&#40;</span>method<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">def</span> inner<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
            silent = kwargs.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'silent'</span>, <span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># check that whence == curent state</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> whence == <span style="color: #483d8b;">'*'</span>:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> whence:
                <span style="color: #ff7700;font-weight:bold;">pass</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> silent:
                    <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">Exception</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'You shall not pass!'</span><span style="color: black;">&#41;</span>  <span style="color: #808080; font-style: italic;">#LOTR</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
            result = method<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
            <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, attr, target<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># set state to target</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">return</span> result
        <span style="color: #ff7700;font-weight:bold;">return</span> inner
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> dec</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://bryanhelmig.com/simple-finite-state-machine-decorator-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

