Popular recipes by Stephan Diehl http://code.activestate.com/recipes/users/774251/2003-12-03T12:05:01-08:00ActiveState Code RecipesFind the common beginning in a list of strings (Python)
2003-12-03T12:05:01-08:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/252177-find-the-common-beginning-in-a-list-of-strings/
<p style="color: grey">
Python
recipe 252177
by <a href="/recipes/users/774251/">Stephan Diehl</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>I came up with this when I tried to implement some autocompletion feature.
The problem is to find a common substring (beginning from the start) for all strings in a given list.</p>
<p>I couldn't find an existing recipe that is doing this task, but I'm wondering
if I just didn't look hard enough</p>
Quantum Superposition (Python)
2003-11-14T18:13:04-08:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/252125-quantum-superposition/
<p style="color: grey">
Python
recipe 252125
by <a href="/recipes/users/774251/">Stephan Diehl</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This lets treat you a python sets.Set as a scalar (number,string,...)
The german linux magazine featured an article about the PERL module
"Quantum::Superpositions": <a href="http://www.linux-magazin.de/Artikel/ausgabe/2003/12/perl/perl.html" rel="nofollow">http://www.linux-magazin.de/Artikel/ausgabe/2003/12/perl/perl.html</a>
This sounded like fun and I implemented it in Python (probably not everything).
Perl code lines: over 700
Python code lines: 100</p>
Changing return value for mutating list methods (meta programming) (Python)
2003-09-13T16:16:51-07:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/220234-changing-return-value-for-mutating-list-methods-me/
<p style="color: grey">
Python
recipe 220234
by <a href="/recipes/users/774251/">Stephan Diehl</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>Mutating list methods such as 'append' or 'extend' return None instead of the (mutated) list itself. Sometimes, this is not the desired behaviour.
The <a href="http://groups.google.de/groups?dq=&hl=de&lr=&ie=UTF-8&oe=UTF-8&threadm=I706b.17723%24hE5.626547%40news1.tin.it&prev=/groups%3Fdq%3D%26num%3D25%26hl%3Dde%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26group%3Dcomp.lang.python%26start%3D75"> Discussion</a> on comp.lang.python resulted in the following solution.
The shown code is my own, while two other solutions are presented in the discussion.</p>
Simple read only attributes with meta-class programming (Python)
2003-05-03T02:57:27-07:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/197965-simple-read-only-attributes-with-meta-class-progra/
<p style="color: grey">
Python
recipe 197965
by <a href="/recipes/users/774251/">Stephan Diehl</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This recipe is a rewrite of a portion of <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768" rel="nofollow">http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/157768</a> .
It shows an easy way to write read-only attributes with the help of meta classes.</p>
Wrapping method calls (meta-class example) (Python)
2003-05-04T15:49:40-07:00Stephan Diehlhttp://code.activestate.com/recipes/users/774251/http://code.activestate.com/recipes/198078-wrapping-method-calls-meta-class-example/
<p style="color: grey">
Python
recipe 198078
by <a href="/recipes/users/774251/">Stephan Diehl</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 4.
</p>
<p>A metaclass is used to wrap all (or just some) methods for logging purposes. The underlying mechanism can be used as well to check pre/post conditions, attribute access,...
The basic point is, that the actual class must not be changed in any way to achive the desired effect.</p>