Most viewed recipes tagged "meta:requires=time"http://code.activestate.com/recipes/tags/meta:requires=time/views/2017-07-15T00:46:59-07:00ActiveState Code RecipesUno (Text-Based) (Python) 2017-07-15T00:46:59-07:00Brandon Martinhttp://code.activestate.com/recipes/users/4194238/http://code.activestate.com/recipes/580811-uno-text-based/ <p style="color: grey"> Python recipe 580811 by <a href="/recipes/users/4194238/">Brandon Martin</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/text_game/">text_game</a>, <a href="/recipes/tags/uno/">uno</a>). </p> <p>A text based recreation of the classic card game featuring functional AIs to play with. Some rules have been modified. User interface is text based, non-curses, using only simple python commands to draw it. </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> 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> Implementing function-based callbacks in Python (Python) 2017-04-19T18:03:11-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580787-implementing-function-based-callbacks-in-python/ <p style="color: grey"> Python recipe 580787 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/callback/">callback</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/techniques/">techniques</a>). </p> <p>This recipe shows a simple way of implementing callbacks in Python. There are a few ways this can be done. The way shown here uses a simple function-based approach.</p> <p>In a nutshell, a callback can be informally described like this: function <strong>a</strong> calls function <strong>b</strong>, and wants to make <strong>b</strong> run a specific independent chunk of code at some point during <strong>b</strong>'s execution. We want to be able to vary which chunk of code gets called in different calls to <strong>b</strong>, so it cannot be hard-coded inside <strong>b</strong>. So function <strong>a</strong> passes another function, <strong>c</strong>, to <strong>b</strong>, as one argument, and <strong>b</strong> uses that parameter <strong>c</strong> to call the functionality that <strong>a</strong> wants <strong>b</strong> to call. (Function <strong>b</strong> may pass some parameters to the function represented by <strong>c</strong>, when it calls it. These could be either internally generated, passed from <strong>a</strong>, or a combination of both). So, by changing the value of the function <strong>c</strong> that gets passed to <strong>b</strong> (on different calls to <strong>b</strong>), <strong>a</strong> can change what chunk of code <strong>b</strong> calls.</p> <p>More details and full code, description and output here:</p> <p><a href="https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html" rel="nofollow">https://jugad2.blogspot.in/2017/04/implementing-and-using-callbacks-in.html</a></p> First / Last Day of the Month (Python) 2006-03-29T20:10:43-08:00Jeff Hinrichshttp://code.activestate.com/recipes/users/2833360/http://code.activestate.com/recipes/476197-first-last-day-of-the-month/ <p style="color: grey"> Python recipe 476197 by <a href="/recipes/users/2833360/">Jeff Hinrichs</a> . </p> <p>I often do reporting that requires the first and last day of the month as bounds. I ended up writing repetitive code until I wrote these utility functions. You might find them useful too.</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> 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> 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> high precision FPS (Python) 2015-05-12T12:04:38-07:00Jiri Justrahttp://code.activestate.com/recipes/users/4192188/http://code.activestate.com/recipes/579053-high-precision-fps/ <p style="color: grey"> Python recipe 579053 by <a href="/recipes/users/4192188/">Jiri Justra</a> (<a href="/recipes/tags/fps/">fps</a>, <a href="/recipes/tags/game/">game</a>, <a href="/recipes/tags/games/">games</a>, <a href="/recipes/tags/pure/">pure</a>). Revision 2. </p> <p>This code adjust itself for set FPS value. It is much more precise that time.sleep fps implementation.</p> tail -f in Python (Python) 2002-10-16T20:17:44-07:00Erik Max Francishttp://code.activestate.com/recipes/users/752960/http://code.activestate.com/recipes/157035-tail-f-in-python/ <p style="color: grey"> Python recipe 157035 by <a href="/recipes/users/752960/">Erik Max Francis</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>A simple implementation of the standard UNIX utility tail -f in Python.</p> simplest useful (I hope!) thread pool example (Python) 2004-09-08T13:53:48-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/302746-simplest-useful-i-hope-thread-pool-example/ <p style="color: grey"> Python recipe 302746 by <a href="/recipes/users/135654/">John Nielsen</a> . Revision 6. </p> <p>You can find examples on how to do threading, but they do not show off a thread pool. My goal was to get as small and simple as possible working thread pool example to show off the basic ideas without having extraneous things to understand. To show off the thread pool, I want stopping and starting of the threads to be explicit. This means the pool won't start until you are ready and will run forever until you are ready for it to stop. The main thread puts into the input queue and removes data from the output queue. The thread pool simply does the converse. Errors are also managed with another queue, so there is a clean distinction between errors and successful results.</p> A very simple session handling example (Python) 2004-10-28T10:38:30-07:00Jonas Galvezhttp://code.activestate.com/recipes/users/2122857/http://code.activestate.com/recipes/325484-a-very-simple-session-handling-example/ <p style="color: grey"> Python recipe 325484 by <a href="/recipes/users/2122857/">Jonas Galvez</a> (<a href="/recipes/tags/cgi/">cgi</a>). Revision 4. </p> <p>This is a very simple session handling example that uses plain Python CGI (tested only under Python 2.2+). Its goal is to show how cookies are set via HTTP and how easily they can be used for session management.</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> Caching decorator with timeout, selective invalidation (Python) 2005-11-05T05:47:03-08:00Greg Steffensenhttp://code.activestate.com/recipes/users/2649990/http://code.activestate.com/recipes/442513-caching-decorator-with-timeout-selective-invalidat/ <p style="color: grey"> Python recipe 442513 by <a href="/recipes/users/2649990/">Greg Steffensen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>A caching decorator that garbage collects in a separate thread (for performance), allows each cached function to (optionally) set a custom maximum age for entries, and allows individual cache entries to be selectively invalidated.</p> icmplib: library for creating and reading ICMP packets (Python) 2007-07-15T19:37:46-07:00Brett Cannonhttp://code.activestate.com/recipes/users/98030/http://code.activestate.com/recipes/409689-icmplib-library-for-creating-and-reading-icmp-pack/ <p style="color: grey"> Python recipe 409689 by <a href="/recipes/users/98030/">Brett Cannon</a> (<a href="/recipes/tags/network/">network</a>). Revision 2. </p> <p>[co-authored with Philip E. Nu�ez]</p> <p>Python's stdlib does not have any included library for supporting ICMP packets; both reading them or creating them. But ICMP packets are common and useful; they are used for both the traceroute and ping utilities. And thus they can be useful to control to do network diagnostics.</p> <p>The Packet class is what is used to create and read ICMP packets. To create a packet you instantiate the class, set the header and data fields, and then call the create() method which will the string representation that can be passed to a socket.</p> <p>To read a packet, use the Packet.parse() classmethod, which will return an instance of Packet with the fields filled out.</p> <p>To show its use you can also see the ping() method that is included. Just use the code as a script and pass in an address to ping. Response time is printed to stdout.</p> <p>One word of warning, though, when using this module. Raw sockets tend to require root permissions on the process. Thus you might need to use sudo to execute the Python interpreter to make this all work. ping() does drop sudo permissions as soon as it can, though, for security reasons.</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> portalocker - Cross-platform (posix/nt) API for flock-style file locking. (Python) 2008-05-16T21:12:08-07:00Jonathan Feinberghttp://code.activestate.com/recipes/users/1511/http://code.activestate.com/recipes/65203-portalocker-cross-platform-posixnt-api-for-flock-s/ <p style="color: grey"> Python recipe 65203 by <a href="/recipes/users/1511/">Jonathan Feinberg</a> (<a href="/recipes/tags/files/">files</a>). Revision 7. </p> <p>Synopsis:</p> <p>import portalocker file = open("somefile", "r+") portalocker.lock(file, portalocker.LOCK_EX) file.seek(12) file.write("foo") file.close()</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> Simple way to execute multiple process in parallel (Python) 2010-08-31T00:11:00-07:00Benjamin Sergeanthttp://code.activestate.com/recipes/users/4039626/http://code.activestate.com/recipes/577376-simple-way-to-execute-multiple-process-in-parallel/ <p style="color: grey"> Python recipe 577376 by <a href="/recipes/users/4039626/">Benjamin Sergeant</a> (<a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/process/">process</a>). Revision 4. </p> <p>Does not require multiprocessing, easy to hack, maybe not optimal but did the job for a make like tool I wrote.</p> Find the oldest (or yougest) of a list of files (Python) 2009-06-10T16:08:13-07:00Micah Elliotthttp://code.activestate.com/recipes/users/2649403/http://code.activestate.com/recipes/576804-find-the-oldest-or-yougest-of-a-list-of-files/ <p style="color: grey"> Python recipe 576804 by <a href="/recipes/users/2649403/">Micah Elliott</a> (<a href="/recipes/tags/age/">age</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/find/">find</a>, <a href="/recipes/tags/queue/">queue</a>). </p> <p>Sometimes you need to perform an operation on the oldest of a set of files. Using <em>get_oldest_file</em> you could implement an age-based priority queue that processes files from oldest to newest. The list of files you pass in may be from a <em>glob</em> of a single directory or some more elaborate search routine.</p>