The introduction of the processing time and the method of realizing timing task in Python3 _python

Source: Internet
Author: User
Tags assert timedelta

Regardless of the programming language, time is certainly a very important part of today to see how Python handles time and Python timed tasks, note: This is the Python3 version of the implementation, in the Python2 version of the implementation of a slightly different, there will be time to write another article to distinguish.
1. Calculate the date of tomorrow and yesterday

#! /usr/bin/env python
#coding =utf-8
# Get today, Yesterday and Tomorrow
# introduce a datetime module
import datetime 
#计算今天的时间
Today = Datetime.date.today ()
#计算昨天的时间 
yesterday = Today-datetime.timedelta (days = 1)
#计算明天的时间
Tomorrow = Today + Datetime.timedelta (days = 1) 
#打印这三个时间
print (Yesterday, today, tomorrow)

2. Calculate the previous time

Method One:

#! /usr/bin/env python
#coding =utf-8
# Calculates the previous time
#引入datetime, calendar two modules
import datetime, Calendar
 
last_friday = Datetime.date.today () 
oneday = Datetime.timedelta (days = 1) while 
 
Last_ Friday.weekday ()!= calendar. FRIDAY: 
 Last_friday-= oneday 
 
print (Last_friday.strftime ('%A,%d-%b-%y ')

Method Two: Look for the last Friday with the help of modulo arithmetic

#! /usr/bin/env python
#coding =utf-8
# with modulo, you can work out the number of days you need to subtract, calculate the last Friday
#同样引入datetime, calendar two modules
Import datetime 
Import calendar 
 
today = Datetime.date.today () 
target_day = Calendar. FRIDAY 
this_day = Today.weekday () 
delta_to_target = (this_day-target_day)% 7
last_friday = Today-dateti Me.timedelta (days = delta_to_target) 
 
print (Last_friday.strftime ("%d-%b-%y"))

3. Calculate the total playback time of a song

#! /usr/bin/env python
#coding =utf-8
# Gets the play time of all songs in a list and 
import datetime 
 
def Total_timer (Times): 
 td = Datetime.timedelta (0) 
 duration = SUM ([Datetime.timedelta (minutes = m, seconds = s) for M, s in times], TD) 
   return duration 
 
times1 = [(2), 
   (3, (3), 
   ] 
times2 = [(3, 0), 
   (5 
   ), (4 , 
   (1), 
   ] 
 
assert Total_timer (times1) = = Datetime.timedelta (0, 596) 
assert Total_timer ( Times2 = = Datetime.timedelta (0, 815) 
 
print ("Tests passed.\n"
  "total:%s\n" "
  Second Test Total:%s% (Total_timer (times1), Total_timer (Times2)))

4. Repeatedly executing a command

#! /usr/bin/env python
#coding =utf-8
# Executes a command import time at the required interval 
 
, OS 
 
def re_exe (cmd, inc = 60): While 
 True: 
  os.system (cmd); 
  Time.sleep (inc) 
 
Re_exe ("Echo%time%", 5)

5. Timed Tasks

#! /usr/bin/env python
#coding =utf-8
#这里需要引入三个模块
import time, OS, Sched 
 
# The first parameter determines the duration of the task. Returns the number of seconds from a specific time to the current experience 
# The second parameter measures time in some artificial way 
schedule = Sched.scheduler (Time.time, Time.sleep) 
 
def perform_ Command (CMD, Inc): 
 Os.system (cmd) 
  
def timming_exe (cmd, inc =): 
 # Enter to schedule the occurrence of an event, starting from now n seconds 
 Schedule.enter (inc, 0, Perform_command, (CMD, Inc)) 
 # continues to run until the scheduled time queue becomes empty 
 schedule.run () 
  
 
print ("Show After seconds: " 
timming_exe (" Echo%time% ", 10)

6. Using Sched to implement cycle calls

#! /usr/bin/env python
#coding =utf-8
import time, OS, Sched 
 
# The first parameter determines the duration of the task, and returns the number of seconds from a specific time to the current experience. 
The second parameter measures time in some artificial way 
schedule = Sched.scheduler (Time.time, Time.sleep) 
 
def Perform_command (CMD, Inc.): 
 # Schedule it to run itself again after Inc, which is the cycle run 
 Schedule.enter (inc, 0, Perform_command, (CMD, Inc) 
 Os.system (cmd) 
  
def timming_exe (cmd, inc =): 
 # Enter to schedule the occurrence of an event, starting from now n seconds to start 
 Schedule.enter (inc, 0, Perform_command, (CMD, Inc)) 
 # Run continuously until the scheduled time queue becomes empty 
 schedule.run () 
  
 
print ("Show Seconds:") 
Timming_exe ("Echo%time%", 10)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.