Top-rated recipes tagged "meta:requires=time"http://code.activestate.com/recipes/tags/meta:requires=time/top/2016-10-25T17:53:01-07:00ActiveState Code RecipesModule 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>
A DEMO to show how to write text into the Python terminal Title Bar... (Python)
2013-09-17T17:55:53-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578662-a-demo-to-show-how-to-write-text-into-the-python-t/
<p style="color: grey">
Python
recipe 578662
by <a href="/recipes/users/4177147/">Barry Walker</a>
(<a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/bar/">bar</a>, <a href="/recipes/tags/cygwin/">cygwin</a>, <a href="/recipes/tags/demo/">demo</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/title/">title</a>, <a href="/recipes/tags/title_bar/">title_bar</a>).
</p>
<p>This DEMO is show how to write some text of your choice into the Title Bar of the Python terminal window.</p>
<p>Firstly it imports "time" just for this demo so that a simple time delay can be generated.
Next the Python terminal window is written to for the 5 seconds of delay; see code.
And finally the Title Bar is restored to normal...</p>
<p>A snapshot image can be seen here:-</p>
<p><a href="http://wisecracker.host22.com/public/Title_Bar.jpg" rel="nofollow">http://wisecracker.host22.com/public/Title_Bar.jpg</a></p>
<p>Bazza...</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>
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>
download the Activestate cook book recipe (Python)
2013-01-29T16:24:22-08:00lwz7512http://code.activestate.com/recipes/users/4185066/http://code.activestate.com/recipes/578439-download-the-activestate-cook-book-recipe/
<p style="color: grey">
Python
recipe 578439
by <a href="/recipes/users/4185066/">lwz7512</a>
.
</p>
<p>Small effort to store the python recipes to our local</p>
<p>similar effort by other people:
543267-i-will-download-all-of-the-recipes-from-the-python
535162-i-download-all-the-python-cookbook-recipes</p>
Simple Web Crawler (Python)
2011-01-31T21:57:58-08:00James Millshttp://code.activestate.com/recipes/users/4167757/http://code.activestate.com/recipes/576551-simple-web-crawler/
<p style="color: grey">
Python
recipe 576551
by <a href="/recipes/users/4167757/">James Mills</a>
(<a href="/recipes/tags/crawler/">crawler</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/parsing/">parsing</a>, <a href="/recipes/tags/web/">web</a>).
Revision 2.
</p>
<p>NOTE: This recipe has been updated with suggested improvements since the last revision.</p>
<p>This is a simple web crawler I wrote to
test websites and links. It will traverse
all links found to any given depth.</p>
<p>See --help for usage.</p>
<p>I'm posting this recipe as this kind of
problem has been asked on the Python
Mailing List a number of times... I
thought I'd share my simple little
implementation based on the standard
library and BeautifulSoup.</p>
<p>--JamesMills</p>
PathCatcher - Windows utility for right-click capture of file or folder path (Python)
2008-04-19T17:14:18-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/528893-pathcatcher-windows-utility-for-right-click-captur/
<p style="color: grey">
Python
recipe 528893
by <a href="/recipes/users/4076953/">Jack Trainor</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 3.
</p>
<p>PathCatcher is a Windows utility that allows one to right-click on a file or a folder or a group of files and folders in Explorer and save its path to the clipboard.</p>
<p>To install, save the code as PathCatcher.py, then double-click the PathCatcher.py file. Afterwards, PathCatcher will appear in the Explorer right-click menu.</p>
Retry loop (Python)
2013-05-23T20:45:01-07:00Ryan Nowakowskihttp://code.activestate.com/recipes/users/4186624/http://code.activestate.com/recipes/578527-retry-loop/
<p style="color: grey">
Python
recipe 578527
by <a href="/recipes/users/4186624/">Ryan Nowakowski</a>
(<a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/loop/">loop</a>, <a href="/recipes/tags/retry/">retry</a>, <a href="/recipes/tags/timeout/">timeout</a>).
</p>
<p>Encapsulates the logic of a retry loop using a generator function.</p>
Pipeline made of coroutines (Python)
2012-09-14T09:53:58-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578265-pipeline-made-of-coroutines/
<p style="color: grey">
Python
recipe 578265
by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a>
(<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>A pipeline made of several coroutines that can be turned off gracefully. </p>
Basic synchronization decorator (Python)
2005-12-19T22:29:46-08:00John Fouhyhttp://code.activestate.com/recipes/users/2466667/http://code.activestate.com/recipes/465057-basic-synchronization-decorator/
<p style="color: grey">
Python
recipe 465057
by <a href="/recipes/users/2466667/">John Fouhy</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Synchronization has got to be one of the most popular uses for decorators, but there's no recipe here. So, this is a simple synchronization decorator.</p>
Simple web request benchmark (Python)
2006-10-31T00:27:13-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/442412-simple-web-request-benchmark/
<p style="color: grey">
Python
recipe 442412
by <a href="/recipes/users/1241800/">Josiah Carlson</a>
(<a href="/recipes/tags/web/">web</a>).
Revision 4.
</p>
<p>Recently, there has been a discussion between myself and another individual as to the performance of particular web servers under certain situations. Being that web server testing frameworks have different ways of measuring how long a 'request' takes, I thought I would take the guesswork out of it and measure everything explicitly.</p>
<p>There are generally 5 portions to a web request:
1. create the connection
2. start sending the request
3. finish sending the request
4. start recieving the response
5. finish reading the response</p>
<p>This recipe measures the amount of time to perform each portion of a request to a single file many times, and prints the results in a somewhat reasonable fashion.</p>
Decorator to make a decorated call in a separate thread with timeout (Python)
2005-10-31T05:15:57-08:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/440569-decorator-to-make-a-decorated-call-in-a-separate-t/
<p style="color: grey">
Python
recipe 440569
by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 8.
</p>
<p>Makes it easier to execute async calls or deal with external systems calls to which can block forever or occassionally take long time to complete.</p>
Win32com - Batch repathing of Autocad Xrefs (Python)
2005-08-31T10:28:24-07:00Ed Blakehttp://code.activestate.com/recipes/users/2492237/http://code.activestate.com/recipes/440498-win32com-batch-repathing-of-autocad-xrefs/
<p style="color: grey">
Python
recipe 440498
by <a href="/recipes/users/2492237/">Ed Blake</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
</p>
<p>This script asks for a base directory and then changes all xrefs in all drawings in all subdirectories so that they use relative paths. To use it just copy it somewhere in your target directory structure and run.</p>
ColumnSorterMixin with a Virtual wx.ListCtrl (Python)
2006-08-02T13:07:53-07:00Egor Zindyhttp://code.activestate.com/recipes/users/2493385/http://code.activestate.com/recipes/426407-columnsortermixin-with-a-virtual-wxlistctrl/
<p style="color: grey">
Python
recipe 426407
by <a href="/recipes/users/2493385/">Egor Zindy</a>
.
Revision 2.
</p>
<p>This recipe shows a way of using ColumnSorterMixin with virtual lists (wx.ListCtrl defined with the wx.LC_VIRTUAL flag). The sample code pretty much follows the wx.listctrl demo (provided with wxPython). The interesting bit is the TestVirtualList class and the musicdata dictionary (altogether lines 42 to 257). Remainder is provided to run the TestVirtualList class (or others from the wxdemo) standalone.</p>
<p>The idea is to combine an itemDataMap dictionary with an index table which defines the order in which the dictionary items are displayed. The actual display is handled by the virtual list using only the OnGetItemText, OnGetItemImage and OnGetItemAttr methods.</p>
<p>First of all, the 3 methods OnGetItemText OnGetItemImage and OnGetItemAttr must be declared. Following the ListCtrl demo and in addition to the self.itemDataMap dictionary, a self.itemIndexMap table defines the items order.</p>
<p>ColumnSorterMixin uses the SortItems, GetListrCtrl and GetSortImages methods which are redefined here.
GetListCtrl now returns self (to keep the mixin happy). GetSortImages is pretty much the same as in the ListCtrl demo, only it uses the art provider (needs better looking arrows, maybe). SortItems handles the sorting. It gets the column clicked from the mixin _col variable and the sorting order (ascending or descending) from the _colSortFlag table.</p>
socket.recv -- three ways to turn it into recvall (Python)
2005-04-05T13:47:57-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/408859-socketrecv-three-ways-to-turn-it-into-recvall/
<p style="color: grey">
Python
recipe 408859
by <a href="/recipes/users/135654/">John Nielsen</a>
(<a href="/recipes/tags/network/">network</a>).
Revision 5.
</p>
<p>An issue with socket.recv is how to know when you are done receiving data. A TCP stream guarantees the bytes will not arrive out of order or be sent more than once. But you do not know the size of the data that will be sent to you. 100 bytes could be sent as group of 10 bytes or maybe in one shot. Ultimately, this means you have to use a loop in some fashion until you know it is done.</p>
<p>The basic recv returns an empty string when the socket is disconnected.
From that you can build a simple loop that will work as long as the sender manages to disconnect the socket at the appropriate time. However, there could be situations where a local error will mask as a clean shutdown or maybe a close() is never called.</p>
<p>Three very basic methods are shown below that try to fix that problem. They use either a time-based, end marker, or size of payload method. Since you cannot be sure just what you are going to receive, you have to be careful that you get enough of a message to determine the size of payload or end marker.</p>
<p>I updated the recv_size method to allocate data in larger chunks if it gets a large stream of data, which can increase performance.</p>
handler stack (Python)
2004-09-01T18:00:14-07:00Dan Perlhttp://code.activestate.com/recipes/users/1634351/http://code.activestate.com/recipes/302422-handler-stack/
<p style="color: grey">
Python
recipe 302422
by <a href="/recipes/users/1634351/">Dan Perl</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>Design pattern that is highly reusable. Simple handlers implement one specific task from a complex set of tasks to be performed on an object. Such handlers can then be layered in a stack, in different combinations, together achieving complex processing of an object. New handlers are easy to implement and add.</p>
A generic programming thread pool (Python)
2003-09-24T17:24:43-07:00Tim Lesherhttp://code.activestate.com/recipes/users/161138/http://code.activestate.com/recipes/203871-a-generic-programming-thread-pool/
<p style="color: grey">
Python
recipe 203871
by <a href="/recipes/users/161138/">Tim Lesher</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 3.
</p>
<p>A thread pool class that takes arbitrary callables as work units, and supports callbacks when the work unit is complete.</p>
Threads, Tkinter and asynchronous I/O (Python)
2001-10-21T07:14:35-07:00Jacob Hallénhttp://code.activestate.com/recipes/users/136936/http://code.activestate.com/recipes/82965-threads-tkinter-and-asynchronous-io/
<p style="color: grey">
Python
recipe 82965
by <a href="/recipes/users/136936/">Jacob Hallén</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>This recipe shows the easiest way of handling access to sockets, serial ports
and other asynchronous I/O ports while running a Tkinter based GUI.
It allows for a worker thread to block in a select(). Whenever something arrives
it will received and inserted in a queue. The main (GUI) thread then polls
the queue 10 times per second (often enough so the user will not notice any
significant delay), and processes all messages that have arrived.</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>
Simple command-line alarm clock in Python (Python)
2015-10-25T18:27:27-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579117-simple-command-line-alarm-clock-in-python/
<p style="color: grey">
Python
recipe 579117
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how to create a simple alarm clock in Python, that can be run from the command line in a terminal. It lets you specify the alarm time in minutes as a command line argument, and prints a wake-up message and beeps a few times, after that time arrives. You can use 0 for the minutes to test it immediately, including to adjust the volume using your speaker controls.</p>