Popular recipes tagged "python3" but not "python"http://code.activestate.com/recipes/tags/python3-python/2016-10-25T17:53:01-07:00ActiveState Code RecipesLines Of Code (LOC) (Python) 2016-10-25T17:53:01-07:00Jean Brouwershttp://code.activestate.com/recipes/users/2984142/http://code.activestate.com/recipes/580709-lines-of-code-loc/ <p style="color: grey"> Python recipe 580709 by <a href="/recipes/users/2984142/">Jean Brouwers</a> (<a href="/recipes/tags/count/">count</a>, <a href="/recipes/tags/lines/">lines</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/source/">source</a>). Revision 3. </p> <p>Count the number of lines (code, comment, blank) in one or several Python source files.</p> LRU 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> [python3-tk/ttk] Onager Scratchpad (Python) 2016-04-24T02:34:03-07:00Mickey Kocichttp://code.activestate.com/recipes/users/4193984/http://code.activestate.com/recipes/580650-python3-tkttk-onager-scratchpad/ <p style="color: grey"> Python recipe 580650 by <a href="/recipes/users/4193984/">Mickey Kocic</a> (<a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text_processing/">text_processing</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/ttk/">ttk</a>, <a href="/recipes/tags/windows/">windows</a>). Revision 2. </p> <p>I wrote this simple text editor to use for my diary. It's customized the way I like it, but the code is set up so it's easy for other people to change bg, fg, font, etc. I've also compiled a standalone Windows executable (thank you very much ActiveState! without ActivePython the compilation would have been impossible). Anyone who wants a copy of the executable is free to message or email me.</p> <p>NOTE: If you get an error that the theme is not recognized, just comment out line 18 or run the following code in your python3 interpreter:</p> <pre class="prettyprint"><code>&gt;&gt;&gt;from tkinter.ttk import Style &gt;&gt;&gt;s = Style() &gt;&gt;&gt;s.theme_use() </code></pre> <p>You'll get a list of the available themes and can replace the 'alt' in line 18 with any one of them you want.</p> generates a set of binary files by doing ASCII replacements on a master binary file, controlled by a csv file (Python) 2015-09-16T16:25:49-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/579099-generates-a-set-of-binary-files-by-doing-ascii-rep/ <p style="color: grey"> Python recipe 579099 by <a href="/recipes/users/4182514/">Antoni Gual</a> (<a href="/recipes/tags/ascii/">ascii</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/replace/">replace</a>). </p> <p>A hack that saved me a lot of time. Well perhaps not so much, but it was funnier than to do it manually. </p> Decorator for defining functions with keyword-only arguments (Python) 2015-07-10T15:25:04-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579079-decorator-for-defining-functions-with-keyword-only/ <p style="color: grey"> Python recipe 579079 by <a href="/recipes/users/4192487/">Oscar Byrne</a> (<a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/metadecorator/">metadecorator</a>, <a href="/recipes/tags/positional/">positional</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/python3/">python3</a>). Revision 2. </p> <p>Python2.x implementation of python3's keyword-only arguments (aka arguments that must be specified as keywords, and are not automatically filled in by positional arguments - see PEP 3102).</p> Game of Life - Python 3.4 & tkinter (Python) 2014-09-05T15:02:11-07:00Peter Mhttp://code.activestate.com/recipes/users/4190729/http://code.activestate.com/recipes/578928-game-of-life-python-34-tkinter/ <p style="color: grey"> Python recipe 578928 by <a href="/recipes/users/4190729/">Peter M</a> (<a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). </p> <p>I took this well-know game to design my own solution, and make a GUI for it.</p> <p>To run the program, you will need to create a txt file with rows of dots, so like this:</p> <p>...... ...... ...... ......</p> <p>and name it empty_board.gol (I have tested this program withwith 55x55)</p> <p>It is functional and not object oriented.</p> <p>My game of life algorithm is probably not the best one around (suggestions welcome), but I think this program is a nice demo of with tkinter can do and how easy it is to program GUI's with tkinter.</p> <p>It has been written and tested in python 3.4 but I guess any version 3 should work, and probably only minor changes are needed to make it work in 2.6 or 2.7 (e.g. change tkinter to Tkinter).</p> All in one Area Calculator (Python) 2014-07-03T09:16:14-07:00karimhttp://code.activestate.com/recipes/users/4190244/http://code.activestate.com/recipes/578901-all-in-one-area-calculator/ <p style="color: grey"> Python recipe 578901 by <a href="/recipes/users/4190244/">karim</a> (<a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/circlearea/">circlearea</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/tkinter/">tkinter</a>, <a href="/recipes/tags/tranglearea/">tranglearea</a>, <a href="/recipes/tags/trianglearea/">trianglearea</a>). Revision 2. </p> <p>Its a python 3 based All in one Area Calculator.you can calculate Area of Rectangle,Area of Triangle and Area of Circle</p> Droids ( a text adventure ) (Python) 2013-07-27T09:44:06-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578618-droids-a-text-adventure/ <p style="color: grey"> Python recipe 578618 by <a href="/recipes/users/4186699/">jay</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text/">text</a>). </p> <p>for beginners.</p> Droids 2 ( IF ) (Python) 2013-08-13T09:12:11-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578636-droids-2-if/ <p style="color: grey"> Python recipe 578636 by <a href="/recipes/users/4186699/">jay</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/if/">if</a>, <a href="/recipes/tags/learning/">learning</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/text/">text</a>). Revision 2. </p> <h4 id="-for-learners-and-beginners-like-myself-">-For learner's and beginner's like myself -</h4> <h4 id="-and-for-anyone-who-may-have-enjoyed-droids-">-and for anyone who may have enjoyed Droids. -</h4> 2 very basic text games. (Python) 2013-07-09T03:08:47-07:00jayhttp://code.activestate.com/recipes/users/4186699/http://code.activestate.com/recipes/578596-2-very-basic-text-games/ <p style="color: grey"> Python recipe 578596 by <a href="/recipes/users/4186699/">jay</a> (<a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/fun/">fun</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>my versions of two games found in Al Sweigart's book "invent with python"</p> Type checking using Python 3.x annotations (Python) 2013-05-23T22:46:19-07:00David Mertzhttp://code.activestate.com/recipes/users/4173018/http://code.activestate.com/recipes/578528-type-checking-using-python-3x-annotations/ <p style="color: grey"> Python recipe 578528 by <a href="/recipes/users/4173018/">David Mertz</a> (<a href="/recipes/tags/annotations/">annotations</a>, <a href="/recipes/tags/check/">check</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/type/">type</a>, <a href="/recipes/tags/typecheck/">typecheck</a>). Revision 5. </p> <p>Some other recipes have been suggested to allow type checking by various means. Some of these require the use of type specification in a decorator itself. Others try to be much more elaborate in processing a large variety of annotations (but hence require much more and more convoluted code).</p> <p>The recipe provided below is very short, and simply provides actual <strong>type</strong> checking of arguments and return values. It utilizes an unadorned decorator, rather than manufacture one that is parameterized by types or other arguments.</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> emingoo - Extract emails from google search results (Python) 2014-07-30T07:23:36-07:00p@ntut$http://code.activestate.com/recipes/users/4183895/http://code.activestate.com/recipes/578498-emingoo-extract-emails-from-google-search-results/ <p style="color: grey"> Python recipe 578498 by <a href="/recipes/users/4183895/">p@ntut$</a> (<a href="/recipes/tags/python3/">python3</a>). Revision 2. </p> <p>This is a quick and dirty script that does not use google REST api so use this with warning. It extracts emails from google search results. <a href="http://pantuts.com/2013/03/20/emingoo-extract-emails-from-google-search-results/" rel="nofollow">http://pantuts.com/2013/03/20/emingoo-extract-emails-from-google-search-results/</a></p> Archimedes Method for PI (arbitrary precision) (Python) 2013-03-02T21:56:42-08:00Bjorn Madsenhttp://code.activestate.com/recipes/users/4176352/http://code.activestate.com/recipes/578478-archimedes-method-for-pi-arbitrary-precision/ <p style="color: grey"> Python recipe 578478 by <a href="/recipes/users/4176352/">Bjorn Madsen</a> (<a href="/recipes/tags/decimal/">decimal</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/pi/">pi</a>, <a href="/recipes/tags/precision/">precision</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>The code below incorporates the newly revised python 3.3.0 decimal module to permit arbitrary precision (<a href="http://docs.python.org/3/library/decimal.html%29" rel="nofollow">http://docs.python.org/3/library/decimal.html)</a></p> <p>The recipe was originally by FB35 from <a href="http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/" rel="nofollow">http://code.activestate.com/recipes/576981-archimedes-method-for-calculating-pi/</a> </p> Use rot13 to en/decrypt clear text (Python) 2012-11-07T09:17:51-08:00Eric.sunhttp://code.activestate.com/recipes/users/4183602/http://code.activestate.com/recipes/578322-use-rot13-to-endecrypt-clear-text/ <p style="color: grey"> Python recipe 578322 by <a href="/recipes/users/4183602/">Eric.sun</a> (<a href="/recipes/tags/encrypt/">encrypt</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>Encryption.Using your solution to the previous problem, and create a "rot13" translator. "rot13" is an old and fairly simplistic encryption routine where by each letter of the alphabet is rotated 13 characters. Letters in the first half of the alphabet will be rotated to the equivalent letter in the second half and vice versa, retaining case. For example, 'a' goes to 'n' and 'X' goes to 'K'. Obviously, numbers and symbols are immune from translation.</p> topological sorting again (Python) 2013-03-06T19:21:11-08:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578406-topological-sorting-again/ <p style="color: grey"> Python recipe 578406 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/graph/">graph</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/topological/">topological</a>). Revision 9. </p> <p>Topological sorting is the answer to the following question : in a direct acyclic graph, how can I pick up nodes "in order", such as upstream nodes are always before downstream ones ? Many solutions may exists, many algorithms too.</p> <p>Alas, it seems I'm too stupid to understand already proposed recipes on topological sorting. Hopefully I do grasp the "write once, read many" concept.</p> <p>Here, you will find a plain algorithm, optimized only for code clarity, of a topological sorting for direct acyclic graphs, implemented in python from the pseudo code found on <a href="http://en.wikipedia.org/wiki/Topological_sorting">wikipedia</a>:</p> <pre class="prettyprint"><code>L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then return error (graph has at least one cycle) else return L (a topologically sorted order) </code></pre> <p>Only tested with python3.2, should work with other versions. Be careful, code indented with tabs, since space is evil è_é</p> Sending non-ASCII emails from Python 3 (Python) 2012-09-16T07:50:17-07:00Krystian Rosińskihttp://code.activestate.com/recipes/users/4182314/http://code.activestate.com/recipes/578150-sending-non-ascii-emails-from-python-3/ <p style="color: grey"> Python recipe 578150 by <a href="/recipes/users/4182314/">Krystian Rosiński</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/smtp/">smtp</a>). Revision 17. </p> <p>Simple Python 3 module for sending emails with attachments through an SMTP server.</p> <p>The module supports non-ASCII characters in sender name, subject, message and file names.</p> Demonstrate Rochambeau (Python) 2012-11-01T14:07:54-07:00Eric.sunhttp://code.activestate.com/recipes/users/4183602/http://code.activestate.com/recipes/578313-demonstrate-rochambeau/ <p style="color: grey"> Python recipe 578313 by <a href="/recipes/users/4183602/">Eric.sun</a> (<a href="/recipes/tags/python3/">python3</a>). </p> <p>demonstrate "stone,stainless,paper" game, user could select a sign, and system will generate random sign to compete with user.</p> Random user generator (Python) 2012-02-27T16:58:33-08:00Koppula Varun Rajhttp://code.activestate.com/recipes/users/4181072/http://code.activestate.com/recipes/578055-random-user-generator/ <p style="color: grey"> Python recipe 578055 by <a href="/recipes/users/4181072/">Koppula Varun Raj</a> (<a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/pickle/">pickle</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>I've worked on a random user generator in C a long time ago, so I thought I'd make an advanced version of it in Python(3.2). I'll have you know, I am an amateur in Python and even programming for that matter so I'd really appreciate some criticism on my code and what more can be added.</p> <p>About the program - I've added a default list of users, you can modify the list to your convenience and for data persistence, I made use of the pickle module. The random module plays the pivotal role generating the output making use of the choice method. Those along with the os module if the program is being run for the first time on a pc, it will create a data file with the default list.After some digging around, I managed to find os.getlogin() from the python library which was exactly what I was looking for (to get the computer's name making sure it can get the data file to the correct directory). I'm a little skeptical about that part, wondering whether or not it will work on every pc.</p> Rsync Algorithm (Python) 2011-01-09T16:32:22-08:00Eric Pruitthttp://code.activestate.com/recipes/users/4170757/http://code.activestate.com/recipes/577518-rsync-algorithm/ <p style="color: grey"> Python recipe 577518 by <a href="/recipes/users/4170757/">Eric Pruitt</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/delta/">delta</a>, <a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/patch/">patch</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/rsync/">rsync</a>). Revision 4. </p> <p>This is a pure Python implementation of the <a href="http://samba.anu.edu.au/rsync/">rsync algorithm</a>. On my desktop (3.0GHz dual core, 7200RPM), best case throughput for target file hash generation and delta generation is around 2.9MB/s. Absolute worst case scenario (no blocks in common) throughput for delta generation is 200KB/s to 300KB/s on the same system.</p> <p>Tested in Python 2.5, 2.6, and 3.1. In 2.7, io.BufferedReader should yield the best throughput. On all other versions use __builtin__.open.</p>