Popular recipes tagged "meta:loc=64"http://code.activestate.com/recipes/tags/meta:loc=64/2016-03-16T08:56:08-07:00ActiveState Code RecipesCreate Sierpinski Carpet (Fractal) FAST (Python) 2016-03-16T08:56:08-07:00Jorj X. McKiehttp://code.activestate.com/recipes/users/4193772/http://code.activestate.com/recipes/580624-create-sierpinski-carpet-fractal-fast/ <p style="color: grey"> Python recipe 580624 by <a href="/recipes/users/4193772/">Jorj X. McKie</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/sierpinski/">sierpinski</a>). </p> <p>Create a Sierpinski carpet using MuPDF's graphics library fitz (binding PyMuPDF) at high speed.</p> Find Duplicate Files (Python) 2014-10-12T21:14:05-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578950-find-duplicate-files/ <p style="color: grey"> Python recipe 578950 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Finds duplicate files which have same size and same content in the same directory or two different directories.</p> Inherit method docstrings without breaking decorators or violating DRY (Python) 2013-07-01T02:29:40-07:00nikratiohttp://code.activestate.com/recipes/users/4180248/http://code.activestate.com/recipes/578587-inherit-method-docstrings-without-breaking-decorat/ <p style="color: grey"> Python recipe 578587 by <a href="/recipes/users/4180248/">nikratio</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>). Revision 2. </p> <p>There are several recipes for inheriting method docstrings. However, most of them either violate DRY (and have you repeat the ancestor class name in the method decorator that sets the docstring), or do break decorators that try to access the docstring (because the docstring is only assigned after class creation). This recipe avoids both problems.</p> <p><strong>Note</strong>: This recipe uses the mro method from <a href="http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/" rel="nofollow">http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/</a></p> String-like partition and rpartition functions for lists (Python) 2013-01-30T06:44:03-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/578442-string-like-partition-and-rpartition-functions-for/ <p style="color: grey"> Python recipe 578442 by <a href="/recipes/users/4172262/">Sam Denton</a> . </p> <p>Python 2.5 added the partition and rpartition methods for strings, but nothing similar for lists. These two functions provide that, with the addition of an optional key parameter with the same functionality as in itertools.groupby(. </p> Platform Independent White Noise Generator... (Python) 2012-11-25T10:10:45-08:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578350-platform-independent-white-noise-generator/ <p style="color: grey"> Python recipe 578350 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/noise/">noise</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/sound/">sound</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This code is a derivative of the Pure Sinewave Generator and produces a continuous noise out of the speakers or headphone sockets.</p> <p>It is for the hobbyist to be able to generate a pseudo-random noise signal for testing with.</p> <p>It is issued as Public Domian and you may do with it as you please.</p> <p>It is very easy to convert to Python 3.x.x but as OSX only has 2.7.x and lower ATM then these are what are used...</p> <p>An installation of pyaudio IS needed for this to work; see the code for more information.</p> <p>It is near platform independent but sadly the AMIGA is not included, but hey, I have already shown how generate sound for Classic AMIGAs.</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <p>Bazza, G0LCU...</p> Test various OpenCV feature detectors in Python (Python) 2012-09-13T16:07:21-07:00J W Jhttp://code.activestate.com/recipes/users/4181154/http://code.activestate.com/recipes/578261-test-various-opencv-feature-detectors-in-python/ <p style="color: grey"> Python recipe 578261 by <a href="/recipes/users/4181154/">J W J</a> (<a href="/recipes/tags/feature_detection/">feature_detection</a>, <a href="/recipes/tags/opencv/">opencv</a>). </p> <p>Iterates through all feature detectors (that didn't crash my computer) and plots the point results.</p> NamedList (Python) 2012-02-16T18:38:54-08:00Sergey Shepelevhttp://code.activestate.com/recipes/users/4180945/http://code.activestate.com/recipes/578041-namedlist/ <p style="color: grey"> Python recipe 578041 by <a href="/recipes/users/4180945/">Sergey Shepelev</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/mutable/">mutable</a>). </p> <p>Fast lightweight attribute style access to list. I think this is closest to mutable C struct type.</p> <p>Same as collections.namedtuple but subclasses list, so fields may be modified. Same as popular Record class but smaller and faster. <a href="http://code.activestate.com/recipes/502237-simple-record-aka-struct-type/" rel="nofollow">http://code.activestate.com/recipes/502237-simple-record-aka-struct-type/</a></p> Convert Audio CD track in iTunes with Python (Python) 2011-10-12T05:29:30-07:00Kai Xiahttp://code.activestate.com/recipes/users/4168327/http://code.activestate.com/recipes/577904-convert-audio-cd-track-in-itunes-with-python/ <p style="color: grey"> Python recipe 577904 by <a href="/recipes/users/4168327/">Kai Xia</a> (<a href="/recipes/tags/itunes/">itunes</a>, <a href="/recipes/tags/win32com/">win32com</a>). </p> <p>This script would do convert tracks from an audio cd into Apple lossless music using iTunes.</p> <p>When we have a bad cd rom or a bad disk, conversion would usually fail and iTunes would not complain about it. I usually have to check every converted track manually. This script would save the pain: it would check every converted track, if the duration of the converted track is less than the original track, the script would retry the conversion.</p> <p>This script is inspired by <a href="http://code.activestate.com/recipes/users/4010550/">Fabien C.</a> 's <a href="http://code.activestate.com/recipes/498241/">Recipe 498241</a>.</p> Convert XML into JSON (Python dicts and lists structure) (Python) 2011-08-22T13:30:33-07:00Nikolahttp://code.activestate.com/recipes/users/4176190/http://code.activestate.com/recipes/577494-convert-xml-into-json-python-dicts-and-lists-struc/ <p style="color: grey"> Python recipe 577494 by <a href="/recipes/users/4176190/">Nikola</a> (<a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>There are many ways to convert a specific XML dialect into JSON for easier consumption by Javascript code. This recipe works for some XML-based data, where the format is not really a document and html tags are used as field placeholders. You can use it as-is, but it is intended to serve as a starting point where you can plug your specific needs.</p> Convert XML files to structurally equivalent garbage (Python) 2010-12-15T18:57:42-08:00Eric Promislowhttp://code.activestate.com/recipes/users/4166930/http://code.activestate.com/recipes/577501-convert-xml-files-to-structurally-equivalent-garba/ <p style="color: grey"> Python recipe 577501 by <a href="/recipes/users/4166930/">Eric Promislow</a> (<a href="/recipes/tags/randomization/">randomization</a>, <a href="/recipes/tags/shrouding/">shrouding</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>You've found a bug in an application that reads XML files. You'd prefer not to share your data, though, but you'd really like to help the vendor fix the bug. This program maintains the document's structure, but it randonly converts each word into random gibberish, replacing vowels with vowels, consonants with consonants, and digits with digits. It also interns each element and attribute name, so all instances of "foo" as an element or attribute name would show up as, say, "xeu", but instances of "foo" in character data would be mapped to a different string each time.</p> Non-recursive Flatten, leaves Strings and Dicts alone, minimal type assumptions, with tests (Python) 2010-09-10T00:10:47-07:00Manuel Garciahttp://code.activestate.com/recipes/users/2827266/http://code.activestate.com/recipes/577387-non-recursive-flatten-leaves-strings-and-dicts-alo/ <p style="color: grey"> Python recipe 577387 by <a href="/recipes/users/2827266/">Manuel Garcia</a> (<a href="/recipes/tags/nonrecursive/">nonrecursive</a>). </p> <p>I have written Flatten a few dozen times, and also searched the interwebs - I don't feel good about a heavily recursive function for Python, and some of the "type-sniffing" I saw in some codes seemed fishy - so I coded up this version. At least this increases the options. Handles strings, dictionaries, generators, sequences, lists, tuples -- in sensible ways. Handles arbitrarily deep levels of nesting. Does the bare minimum of type-sniffing. With Doctest tests.</p> Mandelbrot Fractal using HTML5 Canvas (JavaScript) 2012-12-28T01:01:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577277-mandelbrot-fractal-using-html5-canvas/ <p style="color: grey"> JavaScript recipe 577277 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/math/">math</a>). Revision 3. </p> <p>Mandelbrot Fractal using HTML5 Canvas. (Do not forget to change file extension to HTML after downloading it.)</p> Komodo Macro: Show all Bookmarks (JavaScript) 2013-04-09T16:45:53-07:00Troy Topnikhttp://code.activestate.com/recipes/users/4165698/http://code.activestate.com/recipes/577272-komodo-macro-show-all-bookmarks/ <p style="color: grey"> JavaScript recipe 577272 by <a href="/recipes/users/4165698/">Troy Topnik</a> (<a href="/recipes/tags/bookmarks/">bookmarks</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>). Revision 5. </p> <p>Komodo JavaScript macro that shows a click-able list of all bookmarks in the Command Output tab.</p> Adding the directory of the python executable to the system PATH under windows (Python) 2010-05-19T19:01:31-07:00Anthon van der Neuthttp://code.activestate.com/recipes/users/2403822/http://code.activestate.com/recipes/577233-adding-the-directory-of-the-python-executable-to-t/ <p style="color: grey"> Python recipe 577233 by <a href="/recipes/users/2403822/">Anthon van der Neut</a> (<a href="/recipes/tags/environment/">environment</a>, <a href="/recipes/tags/executable/">executable</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/variable/">variable</a>). </p> <p>If you install python under windows and then open a command shell (DOS-prompt, you normally get an error message if you type "python" at the prompt. This is because the directory of the python executable is not in the PATH environment variable. If you know where you installed python, you can add this via Control Panel -> System -> Advanced -> Environment Variables but this is not very user friendly way of doing things and error prone.</p> <p>This program, if run by double clicking the file or by dragging the file to a command shell, will add the directory of the executable associated with the .py extension to the PATH env. var (if it is not already in there). It will notify other programs of this change, but unfortunately <a href="http://command.com" rel="nofollow">command.com</a> is not smart enough to understand that. You have to open a new command shell after running the program in order to be able to run "python" at the dos prompt.</p> <p>If run with the optional command line parameter 'remove' the directory will be removed from the PATH.</p> Fix mbox files after importing EML into TB using ImportExportTools (Python) 2010-05-02T13:21:00-07:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/577214-fix-mbox-files-after-importing-eml-into-tb-using-i/ <p style="color: grey"> Python recipe 577214 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/eml/">eml</a>, <a href="/recipes/tags/from/">from</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/importexporttools/">importexporttools</a>, <a href="/recipes/tags/mbox/">mbox</a>, <a href="/recipes/tags/tb/">tb</a>, <a href="/recipes/tags/thunderbird/">thunderbird</a>). Revision 2. </p> <p>I've found a bug in import EML file into Thunderbird using ImportExportTools addon: when I import eml file into TB there are a 'From' line added to mbox followed with EML file contents. TB maintains right 'From' line for messages fetched from mailservers:</p> <pre class="prettyprint"><code>From - Tue Apr 27 19:42:22 2010 </code></pre> <p>ImportExportTools formats this line wrong I suppose that used some system function with default specifier so I saw in mbox file:</p> <pre class="prettyprint"><code>From - Sat May 01 2010 15:07:31 GMT+0400 (Russian Daylight Time) </code></pre> <p>So there are two errors: 1) sequence 'time year' broken into 'year time' 2) extra trash with GMT info along with time zone name</p> <p>This prevents the mbox file parsing using Python standard library (for sample) because there are a hardcoded regexp for matching From line (file lib/mailbox.py, class UnixMailbox):</p> <pre class="prettyprint"><code>_fromlinepattern = r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+" \ r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*$" </code></pre> <p>Attached script fixes incorrect From lines so parsing those mboxes using Python standard library will become ok.</p> Super lazy load object (Python) 2010-03-16T05:02:12-07:00Russellhttp://code.activestate.com/recipes/users/4173357/http://code.activestate.com/recipes/577117-super-lazy-load-object/ <p style="color: grey"> Python recipe 577117 by <a href="/recipes/users/4173357/">Russell</a> (<a href="/recipes/tags/lazy/">lazy</a>). </p> <p>A really light implementation of lazy load technique, yet powerful and conveniet. </p> <p>Simply call this:</p> <pre class="prettyprint"><code>var1 = superlazy('key', default_value) </code></pre> <p>Your var1 will be loaded in load_setting(key) when accessed first time.</p> <p>That's it. No subclassing is needed, no declaration is needed. Value type is auto detected and handled gracefully. str, int, list, dict can all be lazily loaded from anywhere you want now.</p> Patch extension binaries compiled for previous (different) Python version (Python) 2010-03-12T15:50:04-08:00Denis Barmenkovhttp://code.activestate.com/recipes/users/57155/http://code.activestate.com/recipes/576873-patch-extension-binaries-compiled-for-previous-dif/ <p style="color: grey"> Python recipe 576873 by <a href="/recipes/users/57155/">Denis Barmenkov</a> (<a href="/recipes/tags/binaries/">binaries</a>, <a href="/recipes/tags/dll/">dll</a>, <a href="/recipes/tags/patch/">patch</a>, <a href="/recipes/tags/precompiled/">precompiled</a>, <a href="/recipes/tags/pyd/">pyd</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 4. </p> <p>It is helpful to download and install precompiled binaries of Python modules especially for Windows. Second way is longer: get module sources, build the C compiler environment, compile binaries, beware of compiler warnings, install module.</p> <p>Often precompiled binaries can be patched to meet used Python version. </p> <p>See 'discussions' below for details.</p> how to create a windows service in python (Python) 2008-08-26T01:32:47-07:00alexander bakerhttp://code.activestate.com/recipes/users/4166679/http://code.activestate.com/recipes/576451-how-to-create-a-windows-service-in-python/ <p style="color: grey"> Python recipe 576451 by <a href="/recipes/users/4166679/">alexander baker</a> . </p> <p>The following code snippet shows you have to create a windows service from a python script. The most important thing here is the username and password, if you ignore supplying these the server will never start and you will get a message saying that the service has not responded in time, this is a red herring. The default account that the pythonservice wrapper uses is not permissioned to run the service. </p> Check uptime and version whith ftplib (Python) 2008-12-06T23:53:08-08:00h-kanhttp://code.activestate.com/recipes/users/4168243/http://code.activestate.com/recipes/576581-check-uptime-and-version-whith-ftplib/ <p style="color: grey"> Python recipe 576581 by <a href="/recipes/users/4168243/">h-kan</a> . </p> <p>My first program ever and it is written in python. I'm sure it's ful of buggs and flaws but it's tested and the code works... I hope someone could help me improve the code.</p> MultiThread support for SQLite access. (Python) 2010-07-20T14:29:35-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/526618-multithread-support-for-sqlite-access/ <p style="color: grey"> Python recipe 526618 by <a href="/recipes/users/4035877/">Louis RIVIERE</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/sqlite/">sqlite</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 4. </p> <p>Workaround for the SQLite limitation that prevents multiple threads from sharing a Connection object.</p>