<?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>Coster&#039;s tech corner</title>
	<atom:link href="http://blog.acoster.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.acoster.net</link>
	<description>My technical ramblings.</description>
	<lastBuildDate>Sat, 08 Dec 2012 23:42:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Nerdy Birthday Card</title>
		<link>http://blog.acoster.net/2012/12/08/nerdy-birthday-card/</link>
		<comments>http://blog.acoster.net/2012/12/08/nerdy-birthday-card/#comments</comments>
		<pubDate>Sat, 08 Dec 2012 23:42:02 +0000</pubDate>
		<dc:creator>Coster</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://blog.acoster.net/?p=138</guid>
		<description><![CDATA[That was the message that Raf and me done for one of our fellow programmer&#8217;s birthday card:]]></description>
				<content:encoded><![CDATA[<p>That was the message that Raf and me done for one of our fellow programmer&#8217;s birthday card:</p>
<div id="gist4242508" class="gist">
        <div class="gist-file">
          <div class="gist-data gist-syntax">
            



    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-bday-cpp-L1" rel="file-bday-cpp-L1">1</span>
            <span class="line-number" id="file-bday-cpp-L2" rel="file-bday-cpp-L2">2</span>
            <span class="line-number" id="file-bday-cpp-L3" rel="file-bday-cpp-L3">3</span>
            <span class="line-number" id="file-bday-cpp-L4" rel="file-bday-cpp-L4">4</span>
            <span class="line-number" id="file-bday-cpp-L5" rel="file-bday-cpp-L5">5</span>
            <span class="line-number" id="file-bday-cpp-L6" rel="file-bday-cpp-L6">6</span>
            <span class="line-number" id="file-bday-cpp-L7" rel="file-bday-cpp-L7">7</span>
            <span class="line-number" id="file-bday-cpp-L8" rel="file-bday-cpp-L8">8</span>
            <span class="line-number" id="file-bday-cpp-L9" rel="file-bday-cpp-L9">9</span>
            <span class="line-number" id="file-bday-cpp-L10" rel="file-bday-cpp-L10">10</span>
            <span class="line-number" id="file-bday-cpp-L11" rel="file-bday-cpp-L11">11</span>
            <span class="line-number" id="file-bday-cpp-L12" rel="file-bday-cpp-L12">12</span>
            <span class="line-number" id="file-bday-cpp-L13" rel="file-bday-cpp-L13">13</span>
            <span class="line-number" id="file-bday-cpp-L14" rel="file-bday-cpp-L14">14</span>
            <span class="line-number" id="file-bday-cpp-L15" rel="file-bday-cpp-L15">15</span>
            <span class="line-number" id="file-bday-cpp-L16" rel="file-bday-cpp-L16">16</span>
            <span class="line-number" id="file-bday-cpp-L17" rel="file-bday-cpp-L17">17</span>
            <span class="line-number" id="file-bday-cpp-L18" rel="file-bday-cpp-L18">18</span>
            <span class="line-number" id="file-bday-cpp-L19" rel="file-bday-cpp-L19">19</span>
            <span class="line-number" id="file-bday-cpp-L20" rel="file-bday-cpp-L20">20</span>
            <span class="line-number" id="file-bday-cpp-L21" rel="file-bday-cpp-L21">21</span>
            <span class="line-number" id="file-bday-cpp-L22" rel="file-bday-cpp-L22">22</span>
            <span class="line-number" id="file-bday-cpp-L23" rel="file-bday-cpp-L23">23</span>
            <span class="line-number" id="file-bday-cpp-L24" rel="file-bday-cpp-L24">24</span>
            <span class="line-number" id="file-bday-cpp-L25" rel="file-bday-cpp-L25">25</span>
            <span class="line-number" id="file-bday-cpp-L26" rel="file-bday-cpp-L26">26</span>
            <span class="line-number" id="file-bday-cpp-L27" rel="file-bday-cpp-L27">27</span>
            <span class="line-number" id="file-bday-cpp-L28" rel="file-bday-cpp-L28">28</span>
            <span class="line-number" id="file-bday-cpp-L29" rel="file-bday-cpp-L29">29</span>
            <span class="line-number" id="file-bday-cpp-L30" rel="file-bday-cpp-L30">30</span>
            <span class="line-number" id="file-bday-cpp-L31" rel="file-bday-cpp-L31">31</span>
          </td>
          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-bday-cpp-LC1"><span class="cp">#include &lt;string&gt;</span></div><div class="line" id="file-bday-cpp-LC2"><span class="cp">#include &lt;algorithm&gt;</span></div><div class="line" id="file-bday-cpp-LC3"><span class="cp">#include &lt;iostream&gt;</span></div><div class="line" id="file-bday-cpp-LC4">&nbsp;</div><div class="line" id="file-bday-cpp-LC5"><span class="cp">#define __A &quot;!zbei&quot;</span></div><div class="line" id="file-bday-cpp-LC6"><span class="cp">#define __B &quot;zqqbI&quot;</span></div><div class="line" id="file-bday-cpp-LC7"><span class="cp">#define __C &quot;\&quot;mmjX&quot;</span></div><div class="line" id="file-bday-cpp-LC8"><span class="cp">#define __D &quot;usjc!&quot;</span></div><div class="line" id="file-bday-cpp-LC9">&nbsp;</div><div class="line" id="file-bday-cpp-LC10"><span class="k">class</span> <span class="nc">App</span></div><div class="line" id="file-bday-cpp-LC11"><span class="p">{</span></div><div class="line" id="file-bday-cpp-LC12"><span class="k">public</span><span class="o">:</span></div><div class="line" id="file-bday-cpp-LC13">    <span class="n">App</span><span class="p">(</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*</span><span class="n">p</span><span class="p">)</span></div><div class="line" id="file-bday-cpp-LC14">        <span class="o">:</span> <span class="n">mStrMsg</span><span class="p">(</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*&gt;</span><span class="p">(</span><span class="n">p</span><span class="p">))</span></div><div class="line" id="file-bday-cpp-LC15">    <span class="p">{}</span></div><div class="line" id="file-bday-cpp-LC16">    </div><div class="line" id="file-bday-cpp-LC17">    <span class="o">~</span><span class="n">App</span><span class="p">()</span></div><div class="line" id="file-bday-cpp-LC18">    <span class="p">{</span></div><div class="line" id="file-bday-cpp-LC19">        <span class="k">auto</span> <span class="n">f</span> <span class="o">=</span> <span class="p">[](</span><span class="kt">char</span> <span class="n">c</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="kt">char</span> <span class="p">{</span> <span class="k">return</span> <span class="n">c</span> <span class="o">-</span><span class="mi">1</span><span class="p">;</span> <span class="p">};</span></div><div class="line" id="file-bday-cpp-LC20">        <span class="n">std</span><span class="o">::</span><span class="n">for_each</span><span class="p">(</span><span class="n">mStrMsg</span><span class="p">.</span><span class="n">rbegin</span><span class="p">(),</span> <span class="n">mStrMsg</span><span class="p">.</span><span class="n">rend</span><span class="p">(),</span> <span class="p">[</span><span class="o">&amp;</span><span class="p">](</span><span class="kt">char</span> <span class="n">c</span><span class="p">)</span> <span class="p">{</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">f</span><span class="p">(</span><span class="n">c</span><span class="p">);</span> <span class="p">});</span></div><div class="line" id="file-bday-cpp-LC21">        <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span></div><div class="line" id="file-bday-cpp-LC22">    <span class="p">}</span></div><div class="line" id="file-bday-cpp-LC23">    </div><div class="line" id="file-bday-cpp-LC24"><span class="k">private</span><span class="o">:</span></div><div class="line" id="file-bday-cpp-LC25">    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">mStrMsg</span><span class="p">;</span></div><div class="line" id="file-bday-cpp-LC26"><span class="p">};</span></div><div class="line" id="file-bday-cpp-LC27">&nbsp;</div><div class="line" id="file-bday-cpp-LC28"><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">char</span> <span class="o">**</span><span class="p">)</span></div><div class="line" id="file-bday-cpp-LC29"><span class="p">{</span></div><div class="line" id="file-bday-cpp-LC30">    <span class="n">App</span> <span class="n">a</span><span class="p">(</span><span class="k">static_cast</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">void</span> <span class="o">*&gt;</span><span class="p">(</span><span class="n">__C</span> <span class="n">__A</span> <span class="n">__D</span> <span class="n">__B</span><span class="p">));</span></div><div class="line" id="file-bday-cpp-LC31"><span class="p">}</span></div></pre>
          </td>
        </tr>
      </table>
    </div>

          </div>
          <div class="gist-meta">
            <a href="https://gist.github.com/acoster/4242508/raw/bday.cpp" style="float:right">view raw</a>
            <a href="https://gist.github.com/acoster/4242508#file-bday-cpp">bday.cpp</a>
            hosted with &#10084; by <a href="https://github.com">GitHub</a>
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://blog.acoster.net/2012/12/08/nerdy-birthday-card/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RFC 6238 (Time-Based One-Time Password) implementation in Python</title>
		<link>http://blog.acoster.net/2012/11/21/rfc-6238-time-based-one-time-password-implementation-in-python/</link>
		<comments>http://blog.acoster.net/2012/11/21/rfc-6238-time-based-one-time-password-implementation-in-python/#comments</comments>
		<pubDate>Wed, 21 Nov 2012 09:36:54 +0000</pubDate>
		<dc:creator>Coster</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[crypto]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://blog.acoster.net/?p=126</guid>
		<description><![CDATA[While working on one of my pet projects, I&#8217;ve decided to add support to Time-based One-Time Passwords (as per RFC 6238), which is based on HMAC-Based One-Time Passwords (RFC 4226). This is the same protocol used by Google Authenticator &#8211; in fact, you can use their iOS/Android application to generate passwords for services that use [...]]]></description>
				<content:encoded><![CDATA[<p>While working on one of my pet projects, I&#8217;ve decided to add support to Time-based One-Time Passwords (as per <a href="http://tools.ietf.org/html/rfc6238">RFC 6238</a>), which is based on HMAC-Based One-Time Passwords (<a href="http://tools.ietf.org/html/rfc4226">RFC 4226</a>).</p>
<p>This is the same protocol used by Google Authenticator &#8211; in fact, you can use their iOS/Android application to generate passwords for services that use this lib.</p>
<div id="gist4121786" class="gist">
        <div class="gist-file">
          <div class="gist-data gist-syntax">
            



    <div class="file-data">
      <table cellpadding="0" cellspacing="0" class="lines highlight">
        <tr>
          <td class="line-numbers">
            <span class="line-number" id="file-totp-py-L1" rel="file-totp-py-L1">1</span>
            <span class="line-number" id="file-totp-py-L2" rel="file-totp-py-L2">2</span>
            <span class="line-number" id="file-totp-py-L3" rel="file-totp-py-L3">3</span>
            <span class="line-number" id="file-totp-py-L4" rel="file-totp-py-L4">4</span>
            <span class="line-number" id="file-totp-py-L5" rel="file-totp-py-L5">5</span>
            <span class="line-number" id="file-totp-py-L6" rel="file-totp-py-L6">6</span>
            <span class="line-number" id="file-totp-py-L7" rel="file-totp-py-L7">7</span>
            <span class="line-number" id="file-totp-py-L8" rel="file-totp-py-L8">8</span>
            <span class="line-number" id="file-totp-py-L9" rel="file-totp-py-L9">9</span>
            <span class="line-number" id="file-totp-py-L10" rel="file-totp-py-L10">10</span>
            <span class="line-number" id="file-totp-py-L11" rel="file-totp-py-L11">11</span>
            <span class="line-number" id="file-totp-py-L12" rel="file-totp-py-L12">12</span>
            <span class="line-number" id="file-totp-py-L13" rel="file-totp-py-L13">13</span>
            <span class="line-number" id="file-totp-py-L14" rel="file-totp-py-L14">14</span>
            <span class="line-number" id="file-totp-py-L15" rel="file-totp-py-L15">15</span>
            <span class="line-number" id="file-totp-py-L16" rel="file-totp-py-L16">16</span>
            <span class="line-number" id="file-totp-py-L17" rel="file-totp-py-L17">17</span>
            <span class="line-number" id="file-totp-py-L18" rel="file-totp-py-L18">18</span>
            <span class="line-number" id="file-totp-py-L19" rel="file-totp-py-L19">19</span>
            <span class="line-number" id="file-totp-py-L20" rel="file-totp-py-L20">20</span>
            <span class="line-number" id="file-totp-py-L21" rel="file-totp-py-L21">21</span>
            <span class="line-number" id="file-totp-py-L22" rel="file-totp-py-L22">22</span>
            <span class="line-number" id="file-totp-py-L23" rel="file-totp-py-L23">23</span>
            <span class="line-number" id="file-totp-py-L24" rel="file-totp-py-L24">24</span>
            <span class="line-number" id="file-totp-py-L25" rel="file-totp-py-L25">25</span>
            <span class="line-number" id="file-totp-py-L26" rel="file-totp-py-L26">26</span>
            <span class="line-number" id="file-totp-py-L27" rel="file-totp-py-L27">27</span>
            <span class="line-number" id="file-totp-py-L28" rel="file-totp-py-L28">28</span>
            <span class="line-number" id="file-totp-py-L29" rel="file-totp-py-L29">29</span>
            <span class="line-number" id="file-totp-py-L30" rel="file-totp-py-L30">30</span>
            <span class="line-number" id="file-totp-py-L31" rel="file-totp-py-L31">31</span>
            <span class="line-number" id="file-totp-py-L32" rel="file-totp-py-L32">32</span>
            <span class="line-number" id="file-totp-py-L33" rel="file-totp-py-L33">33</span>
            <span class="line-number" id="file-totp-py-L34" rel="file-totp-py-L34">34</span>
            <span class="line-number" id="file-totp-py-L35" rel="file-totp-py-L35">35</span>
            <span class="line-number" id="file-totp-py-L36" rel="file-totp-py-L36">36</span>
            <span class="line-number" id="file-totp-py-L37" rel="file-totp-py-L37">37</span>
            <span class="line-number" id="file-totp-py-L38" rel="file-totp-py-L38">38</span>
            <span class="line-number" id="file-totp-py-L39" rel="file-totp-py-L39">39</span>
          </td>
          <td class="line-data">
            <pre class="line-pre"><div class="line" id="file-totp-py-LC1"><span class="c">#!/usr/bin/env python</span></div><div class="line" id="file-totp-py-LC2">&nbsp;</div><div class="line" id="file-totp-py-LC3"><span class="sd">&quot;&quot;&quot;Implementation of RFC 4226: HMAC-Based One-Time Password Algorithm (HTOP),</span></div><div class="line" id="file-totp-py-LC4"><span class="sd">and RFC 6238: Time-Based One-Time Password Algorithm (TOTP).</span></div><div class="line" id="file-totp-py-LC5"><span class="sd">&quot;&quot;&quot;</span></div><div class="line" id="file-totp-py-LC6">&nbsp;</div><div class="line" id="file-totp-py-LC7"><span class="n">__author__</span> <span class="o">=</span> <span class="s">&#39;acoster&#39;</span></div><div class="line" id="file-totp-py-LC8"><span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&#39;Copyright 2012, Alexandre Coster&#39;</span></div><div class="line" id="file-totp-py-LC9">&nbsp;</div><div class="line" id="file-totp-py-LC10"><span class="kn">import</span> <span class="nn">hmac</span></div><div class="line" id="file-totp-py-LC11"><span class="kn">import</span> <span class="nn">time</span></div><div class="line" id="file-totp-py-LC12"><span class="kn">import</span> <span class="nn">base64</span></div><div class="line" id="file-totp-py-LC13"><span class="kn">import</span> <span class="nn">struct</span></div><div class="line" id="file-totp-py-LC14"><span class="kn">import</span> <span class="nn">hashlib</span></div><div class="line" id="file-totp-py-LC15">&nbsp;</div><div class="line" id="file-totp-py-LC16"><span class="k">def</span> <span class="nf">get_hotp</span><span class="p">(</span><span class="n">secret</span><span class="p">,</span> <span class="n">counter</span><span class="p">):</span></div><div class="line" id="file-totp-py-LC17">    <span class="sd">&quot;&quot;&quot;Return the HMAC-Based One-Time Password for the the given secret (base32 encoded) and the counter.</span></div><div class="line" id="file-totp-py-LC18">&nbsp;</div><div class="line" id="file-totp-py-LC19"><span class="sd">    &gt;&gt;&gt; [get_hotp(&#39;GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&#39;, i) for i in xrange(10)]</span></div><div class="line" id="file-totp-py-LC20"><span class="sd">    [755224, 287082, 359152, 969429, 338314, 254676, 287922, 162583, 399871, 520489]</span></div><div class="line" id="file-totp-py-LC21"><span class="sd">    &quot;&quot;&quot;</span></div><div class="line" id="file-totp-py-LC22">    <span class="n">secret</span>  <span class="o">=</span> <span class="n">base64</span><span class="o">.</span><span class="n">b32decode</span><span class="p">(</span><span class="n">secret</span><span class="p">)</span></div><div class="line" id="file-totp-py-LC23">    <span class="n">counter</span> <span class="o">=</span> <span class="n">struct</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="s">&#39;&gt;Q&#39;</span><span class="p">,</span> <span class="n">counter</span><span class="p">)</span></div><div class="line" id="file-totp-py-LC24">&nbsp;</div><div class="line" id="file-totp-py-LC25">    <span class="nb">hash</span>   <span class="o">=</span> <span class="n">hmac</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">secret</span><span class="p">,</span> <span class="n">counter</span><span class="p">,</span> <span class="n">hashlib</span><span class="o">.</span><span class="n">sha1</span><span class="p">)</span><span class="o">.</span><span class="n">digest</span><span class="p">()</span></div><div class="line" id="file-totp-py-LC26">    <span class="n">offset</span> <span class="o">=</span> <span class="nb">ord</span><span class="p">(</span><span class="nb">hash</span><span class="p">[</span><span class="mi">19</span><span class="p">])</span> <span class="o">&amp;</span> <span class="mh">0xF</span></div><div class="line" id="file-totp-py-LC27">&nbsp;</div><div class="line" id="file-totp-py-LC28">    <span class="k">return</span> <span class="p">(</span><span class="n">struct</span><span class="o">.</span><span class="n">unpack</span><span class="p">(</span><span class="s">&quot;&gt;I&quot;</span><span class="p">,</span> <span class="nb">hash</span><span class="p">[</span><span class="n">offset</span><span class="p">:</span><span class="n">offset</span> <span class="o">+</span> <span class="mi">4</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&amp;</span> <span class="mh">0x7FFFFFFF</span><span class="p">)</span> <span class="o">%</span> <span class="mi">1000000</span></div><div class="line" id="file-totp-py-LC29">&nbsp;</div><div class="line" id="file-totp-py-LC30"><span class="k">def</span> <span class="nf">get_totp</span><span class="p">(</span><span class="n">secret</span><span class="p">):</span></div><div class="line" id="file-totp-py-LC31">    <span class="sd">&quot;&quot;&quot;Return the Time-Based One-Time Password for the current time, and the provided secret (base32 encoded)</span></div><div class="line" id="file-totp-py-LC32">&nbsp;</div><div class="line" id="file-totp-py-LC33"><span class="sd">       For obvious reasons, no unit-test is provided for this function.</span></div><div class="line" id="file-totp-py-LC34"><span class="sd">    &quot;&quot;&quot;</span></div><div class="line" id="file-totp-py-LC35">    <span class="k">return</span> <span class="n">get_hotp</span><span class="p">(</span><span class="n">secret</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">())</span> <span class="o">//</span> <span class="mi">30</span><span class="p">)</span></div><div class="line" id="file-totp-py-LC36">&nbsp;</div><div class="line" id="file-totp-py-LC37"><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span></div><div class="line" id="file-totp-py-LC38">    <span class="kn">import</span> <span class="nn">doctest</span></div><div class="line" id="file-totp-py-LC39">    <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span></div></pre>
          </td>
        </tr>
      </table>
    </div>

          </div>
          <div class="gist-meta">
            <a href="https://gist.github.com/acoster/4121786/raw/totp.py" style="float:right">view raw</a>
            <a href="https://gist.github.com/acoster/4121786#file-totp-py">totp.py</a>
            hosted with &#10084; by <a href="https://github.com">GitHub</a>
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://blog.acoster.net/2012/11/21/rfc-6238-time-based-one-time-password-implementation-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Factory in Python</title>
		<link>http://blog.acoster.net/2012/10/17/factory-in-python/</link>
		<comments>http://blog.acoster.net/2012/10/17/factory-in-python/#comments</comments>
		<pubDate>Wed, 17 Oct 2012 09:48:27 +0000</pubDate>
		<dc:creator>Coster</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[design patterns]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://coster.wait4.org/blog/?p=122</guid>
		<description><![CDATA[I&#8217;ve had to implement a factory for Python, which objects being created depending on a certain configuration object &#8211; and this is the generic solution I&#8217;ve reached to handle it: class FactoryConstructible&#40;object&#41;:&#160;&#160;&#160;&#160;def __init__&#40;self, build_info&#41;:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;self.build_info = build_info&#160;&#160;&#160;&#160;&#160;@classmethod&#160;&#160;&#160;&#160;def create&#40;cls, build_info, *args, **kwargs&#41;:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for c in cls.__subclasses__&#40;&#41;:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;if c.supports_config&#40;config&#41;:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return c&#40;config, *args, **kwargs&#41;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return None&#160;&#160;&#160;&#160;&#160;@classmethod&#160;&#160;&#160;&#160;def supports_config&#40;cls, config&#41;:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;raise NotImplementedError&#40;&#41; A simple example of [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve had to implement a factory for Python, which objects being created depending on a certain configuration object &#8211; and this is the generic solution I&#8217;ve reached to handle it:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Python"><div class="devcodeoverflow"><ol><li><span style="color: #ff7700;font-weight:bold;">class</span> FactoryConstructible<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&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: #66cc66;">,</span> build_info<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">self</span>.<span style="color: black;">build_info</span> <span style="color: #66cc66;">=</span> build_info</li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #66cc66;">@</span><span style="color: #008000;">classmethod</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">def</span> create<span style="color: black;">&#40;</span>cls<span style="color: #66cc66;">,</span> build_info<span style="color: #66cc66;">,</span> *args<span style="color: #66cc66;">,</span> **kwargs<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">for</span> c <span style="color: #ff7700;font-weight:bold;">in</span> cls.__subclasses__<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">if</span> c.<span style="color: black;">supports_config</span><span style="color: black;">&#40;</span>config<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> c<span style="color: black;">&#40;</span>config<span style="color: #66cc66;">,</span> *args<span style="color: #66cc66;">,</span> **kwargs<span style="color: black;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #66cc66;">@</span><span style="color: #008000;">classmethod</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">def</span> supports_config<span style="color: black;">&#40;</span>cls<span style="color: #66cc66;">,</span> config<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">NotImplementedError</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>A simple example of usage is shown below &#8211; naturally those classes are empty, but you can get the idea <img src='http://blog.acoster.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="Python"><div class="devcodeoverflow"><ol><li><span style="color: #ff7700;font-weight:bold;">class</span> Shape<span style="color: black;">&#40;</span>FactoryConstructible<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">pass</span></li><li>&nbsp;</li><li><span style="color: #ff7700;font-weight:bold;">class</span> Square<span style="color: black;">&#40;</span>Shape<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #66cc66;">@</span><span style="color: #008000;">classmethod</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">def</span> supports_config<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> config<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> config <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'Square'</span></li><li>&nbsp;</li><li><span style="color: #ff7700;font-weight:bold;">class</span> Circle<span style="color: black;">&#40;</span>Shape<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #66cc66;">@</span><span style="color: #008000;">classmethod</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">def</span> supports_config<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: #66cc66;">,</span> config<span style="color: black;">&#41;</span>:</li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #ff7700;font-weight:bold;">return</span> config <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'Circle'</span></li><li>&nbsp;</li><li>circle <span style="color: #66cc66;">=</span> Shape.<span style="color: black;">create</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Circle'</span><span style="color: black;">&#41;</span></li><li>square <span style="color: #66cc66;">=</span> Shape.<span style="color: black;">create</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Square'</span><span style="color: black;">&#41;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.acoster.net/2012/10/17/factory-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using std::function</title>
		<link>http://blog.acoster.net/2012/07/01/using-stdfunction/</link>
		<comments>http://blog.acoster.net/2012/07/01/using-stdfunction/#comments</comments>
		<pubDate>Sun, 01 Jul 2012 13:58:16 +0000</pubDate>
		<dc:creator>Coster</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C++11]]></category>

		<guid isPermaLink="false">http://coster.wait4.org/blog/?p=64</guid>
		<description><![CDATA[std::function is a new template added on C++11 that provides a versatile callable object wrapper: in a nutshell it can wrap around any kind of callable target, e.g., functions, non-static methods, objects with the call operator, and, naturally, lambda functions (which are either a function or an object with the () operator, anyway).&#160; Declaring and [...]]]></description>
				<content:encoded><![CDATA[<p>std::function is a new template added on C++11 that provides a versatile callable object wrapper: in a nutshell it can wrap around any kind of callable target, e.g., functions, non-static methods, objects with the call operator, and, naturally, lambda functions (which are either a function or an object with the () operator, anyway).&nbsp;</p>
<p>Declaring and using a wrapper is quite simple:</p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="C++"><div class="devcodeoverflow"><ol><li><span style="color: #339900;">#include &lt;math.h&gt;</span></li><li><span style="color: #339900;">#include &lt;iostream&gt;</span></li><li><span style="color: #339900;">#include &lt;functional&gt;</span></li><li>&nbsp;</li><li><span style="color: #0000ff;">struct</span> SExample</li><li><span style="color: #008000;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">double</span> operator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> a, <span style="color: #0000ff;">double</span> b<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> a <span style="color: #000040;">-</span> b<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span></li><li><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></li><li>&nbsp;</li><li><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span> </li><li><span style="color: #008000;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">double</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span>, <span style="color: #0000ff;">double</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> fn<span style="color: #008080;">;</span> </li><li>&nbsp;&nbsp;&nbsp;&nbsp;SExample obj<span style="color: #008080;">;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span><span style="color: #0000dd;">pow</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span> <span style="color: #666666;">// outputs 256</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn <span style="color: #000080;">=</span> obj<span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span> <span style="color: #666666;">// outputs 0</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">double</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">650</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span><span style="color: #000040;">&amp;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">double</span> a, <span style="color: #0000ff;">double</span> b<span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span><span style="color: #000080;">&gt;</span> <span style="color: #0000ff;">double</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000ff;">return</span> x <span style="color: #000040;">+</span> a <span style="color: #000040;">*</span> b<span style="color: #008080;">;</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> fn<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span>, <span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span> <span style="color: #666666;">// outputs 666</span></li><li><span style="color: #008000;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>As we can see, they behave quite closely to function pointers, but with a neater syntax and the advantage of accepting pretty much anything.</p>
<p>One interesting feature is that if the first parameter&#8217;s type is a class/struct, &nbsp;the wrapper can be bound to a method of it, &nbsp;so std::function<bool(sexample std::string=""> will accept either functions/fuctors that match this signature, or pointers to a method of SExample that received a reference to a std::string, and returns a boolean, as we can see on the example below.</bool(sexample></p>
<p><!--DEVFMTCODE--><pre class="devcodeblock" title="C++"><div class="devcodeoverflow"><ol><li><span style="color: #339900;">#include &lt;math.h&gt;</span></li><li><span style="color: #339900;">#include &lt;iostream&gt;</span></li><li><span style="color: #339900;">#include &lt;functional&gt;</span></li><li>&nbsp;</li><li><span style="color: #0000ff;">struct</span> SExample</li><li><span style="color: #008000;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">bool</span> HandleCommand<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000040;">&amp;</span> _rCommand<span style="color: #008000;">&#41;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">&#123;</span>&nbsp;&nbsp; </li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;SExample::HandleCommand: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> _rCommand <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #008000;">&#125;</span>&nbsp;&nbsp; </li><li><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></li><li>&nbsp;</li><li><span style="color: #0000ff;">bool</span> HandleCommand<span style="color: #008000;">&#40;</span>SExample <span style="color: #000040;">&amp;</span>, <span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> <span style="color: #000040;">&amp;</span>_rCommand<span style="color: #008000;">&#41;</span></li><li><span style="color: #008000;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;::HandleCommand &quot;</span> <span style="color: #000080;">&lt;&lt;</span> _rCommand <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span></li><li><span style="color: #008000;">&#125;</span></li><li>&nbsp;</li><li><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span>, <span style="color: #0000ff;">char</span> <span style="color: #000040;">**</span><span style="color: #008000;">&#41;</span> </li><li><span style="color: #008000;">&#123;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;std<span style="color: #008080;">::</span><span style="color: #007788;">function</span><span style="color: #000080;">&lt;</span><span style="color: #0000ff;">bool</span><span style="color: #008000;">&#40;</span>SExample <span style="color: #000040;">&amp;</span>, <span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">string</span> <span style="color: #000040;">&amp;</span><span style="color: #008000;">&#41;</span><span style="color: #000080;">&gt;</span> fn<span style="color: #008080;">;</span> </li><li>&nbsp;&nbsp;&nbsp;&nbsp;SExample obj<span style="color: #008080;">;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>HandleCommand<span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn<span style="color: #008000;">&#40;</span>obj, <span style="color: #FF0000;">&quot;rehash&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>nbsp<span style="color: #008080;">;</span><span style="color: #666666;">// outputs &quot;::HandleCommand rehash&quot;</span></li><li>&nbsp;</li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn <span style="color: #000080;">=</span> <span style="color: #000040;">&amp;</span>SExample<span style="color: #008080;">::</span><span style="color: #007788;">HandleCommand</span><span style="color: #008080;">;</span></li><li>&nbsp;&nbsp;&nbsp;&nbsp;fn<span style="color: #008000;">&#40;</span>obj, <span style="color: #FF0000;">&quot;rehash&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// outputs &quot;SExample::HandleCommand: rehash&quot;</span></li><li><span style="color: #008000;">&#125;</span></li></ol></div></pre><!--END_DEVFMTCODE--></p>
<p>Internally this is achieved by some template trickery, where the function&#8217;s first parameter&#8217;s type is separated from the other arguments, so the assignment operator can accept a member function. Naturally this doesn&#8217;t apply to static methods, which are functions with slightly different scoping/access rules.</p>
<p>You can check if an object is bound to a function by checking comparing it to NULL/nullptr, or simply using it as a boolean (it has an implicit cast operator defined).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.acoster.net/2012/07/01/using-stdfunction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
