Top-rated recipes tagged "python3"http://code.activestate.com/recipes/tags/python3/top/2017-01-05T16:57:15-08:00ActiveState Code RecipesRsync 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> Lines 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> 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> 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> Give Python code a web plus command-line interface with hug (Python) 2017-01-05T16:57:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580742-give-python-code-a-web-plus-command-line-interface/ <p style="color: grey"> Python recipe 580742 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/hug/">hug</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/user_interface/">user_interface</a>, <a href="/recipes/tags/web/">web</a>, <a href="/recipes/tags/web_server/">web_server</a>). </p> <p>This recipe shows how to take a Python function and wrap it with both a web and a command-line interface, somewhat easily, using the hug Python library. The example used shows how to wrap a function that uses the psutil library to get information on disk partitions. So you can see the disk partition info either via the web browser or the command line. The code for the recipe is shown below. It is also possible to wrap multiple functions in the same Python file, and expose all of them via both the web and the command-line.</p> <p>More information and multiple sample outputs are available here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/give-your-python-function-webcli-hug.html</a></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> Handle exit context manager (Python) 2014-08-01T08:28:07-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577997-handle-exit-context-manager/ <p style="color: grey"> Python recipe 577997 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/atexit/">atexit</a>, <a href="/recipes/tags/contextlib/">contextlib</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/sigint/">sigint</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sigterm/">sigterm</a>). Revision 23. </p> <p>A context manager which properly handles SIGTERM (SystemExit) and SIGINT (KeyboardInterrupt) signals, registering a function which is always guaranteed to be called on interpreter exit. Also, it makes sure to execute previously registered functions as well (if any).</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> 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> 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> PyOOCalc - Python Libre/Open Office Calc Interface API (UNO) (Python) 2016-01-10T13:12:53-08:00Yuriihttp://code.activestate.com/recipes/users/4193384/http://code.activestate.com/recipes/579147-pyoocalc-python-libreopen-office-calc-interface-ap/ <p style="color: grey"> Python recipe 579147 by <a href="/recipes/users/4193384/">Yurii</a> (<a href="/recipes/tags/openoffice/">openoffice</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>Create Libre/Open Office Calc documents, reports on Python</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> 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> 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> @unittest.skip_others decorator (Python) 2012-08-06T02:20:19-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578234-unittestskip_others-decorator/ <p style="color: grey"> Python recipe 578234 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/skip/">skip</a>, <a href="/recipes/tags/unittest/">unittest</a>). Revision 3. </p> <p>Decorator which skips all tests except the one decorated with it. Based on original proposal on python-ideas: <a href="http://mail.python.org/pipermail/python-ideas/2010-August/007992.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2010-August/007992.html</a></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>