Popular recipes tagged "meta:loc=209"http://code.activestate.com/recipes/tags/meta:loc=209/2014-04-24T18:13:34-07:00ActiveState Code RecipesPython Game of Life (Python) 2014-01-14T14:34:17-08:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578808-python-game-of-life/ <p style="color: grey"> Python recipe 578808 by <a href="/recipes/users/4186639/">Christian Careaga</a> (<a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/pygame/">pygame</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Conway's Game of Life written in Python with Pygame!</p> <p>Here is a screenshot: <a href="http://adf.ly/c4cnA" rel="nofollow">http://adf.ly/c4cnA</a></p> <p>Here is the exe version!: <a href="http://adf.ly/c4bMO" rel="nofollow">http://adf.ly/c4bMO</a></p> <p>Hope you like it!</p> A point-in-polygon program (S.W. Sloan algorithm) (Python) 2014-04-24T18:13:34-07:00PGhttp://code.activestate.com/recipes/users/4184598/http://code.activestate.com/recipes/578381-a-point-in-polygon-program-sw-sloan-algorithm/ <p style="color: grey"> Python recipe 578381 by <a href="/recipes/users/4184598/">PG</a> (<a href="/recipes/tags/point/">point</a>, <a href="/recipes/tags/polygon/">polygon</a>). Revision 2. </p> <p>Class to compute if a point(s) lies inside/outside/on-side of a polygon.</p> <p>UPDATE: The class now works with sequences of points.</p> <p>This is a Python 3 implementation of the Sloan's improved version (FORTRAN 77 code) of the Nordbeck and Rystedt algorithm, published in the paper:</p> <p>SLOAN, S.W. (1985): A point-in-polygon program. Adv. Eng. Software, Vol 7, No. 1, pp 45-47.</p> <p>This class has 1 public method (is_inside) that returns the minimum distance to the nearest point of the polygon:</p> <p>If is_inside &lt; 0 then point is outside the polygon. If is_inside = 0 then point in on a side of the polygon. If is_inside &gt; 0 then point is inside the polygon.</p> Multiprocess-safe logging file-handler + interprocess RLock (Python) 2010-09-22T17:30:10-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577395-multiprocess-safe-logging-file-handler-interproces/ <p style="color: grey"> Python recipe 577395 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/concurrency/">concurrency</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/file_lock/">file_lock</a>, <a href="/recipes/tags/flock/">flock</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/rlock/">rlock</a>, <a href="/recipes/tags/threading/">threading</a>, <a href="/recipes/tags/threadsafe/">threadsafe</a>). Revision 11. </p> <p>A Python 2.x/3.x-compatibile <strong>multiprocess-safe logging file-handler</strong> (logging.FileHandler replacement, designed for logging to a single file from multiple independent processes) together with a simple <strong>interprocess recursive lock</strong> -- universal abstract classes + Unix/Linux implementation.</p> <p><strong>Update:</strong> It's is a deeply revised version. Especially, now it --</p> <ul> <li>is Python 2.4, 2.5, 2.6, 3.1 -compatibile (previously Py>=2.6 was needed); probably works also with 2.7, 3.0 and 3.2 (but not tested if it does);</li> <li>is multiprocess-safe as well as thread-safe (proviously thread safety within a process was missed);</li> <li>is based on public interfaces only (previously FileHandler._open() was called and overriden);</li> <li>implement full RLock instance interface, as documented for threading.RLock (previously non-blocking mode and context-manager interface were missing).</li> </ul> <p>The module contains:</p> <ul> <li>Unix/Linux-only example implementation (with flock-based locking): <strong>FLockRLock</strong> and <strong>FLockFileHandler</strong> classes.</li> <li>universal abstract classes -- which may be useful at developing implementation for non-Unix platforms: <strong>MultiprocessRLock</strong>, <strong>MultiprocessFileHandler</strong>, <strong>LockedFileHandler</strong>,</li> </ul> <p>Also a quick-and-dirty test was added.</p> <p><strong>It is still an alpha version -- I'll be very grateful for any feedback.</strong></p> <hr /> <p><strong>Further updates:</strong></p> <ul> <li><p>2010-09-20: Some corrections, especially: non-blocking mode bug in MultiprocessRLock.acquire() fixed; _test() function improved; plus fixes in the description below.</p></li> <li><p>2010-09-22: _test() improved and moved to description section. Mistaken copyright-notice removed.</p></li> </ul> Object snoop - experiment with Python special methods (Python) 2010-09-05T17:54:50-07:00Wai Yip Tunghttp://code.activestate.com/recipes/users/2382677/http://code.activestate.com/recipes/577383-object-snoop-experiment-with-python-special-method/ <p style="color: grey"> Python recipe 577383 by <a href="/recipes/users/2382677/">Wai Yip Tung</a> (<a href="/recipes/tags/metaprogramming/">metaprogramming</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/object/">object</a>). </p> <p>In Python, classes can define their own behavior with respect to language operators. For example, if a class defines __getitem__(), then x[i], where x is an instance of the clas, will be execute by a call to x.__getitem__(i).</p> <p>While Python has an extensive documentation on the special methods, reading a specification may not be the best way to reveal the intricate details. <strong>object_snoop</strong> allows user to observe how Python expressions and statements are translated into special method calls. object_snoop defines most special methods. It simple print a trace and returns a fixed but sensible result. Users are invited to build complex expressions to experiment how Python special methods work.</p> Method-based URL dispatcher for the Tornado web server (Python) 2009-11-20T11:51:47-08:00Dan McDougallhttp://code.activestate.com/recipes/users/4169722/http://code.activestate.com/recipes/576958-method-based-url-dispatcher-for-the-tornado-web-se/ <p style="color: grey"> Python recipe 576958 by <a href="/recipes/users/4169722/">Dan McDougall</a> (<a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>, <a href="/recipes/tags/subclass/">subclass</a>, <a href="/recipes/tags/tornado/">tornado</a>, <a href="/recipes/tags/url/">url</a>, <a href="/recipes/tags/web/">web</a>). Revision 5. </p> <p>The MethodDispatcher is a subclass of <a href="http://www.tornadoweb.org/">tornado</a>.web.RequestHandler that will use the methods contained in subclasses of MethodDispatcher to handle requests. In other words, instead of having to make a new RequestHandler class for every URL in your application you can subclass MethodDispatcher and use the methods contained therein <em>as</em> your URLs.</p> <p>The MethodDispatcher also adds the convenience of automatically passing arguments to your class methods. So there is no need to use Tornado's get_argument() method.</p> <h5><strong>Example</strong></h5> <p>To demonstrate the advantages of using MethodDispatcher I'll present a standard Tornado app with multiple URLs and re-write it using MethodDispatcher...</p> <h5><strong>The standard Tornado way</strong></h5> <pre class="prettyprint"><code>class Foo(tornado.web.RequestHandler): def get(self): self.write('foo') class Bar(tornado.web.RequestHandler): def get(self): self.write('bar') class SimonSays(tornado.web.RequestHandler): def get(self): say = self.get_argument("say") self.write('Simon says, %s' % `say`) application = tornado.web.Application([ (r"/foo", Foo), (r"/bar", Bar), (r"/simonsays", SimonSays), ]) </code></pre> <h5><strong>The MethodDispatcher way</strong></h5> <pre class="prettyprint"><code>class FooBar(MethodDispatcher): def foo(self): self.write("foo") def bar(self): self.write("bar") def simonsays(self, say): self.write("Simon Says, %s" % `say`) application = tornado.web.Application([ (r"/.*", FooBar) ]) </code></pre> <h5><strong>Notes</strong></h5> <p>As you can see from the above example, using the MethodDispatcher can significantly reduce the complexity of Tornado applications. Here's some other things to keep in mind when using the MethodDispatcher:</p> <ul> <li>MethodDispatcher will ignore any methods that begin with an underscore (_). This prevents builtins and private methods from being exposed to the web.</li> <li>The '/' path is special: It always maps to self.index().</li> <li>MethodDispatcher does not require that your methods distinquish between GET and POST requests. Whether a GET or POST is performed the matching method will be called with any passed arguments or POSTed data. Because of the way this works you should not use get() and post() in your MethodDispatcher subclasses unless you want to override this functionality.</li> <li>When an argument is passed with a single value (/simonsays?say=hello) the value passed to the argument will be de-listed. In other words, it will be passed to your method like so: {'say': 'hello'}. This overrides the default Tornado behavior which would return the value as a list: {'say': ['hello']}. If more than one value is passed MethodDispatcher will use the default behavior.</li> </ul> Python reader for SPSS .por files (Python) 2009-06-15T15:37:39-07:00wouterhttp://code.activestate.com/recipes/users/4170747/http://code.activestate.com/recipes/576809-python-reader-for-spss-por-files/ <p style="color: grey"> Python recipe 576809 by <a href="/recipes/users/4170747/">wouter</a> (<a href="/recipes/tags/por/">por</a>, <a href="/recipes/tags/portable/">portable</a>, <a href="/recipes/tags/reader/">reader</a>, <a href="/recipes/tags/spp/">spp</a>, <a href="/recipes/tags/value_labels/">value_labels</a>). </p> <p>SPSS can output ASCII .por files that include variable definitions (labels, value labels) and data. These files are useful to be able to read but strange enough I couldn't find any documentation anywhere.</p> <p>I've reverse engineered the format so that it works for the (fairly complex) files I need to open, so maybe it is of some use for other people interfacing SPSS with python. </p> <p>This code is placed in the public domain as far as allowed by law, parts (if any) that cannot be relased in the public domain are irrevocably licensed to all readers under the Creative Commons license without restrictions ... but if you improve it it would be great if you could share it. The code uses a base N decoder plucked from the web somewhere (can't find it) that was also public domain.</p> <p>-- wouter (wouter@vanatteveldt.com)</p> Class for writing content to Excel and formatting it (Python) 2007-09-07T03:08:02-07:00Grant Paton-Simpsonhttp://code.activestate.com/recipes/users/4074475/http://code.activestate.com/recipes/528870-class-for-writing-content-to-excel-and-formatting-/ <p style="color: grey"> Python recipe 528870 by <a href="/recipes/users/4074475/">Grant Paton-Simpson</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>Class for creating Excel spreadsheets - esp writing data and formatting them. NB OpenOffice Calc will be easily able to open the outputs too.</p> typeparser (Python) 2007-04-15T04:24:53-07:00Florian Leitnerhttp://code.activestate.com/recipes/users/4049249/http://code.activestate.com/recipes/511473-typeparser/ <p style="color: grey"> Python recipe 511473 by <a href="/recipes/users/4049249/">Florian Leitner</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Python type-string parser. The code evolved from a post in python-list on 11/22/05 by Fredrik Lundh on a dictionary parser. It parses a type-string to their type objects for all basic types. Raises SyntaxError and SemanticError on failures.</p> <p>Supported types: * containers: defaultdict, deque, dict, list, tuple, set * basic types: Decimal, bool, float, int, long, str * None type</p> <p>REQUIRES PYTHON &gt;= 2.5</p> Helper subclass for win32pdhquery (Python) 2004-09-03T10:03:51-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/303338-helper-subclass-for-win32pdhquery/ <p style="color: grey"> Python recipe 303338 by <a href="/recipes/users/98030/">Brett Cannon</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>One of the few perks one can have when developing on Windows (at least NT or newer) is the use of the Performance Data Helper (the PDH can be found on Windows XP under the Control Panel at Administrative Tools -> Performance) for system statistics gathering. There is a bevy of counters available that can greatly help you measure the impact your code has on the current system.</p> <p>And luckily win32all provides a wrapper to create counters. The only drawback, though, is that the API is not the best in the world. So, to help deal with that I wrote a subclass to help with adding counters and formatting the output in a more usable fashion as either CSV or a dict that can be pickled.</p>