Python3, python

Source: Internet
Author: User
Tags timedelta

Python3, python
1. Calculate the date of tomorrow and yesterday

#! /Usr/bin/env python # coding = UTF-8 # Get the date of today, yesterday, and tomorrow # introduce the datetime module import datetime # Calculate today's time today = datetime. date. today () # Calculate yesterday's time yesterday = today-datetime. timedelta (days = 1) # Calculate tomorrow's time tomorrow = today + datetime. timedelta (days = 1) # print the three time prints (yesterday, today, tomorrow)
2. Calculate the last time

Method 1:

#! /Usr/bin/env python # coding = UTF-8 # Calculate the previous time # introduce datetime and calendar modules import datetime and 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 2: Use the modulo operation to find the last Friday

#! /Usr/bin/env python # coding = UTF-8 # With the help of the modulo operation, you can calculate the number of days to be subtracted at a time, and calculate the number of days to be subtracted from the previous Friday # datetime is also introduced, the two calendar modules import datetime import calendar today = datetime. date. today () target_day = calendar. FRIDAY this_day = today. weekday () delta_to_target = (this_day-target_day) % 7last_friday = today-datetime. 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 # Get the sum of playing time of all songs in a list 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, 36), (3, 35), (3, 45),] times2 = [(3, 0), (5, 13), (4, 12), (1, 10),] assert total_timer (times1) = datetime. timedelta (0,596) assert total_timer (times2) = datetime. timedelta (0,815) print ("Tests passed. \ n "" First test total: % s \ n "" Second test total: % s "% (total_timer (times1), total_timer (times2 )))
4. Execute a command repeatedly
#! /Usr/bin/env python # coding = UTF-8 # execute a command import time, OS def re_exe (cmd, inc = 60) at the required time interval: while True: OS. system (cmd); time. sleep (inc) re_exe ("echo % time %", 5)
5. scheduled tasks
#! /Usr/bin/env python # coding = UTF-8 # Here we need to introduce three modules import time, OS, sched # The first parameter to determine the task time, returns the number of seconds that have elapsed since a specific time # The second parameter measures the time schedule = sched in a certain way. scheduler (time. time, time. sleep) def into m_command (cmd, inc): OS. system (cmd) def timming_exe (cmd, inc = 60): # enter is used to schedule the occurrence time of an event. From now on, start schedule in the nth second. enter (inc, 0, cmdm_command, (cmd, inc) # run continuously until the scheduled time queue becomes empty. run () print ("show time after 10 seconds:") timming_exe ("echo % time %", 10)
6. Implement periodic call using sched
#! /Usr/bin/env python # coding = utf-8import time, OS, sched # The first parameter determines the task time, returns the number of seconds that have elapsed since a specific time # The second parameter measures the time schedule = sched in a certain way. scheduler (time. time, time. sleep) def into m_command (cmd, inc): # schedule inc seconds and run schedule again. enter (inc, 0, cmdm_command, (cmd, inc) OS. system (cmd) def timming_exe (cmd, inc = 60): # enter is used to schedule the occurrence time of an event. From now on, start schedule in the nth second. enter (inc, 0, cmdm_command, (cmd, inc) # run continuously until the scheduled time queue becomes empty. run () print ("show time after 10 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.