Popular recipes tagged "meta:loc=43"http://code.activestate.com/recipes/tags/meta:loc=43/2017-04-25T11:44:27-07:00ActiveState Code RecipesBatch prime generator (Batch) 2017-04-25T11:44:27-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580789-batch-prime-generator/ <p style="color: grey"> Batch recipe 580789 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/primes/">primes</a>). Revision 3. </p> <p>Here is a radically differnt approach to generating primes in pure batch that overperforms everything else I have found . The idea comes from an exercise in Knuth's TAOCP Vol 3 page 617.</p> dunderdoc, a simple Python introspection tool (Python) 2015-03-30T20:22:20-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579042-dunderdoc-a-simple-python-introspection-tool/ <p style="color: grey"> Python recipe 579042 by <a href="/recipes/users/4173351/">Vasudev Ram</a> . </p> <p>The dunderdoc() function, in module dunderdoc.py, is a simple Python introspection tool that I wrote. It prints the __doc__ attribute (the docstring) of each item in a list of names given as argument to it. </p> Non-blocking readlines() (Python) 2014-06-30T20:30:57-07:00Zack Weinberghttp://code.activestate.com/recipes/users/4190298/http://code.activestate.com/recipes/578900-non-blocking-readlines/ <p style="color: grey"> Python recipe 578900 by <a href="/recipes/users/4190298/">Zack Weinberg</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>). </p> <p>A generator function which takes a file object (assumed to be some sort of pipe or socket, open for reading), and yields lines from it without blocking. If there is no input available, it will yield an endless stream of empty strings until input becomes available again; caller is responsible for not going into a busy loop. (Newlines are normalized but not stripped, so if there is actually a blank line in the input, the value yielded will be <code>'\n'</code>.) The intended use case is a thread which must respond promptly to input from a pipe, and also something else which cannot be fed to <code>select</code> (e.g. a <code>queue.Queue</code>). Note that the file object is ignored except for its <code>fileno</code>.</p> <p>Only tested on Unix. Only tested on 3.4; ought to work with any python that has <code>bytearray</code>, <code>locale.getpreferredencoding</code>, and <code>fcntl</code>.</p> Random Maze Generator (Python) 2012-12-14T09:09:22-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578356-random-maze-generator/ <p style="color: grey"> Python recipe 578356 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/ai/">ai</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/pil/">pil</a>). Revision 2. </p> <p>Random maze generator using depth-first search.</p> <p>It starts the maze path from a random cell and there is no exit defined but actually any 2 cells on the path (white cells) can be assigned to be entry and exit locations. (I could just add code to randomly select 2 white cells and change their colors to something else but I thought it looks better this way.)</p> make some file named year+month+day (Python) 2012-02-10T23:46:36-08:00ryotaro gotohttp://code.activestate.com/recipes/users/4180840/http://code.activestate.com/recipes/578036-make-some-file-named-yearmonthday/ <p style="color: grey"> Python recipe 578036 by <a href="/recipes/users/4180840/">ryotaro goto</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sys/">sys</a>). Revision 6. </p> <p>This program make some file named year+month+day How to use "python program argument example:argument=01,02,...,12 If you input 02,it will return files named 20120201,...,20120228</p> Simplified LRU Cache (Python) 2013-03-06T06:04:18-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577970-simplified-lru-cache/ <p style="color: grey"> Python recipe 577970 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/lru/">lru</a>). Revision 6. </p> <p>An O(1) LRU cache. Short, sweet, and fast.</p> Conditionnal Breakpoint (Python) 2011-09-20T18:49:23-07:00s_h_a_i_ohttp://code.activestate.com/recipes/users/4177334/http://code.activestate.com/recipes/577874-conditionnal-breakpoint/ <p style="color: grey"> Python recipe 577874 by <a href="/recipes/users/4177334/">s_h_a_i_o</a> (<a href="/recipes/tags/breakpoint/">breakpoint</a>, <a href="/recipes/tags/debug/">debug</a>). Revision 2. </p> <p>Decorates a class so that it encounters a breakpoint for any call to class.method_name(self,<em>args,</em>*kwargs), where</p> <ul> <li>method_name is given by string</li> <li>only if arguments meet a test: arg_test(self,<em>args,</em>*kwargs) -> bool [default -> True]</li> </ul> <p>Behaviour easily changed for other actions than breakpoint (logging,...)</p> Gumowski-Mira Strange Attractor (Python) 2010-12-07T09:54:51-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577486-gumowski-mira-strange-attractor/ <p style="color: grey"> Python recipe 577486 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/chaos/">chaos</a>, <a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>, <a href="/recipes/tags/pil/">pil</a>). Revision 4. </p> <p>It draws a random Gumowski-Mira Strange Attractor. (It would retry until a good one is found to display.)</p> Getting the SHA-1 (or MD5) hash of a directory (Python) 2009-12-06T19:02:40-08:00Stephen Akikihttp://code.activestate.com/recipes/users/4172143/http://code.activestate.com/recipes/576973-getting-the-sha-1-or-md5-hash-of-a-directory/ <p style="color: grey"> Python recipe 576973 by <a href="/recipes/users/4172143/">Stephen Akiki</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/getting/">getting</a>, <a href="/recipes/tags/hash/">hash</a>, <a href="/recipes/tags/md5/">md5</a>, <a href="/recipes/tags/sha_1/">sha_1</a>, <a href="/recipes/tags/the/">the</a>). Revision 4. </p> <h5><a href="http://akiscode.com/articles/sha-1directoryhash.shtml" rel="nofollow">http://akiscode.com/articles/sha-1directoryhash.shtml</a></h5> <p>By definition a cryptographic hash is, "a deterministic procedure that takes an arbitrary block of data and returns a fixed-size bit string, the (cryptographic) hash value, such that an accidental or intentional change to the data will change the hash value". </p> <p>Usually these hashes are used on files to "fingerprint" them, but in order to do the same to a directory you have to do something like this: </p> Plot database table dependecies for a mySQL database (Python) 2010-07-10T08:14:09-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577298-plot-database-table-dependecies-for-a-mysql-databa/ <p style="color: grey"> Python recipe 577298 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/dot/">dot</a>, <a href="/recipes/tags/graphs/">graphs</a>, <a href="/recipes/tags/graphviz/">graphviz</a>, <a href="/recipes/tags/mysql/">mysql</a>). </p> <p>The following snippet will output a spec suitable for the graphviz dot program. It will go through the database specified and for all the tables that start with any of the names in <code>table_prefixes</code>, it will create a dependency graph. A dependency in this context is when a field of one table has a foreign key reference to another. The edges are labelled with the actual field names. </p> <p>It can be used like this</p> <pre class="prettyprint"><code>python deps.py | dot -Tpng | display </code></pre> <p>This requires the InnoDB engine (i.e. something that honours foreign keys. MyISAM doesn't do so).</p> Downloading website's favicon (Python) 2010-03-15T13:40:29-07:00Łukasz Fidoszhttp://code.activestate.com/recipes/users/4173347/http://code.activestate.com/recipes/577114-downloading-websites-favicon/ <p style="color: grey"> Python recipe 577114 by <a href="/recipes/users/4173347/">Łukasz Fidosz</a> (<a href="/recipes/tags/favicon/">favicon</a>, <a href="/recipes/tags/urllib2/">urllib2</a>, <a href="/recipes/tags/websites/">websites</a>). </p> <p>Downloading favicon of the website and save it to file, if website doesn't have favicon using default one.</p> Maintenance free Factory design pattern (Python) 2009-03-12T08:54:39-07:00Akira Forahttp://code.activestate.com/recipes/users/4114779/http://code.activestate.com/recipes/576687-maintenance-free-factory-design-pattern/ <p style="color: grey"> Python recipe 576687 by <a href="/recipes/users/4114779/">Akira Fora</a> (<a href="/recipes/tags/design/">design</a>, <a href="/recipes/tags/factory/">factory</a>, <a href="/recipes/tags/free/">free</a>, <a href="/recipes/tags/maintenance/">maintenance</a>, <a href="/recipes/tags/pattern/">pattern</a>). Revision 3. </p> <p>An implementation of the Factory design pattern that doesn't require maintenance when a new specialized subclass is added.</p> Get sys.argv with Unicode characters under Windows (Python) 2008-05-13T17:44:08-07:00Brodie Raohttp://code.activestate.com/recipes/users/2965587/http://code.activestate.com/recipes/572200-get-sysargv-with-unicode-characters-under-windows/ <p style="color: grey"> Python recipe 572200 by <a href="/recipes/users/2965587/">Brodie Rao</a> (<a href="/recipes/tags/sysadmin/">sysadmin</a>). </p> <p>A function that employs ctypes to call the underlying UTF-8 APIs for getting command line arguments on Windows.</p> Mini Fake DNS server (Python) 2006-04-18T17:41:59-07:00Francisco Santoshttp://code.activestate.com/recipes/users/2853535/http://code.activestate.com/recipes/491264-mini-fake-dns-server/ <p style="color: grey"> Python recipe 491264 by <a href="/recipes/users/2853535/">Francisco Santos</a> (<a href="/recipes/tags/network/">network</a>). Revision 4. </p> <p>Minimal python dns server, it only replies with a selected ip in an A record</p> PIL and Tkinter to display images. (Python) 2007-06-20T19:25:41-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/521918-pil-and-tkinter-to-display-images/ <p style="color: grey"> Python recipe 521918 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/graphics/">graphics</a>). Revision 3. </p> <p>Using PIL and Tkinter you can easily display images in a window.</p> Simple file-based mutex for very basic IPC (Python) 2007-05-09T03:03:48-07:00Vinay Sajiphttp://code.activestate.com/recipes/users/4034162/http://code.activestate.com/recipes/519626-simple-file-based-mutex-for-very-basic-ipc/ <p style="color: grey"> Python recipe 519626 by <a href="/recipes/users/4034162/">Vinay Sajip</a> . </p> <p>Since the year dot, file locking has been used as a very simple synchronizing primitive between processes. Here's my take on the approach. To try it, simply start two command windows/terminal windows and run the script in each.</p> <p>Tested on WinXP and Ubuntu (Dapper and Feisty).</p> Line Of Code Counter (Python) 2007-08-09T16:42:45-07:00Robin Parmarhttp://code.activestate.com/recipes/users/99666/http://code.activestate.com/recipes/527746-line-of-code-counter/ <p style="color: grey"> Python recipe 527746 by <a href="/recipes/users/99666/">Robin Parmar</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This simple function counts Lines Of Code in Python files in two ways: maximal size (source LOC) with blank lines and comments, minimal size (logical LOC) stripping same. It includes a simplified version of my directory tree walker from <a href="http://code.activestate.com/recipes/52664/">recipe 52664</a>.</p> Unicode class which adds proper XML declaration on encoding (Python) 2007-08-09T04:52:45-07:00Dmitry Vasilievhttp://code.activestate.com/recipes/users/1571302/http://code.activestate.com/recipes/526626-unicode-class-which-adds-proper-xml-declaration-on/ <p style="color: grey"> Python recipe 526626 by <a href="/recipes/users/1571302/">Dmitry Vasiliev</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Sometimes you want to pass XML document as unicode object which later should be encoded for output. Unfortunately very often you don't know the output encoding and can't set XML declaration properly. UnicodeXML adds XML declaration right on encoding operation.</p> Logging to a Jabber account (Python) 2006-10-03T15:35:06-07:00Willi Richerthttp://code.activestate.com/recipes/users/4003859/http://code.activestate.com/recipes/498158-logging-to-a-jabber-account/ <p style="color: grey"> Python recipe 498158 by <a href="/recipes/users/4003859/">Willi Richert</a> (<a href="/recipes/tags/programs/">programs</a>). </p> <p>If you have long-running scripts on many remote machines and you want to be alarmed if one of them crashes, use the JabberHandler to log it to your account.</p> threaded pipes (Python) 2006-08-20T14:43:57-07:00Andrew Moffathttp://code.activestate.com/recipes/users/2979564/http://code.activestate.com/recipes/496979-threaded-pipes/ <p style="color: grey"> Python recipe 496979 by <a href="/recipes/users/2979564/">Andrew Moffat</a> (<a href="/recipes/tags/threads/">threads</a>). </p> <p>combines queues and threads for efficient data processing.</p>