Top-rated recipes tagged "programs"http://code.activestate.com/recipes/tags/programs/top/2013-03-07T07:33:09-08:00ActiveState Code RecipesDecorator for BindingConstants at compile time (Python)
2010-11-16T08:36:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/
<p style="color: grey">
Python
recipe 277940
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 9.
</p>
<p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p>
Spreadsheet (Python)
2004-12-03T09:13:43-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/355045-spreadsheet/
<p style="color: grey">
Python
recipe 355045
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Use eval() to drive spreadsheet style logic. The sleeper feature of Py2.4 is the ability to use any object with a mapping interface as the locals argument to eval().</p>
Linear equations solver in 3 lines (Python)
2005-01-31T22:06:30-08:00Maxim Krikunhttp://code.activestate.com/recipes/users/1085177/http://code.activestate.com/recipes/365013-linear-equations-solver-in-3-lines/
<p style="color: grey">
Python
recipe 365013
by <a href="/recipes/users/1085177/">Maxim Krikun</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Just a little bit of hack: a linear equations solver using eval and built-in complex numbers:</p>
<pre class="prettyprint"><code>>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0
</code></pre>
First Class Enums in Python (Python)
2005-05-09T16:53:44-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413486-first-class-enums-in-python/
<p style="color: grey">
Python
recipe 413486
by <a href="/recipes/users/2400454/">Zoran Isailovski</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 7.
</p>
<p>True immutable symbolic enumeration with qualified value access.</p>
Pythologic -- Prolog syntax in Python (Python)
2004-09-01T21:11:14-07:00Shai Bergerhttp://code.activestate.com/recipes/users/2014324/http://code.activestate.com/recipes/303057-pythologic-prolog-syntax-in-python/
<p style="color: grey">
Python
recipe 303057
by <a href="/recipes/users/2014324/">Shai Berger</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>Some of Python's powerful meta-programming features are used to enable writing Python functions which include Prolog-like statements. Such functions load a Prolog-like database. When coupled with a suitable inference engine for logic databases, this is a way to add logical programming -- the last unsupported major paradigm -- to Python. Start at the bottom of the code for an example of the enabled syntax.</p>
Decorator for BindingConstants at compile time (Python)
2009-09-15T00:34:37-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576904-decorator-for-bindingconstants-at-compile-time/
<p style="color: grey">
Python
recipe 576904
by <a href="/recipes/users/924636/">Gabriel Genellina</a>
(<a href="/recipes/tags/optimisation/">optimisation</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/programs/">programs</a>).
</p>
<p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p>
<p>This is only an update of <a href="http://code.activestate.com/recipes/277940/">Recipe 277940</a>, making it compatible with Python 3. All credit must go to the original author, Raymond Hettinger.</p>
List classes, methods and functions in a module (Python)
2008-10-22T08:15:26-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/553262-list-classes-methods-and-functions-in-a-module/
<p style="color: grey">
Python
recipe 553262
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 5.
</p>
<p>The recipe provides a method "describe" which takes a module as argument and describes classes, methods and functions in the module. The method/function description provides information on the function/method arguments using the inspect module.</p>
Regular expression for python string literals (Python)
2006-03-10T20:52:36-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/475109-regular-expression-for-python-string-literals/
<p style="color: grey">
Python
recipe 475109
by <a href="/recipes/users/2637812/">Edward Loper</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>A regular expression that matches Python string literals. Tripple-quoted, unicode, and raw strings are supported.</p>
neat notation for python closures (Python)
2006-03-07T04:53:21-08:00Maciej Obarskihttp://code.activestate.com/recipes/users/2559120/http://code.activestate.com/recipes/474122-neat-notation-for-python-closures/
<p style="color: grey">
Python
recipe 474122
by <a href="/recipes/users/2559120/">Maciej Obarski</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>Alternative notation for defining closures in python; It avoids packing closure variables into containers by assigning attributes to the inner function.</p>
Linear equations solver in 3 lines (Python)
2013-03-07T07:33:09-08:00Deepakhttp://code.activestate.com/recipes/users/4183429/http://code.activestate.com/recipes/578481-linear-equations-solver-in-3-lines/
<p style="color: grey">
Python
recipe 578481
by <a href="/recipes/users/4183429/">Deepak</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>Just a little bit of hack: a linear equations solver using eval and built-in complex numbers:</p>
<pre class="prettyprint"><code>>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0
</code></pre>
Pyscanlogger - Python Port scan detector (Python)
2010-03-17T07:27:15-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/576690-pyscanlogger-python-port-scan-detector/
<p style="color: grey">
Python
recipe 576690
by <a href="/recipes/users/760763/">Anand</a>
(<a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/programs/">programs</a>, <a href="/recipes/tags/security/">security</a>).
Revision 5.
</p>
<p>A pure Python program to detect network port scanning attacks. Currently logs different TCP port scans. Can run in the background like a daemon and log attacks to a log file.</p>
How __slots__ are implemented (Python)
2007-09-25T22:48:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/532903-how-__slots__-are-implemented/
<p style="color: grey">
Python
recipe 532903
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Pure python equivalent of the __slots__ implementation using descriptors and a metaclass.</p>
Extracting *.tar.gz files in Windows ... (Python)
2005-11-01T10:36:05-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/442503-extracting-targz-files-in-windows/
<p style="color: grey">
Python
recipe 442503
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/programs/">programs</a>).
</p>
<p>This recipe is meant to be used as a commmand line *.tar.gz file extractor. If it fails, then a usage note is given. It may be small, but it can be very useful for some people.</p>
Quick Test If Strings Are Identifiers (Python)
2005-05-08T10:42:59-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413487-quick-test-if-strings-are-identifiers/
<p style="color: grey">
Python
recipe 413487
by <a href="/recipes/users/2400454/">Zoran Isailovski</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>Sometimes you need strings to be true identifiers, for ex. to represent symbolic names. Smalltalk offers the type 'Symbol' for this purpose. In python, you need to test this explicitly. Here is a quick way.</p>
A Python-based descriptive statistical analysis tool. (Python)
2005-04-18T23:52:25-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/409413-a-python-based-descriptive-statistical-analysis-to/
<p style="color: grey">
Python
recipe 409413
by <a href="/recipes/users/1760491/">Chad J. Schroeder</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>A Python module implementing a class which can be used for computing
numerical statistics for a given data set.</p>
RunningCalc (Python)
2005-02-18T23:22:55-08:00Shannon -jj Behrenshttp://code.activestate.com/recipes/users/2269827/http://code.activestate.com/recipes/363786-runningcalc/
<p style="color: grey">
Python
recipe 363786
by <a href="/recipes/users/2269827/">Shannon -jj Behrens</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>This is a trivial calculator "shell" with a running total.
As trivial as it is, I find it to be more useful than a normal calculator when
doing my checkbook because of the ever-present running total.</p>
<p>Sed is to Vi as
RunningCalc is to Python</p>
<p>python ~/programming/python/hacks/RunningCalc.py
$ 0.0+50
$ 50.0-10
$ 40.0*0
$ 0.0+5
$ 5.0-2
$ 3.0</p>
A speed limited, or governed range (Python)
2004-12-15T02:54:25-08:00S Whttp://code.activestate.com/recipes/users/1759688/http://code.activestate.com/recipes/358603-a-speed-limited-or-governed-range/
<p style="color: grey">
Python
recipe 358603
by <a href="/recipes/users/1759688/">S W</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>This class implements a generator, which returns consecutive floats which are incremented by a speed * (time between calls).</p>
XML Directory Tree (Python)
2004-09-28T19:12:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/305313-xml-directory-tree/
<p style="color: grey">
Python
recipe 305313
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 4.
</p>
<p>Builds a directory tree in XML format. Demonstrates how to use xml.dom.minidom to rapidly construct nested XML with multiple element types and attributes. Serves as a model for building model complex documents.</p>
Function Decorators by Hacking Bytecodes (Python)
2004-06-18T10:57:33-07:00Sami Hangaslammihttp://code.activestate.com/recipes/users/118800/http://code.activestate.com/recipes/286147-function-decorators-by-hacking-bytecodes/
<p style="color: grey">
Python
recipe 286147
by <a href="/recipes/users/118800/">Sami Hangaslammi</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Wether or not PEP 318 makes it to Python 2.4, you can experiment with an alternative decorator syntax in Python 2.3 by hacking with bytecodes.</p>
Improve dictionary lookup performance (Python)
2003-05-06T09:36:03-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/198157-improve-dictionary-lookup-performance/
<p style="color: grey">
Python
recipe 198157
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>Reduce average dictionary lookup time by making the internal tables more sparse.</p>