Popular recipes tagged "meta:loc=34"http://code.activestate.com/recipes/tags/meta:loc=34/2017-05-06T18:45:00-07:00ActiveState Code RecipesTkinter frame with different border sizes (Python)
2017-05-06T18:45:00-07:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580798-tkinter-frame-with-different-border-sizes/
<p style="color: grey">
Python
recipe 580798
by <a href="/recipes/users/4189907/">Miguel Martínez López</a>
(<a href="/recipes/tags/border/">border</a>, <a href="/recipes/tags/size/">size</a>, <a href="/recipes/tags/tkinter/">tkinter</a>).
</p>
<p>This trick shows how to create a bordered frame with different border size in each side.</p>
A utility like Unix seq (command-line), in Python (Python)
2017-01-08T17:48:57-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580744-a-utility-like-unix-seq-command-line-in-python/
<p style="color: grey">
Python
recipe 580744
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/bash/">bash</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/script/">script</a>, <a href="/recipes/tags/seq/">seq</a>, <a href="/recipes/tags/sequence/">sequence</a>, <a href="/recipes/tags/shell/">shell</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>This recipe shows how to create a utility like Unix seq (command-line), in Python.
seq is described here: </p>
<p><a href="https://en.wikipedia.org/wiki/Seq_%28Unix%29" rel="nofollow">https://en.wikipedia.org/wiki/Seq_(Unix)</a></p>
<p>but briefly, it is a command-line utility that takes 1 to 3 arguments (some being optional), the start, stop and step, and prints numbers from the start value to the stop value, on standard output. So seq has many uses in bigger commands or scripts; a common category of use is to quickly generate multiple filenames or other strings that contain numbers in them, for exhaustive testing, load testing or other purposes. A similar command called jot is found on some Unix systems.</p>
<p>This recipe does not try to be exactly the same in functionality as seq. It has some differences. However the core functionality of generating integer sequences is the same (but without steps other than 1 for the range).</p>
<p>More details and sample output are here:</p>
<p><a href="https://jugad2.blogspot.in/2017/01/an-unix-seq-like-utility-in-python.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/an-unix-seq-like-utility-in-python.html</a></p>
<p>The code is below.</p>
Last sunday of each month for a given year (Batch)
2016-06-07T20:25:00-07:00Antoni Gualhttp://code.activestate.com/recipes/users/4182514/http://code.activestate.com/recipes/580677-last-sunday-of-each-month-for-a-given-year/
<p style="color: grey">
Batch
recipe 580677
by <a href="/recipes/users/4182514/">Antoni Gual</a>
(<a href="/recipes/tags/calendar/">calendar</a>).
Revision 2.
</p>
<p>Includes batch routines for day of week, is leap year and last dat of the month</p>
Text ruler for console (record-oriented data processing utility) (Python)
2016-04-17T19:32:26-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580647-text-ruler-for-console-record-oriented-data-proces/
<p style="color: grey">
Python
recipe 580647
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/rulers/">rulers</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple text-based ruler for your command-line console. It can help you find the position of your own program's output on the line, or to find the positions and lengths of fields in fixed- or variable-length records in a text file, fields in CSV files, etc.</p>
Generate behave table (Python)
2015-05-19T07:16:06-07:00Nicolas Laurancehttp://code.activestate.com/recipes/users/4192224/http://code.activestate.com/recipes/579055-generate-behave-table/
<p style="color: grey">
Python
recipe 579055
by <a href="/recipes/users/4192224/">Nicolas Laurance</a>
(<a href="/recipes/tags/bdd/">bdd</a>, <a href="/recipes/tags/behave/">behave</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>When writing BDD code with behave, you may want to include a set of examples for scenario outline, or provide a table for setting up initial conditions. This snippet ease the pain of formatting the table properly as text</p>
Send messages between computers (Python)
2014-01-01T22:11:30-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578802-send-messages-between-computers/
<p style="color: grey">
Python
recipe 578802
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/chat/">chat</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/socket/">socket</a>).
</p>
<p>Simple scripts to chat between computers in the same network.</p>
<p>Both computers must be running both of these scripts and target ip addresses must be set correctly.</p>
<p>(IP address of a computer can be found using ipconfig command.)</p>
JavaScript WebSocket client for Python + Go WebSocket-based system monitoring example (JavaScript)
2014-01-03T21:09:54-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578806-javascript-websocket-client-for-python-go-websocke/
<p style="color: grey">
JavaScript
recipe 578806
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/golang/">golang</a>, <a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/websockets/">websockets</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe is the JavaScript client side of an overall recipe that shows how to do system monitoring using WebSockets, Python, Go (using websocketd), and JavaScript + HTML. The server side of the recipe (in Python, and using websocketd which is written in Go), is here:</p>
<p><a href="http://code.activestate.com/recipes/578803-using-websocketd-with-python-for-web-based-system-/?in=user-4173351" rel="nofollow">http://code.activestate.com/recipes/578803-using-websocketd-with-python-for-web-based-system-/?in=user-4173351</a></p>
<p>The system monitoring example shows the system disk space info (total, used and free) using the Python psutil module.</p>
System Authentication against /etc/shadow or /etc/passwd (Python)
2013-03-11T12:40:10-07:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/578489-system-authentication-against-etcshadow-or-etcpass/
<p style="color: grey">
Python
recipe 578489
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/authentication/">authentication</a>, <a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/system/">system</a>).
</p>
<p>Sometimes it's useful to perform System Authentication against a Local System using the /etc/shadow or /etc/passwd password databases. This recipe provides a simple function that does exactly that.</p>
Human readable file/memory sizes (Python)
2012-11-05T11:59:20-08:00Tony Fluryhttp://code.activestate.com/recipes/users/4184150/http://code.activestate.com/recipes/578321-human-readable-filememory-sizes/
<p style="color: grey">
Python
recipe 578321
by <a href="/recipes/users/4184150/">Tony Flury</a>
(<a href="/recipes/tags/formatting/">formatting</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/size/">size</a>).
Revision 5.
</p>
<p>In writing a application to display the file sizes of set of files, I wanted to provide a human readable size rather then just displaying a byte count (which can get rather big).</p>
<p>I developed this useful short recipe that extends the format specifier mini Language to add the S presentation type - which will intelligently convert the value to be displayed into a known human readable size format - i.e. b, Kb,Mb, Gb etc. It honours the rest of the format specification language (<a href="http://docs.python.org/2/library/string.html#format-string-syntax%29" rel="nofollow">http://docs.python.org/2/library/string.html#format-string-syntax)</a></p>
<p>It uses a factor of 1024 at each stage</p>
Password Request Code + Reading Password from File (Python)
2013-01-19T04:06:45-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578429-password-request-code-reading-password-from-file/
<p style="color: grey">
Python
recipe 578429
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This snippet of code was written as part of an article - <a href="http://thelivingpearl.com/2013/01/18/password-protecting-your-python-application/">Password Protecting You Python Application</a></p>
Genreate x digits prime number in python, version 2 (Python)
2013-01-06T20:27:09-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578405-genreate-x-digits-prime-number-in-python-version-2/
<p style="color: grey">
Python
recipe 578405
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/primes/">primes</a>, <a href="/recipes/tags/prime_generator/">prime_generator</a>).
</p>
<p>This is a prime number generator in python that I put together for an article I wrote <a href="http://thelivingpearl.com/2013/01/06/how-to-find-prime-numbers-in-python/">How To Find Prime Numbers In Python</a>. The script will generate a prime number of x digits, where x is less than 15. It might work for larger x, but it will take a while. </p>
Product Key (Text)
2012-11-01T19:03:23-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578317-product-key/
<p style="color: grey">
Text
recipe 578317
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/ironpython/">ironpython</a>, <a href="/recipes/tags/productkey/">productkey</a>).
</p>
<p>IronPython script for retrieving MS products' keys. There is how to get Windows key in following example.</p>
Extract Multiple TAR Files & Directories (Python)
2012-07-04T00:57:37-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578191-extract-multiple-tar-files-directories/
<p style="color: grey">
Python
recipe 578191
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/tarfile/">tarfile</a>).
</p>
<p>Related to the popular <a href="http://code.activestate.com/recipes/442503/">recipe 442503</a>, this command-line program takes multiple TAR files or directories and can extract them in a batch operation. This is committed for archival to be run under Python 2.5 or later versions.</p>
CGI Directory Contents (Python)
2012-07-02T22:38:31-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578183-cgi-directory-contents/
<p style="color: grey">
Python
recipe 578183
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/old/">old</a>).
</p>
<p>CGI Directory Contents was a simple experiment meant to work similarly to the <code>os.listdir</code> function in Python. This is committed for archival to be run under Python 2.5 or later versions.</p>
Get single keypress (Python)
2011-12-06T08:46:33-08:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577977-get-single-keypress/
<p style="color: grey">
Python
recipe 577977
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/getch/">getch</a>, <a href="/recipes/tags/key/">key</a>).
</p>
<p>Here's a platform-independent module that exposes a single function, getch, which reads stdin for a single character. It uses msvcrt.getch on Windows, and should work on any platform that supports the tty and termios modules (e.g. Linux).</p>
<p>This has been tested on Python 2.4, 2.5 and 2.6 on Linux.</p>
Simple Cli Progress Bar (Python)
2011-11-22T22:00:44-08:00Samuele Millevoltehttp://code.activestate.com/recipes/users/4180021/http://code.activestate.com/recipes/577956-simple-cli-progress-bar/
<p style="color: grey">
Python
recipe 577956
by <a href="/recipes/users/4180021/">Samuele Millevolte</a>
(<a href="/recipes/tags/bar/">bar</a>, <a href="/recipes/tags/progress/">progress</a>, <a href="/recipes/tags/progress_bar/">progress_bar</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>It's a simple function to print a progress bar under cli. When you call the function you have to indicate the max level that indicates 100% and what has been done considering the max level.</p>
Generate automatically list of actions with argparse (Python)
2011-12-02T10:25:13-08:00Andrea Crottihttp://code.activestate.com/recipes/users/4179946/http://code.activestate.com/recipes/577947-generate-automatically-list-of-actions-with-argpar/
<p style="color: grey">
Python
recipe 577947
by <a href="/recipes/users/4179946/">Andrea Crotti</a>
(<a href="/recipes/tags/argparse/">argparse</a>, <a href="/recipes/tags/cli/">cli</a>).
Revision 2.
</p>
<p>This little sample script looks up in locals() to determine all the callables in <em>main</em>.</p>
<p>Then it passes them to parse_arguments, which show an help message as following
usage:</p>
<pre class="prettyprint"><code>prova.py [-h] {func2,func1}
positional arguments:
{func2,func1}
optional arguments:
-h, --help show this help message and exit
</code></pre>
<p>Useful to avoid duplication and make it easy to extend script functions.</p>
Line-oriented processing in Python from command line (like AWK) (Python)
2011-04-14T19:49:16-07:00Artur Siekielskihttp://code.activestate.com/recipes/users/4177664/http://code.activestate.com/recipes/577656-line-oriented-processing-in-python-from-command-li/
<p style="color: grey">
Python
recipe 577656
by <a href="/recipes/users/4177664/">Artur Siekielski</a>
(<a href="/recipes/tags/awk/">awk</a>, <a href="/recipes/tags/bash/">bash</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/shell/">shell</a>).
</p>
<p>A very simple but powerful shell script which enables writing ad-hoc Python scripts for processing line-oriented input. It executes the following code template:</p>
<pre class="prettyprint"><code>$INIT
for line in sys.stdin:
$LOOP
$END
</code></pre>
<p>where $INIT, $LOOP and $END code blocks are given from command line. If only one argument is given, then $INIT and $END are empty. If two arguments are given, $END is empty.</p>
<p>Examples (script is saved as 'pyk' in the $PATH):</p>
<ul>
<li>"wc -l" replacement:
$ cat file | pyk 'c=0' 'c+=1' 'print c'</li>
<li>grep replacement:
$ cat file | pyk 'import re' 'if re.search("\d+", line): print line'</li>
<li>adding all numbers:
$ seq 1 10 | pyk 's=0' 's+=int(line)' 'print s'</li>
<li>prepending lines with it's length:
$ cat file | pyk 'print len(line), line'</li>
<li>longest file name:
$ ls -1 | pyk 'longest=""' 'if len(line) > len(longest): longest=line' 'print longest'</li>
<li>number of unique words in a document:
$ pyk 'words=[]' 'words.extend(line.split())' 'print "All words: {}, unique: {}".format(len(words), len(set(words))'</li>
</ul>
Monkey-patch execnet with more ssh settings, port, identity file, non-interactive (Python)
2011-01-15T16:35:00-08:00Dima Tisnekhttp://code.activestate.com/recipes/users/4068698/http://code.activestate.com/recipes/577545-monkey-patch-execnet-with-more-ssh-settings-port-i/
<p style="color: grey">
Python
recipe 577545
by <a href="/recipes/users/4068698/">Dima Tisnek</a>
(<a href="/recipes/tags/batchmode/">batchmode</a>, <a href="/recipes/tags/execnet/">execnet</a>, <a href="/recipes/tags/identity/">identity</a>, <a href="/recipes/tags/key/">key</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/noninteractive/">noninteractive</a>, <a href="/recipes/tags/port/">port</a>, <a href="/recipes/tags/public/">public</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/ssh/">ssh</a>).
</p>
<p>execnet ( <a href="http://codespeak.net/execnet/" rel="nofollow">http://codespeak.net/execnet/</a> ) is pretty cool, but its ssh support needs a few tweaks, here they are.</p>
<p>This snipped is a monkey patch, not a diff, import it and you can use execnet.makeportgateway instead of makegateway.</p>
<p>Adapt to your needs even more!</p>
Quiz Me 2.5 (exe_queue) (Python)
2011-01-09T21:19:15-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/577526-quiz-me-25-exe_queue/
<p style="color: grey">
Python
recipe 577526
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/program/">program</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quiz/">quiz</a>).
</p>
<p>Before discovering <a href="http://quizlet.com/" rel="nofollow">http://quizlet.com/</a>, the following program was developed for running custom quizzes to help with studying for college courses. The program is not very advanced, but it works reasonably well for what it was designed to do. If the program were developed further, it would need greater capabilities than it currently has and would require a secondary system for actually creating the quizzes (currently, they are hand-typed). Quiz Me could be a starting point for anyone who wishes to actually write a program such as this and inspire others to write much better programs than what this recipe currently offers.</p>
<p>The Pipe and _Method classes presented here are for providing optional thread support to the tkinter GUI library. Oftentimes, it can be inconvenient to play with threads and GUI libraries simultaneously, and most GUI libraries require GUI specific operations to be performed within a certain thread. The classes below are for wrapping the root of the application so that method calls can be executed at a later time within whatever thread they are supposed to be executed in. This program was used as a test platform of the concept though it did not see any of its expected use: threading is not used in this application.</p>