Popular recipes tagged "meta:loc=58"http://code.activestate.com/recipes/tags/meta:loc=58/2017-02-13T05:34:47-08:00ActiveState Code RecipesFinding the sizes of various Python data types (Python)
2016-04-28T18:28:59-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580655-finding-the-sizes-of-various-python-data-types/
<p style="color: grey">
Python
recipe 580655
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/type/">type</a>).
</p>
<p>This recipe shows how to find the sizes of various common data types in Python, both built-in and user-defined. It uses the sys.getsizeof() function and also discusses a few other points of interest.</p>
FreshFish (Python)
2013-12-29T15:38:33-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/578800-freshfish/
<p style="color: grey">
Python
recipe 578800
by <a href="/recipes/users/1523109/">Justin Shaw</a>
.
Revision 2.
</p>
<p>FreshFish is a function decorator for functions with no arguments (hardware query).</p>
<p>The first time the function is called, the wrapper caches the result (the fish). Subsequent calls to the wrapped function simply return the cached result as long as the fish is still fresh. When the fish goes stale, FreshFish calls the underlying function again for FreshFish.</p>
<p>I used this on a BeagleBone project to monitor heart rate, speed, and cadence.</p>
Wigle wifi geolocation [deprecated] (Python)
2017-02-13T05:34:47-08:00Jamie Bullhttp://code.activestate.com/recipes/users/4183059/http://code.activestate.com/recipes/578637-wigle-wifi-geolocation-deprecated/
<p style="color: grey">
Python
recipe 578637
by <a href="/recipes/users/4183059/">Jamie Bull</a>
(<a href="/recipes/tags/geocoding/">geocoding</a>, <a href="/recipes/tags/location/">location</a>, <a href="/recipes/tags/mac_address/">mac_address</a>, <a href="/recipes/tags/wigle/">wigle</a>).
Revision 5.
</p>
<p><strong>EDIT: WiGLE have retired this API and moved to api/v2</strong></p>
<p>There is now a Python wrapper for the API which you can install using <code>pip install pygle</code>.</p>
Canvas: static forms (JavaScript)
2013-08-08T13:34:41-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578626-canvas-static-forms/
<p style="color: grey">
JavaScript
recipe 578626
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/canvas/">canvas</a>).
Revision 2.
</p>
<p>Drawing static forms with canvas.</p>
Human readable file/memory sizes v2 (Python)
2012-11-11T17:28:57-08:00Tony Fluryhttp://code.activestate.com/recipes/users/4184150/http://code.activestate.com/recipes/578323-human-readable-filememory-sizes-v2/
<p style="color: grey">
Python
recipe 578323
by <a href="/recipes/users/4184150/">Tony Flury</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/size/">size</a>).
</p>
<p>In writing a application to display the file sizes of set of files, I wanted to provide a human readable size rather then just displaying a byte count (which can get rather big).</p>
<p>I developed this useful short recipe that extends the format specifier mini Language to add new presentation type s- which will intelligently convert the value to be displayed into a known human readable size format - i.e. b, Kb,Mb, Gb, B, KB etc. It honours the rest of the format specification language (<a href="http://docs.python.org/2/library/string.html#format-string-syntax%29" rel="nofollow">http://docs.python.org/2/library/string.html#format-string-syntax)</a></p>
<p>It uses a factor of 1024 for IEC and common formats, and factor of 1000 for SI units.</p>
Platform Independent 1KHz Pure Audio Sinewave Generator... (Python)
2012-10-23T12:53:37-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578301-platform-independent-1khz-pure-audio-sinewave-gene/
<p style="color: grey">
Python
recipe 578301
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</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/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/pyaudio/">pyaudio</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sinewave/">sinewave</a>, <a href="/recipes/tags/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 2.
</p>
<p>IKHz_SW_OSX.py</p>
<p>A mono _pure_ sinewave generator using standard text mode Python 2.6.7 to at least 2.7.3.</p>
<p>This DEMO kids level 1KHz generator is mainly for a MacBook Pro, (13 inch in my case),
OSX 10.7.5 and above. See below...</p>
<p>It is another simple piece of testgear for the young amateur electronics enthusiast and
uses pyaudio fully installed for it to work.</p>
<p>PyAudio can be obtained from here:- <a href="http://people.csail.mit.edu/hubert/pyaudio/" rel="nofollow">http://people.csail.mit.edu/hubert/pyaudio/</a></p>
<p>This was primarily for a MacBook Pro, but works on at least 2 Linux flavours and Windows Vista 32 bit...</p>
<p>The sinewave generated is near excellent...</p>
<p>Enjoy finding simple solutions to often very difficult problems... Bazza, G0LCU...</p>
reshape a sequence (Python)
2012-11-02T04:01:06-07:00Chris Smithhttp://code.activestate.com/recipes/users/2725752/http://code.activestate.com/recipes/578262-reshape-a-sequence/
<p style="color: grey">
Python
recipe 578262
by <a href="/recipes/users/2725752/">Chris Smith</a>
(<a href="/recipes/tags/group/">group</a>, <a href="/recipes/tags/reshape/">reshape</a>, <a href="/recipes/tags/sequence/">sequence</a>).
Revision 4.
</p>
<p>This function accepts a sequence and a template of how the sequence should be reshaped. </p>
Solve simultaneous linear equations in two variables (Python)
2012-01-24T14:20:01-08:00Anand B Pillaihttp://code.activestate.com/recipes/users/4169530/http://code.activestate.com/recipes/578024-solve-simultaneous-linear-equations-in-two-variabl/
<p style="color: grey">
Python
recipe 578024
by <a href="/recipes/users/4169530/">Anand B Pillai</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/solver/">solver</a>).
</p>
<p>A function to solve simultaneous equations in two variables. </p>
<pre class="prettyprint"><code>>>> solve('3*x + 5*y = 29; 12*x - 3*y = 24')
(3.0, 4.0)
</code></pre>
"execfile()" IS BACK! Backwards Compatibility part 5... (Python)
2011-11-04T18:17:48-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/577942-execfile-is-back-backwards-compatibility-part-5/
<p style="color: grey">
Python
recipe 577942
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/all_versions/">all_versions</a>, <a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/backwards_compatibility/">backwards_compatibility</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/windows/">windows</a>, <a href="/recipes/tags/winuae/">winuae</a>).
</p>
<p>"execfile()" IS BACK!!!</p>
<p>Some example lines to add to any test code you write that work from Python (1.4.0), 2.0.1 to 3.2.2
on various platforms.</p>
<p>It is all Public Domain and you can do with it as you please...</p>
<p>I really don't care how you vote and/or rate this as I try to write Python code for my
usage to work on the platforms and versions inside the code.</p>
<p>These are a little tongue in cheek so don't take them too seriously.</p>
<p>Enjoy finding simple solutions to often VERY difficult problems...</p>
<p>(Watch out for wordwrapping, etc...)</p>
<p>This will probably be the last one of these uploads as the last one did not "take off".</p>
<p>Bazza, G0LCU...</p>
Cache decorator in python 2.4 (Python)
2011-08-08T00:10:51-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577827-cache-decorator-in-python-24/
<p style="color: grey">
Python
recipe 577827
by <a href="/recipes/users/4178890/">sivarama sarma</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>The latest version of Python introduced a new language feature, function and method decorators (PEP 318, <a href="http://www.python.org/peps/pep-0318.html" rel="nofollow">http://www.python.org/peps/pep-0318.html</a>). This recipe show a common callable transformation that can benefit from the new syntax, often referred to as Memoization pattern.</p>
SEA Example (Python)
2011-07-11T21:51:52-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577785-sea-example/
<p style="color: grey">
Python
recipe 577785
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>).
</p>
<p>This is an example of the output from <a href="http://code.activestate.com/recipes/577485/">recipe 577485</a> after being formatted for 80 character line width. The file that it generated by the self extracting archiver is a simple PNG image that is only slightly larger than its raw version. By combining adaptive compression with base 64 encoding, the SEA can effectively turn a file into a Python program capable of recreating the original source. Please note that the last instruction in the generated program can only be run on Windows, and any suggestions on using a cross platform alternative would be greatly appreciated.</p>
Quiz Me 2.5 (splash) (Python)
2011-01-10T04:05:50-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577529-quiz-me-25-splash/
<p style="color: grey">
Python
recipe 577529
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quiz/">quiz</a>).
</p>
<p>Before discovering <a href="http://quizlet.com/" rel="nofollow">http://quizlet.com/</a>, the following program was developed for running custom quizzes to help with studying for college courses. The program is not very advanced, but it works reasonably well for what it was designed to do. If the program were developed further, it would need greater capabilities than it currently has and would require a secondary system for actually creating the quizzes (currently, they are hand-typed). Quiz Me could be a starting point for anyone who wishes to actually write a program such as this and inspire others to write much better programs than what this recipe currently offers.</p>
<p>Splash provides an easy mechanism for displaying a splash screen as the "Quiz Me" program is starting up. This recipe has been published elsewhere on the Python Cookbook, but is included here for completeness in the layout of the program. The class's constructor takes the root of the application, the path to the logo to be displayed (usually a GIF image), and a minimum amount of time for the splash screen to be displayed. Used as a context manager around initialization code makes this a very easy class to use, and it will automatically clean up after itself when it has finished doing its job.</p>
Maclaurin's_series_function(1/1-x²) (Python)
2010-07-07T12:44:17-07:00Fouad Teniouhttp://code.activestate.com/recipes/users/4155345/http://code.activestate.com/recipes/577292-maclaurins_series_function11-x2/
<p style="color: grey">
Python
recipe 577292
by <a href="/recipes/users/4155345/">Fouad Teniou</a>
(<a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>C. Maclaurin. A Scottish mathematician gained his master degree at age 17, and his major mathematics' work arise from his special knowledge in Newton's ideas and the formulation of Newton's methods.</p>
<p>However, C. Maclaurin also contributed to the astronomy science and helped to improve maps and invented some mechanical devices .</p>
<p>My mathematics python's programs is a set of Maclaurin's series to compute some of the most important functions in calculus.</p>
<p>Though, the computation of an infinite sum which give the value of a function in terms of the derivatives evaluated at a special case where x0 = 0,in contrast with Taylor series.</p>
<p>Maclaurin's series to approximate f(x) = (1/1-x^2)</p>
Windows TREE Emulator (Python)
2010-03-08T12:57:10-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577091-windows-tree-emulator/
<p style="color: grey">
Python
recipe 577091
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/listing/">listing</a>, <a href="/recipes/tags/tree/">tree</a>, <a href="/recipes/tags/utility/">utility</a>).
Revision 2.
</p>
<p>Windows has a nice command line program capable of printing out a tree of the the folders, sub-folders, and possibly the files of a directory. The tool below mimics a behavioral subset of TREE, Microsoft's inspiring utility. This version is designed to output a similar tree and should be compatible on Windows, Linux, and Macintosh. In case it is not used correctly, a simple usage message will be printed (without notes on the optional "/F" switch). ASCII characters are used.</p>
Binary Matrix IFS Fractals (Python)
2010-03-17T18:15:53-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577125-binary-matrix-ifs-fractals/
<p style="color: grey">
Python
recipe 577125
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>It creates a fractal from any given binary matrix.
It can re-create many of famous fractals.</p>
Sensible Loop Status (Python)
2008-10-05T02:14:52-07:00David Lamberthttp://code.activestate.com/recipes/users/4167420/http://code.activestate.com/recipes/576528-sensible-loop-status/
<p style="color: grey">
Python
recipe 576528
by <a href="/recipes/users/4167420/">David Lambert</a>
(<a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/status/">status</a>).
</p>
<p>A LoopStatus object tests true according to geometric progression or time intervals. This enables rapid time estimates for long running codes, typically in a loop, without producing copious highly repetitive output.</p>
Drop shadows with PIL (Python)
2006-03-06T03:26:54-08:00Kevin Schluffhttp://code.activestate.com/recipes/users/1496147/http://code.activestate.com/recipes/474116-drop-shadows-with-pil/
<p style="color: grey">
Python
recipe 474116
by <a href="/recipes/users/1496147/">Kevin Schluff</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
Revision 2.
</p>
<p>This recipe demonstrates the use of the Python Imaging Library to apply a gaussian blur drop shadow to an image.</p>
Bound Method Function Dispatch (Python)
2007-07-12T20:44:20-07:00Peter Shookhttp://code.activestate.com/recipes/users/4067104/http://code.activestate.com/recipes/523043-bound-method-function-dispatch/
<p style="color: grey">
Python
recipe 523043
by <a href="/recipes/users/4067104/">Peter Shook</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
</p>
<p>You can make your dispatch tables easier to maintain by using a decorator to assign a key to each method you wish to save in the dictionary for the dispatch table.</p>
print statement: battle with UnicodeEncodeError (Python)
2009-02-10T07:22:46-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/523011-print-statement-battle-with-unicodeencodeerror/
<p style="color: grey">
Python
recipe 523011
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 5.
</p>
<p>I received UnicodeEncodeError when playing with various codepages in source code/files/standard streams.
Sometime I receive UnicodeEncodeError when script launched via scheduler or in long running batch when parsing unpredictable [alien ;)] HTML.</p>
<p>Function console() helps avoid this exceptions by converting erroneous charatcters to standard python representation.</p>
<p>to do in future: make a codec-wrapper for safe using in statements like this:</p>
<pre class="prettyprint"><code>sys.stdout=codecs.getwriter('cp866')(sys.stdout)
</code></pre>
How to disablе debug logging in release version (Python)
2006-07-12T08:34:36-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/496870-how-to-disabl-debug-logging-in-release-version/
<p style="color: grey">
Python
recipe 496870
by <a href="/recipes/users/57155/">Denis Barmenkov</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 3.
</p>
<p>When you release your program to client, its a good idea to disable all the debug messages.
It is possible via custom configuring debug levels at all modules, but may be implemented using simple wrapper around logging.getLogger() function.</p>