Latest recipes tagged "time"http://code.activestate.com/recipes/tags/time/new/2016-12-30T19:06:32-08:00ActiveState Code RecipesA command-line musical alarm clock (Python) 2016-12-30T19:06:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580739-a-command-line-musical-alarm-clock/ <p style="color: grey"> Python recipe 580739 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/audio/">audio</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/multimedia/">multimedia</a>, <a href="/recipes/tags/music/">music</a>, <a href="/recipes/tags/playsound/">playsound</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>This is a simple musical alarm clock in Python. You pass a command-line argument specifying the time in minutes after which the alarm should go off. When that time arrives, it plays a musical sound.</p> Using ctypes to call C code from Python (Python) 2016-05-09T18:00:44-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580660-using-ctypes-to-call-c-code-from-python/ <p style="color: grey"> Python recipe 580660 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/ffi/">ffi</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This recipe shows basic usage of the ctypes module to call C code from Python code.</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> Stopwatch in 26 lines (Tcl) 2014-09-05T13:33:13-07:00Mike Manilonehttp://code.activestate.com/recipes/users/4190728/http://code.activestate.com/recipes/578927-stopwatch-in-26-lines/ <p style="color: grey"> Tcl recipe 578927 by <a href="/recipes/users/4190728/">Mike Manilone</a> (<a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This stopwatch works on Tcl/Tk 8.5 and higher. It makes use of the [clock] command and reduced the amount of code by -textvariable. A simple one, indeed. Nevertheless, it is still easy-to-read and easy-to-understand and it does show the power that Tcl has.</p> Sending Email From A Python Program (Python) 2014-01-10T20:13:45-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578807-sending-email-from-a-python-program/ <p style="color: grey"> Python recipe 578807 by <a href="/recipes/users/4184772/">Captain DeadBones</a> (<a href="/recipes/tags/email/">email</a>, <a href="/recipes/tags/smtp/">smtp</a>, <a href="/recipes/tags/time/">time</a>). Revision 4. </p> <p>I put together this code for one of my programs and decided to share it. You may never know when you will need you Python Program to send an email to someone. <a href="http://thelivingpearl.com/2014/01/10/sending-email-from-python-using-command-line/">Sending Email From Python</a>. </p> <p>NOTE: You will have to change the account setup variables to your own. The server is currently configured o Gmail, but you can use this script with any server. </p> A Simple Clock, Well Maybe Not That Simple... (Python) 2013-08-09T18:15:09-07:00Barry Walkerhttp://code.activestate.com/recipes/users/4177147/http://code.activestate.com/recipes/578633-a-simple-clock-well-maybe-not-that-simple/ <p style="color: grey"> Python recipe 578633 by <a href="/recipes/users/4177147/">Barry Walker</a> (<a href="/recipes/tags/amiga/">amiga</a>, <a href="/recipes/tags/apple/">apple</a>, <a href="/recipes/tags/big/">big</a>, <a href="/recipes/tags/clock/">clock</a>, <a href="/recipes/tags/digits/">digits</a>, <a href="/recipes/tags/e_uae/">e_uae</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/macbook_pro/">macbook_pro</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/winuae/">winuae</a>). </p> <p>BIG_TIME.py</p> <p>Remember this?</p> <p><a href="http://code.activestate.com/recipes/578610-a-simple-clock-well-maybe-not-that-simple/" rel="nofollow">http://code.activestate.com/recipes/578610-a-simple-clock-well-maybe-not-that-simple/</a></p> <p>And this?</p> <p><a href="http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147" rel="nofollow">http://code.activestate.com/recipes/578079-pure-fun-for-text-mode-python/?in=user-4177147</a></p> <p>Well the Python version of the top URL has been uploaded to AMINET on 01-08-2013...</p> <p>Now the big gun that complained about my fun Python upload and voted it down can see that this was a serious attempt ae viewing "Hello World!" in extra large xharacters.</p> <p>This is the result in Python...</p> <p>It is ONLY uploaded to AMINET and that is where it will stay.</p> <p>If you want it then use the pointer inside code section.</p> <p>Remember to change the BIG_TIME.py.txt file on AMINET to BIG_TIME.py...</p> <p>Designed around a Macbook Pro OSX 10.7.5 for the Classic AMIGA platforms and works on Debian 6.0.x and PCLiuxOS 2009 using Python versions 1.4.0 to 3.3.2 inclusive...</p> <p>Although the description in the AMINET readme below is for an AMIGA environment it does not take a genius to adapt it to the other platforms...</p> <p>Boy was this difficult to get working in a single Python script...</p> <p>ENJOY...</p> <p>This is the AMINET readme file:-</p> <p>Short: A Big Digital Clock Python Script. Author: <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a> (Barry Walker) Uploader: <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a> (Barry Walker) Type: dev/src Version: 1.00.00 Architecture: generic; m68k-amigaos</p> <p>Distribution: Public Domain, Emailware.</p> <p>============================================================================</p> <h5 id="architectures-classic-amigas-e-uae-and-winuae">Architectures: Classic AMIGAs, E-UAE and WinUAE.</h5> <p>MINIMUM Requirements Are:-</p> <hr /> <pre class="prettyprint"><code> 68EC020 CPU and 2MB RAM total, example, a standard A1200(HD). WinUAE Version 1.5.3 minimum. Standard OS3.0x install or better. Python Version 1.4.0 or higher. (Also, OSX 10.7.5, Debian Linux 6.0.x and PCLinuxOS 2009.) </code></pre> <hr /> <h5 id="history">History.</h5> <p>01-08-2013. Version 1.00.00:- Original upload to AMINET...</p> <p>This is dedicated entirely to the AMIGA community and AMINET and will not be uploaded elsewhere.</p> <hr /> <p>To install just download the "BIG_TIME.py.txt" file and rename it to "BIG_TIME.py" without the quotes. Drag this file into your AMIGA PYTHON: volume and...</p> <p>From a minimum of Python Version 1.4.0 prompt:-</p> <pre class="prettyprint"><code>&gt;&gt;&gt; exec(open("PYTHON:BIG_TIME.py").read())&lt;CR&gt; </code></pre> <p>And away you go...</p> <p>This code is/was designed to work in all versions of Python from 1.4.0 to 3.3.2 on at least the platforms shown in the Architectures: section.</p> <p>It was developed entirely on a Macbook Pro OSX 10.7.5 and finalised using MEmacs for the AMIGA.</p> <hr /> <p>This is just a simple DEMO to show how to generate large digits in Python and put them to use as an _AT_A_GLANCE_ digital clock.</p> <p>It starts up by generating all of the characters available for the DEMO for five seconds then goes into the clock proper.</p> <p>As this code is multi platform AND multi Python version then it is issued as GPL2...</p> <p>Enjoy finding simple solutions to often very difficult problems...</p> <hr /> <pre class="prettyprint"><code> IMPORTANT:- ----------- The Legal Stuff:- ----------------- This Python script is issued under the GPL2 licence conditions. The author is not responsible for any damage to, or loss of, or failure of equipment or data caused in any way by the use of this script. There is NO warranty with the use of this software release and YOU USE IT AT YOUR OWN RISK. </code></pre> <hr /> <pre class="prettyprint"><code>Testing Evaluation:- -------------------- All WinUAE, E-UAE and Classic AMIGA test conditions were/are running standard OS3.0x/OS3.1x and using standard ~topaz 8~ fonts throughout. I have no idea what strange configuration setups will create so refer to the ~The Legal Stuff~ above. </code></pre> <hr /> <p>Contact:-</p> <hr /> <p>Mr Barry Walker, G0LCU.</p> <p>Email:- <a href="mailto:wisecracker@tesco.net">wisecracker@tesco.net</a></p> <p>Author of the ~TestGear?~ projects in the ~docs/hard~ drawer of AMINET.</p> <hr /> <p>A very useful HardWare related site, (C) Anthony Hoffman, for modifications, schematics, repairs and the like is:-</p> <pre class="prettyprint"><code> <a href="http://amiga.serveftp.net/">http://amiga.serveftp.net/</a> </code></pre> <p>============================================================================</p> Monotonic local time. And sandwich. (Python) 2013-01-03T08:28:36-08:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578270-monotonic-local-time-and-sandwich/ <p style="color: grey"> Python recipe 578270 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/daylight/">daylight</a>, <a href="/recipes/tags/dst/">dst</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>A Mutton, Lettuce and Tomato sandwich is generally agreed to be the greatest thing in the world. But a different kind of MLT comes in at a close second: Monotonic Local Time. This is a time value in your local timezone that takes care of that annoying hour that happens once a year where time goes backwards. It works by extending the previous day by a couple of hours (24, 25 etc) until after the switch.</p> Human readable format for a given time delta (Python) 2012-04-26T10:37:10-07:00Thomas Lehmannhttp://code.activestate.com/recipes/users/4174477/http://code.activestate.com/recipes/578113-human-readable-format-for-a-given-time-delta/ <p style="color: grey"> Python recipe 578113 by <a href="/recipes/users/4174477/">Thomas Lehmann</a> (<a href="/recipes/tags/back/">back</a>, <a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/human/">human</a>, <a href="/recipes/tags/readable/">readable</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><strong>What is it about?</strong></p> <ul> <li>I need to say someting like <em>1 day ago</em>, <em>5 days ago</em>, <em>2 weeks ago</em>, ...</li> <li>I can control to have it with/without milliseconds and microseconds.</li> <li>I can use it automatically with current date and time or with a provide one.</li> </ul> <p><strong>Why?</strong></p> <ul> <li>I need it for next revision of my <a href="http://code.activestate.com/recipes/578111/">recipe 578111</a>.</li> <li>I found recipes here and there but often it is always assumed that a month has 30 days and that a year has 365 days; this is not true. That's why I've left away months and years.</li> </ul> Simple curses based MySQL 'top' (Python) 2011-11-02T20:28:48-07:00Mike 'Fuzzy' Partinhttp://code.activestate.com/recipes/users/4179778/http://code.activestate.com/recipes/577936-simple-curses-based-mysql-top/ <p style="color: grey"> Python recipe 577936 by <a href="/recipes/users/4179778/">Mike 'Fuzzy' Partin</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/mysql/">mysql</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>This is little more than a modification of my previous recipe, however, I found it useful so I thought I would post it in the hopes that someone else would as well. There is color (BOLD white really) designation for 'Query' states vs others like 'Sleep'. </p> Benchmark code with the with statement (Python) 2011-10-10T04:30:11-07:00vleonhttp://code.activestate.com/recipes/users/4179537/http://code.activestate.com/recipes/577900-benchmark-code-with-the-with-statement/ <p style="color: grey"> Python recipe 577900 by <a href="/recipes/users/4179537/">vleon</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>). </p> <p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p> <p>Usage is incredibly simple:</p> <pre class="prettyprint"><code>with Timer(): ... # code to benchmark goes here </code></pre> <p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p> <pre class="prettyprint"><code>t = Timer() with t: ... # code to benchmark goes here time_taken = t.interval </code></pre> <p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</p> Benchmark code with the with statement (Python) 2011-10-08T09:53:06-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/ <p style="color: grey"> Python recipe 577896 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/speed/">speed</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/timer/">timer</a>). </p> <p>Inspired by <a href="http://preshing.com/20110924/timing-your-code-using-pythons-with-statement">this post</a> I wrote this context manager to benchmark code blocks or function calls.</p> <p>Usage is incredibly simple:</p> <pre class="prettyprint"><code>with Timer(): ... # code to benchmark goes here </code></pre> <p>The time taken (in seconds) will be printed when the code block completes. To capture the time taken programmatically is almost as easy:</p> <pre class="prettyprint"><code>t = Timer() with t: ... # code to benchmark goes here time_taken = t.interval </code></pre> <p>Due to the difficulties of timing small snippets of code <em>accurately</em>, you should only use this for timing code blocks or function calls which take a significant amount of time to process. For micro-benchmarks, you should use the <code>timeit</code> module.</p> Printing current, next, or previous day (Python) 2011-09-29T21:39:22-07:00Andy Barbourhttp://code.activestate.com/recipes/users/4174351/http://code.activestate.com/recipes/577883-printing-current-next-or-previous-day/ <p style="color: grey"> Python recipe 577883 by <a href="/recipes/users/4174351/">Andy Barbour</a> (<a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>simply prints the previous, current, or next day. Inputs may be <em>year month month-day</em> or <em>year julian-day</em> with the output in the same format.</p> user and root directory logfile (Python) 2011-03-22T10:00:49-07:00justin drakehttp://code.activestate.com/recipes/users/4177409/http://code.activestate.com/recipes/577619-user-and-root-directory-logfile/ <p style="color: grey"> Python recipe 577619 by <a href="/recipes/users/4177409/">justin drake</a> (<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/user/">user</a>). </p> <p>script to retrieve time, current user, and root of directory output to spam.txt</p> <p>want to add more like windows version, bios info and other useful diagnostic information </p> <p>free to use and modify welcome any advice or corrections learning python hope this turns into a script to create a organized personal report of customer's pc</p> sleep_min: Sleep for a minimum number of seconds (Python) 2011-02-08T16:44:36-08:00Drew Vogelhttp://code.activestate.com/recipes/users/4176907/http://code.activestate.com/recipes/577568-sleep_min-sleep-for-a-minimum-number-of-seconds/ <p style="color: grey"> Python recipe 577568 by <a href="/recipes/users/4176907/">Drew Vogel</a> (<a href="/recipes/tags/sleep/">sleep</a>, <a href="/recipes/tags/time/">time</a>). </p> <p><code>time.sleep(delay)</code> will sleep for, at most, <code>delay</code> seconds. This function will sleep for at least <code>delay</code> seconds.</p> Binary BCD clock using curses (Python) 2010-10-19T10:40:36-07:00Michal Niklashttp://code.activestate.com/recipes/users/186902/http://code.activestate.com/recipes/577430-binary-bcd-clock-using-curses/ <p style="color: grey"> Python recipe 577430 by <a href="/recipes/users/186902/">Michal Niklas</a> (<a href="/recipes/tags/curses/">curses</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>Converts actual time to vertical BCD digits. So actual time, 12:37:51 looks like:</p> <pre class="prettyprint"><code>...... ...**. .***.. *.**** </code></pre> Simulate number of flushes in a poker hand (Python) 2010-04-08T05:24:12-07:00Dudley G R Gentleshttp://code.activestate.com/recipes/users/4173603/http://code.activestate.com/recipes/577184-simulate-number-of-flushes-in-a-poker-hand/ <p style="color: grey"> Python recipe 577184 by <a href="/recipes/users/4173603/">Dudley G R Gentles</a> (<a href="/recipes/tags/cards/">cards</a>, <a href="/recipes/tags/flush/">flush</a>, <a href="/recipes/tags/poker/">poker</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/time/">time</a>). </p> <p>This program calculates the number of flushes in a number of deals of a poker hand. You can tell it to deal say 10,000 hands and see how many were flushes. This is basically my first Python program. </p> parse a date/time string to a `datetime` instance (Python) 2010-04-02T07:32:17-07:00Trent Mickhttp://code.activestate.com/recipes/users/4173505/http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/ <p style="color: grey"> Python recipe 577135 by <a href="/recipes/users/4173505/">Trent Mick</a> (<a href="/recipes/tags/date/">date</a>, <a href="/recipes/tags/datetime/">datetime</a>, <a href="/recipes/tags/parse/">parse</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <pre class="prettyprint"><code>&gt;&gt;&gt; import datetime &gt;&gt;&gt; str(datetime.datetime.now()) '2010-03-21 21:33:32.750246' &gt;&gt;&gt; str(datetime.date.today()) '2010-03-21' </code></pre> <p>This function goes the other way for date and datetime strings of this format.</p> Countdown to screenshot (PIL). (Python) 2009-12-03T20:07:52-08:00me mehttp://code.activestate.com/recipes/users/4172371/http://code.activestate.com/recipes/576976-countdown-to-screenshot-pil/ <p style="color: grey"> Python recipe 576976 by <a href="/recipes/users/4172371/">me me</a> (<a href="/recipes/tags/countdown/">countdown</a>, <a href="/recipes/tags/screenshot/">screenshot</a>, <a href="/recipes/tags/time/">time</a>). Revision 2. </p> <p>Tested on Linux and Windows-XP, Python 2.6 ...</p> <p>Counts down to a number and takes a screenshot. I simplified one of my long lost screenshot taking codes for linux removing sound, graphics and all other unnecessary bits.</p> Detect when system is idle (Python) 2009-05-31T01:18:03-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576786-detect-when-system-is-idle/ <p style="color: grey"> Python recipe 576786 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/idle/">idle</a>, <a href="/recipes/tags/idle_time/">idle_time</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/user_input/">user_input</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>Detect when system is idle, globally. That is, the user is not moving the mouse nor typing anything, in any application (not just our program).</p> Paratessares Time (Python) 2009-01-27T18:43:51-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576636-paratessares-time/ <p style="color: grey"> Python recipe 576636 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/paratessares/">paratessares</a>, <a href="/recipes/tags/tessaressunago/">tessaressunago</a>, <a href="/recipes/tags/time/">time</a>). Revision 3. </p> <p>This module provides several functions that covert earth seconds into paratessares time. It is used to implement time keeping on a fictional planet and is the standard time system of Tessaressunago. It can be used as an inspiration to other people creating their own units of time or as partitioned timestamps that can be applied to records. There are four different modules listed below: the standard implementation, a unit test, and two examples. To see the Python 2.5 version of this code in action and to test out its functionality, you can go over to the Utility Mill and find Paratessares_Time in the library ( <a href="http://utilitymill.com/utility/Paratessares_Time" rel="nofollow">http://utilitymill.com/utility/Paratessares_Time</a> ).</p>