Popular recipes tagged "meta:loc=111"http://code.activestate.com/recipes/tags/meta:loc=111/2014-12-03T09:55:06-08:00ActiveState Code RecipesEasy attribute setting and pretty representation (Python)
2014-12-03T09:55:06-08:00Joakim Petterssonhttp://code.activestate.com/recipes/users/4174760/http://code.activestate.com/recipes/578973-easy-attribute-setting-and-pretty-representation/
<p style="color: grey">
Python
recipe 578973
by <a href="/recipes/users/4174760/">Joakim Pettersson</a>
(<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/traits/">traits</a>).
Revision 2.
</p>
<p>Mix in one or more of these classes to avoid those tedious lines of administrative code for setting attributes and getting useful representations. </p>
<p>If you inherit HasInitableAttributes, your should be able to obj = eval(repr(obj)) without loosing data.</p>
<p>enthought.traits.api.HasTraits seems to mix in well also.</p>
Tri-nary Tree: insertion and deletion (C++)
2014-02-07T04:21:50-08:00Chenhttp://code.activestate.com/recipes/users/4189170/http://code.activestate.com/recipes/578825-tri-nary-tree-insertion-and-deletion/
<p style="color: grey">
C++
recipe 578825
by <a href="/recipes/users/4189170/">Chen</a>
(<a href="/recipes/tags/tree/">tree</a>).
</p>
<p>This contains the insertion and deletion implement for the tri-nary tree data structure.</p>
Bloom Filter (Python)
2011-06-04T17:44:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577684-bloom-filter/
<p style="color: grey">
Python
recipe 577684
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>).
Revision 18.
</p>
<p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p>
Restricted dictionary (Python)
2012-02-16T21:39:02-08:00arnoqueshttp://code.activestate.com/recipes/users/4180947/http://code.activestate.com/recipes/578042-restricted-dictionary/
<p style="color: grey">
Python
recipe 578042
by <a href="/recipes/users/4180947/">arnoques</a>
(<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/record/">record</a>).
</p>
<p>It's a dictionary that's restricted to a tuple of allowed keys. Any attempt to set an invalid key raises an error.</p>
Yet Another Python Generator... (Python)
2011-12-19T08:14:55-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577990-yet-another-python-generator/
<p style="color: grey">
Python
recipe 577990
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/sweep/">sweep</a>).
</p>
<p>Aha, but not what big guns were expecting...</p>
<p>LF Audio Sweep Generator.</p>
<p>Another kids level project to do for yourselves...</p>
<p>This is a Python DEMO to show the power of the sound card using Linux to
generate an Audio Sweep Signal from 4KHz down to 100Hz and back again.</p>
<p>Written in such a way that anyone can understand how it works...
This is for Linux and Python 2.x.x. Read the code for much more information, and......
A Python 3.x.x version is here:-</p>
<p><a href="http://www.linuxformat.com/forums/viewtopic.php?t=14411" rel="nofollow">http://www.linuxformat.com/forums/viewtopic.php?t=14411</a></p>
<p>Enjoy finding simple solutions to often VERY difficult problems...</p>
<p>Bazza, G0LCU...</p>
Calculate the MRO of a class (Python)
2011-06-11T08:31:09-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/
<p style="color: grey">
Python
recipe 577748
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/c3/">c3</a>, <a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/mro/">mro</a>, <a href="/recipes/tags/order/">order</a>, <a href="/recipes/tags/resolution/">resolution</a>).
</p>
<p>This function allows you to calculate the Method Resolution Order (MRO, or sometimes linearization) of a class or base classes. This is the so-called "C3" algorithm, as used by Python (new-style classes, from version 2.3 and higher). The MRO is the order of base classes that Python uses to search for methods and attributes. For single inheritance, the MRO is obvious and straight-forward and not very exciting, but for multiple inheritance it's not always obvious what the MRO should be.</p>
Bloom Filter (Python)
2011-08-08T00:14:35-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577829-bloom-filter/
<p style="color: grey">
Python
recipe 577829
by <a href="/recipes/users/4178890/">sivarama sarma</a>
(<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>).
</p>
<p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p>
A script evaluating PSNR metric of two YUV420 frames (Python)
2011-04-08T07:26:45-07:00Denis Gorodetskiyhttp://code.activestate.com/recipes/users/4177587/http://code.activestate.com/recipes/577645-a-script-evaluating-psnr-metric-of-two-yuv420-fram/
<p style="color: grey">
Python
recipe 577645
by <a href="/recipes/users/4177587/">Denis Gorodetskiy</a>
(<a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/difference/">difference</a>, <a href="/recipes/tags/frame/">frame</a>, <a href="/recipes/tags/metric/">metric</a>, <a href="/recipes/tags/psnr/">psnr</a>, <a href="/recipes/tags/video/">video</a>, <a href="/recipes/tags/yuv/">yuv</a>).
Revision 2.
</p>
<p>A script evaluating PSNR metric of two YUV420 frames
<strong>usage: psnr.py filename1.yuv filename2.yuv frame_width frame_height</strong></p>
<p>Alternatively if filename1 contains width and height in the form <strong>file-1200x1600.yuv</strong>,
the script will extract width and height of a frame from the filename. Then usage even simplier:
<strong>psnr.py filename1-1200x1600.yuv filename2.yuv</strong></p>
Sphere (Python)
2010-05-06T12:28:12-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577218-sphere/
<p style="color: grey">
Python
recipe 577218
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
Revision 3.
</p>
<p>A sphere is a ball, similar to a circle in three dimension, and all its surface’s points are equidistant from the centre.</p>
<p>However, the distance from the centre point to the surface is called the radius, and the equation of the sphere is : (x-a)² + (y-b)² + (z-c)² = r² .</p>
<p>(a,b,c) are the coordinates of the centre and r is the radius.</p>
Available disk space monitoring for concurrent writes (Python)
2010-03-25T01:10:14-07:00Mateyuzohttp://code.activestate.com/recipes/users/4172453/http://code.activestate.com/recipes/577140-available-disk-space-monitoring-for-concurrent-wri/
<p style="color: grey">
Python
recipe 577140
by <a href="/recipes/users/4172453/">Mateyuzo</a>
(<a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/stat/">stat</a>, <a href="/recipes/tags/writing/">writing</a>).
Revision 5.
</p>
<p>A DiskSpaceProctor is registered with a SyncManager to be shared by forked processes and threads. This object will check the available free space and add up filesz of concurrent writes. If there is not enough space for the requesting write, an exception is raised.</p>
EXIF-date-based JPEG files rename using PIL (Python)
2009-02-10T07:28:48-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576646-exif-date-based-jpeg-files-rename-using-pil/
<p style="color: grey">
Python
recipe 576646
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/exif/">exif</a>, <a href="/recipes/tags/jfif/">jfif</a>, <a href="/recipes/tags/jpeg/">jpeg</a>, <a href="/recipes/tags/jpg/">jpg</a>, <a href="/recipes/tags/photo/">photo</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/rename/">rename</a>).
Revision 4.
</p>
<p>Rename JPEG files according to EXIF-date using PIL [library].</p>
<p>If global variable CREATE_HARDLINK is set, script creates Windows (XP) batch file
for creating hardlink version of source files.</p>
<p>PIL available here: <a href="http://www.pythonware.com/products/pil/" rel="nofollow">http://www.pythonware.com/products/pil/</a></p>
SyncFiles (Python)
2008-12-13T21:28:57-08:00Collin Stockshttp://code.activestate.com/recipes/users/4149235/http://code.activestate.com/recipes/576584-syncfiles/
<p style="color: grey">
Python
recipe 576584
by <a href="/recipes/users/4149235/">Collin Stocks</a>
(<a href="/recipes/tags/backup/">backup</a>, <a href="/recipes/tags/consistency/">consistency</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/gnu/">gnu</a>, <a href="/recipes/tags/gpl/">gpl</a>, <a href="/recipes/tags/just_in_case/">just_in_case</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/state_machine/">state_machine</a>, <a href="/recipes/tags/storage/">storage</a>, <a href="/recipes/tags/sync/">sync</a>, <a href="/recipes/tags/synchronize/">synchronize</a>).
Revision 3.
</p>
<p>This module allows you to handle and protect from inconsistencies sets of files that must all be saved at the same time. It does this be creating backups and temporary files. It has been moderately tested for bugs.</p>
Handling ANSI Terminal. (Python)
2010-02-09T05:51:23-08:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/574451-handling-ansi-terminal/
<p style="color: grey">
Python
recipe 574451
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/ansi/">ansi</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/terminal/">terminal</a>).
Revision 9.
</p>
<p>This function can be used, with or instead of print, to get <a href="http://en.wikipedia.org/wiki/ANSI_escape_code">ANSI</a> output.</p>
Paint 2.0 (Python)
2007-03-28T15:30:34-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/511436-paint-20/
<p style="color: grey">
Python
recipe 511436
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>This program was adapted from the SAMPLES directory
that comes with Python. It allows two people to draw
on a program with someone else watching and, at the
same time watch what the other person is drawing also.</p>
Separating Pattern Implementation from Your Code (Python)
2006-08-26T07:11:20-07:00Jinghong Chenhttp://code.activestate.com/recipes/users/2985210/http://code.activestate.com/recipes/496993-separating-pattern-implementation-from-your-code/
<p style="color: grey">
Python
recipe 496993
by <a href="/recipes/users/2985210/">Jinghong Chen</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Separate pattern implementation from your code so that you can reuse the implementation elsewhere.</p>
<p>The following code is an example that shows a reusable implementation of the Observer pattern of the GoF book.</p>
Install MQ Connection Factory. (Tcl)
2005-10-18T22:50:16-07:00Patrick Finneganhttp://code.activestate.com/recipes/users/1220635/http://code.activestate.com/recipes/442453-install-mq-connection-factory/
<p style="color: grey">
Tcl
recipe 442453
by <a href="/recipes/users/1220635/">Patrick Finnegan</a>
(<a href="/recipes/tags/websphere/">websphere</a>).
</p>
<p>Install MQ Connection Factory at node level.</p>
Integrating Twisted reactor with IPython (Python)
2005-04-22T01:26:20-07:00Matthew Scotthttp://code.activestate.com/recipes/users/2422085/http://code.activestate.com/recipes/410670-integrating-twisted-reactor-with-ipython/
<p style="color: grey">
Python
recipe 410670
by <a href="/recipes/users/2422085/">Matthew Scott</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Runs the Twisted reactor event loop in a thread alongside an IPython shell, for introspecting a running Twisted process.</p>
Find duplicate file names (Python)
2005-01-28T11:12:48-08:00Artur de Sousa Rochahttp://code.activestate.com/recipes/users/343771/http://code.activestate.com/recipes/364953-find-duplicate-file-names/
<p style="color: grey">
Python
recipe 364953
by <a href="/recipes/users/343771/">Artur de Sousa Rocha</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>This script looks for files with identical file names. If requested, file sizes are also compared. You can search current directory or a list of directories specified on the command line. Search can be restricted to files with names containing a string, or with names matching a regular expression. Paths to duplicates are printed in groups separated by empty lines.</p>
Table objects with operations in SQL-like syntax. (Python)
2004-09-02T13:21:24-07:00Uwe Schmitthttp://code.activestate.com/recipes/users/159298/http://code.activestate.com/recipes/286192-table-objects-with-operations-in-sql-like-syntax/
<p style="color: grey">
Python
recipe 286192
by <a href="/recipes/users/159298/">Uwe Schmitt</a>
.
Revision 5.
</p>
<p>This recipe implements a simple Table class which allows searching in a syntax similar to SQLs WHERE statement. So if t is Table, t.a == 3 finds all rows, where the column named "a" has the value 3. Intersection and unions of search results are possible too, for examples look at the __main__ part below.</p>
"Oneliner" for reverse/sort/extend strings/tuple/lists NOT in place (Python)
2002-04-07T03:51:55-07:00H. Krekelhttp://code.activestate.com/recipes/users/267664/http://code.activestate.com/recipes/119596-oneliner-for-reversesortextend-stringstuplelists-n/
<p style="color: grey">
Python
recipe 119596
by <a href="/recipes/users/267664/">H. Krekel</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>If you like obfuscated but powerful Oneliners you will really enjoy this one.
It allows to use reverse, sort and extent "on the fly" with tuples, strings or list types.
It returns a new instance of the same type as the input type. Extending a tuple results in
a new tuple. Sorting a string results in a new string and so on. Thanks to Patrick
Maupin since version 1.2 it handles Null-Strings and tuples correctly.</p>
<p>Though the functionality can be expressed in a 290-character "Oneliner" i recommend
that you use the longer 15-line code (at the end) as it is more understandable and "pythonic".</p>