Popular recipes tagged "meta:min_python_3=2"http://code.activestate.com/recipes/tags/meta:min_python_3=2/2016-04-17T01:22:51-07:00ActiveState Code RecipesLRU dictionary (Python)
2016-04-17T01:22:01-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580644-lru-dictionary/
<p style="color: grey">
Python
recipe 580644
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expiration/">expiration</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
LRU dictionary (Python)
2016-04-17T01:22:51-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580645-lru-dictionary/
<p style="color: grey">
Python
recipe 580645
by <a href="/recipes/users/4193957/">Felix</a>
(<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expires/">expires</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>).
</p>
<p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p>
<p>Read the docstrings for implementation details.</p>
Bytes-to-human / human-to-bytes converter (Python)
2012-02-02T16:09:52-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578019-bytes-to-human-human-to-bytes-converter/
<p style="color: grey">
Python
recipe 578019
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/converter/">converter</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/humanize/">humanize</a>, <a href="/recipes/tags/string/">string</a>).
Revision 15.
</p>
<p>Here goes.</p>
How to use super() effectively -- Python 3.2 version (Python)
2015-12-01T16:23:09-08:00Lance E Sloanhttp://code.activestate.com/recipes/users/4188012/http://code.activestate.com/recipes/579130-how-to-use-super-effectively-python-32-version/
<p style="color: grey">
Python
recipe 579130
by <a href="/recipes/users/4188012/">Lance E Sloan</a>
(<a href="/recipes/tags/cooperative/">cooperative</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/multiple/">multiple</a>, <a href="/recipes/tags/next_in_mro/">next_in_mro</a>, <a href="/recipes/tags/super/">super</a>).
</p>
<p>Python's super() provides a unique and amazing capability. It allows subclasses to be written to reorder a chain method calls. The recipe demonstrates all of the tradecraft needed to get super() to do your bidding.</p>
How to read an input from .txt file in python and the output in .xml (Python)
2014-12-20T00:16:32-08:00Varsha Hollahttp://code.activestate.com/recipes/users/4191376/http://code.activestate.com/recipes/578985-how-to-read-an-input-from-txt-file-in-python-and-t/
<p style="color: grey">
Python
recipe 578985
by <a href="/recipes/users/4191376/">Varsha Holla</a>
(<a href="/recipes/tags/python3_1/">python3_1</a>).
Revision 3.
</p>
<p>Hey, I have a .txt file, I want to read the file and the output should be in .xml format.
Any suggestions? </p>
Hangman (Python)
2014-12-24T23:24:09-08:00Malcolm Jeremy Lewishttp://code.activestate.com/recipes/users/4191332/http://code.activestate.com/recipes/578990-hangman/
<p style="color: grey">
Python
recipe 578990
by <a href="/recipes/users/4191332/">Malcolm Jeremy Lewis</a>
.
</p>
<p>This is the finished version (No visuals though). First you'll want to create some words for you to play with. When playing with maximum memory, it can take a while to check, play, and exit in this application.</p>
TicTacToe (Python)
2014-12-14T16:23:44-08:00Malcolm Jeremy Lewishttp://code.activestate.com/recipes/users/4191332/http://code.activestate.com/recipes/578980-tictactoe/
<p style="color: grey">
Python
recipe 578980
by <a href="/recipes/users/4191332/">Malcolm Jeremy Lewis</a>
(<a href="/recipes/tags/tic/">tic</a>).
</p>
<p>A simple TicTacToe script including AI (Artificial Intelligence)</p>
Pyqt / Pyside: thread-safe callbacks + main loop integration (Python)
2013-10-12T08:43:09-07:00Justin Israelhttp://code.activestate.com/recipes/users/4187487/http://code.activestate.com/recipes/578634-pyqt-pyside-thread-safe-callbacks-main-loop-integr/
<p style="color: grey">
Python
recipe 578634
by <a href="/recipes/users/4187487/">Justin Israel</a>
(<a href="/recipes/tags/events/">events</a>, <a href="/recipes/tags/pyqt/">pyqt</a>, <a href="/recipes/tags/pyside/">pyside</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/thread/">thread</a>).
Revision 4.
</p>
<p>A mechanism for communication from any thread to the main thread.</p>
<p>It uses the same custom Event, but adds a classmethod convenience for taking a callback and params, wrapping it into an event and posting it to the receiver. Also adds in support for weak method references to the callback, in case the source object gets deleted before its callback is actually called.</p>
<p>Merges forks of both:
<a href="http://code.activestate.com/recipes/81253/#c5" rel="nofollow">http://code.activestate.com/recipes/81253/#c5</a>
<a href="http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/" rel="nofollow">http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/</a></p>
Python 3 Token Bucket (Rate Limit) (Python)
2013-09-16T23:41:46-07:00Esteban Castro Borsanihttp://code.activestate.com/recipes/users/4184010/http://code.activestate.com/recipes/578659-python-3-token-bucket-rate-limit/
<p style="color: grey">
Python
recipe 578659
by <a href="/recipes/users/4184010/">Esteban Castro Borsani</a>
(<a href="/recipes/tags/bucket/">bucket</a>, <a href="/recipes/tags/limit/">limit</a>, <a href="/recipes/tags/rate/">rate</a>, <a href="/recipes/tags/ratelimit/">ratelimit</a>, <a href="/recipes/tags/token/">token</a>, <a href="/recipes/tags/tokenbucket/">tokenbucket</a>).
</p>
<p>A thread safe implementation.</p>
Poker Hands (Python)
2013-07-07T04:09:34-07:00Jason Friedmanhttp://code.activestate.com/recipes/users/4183835/http://code.activestate.com/recipes/578589-poker-hands/
<p style="color: grey">
Python
recipe 578589
by <a href="/recipes/users/4183835/">Jason Friedman</a>
(<a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/deck/">deck</a>, <a href="/recipes/tags/poker/">poker</a>, <a href="/recipes/tags/shuffle/">shuffle</a>).
Revision 5.
</p>
<p>Deal random hands and categorize them.
See also <a href="http://code.activestate.com/recipes/415504/">recipe 415504</a>.</p>
wrist friendly dictionary (Python)
2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/
<p style="color: grey">
Python
recipe 578644
by <a href="/recipes/users/4187559/">Ariel Keselman</a>
(<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>).
</p>
<p>this dictionary allows easy manual creation of nested hierarchies, like so:</p>
<p>window.style.width=5</p>
<p>or... </p>
<p>window['background-color'].rgb= 255,255,255</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 <= 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>
deprecate decorator which accepts arguments (Python)
2013-08-23T13:24:40-07:00tlatsashttp://code.activestate.com/recipes/users/4187618/http://code.activestate.com/recipes/578650-deprecate-decorator-which-accepts-arguments/
<p style="color: grey">
Python
recipe 578650
by <a href="/recipes/users/4187618/">tlatsas</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/deprecated/">deprecated</a>).
Revision 2.
</p>
<p>The deprecate decorator can be used to warn for future or current code deprecations. It accepts a message and an optional Warning class which will be passed to the warnings.warn() function.</p>
Self Counter (Python)
2013-08-02T03:15:12-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578621-self-counter/
<p style="color: grey">
Python
recipe 578621
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/counter/">counter</a>).
</p>
<p>This script was written as a revision of <a href="http://code.activestate.com/recipes/577456/">recipe 577456</a> as a variation on a theme.</p>
random send mail, sms or popup window (Python)
2013-09-04T19:43:19-07:00peekaahttp://code.activestate.com/recipes/users/2919471/http://code.activestate.com/recipes/578598-random-send-mail-sms-or-popup-window/
<p style="color: grey">
Python
recipe 578598
by <a href="/recipes/users/2919471/">peekaa</a>
(<a href="/recipes/tags/popup/">popup</a>, <a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sendmail/">sendmail</a>, <a href="/recipes/tags/sms/">sms</a>).
Revision 4.
</p>
<p>If you need to do something irregularly, randomly during the day, you
often forget. This script gives you mail, sms or popup window indefinitely in
random interval to remind you of doing it. It runs forever. If you want to
send emails, uncomment the row sendMail() and fill variable me, to, smtp,
name, login in function sendMail().</p>
Text Editor in Python 3.3 (Python)
2013-06-19T15:58:17-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578569-text-editor-in-python-33/
<p style="color: grey">
Python
recipe 578569
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/text/">text</a>, <a href="/recipes/tags/text_processing/">text_processing</a>).
</p>
<p>This is a simple text editor written in Python using <code>tkinter</code> for graphics.</p>
<p>Check out Captain DeadBones' <a href="http://thelivingpearl.com/">Chronicles</a> blog.</p>
Forward iterator (Python)
2015-01-29T17:12:10-08:00Andrea Corbellinihttp://code.activestate.com/recipes/users/4186880/http://code.activestate.com/recipes/578560-forward-iterator/
<p style="color: grey">
Python
recipe 578560
by <a href="/recipes/users/4186880/">Andrea Corbellini</a>
(<a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/wrapper/">wrapper</a>).
Revision 2.
</p>
<p>A proxy for iterators that lets you <em>read ahead</em> items without consuming the iterator.</p>
Validate XML with schemaLocation (Python)
2013-03-28T10:24:55-07:00Mathias Loeschhttp://code.activestate.com/recipes/users/4185910/http://code.activestate.com/recipes/578503-validate-xml-with-schemalocation/
<p style="color: grey">
Python
recipe 578503
by <a href="/recipes/users/4185910/">Mathias Loesch</a>
(<a href="/recipes/tags/schema/">schema</a>, <a href="/recipes/tags/schemalocation/">schemalocation</a>, <a href="/recipes/tags/validation/">validation</a>, <a href="/recipes/tags/xml/">xml</a>).
Revision 3.
</p>
<p>Addresses problems described at:</p>
<p><a href="http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml" rel="nofollow">http://stackoverflow.com/questions/2979824/in-document-schema-declarations-and-lxml</a>
<a href="https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html" rel="nofollow">https://mailman-mail5.webfaction.com/pipermail/lxml/2011-September/006153.html</a></p>
easy user input (Python)
2013-09-13T06:34:08-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578552-easy-user-input/
<p style="color: grey">
Python
recipe 578552
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/input/">input</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/query/">query</a>, <a href="/recipes/tags/user/">user</a>).
Revision 7.
</p>
<p>Improvement over <a href="http://code.activestate.com/recipes/577058/">Recipe 577058</a> and cie.</p>
<p><code>easy_input()</code> function extends the built-in <code>input()</code> function.
A question is prompted as well as some expected answers.</p>
<p>The user input can be incomplete (ie. <code>y</code> or <code>ye</code> instead of <code>yes</code>)</p>
<ul>
<li>If no list of expected answer is provided, default will be "yes/no".</li>
<li>If no default answer is provided, default will be the first expected answer.</li>
</ul>
<p>Try and see.</p>
<p>Disclaimer: written in python3, meant for *nix shell, indented with tabs</p>
<p><strong>Avoided caveat:</strong> If some expected <code>answer</code> have the same beginning, the user can not enter too few letters. Ex: <code>answer = ['continue', 'test', 'testicle']</code>, user can not input <code>t</code>, <code>te</code> or <code>tes</code> because it will be ambiguous. User can however input <code>test</code>, which is not.</p>
lndir.py (short python version of the BSD/X11 lndir utility) (Python)
2013-05-28T07:21:28-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/578535-lndirpy-short-python-version-of-the-bsdx11-lndir-u/
<p style="color: grey">
Python
recipe 578535
by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a>
(<a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/os_walk/">os_walk</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/walk/">walk</a>).
Revision 2.
</p>
<p>This should be valid for Python 2.6 and up, including the 3.x series.</p>