Popular recipes by Mick Krippendorf http://code.activestate.com/recipes/users/4171813/2009-09-28T03:46:29-07:00ActiveState Code Recipessubclass defaultdict to tell default_factory which key is missing (Python)
2009-09-27T12:15:27-07:00Mick Krippendorfhttp://code.activestate.com/recipes/users/4171813/http://code.activestate.com/recipes/576913-subclass-defaultdict-to-tell-default_factory-which/
<p style="color: grey">
Python
recipe 576913
by <a href="/recipes/users/4171813/">Mick Krippendorf</a>
(<a href="/recipes/tags/defaultdict/">defaultdict</a>).
Revision 5.
</p>
<p>In a little Prolog interpreter I'm writing I needed a simple and concise way to create Var-objects and also store them in a mapping <em>varname</em> -> <em>Var-object</em> representing the scope of the current Prolog rule. Also, anonymous Prolog variables ("_") should not go into the mapping and should be renamed to <em>_<unique-number></em>. I came up with this:</p>
Automagically dispatch commands using regex token classes (Python)
2009-09-28T03:46:29-07:00Mick Krippendorfhttp://code.activestate.com/recipes/users/4171813/http://code.activestate.com/recipes/576914-automagically-dispatch-commands-using-regex-token-/
<p style="color: grey">
Python
recipe 576914
by <a href="/recipes/users/4171813/">Mick Krippendorf</a>
(<a href="/recipes/tags/command_dispatch_pattern/">command_dispatch_pattern</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regex/">regex</a>).
Revision 2.
</p>
<p>The <em>(?P<...>...)</em> notation in Python's regular expressions can be viewed as a classification of matched tokens. The names of these classes can be used to dispatch tokens to appropriate handlers:</p>