Top-rated recipes tagged "meta:requires=sys"http://code.activestate.com/recipes/tags/meta:requires=sys/top/2014-09-06T10:35:54-07:00ActiveState Code RecipesNamed Tuples (Python)
2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/
<p style="color: grey">
Python
recipe 500261
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 15.
</p>
<p>Fast, lightweight attribute-style access to tuples.</p>
Creating a daemon the Python way (Python)
2005-10-03T16:49:09-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
<p style="color: grey">
Python
recipe 278731
by <a href="/recipes/users/1760491/">Chad J. Schroeder</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 6.
</p>
<p>The Python way to detach a process from the controlling terminal and run it in the
background as a daemon.</p>
Module to allow Asynchronous subprocess use on Windows and Posix platforms (Python)
2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/
<p style="color: grey">
Python
recipe 440554
by <a href="/recipes/users/1241800/">Josiah Carlson</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 10.
</p>
<p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p>
<p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p>
<p>recv() and recv_err() both read at most maxsize bytes from the started subprocess.
send() sends strings to the started subprocess.
send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p>
<p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p>
<p>v. 1.3 fixed a few bugs relating to *nix support
v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module.
v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford.
v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing.
v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment.
v. 1.9 Fixed fcntl calls for closed handles.</p>
Determining Current Function Name (Python)
2001-07-17T03:24:20-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66062-determining-current-function-name/
<p style="color: grey">
Python
recipe 66062
by <a href="/recipes/users/97991/">Alex Martelli</a>
.
</p>
<p>You want to determine the name of the currently running function, e.g. to create error messages that don't need to be changed when copied to other functions. Function _getframe of module sys does this and much more.</p>
Fork a daemon process on Unix (Python)
2001-07-10T14:01:38-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/
<p style="color: grey">
Python
recipe 66012
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Forking a daemon on Unix requires a certain sequence of system calls. Since Python exposes a full POSIX interface, this can be done in Python, too.</p>
Win Services helper (Python)
2014-09-06T10:35:54-07:00Louis RIVIEREhttp://code.activestate.com/recipes/users/4035877/http://code.activestate.com/recipes/551780-win-services-helper/
<p style="color: grey">
Python
recipe 551780
by <a href="/recipes/users/4035877/">Louis RIVIERE</a>
(<a href="/recipes/tags/services/">services</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/windows/">windows</a>).
Revision 4.
</p>
<p>A simple way to implement Windows Service.</p>
Using terminfo for portable color output & cursor control (Python)
2006-03-27T19:20:16-08:00Edward Loperhttp://code.activestate.com/recipes/users/2637812/http://code.activestate.com/recipes/475116-using-terminfo-for-portable-color-output-cursor-co/
<p style="color: grey">
Python
recipe 475116
by <a href="/recipes/users/2637812/">Edward Loper</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 3.
</p>
<p>The curses module defines several functions (based on terminfo) that can be used to perform lightweight cursor control & output formatting (color, bold, etc). These can be used without invoking curses mode (curses.initwin) or using any of the more heavy-weight curses functionality. This recipe defines a TerminalController class, which can make portable output formatting very simple. Formatting modes that are not supported by the terminal are simply omitted.</p>
Compute Memory footprint of an object and its contents (Python)
2012-11-23T23:57:31-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577504-compute-memory-footprint-of-an-object-and-its-cont/
<p style="color: grey">
Python
recipe 577504
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/sizeof/">sizeof</a>).
Revision 3.
</p>
<p>Recursive version sys.getsizeof(). Extendable with custom handlers.</p>
Lightweight Unittester (Python)
2009-04-07T19:47:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/572194-lightweight-unittester/
<p style="color: grey">
Python
recipe 572194
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
Revision 4.
</p>
<p>Inspired by py.test, the unittester is a single, simple function that is easily customized. The main virtue is that tests are much clearer than with the unittest.py.</p>
Tail Call Optimization Decorator (Python)
2006-02-26T15:02:54-08:00Crutcher Dunnavanthttp://code.activestate.com/recipes/users/2792865/http://code.activestate.com/recipes/474088-tail-call-optimization-decorator/
<p style="color: grey">
Python
recipe 474088
by <a href="/recipes/users/2792865/">Crutcher Dunnavant</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>This decorator implements tail call optimization through stack introspection.</p>
Parsing the command line (Python)
2004-04-18T08:15:21-07:00Michele Simionatohttp://code.activestate.com/recipes/users/1122360/http://code.activestate.com/recipes/278844-parsing-the-command-line/
<p style="color: grey">
Python
recipe 278844
by <a href="/recipes/users/1122360/">Michele Simionato</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 2.
</p>
<p>The module optparse was a great addition to Python 2.3, since it is much more
powerful and easier to use than getopt. Using optparse, writing command-line
tools is a breeze. However, the power of optparse comes together with a certain
verbosity. This recipe allows to use optparse with a minimum of boilerplate,
trading flexibility for easy of use. Still, it covers 95% of my common needs,
so I think it may be useful to others.</p>
getch()-like unbuffered character reading from stdin on both Windows and Unix (Python)
2003-01-07T02:31:32-08:00Danny Yoohttp://code.activestate.com/recipes/users/98032/http://code.activestate.com/recipes/134892-getch-like-unbuffered-character-reading-from-stdin/
<p style="color: grey">
Python
recipe 134892
by <a href="/recipes/users/98032/">Danny Yoo</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 2.
</p>
<p>A small utility class to read single characters from standard input, on both Windows and UNIX systems. It provides a getch() function-like instance.</p>
Yet Another Python Templating Utility (YAPTU) (Python)
2001-08-31T15:17:32-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52305-yet-another-python-templating-utility-yaptu/
<p style="color: grey">
Python
recipe 52305
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 6.
</p>
<p>"Templating" (copying an input file to output, on the fly inserting Python expressions and statements) is a frequent need, and YAPTU is a small but complete Python module for that; expressions and statements are identified by arbitrary user-chosen regular-rexpressions.</p>
Simple tool for simulating classes using closures and nested scopes (Python)
2012-04-11T06:21:18-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578091-simple-tool-for-simulating-classes-using-closures-/
<p style="color: grey">
Python
recipe 578091
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/object/">object</a>, <a href="/recipes/tags/oriented/">oriented</a>).
Revision 9.
</p>
<p>Closured-based alternative to normal classes. Allows a faster, cleaner coding style at the expense of some functionality.</p>
Email pretty tracebacks to yourself (or someone you love) (Python)
2005-10-19T08:13:27-07:00Cliff Wellshttp://code.activestate.com/recipes/users/2631558/http://code.activestate.com/recipes/442459-email-pretty-tracebacks-to-yourself-or-someone-you/
<p style="color: grey">
Python
recipe 442459
by <a href="/recipes/users/2631558/">Cliff Wells</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Even production applications have bugs, and it would be nice to have Python tracebacks emailed to you rather than dumped to the hapless user's screen. This recipe shows you how.</p>
Pyline: a grep-like, sed-like command-line tool. (Python)
2006-03-30T14:50:24-08:00Graham Fawcetthttp://code.activestate.com/recipes/users/108898/http://code.activestate.com/recipes/437932-pyline-a-grep-like-sed-like-command-line-tool/
<p style="color: grey">
Python
recipe 437932
by <a href="/recipes/users/108898/">Graham Fawcett</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 3.
</p>
<p>This utility was born from the fact that I keep forgetting how to use
"sed", and I suck at Perl. It brings ad-hoc command-line piping
sensibilities to the Python interpeter. (Version 1.2 does better
outputting of list-like results, thanks to Mark Eichin.)</p>
DBF reader and writer (Python)
2009-04-29T07:28:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/362715-dbf-reader-and-writer/
<p style="color: grey">
Python
recipe 362715
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 7.
</p>
<p>Reader iterates over records in Dbase or Xbase files.
Writer creates dbf files from Python sequences.</p>
Calling Windows API using ctypes and win32con (Python)
2008-07-25T20:01:50-07:00Gary Eakinshttp://code.activestate.com/recipes/users/470921/http://code.activestate.com/recipes/208699-calling-windows-api-using-ctypes-and-win32con/
<p style="color: grey">
Python
recipe 208699
by <a href="/recipes/users/470921/">Gary Eakins</a>
(<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/windows_api/">windows_api</a>).
Revision 4.
</p>
<p>It is easy to call Windows API dlls using the
ctypes module with win32con defining the
constant values for message identifiers and
parameter flags. The demo code shows a simple
but complete application that registers a
window class and a Python WndProc callback function,
creates the window and pumps messages.</p>
Povray for python (Python)
2003-06-13T21:22:18-07:00Simon Burtonhttp://code.activestate.com/recipes/users/860809/http://code.activestate.com/recipes/205451-povray-for-python/
<p style="color: grey">
Python
recipe 205451
by <a href="/recipes/users/860809/">Simon Burton</a>
(<a href="/recipes/tags/graphics/">graphics</a>).
</p>
<p>Here is a relatively simple framework for making povray files from your favourite programming language, python. It's good for creating structured/mathematical scenes and animations.</p>
Finite State Machine (FSM) (Python)
2007-12-05T01:25:49-08:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/146262-finite-state-machine-fsm/
<p style="color: grey">
Python
recipe 146262
by <a href="/recipes/users/103276/">Noah Spurrier</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>This recipe shows a Finite State Machine (FSM) that can be used for small parsing tasks. The code is quite simple. The bulk of it is comments. In addition to state this FSM also maintains a user defined "memory". So this FSM is a Push-down Automata (PDA) since a PDA is a FSM + memory. This module contains an example function that demonstrates a simple RPN expression evaluator.</p>