午夜无码人妻aⅴ大片色欲张津瑜,国产69久久久欧美黑人A片,色妺妺视频网,久久久久国产综合AV天堂

Python3優(yōu)雅操作-時間處理與定時任務(wù)

無論哪種編程語言,時間肯定都是非常重要的部分,今天來看一下python如何來處理時間和python定時任務(wù)

成都創(chuàng)新互聯(lián)于2013年創(chuàng)立,先為荔浦等服務(wù)建站,荔浦等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為荔浦企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

注意:本篇所講是python3版本的實現(xiàn),在python2版本中的實現(xiàn)略有不同

Python3優(yōu)雅操作-時間處理與定時任務(wù)

1.計算明天和昨天的日期

#! /usr/bin/env python#coding=utf-8# 獲取今天、昨天和明天的日期# 引入datetime模塊import datetime 
#計算今天的時間today = datetime.date.today()#計算昨天的時間 yesterday = today - datetime.timedelta(days = 1)#計算明天的時間tomorrow = today + datetime.timedelta(days = 1) 
#打印這三個時間print(yesterday, today, tomorrow)

2.計算上一個的時間

方法一:

#! /usr/bin/env python#coding=utf-8# 計算上一個的時間#引入datetime,calendar兩個模塊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'))

方法二:借助模運算尋找上一個星期五

#! /usr/bin/env python#coding=utf-8# 借助模運算,可以一次算出需要減去的天數(shù),計算上一個星期五#同樣引入datetime,calendar兩個模塊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.計算歌曲的總播放時間

#! /usr/bin/env python#coding=utf-8# 獲取一個列表中的所有歌曲的播放時間之和 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.反復(fù)執(zhí)行某個命令

#! /usr/bin/env python#coding=utf-8# 以需要的時間間隔執(zhí)行某個命令 
    import time, os 
    
def re_exe(cmd, inc = 60): 
    while True: 
        os.system(cmd); 
        time.sleep(inc) 
    
re_exe("echo %time%", 5)

5.定時任務(wù)

#! /usr/bin/env python#coding=utf-8#這里需要引入三個模塊import time, os, sched 
    
# 第一個參數(shù)確定任務(wù)的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(shù) # 第二個參數(shù)以某種人為的方式衡量時間 schedule = sched.scheduler(time.time, time.sleep) 
    
def perform_command(cmd, inc): 
    os.system(cmd) 
        
def timming_exe(cmd, inc = 60): 
    # enter用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動 
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    # 持續(xù)運行,直到計劃時間隊列變成空為止 
    schedule.run() 
        
    
print("show time after 10 seconds:") 
timming_exe("echo %time%", 10)

6.利用sched實現(xiàn)周期調(diào)用

#! /usr/bin/env python#coding=utf-8import time, os, sched 
    
# 第一個參數(shù)確定任務(wù)的時間,返回從某個特定的時間到現(xiàn)在經(jīng)歷的秒數(shù) # 第二個參數(shù)以某種人為的方式衡量時間 schedule = sched.scheduler(time.time, time.sleep) 
    
def perform_command(cmd, inc): 
    # 安排inc秒后再次運行自己,即周期運行 
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    os.system(cmd) 
        
def timming_exe(cmd, inc = 60): 
    # enter用來安排某事件的發(fā)生時間,從現(xiàn)在起第n秒開始啟動 
    schedule.enter(inc, 0, perform_command, (cmd, inc)) 
    # 持續(xù)運行,直到計劃時間隊列變成空為止 
    schedule.run() 
        
    
print("show time after 10 seconds:") 
timming_exe("echo %time%", 10)

這里推薦一下我的:Python學(xué)習(xí)交流群【 784758214 】內(nèi)有安裝包和學(xué)習(xí)視頻資料免費分享,好友都會在里面交流,分享一些學(xué)習(xí)的方法和需要注意的小細(xì)節(jié),每天也會準(zhǔn)時的講一些項目實戰(zhàn)案例。希望可以幫助你快速了解Python、學(xué)習(xí)python

當(dāng)前題目:Python3優(yōu)雅操作-時間處理與定時任務(wù)
瀏覽地址:http://www.ekvhdxd.cn/article30/gecpso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、App設(shè)計、服務(wù)器托管、定制網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營