Top-rated recipes tagged "python" but not "game"http://code.activestate.com/recipes/tags/python-game/top/2017-03-31T14:30:30-07:00ActiveState Code RecipesDiscrete PID Controller (Python) 2015-12-18T20:59:43-08:00Canerhttp://code.activestate.com/recipes/users/4114638/http://code.activestate.com/recipes/577231-discrete-pid-controller/ <p style="color: grey"> Python recipe 577231 by <a href="/recipes/users/4114638/">Caner</a> (<a href="/recipes/tags/controller/">controller</a>, <a href="/recipes/tags/derivative/">derivative</a>, <a href="/recipes/tags/discrete/">discrete</a>, <a href="/recipes/tags/integral/">integral</a>, <a href="/recipes/tags/pid/">pid</a>, <a href="/recipes/tags/proportional/">proportional</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>The recipe gives simple implementation of a Discrete Proportional-Integral-Derivative (PID) controller. PID controller gives output value for error between desired reference input and measurement feedback to minimize error value. More information: <a href="http://en.wikipedia.org/wiki/PID_controller" rel="nofollow">http://en.wikipedia.org/wiki/PID_controller</a></p> <p><strong>For new version please check:</strong> <a href="https://github.com/ivmech/ivPID" rel="nofollow">https://github.com/ivmech/ivPID</a></p> Pipeline made of coroutines (Python) 2012-09-14T09:53:58-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578265-pipeline-made-of-coroutines/ <p style="color: grey"> Python recipe 578265 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>A pipeline made of several coroutines that can be turned off gracefully. </p> Simple Back-propagation Neural Network in Python source code (Python) 2012-05-30T17:09:49-07:00David Adlerhttp://code.activestate.com/recipes/users/4182015/http://code.activestate.com/recipes/578148-simple-back-propagation-neural-network-in-python-s/ <p style="color: grey"> Python recipe 578148 by <a href="/recipes/users/4182015/">David Adler</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/back_propagation/">back_propagation</a>, <a href="/recipes/tags/neural/">neural</a>, <a href="/recipes/tags/neural_network/">neural_network</a>, <a href="/recipes/tags/propagation/">propagation</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>This is a slightly different version of this <a href="http://arctrix.com/nas/python/bpnn.py" rel="nofollow">http://arctrix.com/nas/python/bpnn.py</a></p> Python interpreter auto-completion and history (Python) 2014-01-09T10:04:47-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578098-python-interpreter-auto-completion-and-history/ <p style="color: grey"> Python recipe 578098 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/bashrc/">bashrc</a>, <a href="/recipes/tags/completion/">completion</a>, <a href="/recipes/tags/history/">history</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/pythonstartup/">pythonstartup</a>). Revision 5. </p> <p>Useful in case you don't want to install iPython.</p> Log watcher (tail -F *.log) (Python) 2014-04-04T15:54:03-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577968-log-watcher-tail-f-log/ <p style="color: grey"> Python recipe 577968 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/monitor/">monitor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rotate/">rotate</a>, <a href="/recipes/tags/rotations/">rotations</a>, <a href="/recipes/tags/tail/">tail</a>). Revision 10. </p> <p>A python class which "watches" a directory and calls a callback(filename, lines) function every time one of the files being watched gets written, in real time.</p> <p>Practically speaking, this can be compared to <em>"tail -F *.log"</em> UNIX command, but instead of having lines printed to stdout a python function gets called.</p> <p>Similarly to tail, it takes care of "watching" new files which are created after initialization and "unwatching" those ones which are removed in the meantime. This means you'll be able to "follow" and support also rotating log files.</p> <p><strong>History</strong></p> <ul> <li>rev5 (2013-04-05): <ul> <li>sizehint parameter</li> </ul></li> <li>rev4 (2013-03-16): <ul> <li>python 3 support (also dropped support for python &lt;= 2.5)</li> <li>windows support</li> <li>unit tests</li> <li>main class can also be used as a context manager</li> </ul></li> <li>rev3 (2012-01-13): initial release</li> </ul> Unix tee-like functionality via a Python class (Python) 2017-03-31T14:30:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580767-unix-tee-like-functionality-via-a-python-class/ <p style="color: grey"> Python recipe 580767 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tee/">tee</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>The Unix tee commmand, when used in a command pipeline, allows you to capture the output of the preceding command to a file or files, while still sending it on to standard output (stdout) for further processing via other commands in a pipeline, or to print it, etc.</p> <p>This recipe shows how to implement simple tee-like functionality via a Python class. I do not aim to exactly replicate the functionality of the Unix tee, only something similar.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html</a></p> Simple command-line alarm clock in Python (Python) 2015-10-25T18:27:27-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579117-simple-command-line-alarm-clock-in-python/ <p style="color: grey"> Python recipe 579117 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This recipe shows how to create a simple alarm clock in Python, that can be run from the command line in a terminal. It lets you specify the alarm time in minutes as a command line argument, and prints a wake-up message and beeps a few times, after that time arrives. You can use 0 for the minutes to test it immediately, including to adjust the volume using your speaker controls.</p> Python Mandelbrot Fractal with Tkinter (Python) 2015-05-06T09:57:27-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579048-python-mandelbrot-fractal-with-tkinter/ <p style="color: grey"> Python recipe 579048 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/fractal/">fractal</a>, <a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 9. </p> <p>Displays in a Tk window a pretty coloured 640x480 Mandelbrot set in 6 seconds. Calculates each one of the 300K pixels with a maximum of 256 iterations. <br> Only Tkinter used. Tested with Python 3.4 <br> I will test any contribution and add it to the code if worthy.</p> Convert doc and docx files to pdf (Python) 2014-03-31T18:39:16-07:00Fabian Mayerhttp://code.activestate.com/recipes/users/4189629/http://code.activestate.com/recipes/578858-convert-doc-and-docx-files-to-pdf/ <p style="color: grey"> Python recipe 578858 by <a href="/recipes/users/4189629/">Fabian Mayer</a> (<a href="/recipes/tags/doc/">doc</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/win32com/">win32com</a>). Revision 2. </p> <p>The Script converts all doc and docx files in a specified folder to pdf files. It checks whether the provided absolute path does actually exist and whether the specified folder contains any doc and docx files. It does not travers the directory recursively. The script is not portable and runs only a Windows machine. Based on the experience I made, I recommend closing MS Word before running the script.</p> And yet another round-robin generator (Python) 2013-11-13T19:30:29-08:00Jan Müllerhttp://code.activestate.com/recipes/users/4183984/http://code.activestate.com/recipes/578768-and-yet-another-round-robin-generator/ <p style="color: grey"> Python recipe 578768 by <a href="/recipes/users/4183984/">Jan Müller</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/roundrobin/">roundrobin</a>). </p> <p>I know that there is a <a href="http://code.activestate.com/recipes/528936-roundrobin-generator/">nice recipe</a> already that even made it into the <a href="http://docs.python.org/2/library/itertools.html">Python documentation</a>, but this one is more concise and at the same time simpler.</p> <pre class="prettyprint"><code>&gt;&gt;&gt; list(roundrobin('ABC', 'D', 'EF')) ['A', 'D', 'E', 'B', 'F', 'C'] </code></pre> GAE Matplotlib Demo (Python) 2012-12-31T17:39:14-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/578393-gae-matplotlib-demo/ <p style="color: grey"> Python recipe 578393 by <a href="/recipes/users/4068698/">Dima Tisnek</a> (<a href="/recipes/tags/app_engine/">app_engine</a>, <a href="/recipes/tags/gae/">gae</a>, <a href="/recipes/tags/google_app_engine/">google_app_engine</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Google App Engine python 2.7 runtime includes support for numpy and matplotlib since 13-Dec-2012, however, by default, matplotlib is not supported on the development server. This workaround let you run both in developer mode and deployed on google app engine:</p> Python Exception Chains (or Trees) (Python) 2013-02-04T15:15:22-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/578252-python-exception-chains-or-trees/ <p style="color: grey"> Python recipe 578252 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/cause/">cause</a>, <a href="/recipes/tags/chain/">chain</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reason/">reason</a>, <a href="/recipes/tags/reraise/">reraise</a>, <a href="/recipes/tags/tree/">tree</a>). Revision 2. </p> <p>I have here an approach for chaining exceptions in case a lower layer (<em>library</em>) raises an exception which is caught in an upper layer (<em>application</em>) and later given as <em>cause</em> when a different exception is raised. Passing this <em>cause</em> exception is meant to offer access to the stack trace of the inner exception for debugging.</p> <p>This approach is implemented in Python 3 and in Java, so it definitely makes sense; you also quickly find questions on <a href="http://stackoverflow.com">Stackoverflow</a> concerning it.</p> <p>I even extended this feature by not only using chains of exceptions but also trees. Trees, why trees? Because I had situations in which my application layer tried various approaches using the library layer. If all failed (raised an exception), my application layer also raised an exception; this is the case in which I wanted to pass more than one cause exception into my own exception (hence the tree of causes).</p> <p>My approach uses a special Exception class from which all my exceptions will inherit; standard exception must be wrapped (directly after catching, to preserve the exception stack trace). Please see the examples contained in the code below. The exception itself is rather small.</p> <p>I'd be happy to hear any comments regarding memory leaks (I didn't find any but one never knows), usability, enhancements or similar.</p> pygmail (can send mail) (Python) 2012-07-10T01:29:42-07:00Garretthttp://code.activestate.com/recipes/users/4181290/http://code.activestate.com/recipes/578203-pygmail-can-send-mail/ <p style="color: grey"> Python recipe 578203 by <a href="/recipes/users/4181290/">Garrett</a> (<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/gmail/">gmail</a>, <a href="/recipes/tags/google/">google</a>, <a href="/recipes/tags/mail/">mail</a>, <a href="/recipes/tags/pop/">pop</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Can both send and receive mail, as well as do other things.</p> Disk usage (Python) 2012-10-06T15:33:40-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577972-disk-usage/ <p style="color: grey"> Python recipe 577972 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/space/">space</a>, <a href="/recipes/tags/statistics/">statistics</a>, <a href="/recipes/tags/usage/">usage</a>). Revision 6. </p> <p>Provides disk usage statistics (total, used and free disk space) about a given path.</p> <p>This recipe was initially developed for psutil:</p> <ul> <li><a href="http://code.google.com/p/psutil/issues/detail?id=172" rel="nofollow">http://code.google.com/p/psutil/issues/detail?id=172</a></li> </ul> <p>...and then included into shutil module starting from Python 3.3:</p> <ul> <li><a href="http://mail.python.org/pipermail/python-ideas/2011-June/010480.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2011-June/010480.html</a></li> <li><a href="http://bugs.python.org/issue12442" rel="nofollow">http://bugs.python.org/issue12442</a></li> <li><a href="http://docs.python.org/dev/library/shutil.html#shutil.disk_usage" rel="nofollow">http://docs.python.org/dev/library/shutil.html#shutil.disk_usage</a></li> </ul> <p>The recipe you see here is a modified version of the latter one in that the Windows implementation uses ctypes instead of a C extension module. As such it can be used with python &gt;= 2.5.</p> Blocks for python (Python) 2011-10-24T22:26:07-07:00yoav glaznerhttp://code.activestate.com/recipes/users/4178625/http://code.activestate.com/recipes/577920-blocks-for-python/ <p style="color: grey"> Python recipe 577920 by <a href="/recipes/users/4178625/">yoav glazner</a> (<a href="/recipes/tags/block/">block</a>, <a href="/recipes/tags/lambda/">lambda</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/style/">style</a>). </p> <p>A simple <strong>block</strong> function that lets one send multi line blocks of code to a function it doesn't really act like a <em>normal</em> def/lambda but offers cool style</p> Deprecated decorator (Python) 2013-12-14T01:24:31-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577819-deprecated-decorator/ <p style="color: grey"> Python recipe 577819 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/deprecated/">deprecated</a>, <a href="/recipes/tags/python/">python</a>). Revision 3. </p> <p>A decorator to deprecate a function and provide a new one as replacement.</p> entry_point decorator - no more if __name__ == '__main__' (Python) 2011-07-14T20:42:35-07:00yoav glaznerhttp://code.activestate.com/recipes/users/4178625/http://code.activestate.com/recipes/577791-entry_point-decorator-no-more-if-__name__-__main__/ <p style="color: grey"> Python recipe 577791 by <a href="/recipes/users/4178625/">yoav glazner</a> (<a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>Use this decorator to remove the UGLY "if __name__ == '__main__':"</p> Backtracking template method (Python) 2011-07-03T20:56:37-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/577777-backtracking-template-method/ <p style="color: grey"> Python recipe 577777 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/backtracking/">backtracking</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>It's a faster pythonic solution for backtracking. It combine a list of choice points with a list of choices according to a function that define when a choice is assignable to a choice point. As you see, it's easy to formulate a 8 queens and 4 colors problems.</p> Simple local cache and cache decorator (Python) 2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/ <p style="color: grey"> Python recipe 577492 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Simple local cache. It saves local data in singleton dictionary with convenient interface</p> <h4 id="examples-of-use">Examples of use:</h4> <pre class="prettyprint"><code># Initialize SimpleCache({'data':{'example':'example data'}}) # Getting instance c = SimpleCache.getInstance() c.set('re.reg_exp_compiled',re.compile(r'\W*')) reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*')) # -------------------------------------------------------------- c = SimpleCache.getInstance() reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*')) # -------------------------------------------------------------- @scache def func1(): return 'OK' </code></pre> Open a python module given it's name in Emacs (Bash) 2010-07-19T10:25:06-07:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577317-open-a-python-module-given-its-name-in-emacs/ <p style="color: grey"> Bash recipe 577317 by <a href="/recipes/users/4173873/">Noufal Ibrahim</a> (<a href="/recipes/tags/emacs/">emacs</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python_developer_tools/">python_developer_tools</a>, <a href="/recipes/tags/zsh/">zsh</a>). </p> <p>A tiny little <code>zsh</code> function to open a python module in <code>Emacs</code>. Assumes that an <code>Emacs</code> server process is running. </p>