方法一:#! /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# 借助模運算,可以一次算出需要減去的天數,計算上乙個星期五#同樣引入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"))
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.反覆執行某個命令#! /usr/bin/env python#coding=utf-8# 以需要的時間間隔執行某個命令
import time, os
def re_exe(cmd, inc = 60):
while true:
os.system(cmd);
time.sleep(inc)
re_exe("echo %time%", 5)
5.定時任務#! /usr/bin/env python#coding=utf-8#這裡需要引入三個模組import time, os, sched
# 第乙個引數確定任務的時間,返回從某個特定的時間到現在經歷的秒數 # 第二個引數以某種人為的方式衡量時間 schedule = sched.scheduler(time.time, time.sleep)
def perform_command(cmd, inc):
os.system(cmd)
def timming_exe(cmd, inc = 60):
# enter用來安排某事件的發生時間,從現在起第n秒開始啟動
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持續執行,直到計畫時間佇列變成空為止
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
6.利用sched實現週期呼叫#! /usr/bin/env python#coding=utf-8import time, os, sched
# 第乙個引數確定任務的時間,返回從某個特定的時間到現在經歷的秒數 # 第二個引數以某種人為的方式衡量時間 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用來安排某事件的發生時間,從現在起第n秒開始啟動
schedule.enter(inc, 0, perform_command, (cmd, inc))
# 持續執行,直到計畫時間佇列變成空為止
schedule.run()
print("show time after 10 seconds:")
timming_exe("echo %time%", 10)
Python2 處理 Unicode 字串的規則
在 python2 中處理 unicode 字串,需遵循如下規則 1.程式中的字串要加字首 u 2.不要用 str 而應該用 unicode 作為字串轉換函式。不要使用 chr 而應該使用 unichr 3.不要使用 string 模組 4.如非必要,不要使用 encode 和 decode 編譯碼...
Struts2處理流程
基本簡要流程如下 1 客戶端瀏覽器發出 請求。2 根據 web.xml 配置,該請求被 filterdispatcher 接收。3 根據 struts.xml 配置,找到需要呼叫的 action 類和方法,並通過 ioc方式,將值注入給 aciton。4 action 呼叫業務邏輯元件處理業務邏輯,...
C Tips2 處理資料
1.以兩個下劃線或大寫字母和下劃線開始的變數不違法,但可能會導致不確定性。單下劃線開頭用作全域性識別符號。2.c 提供一種靈活的標準 3.在climits 中包含了一些限制資訊,如int max為最大的int,char bit為位元組位數。4.c 新增了一種c中沒有的初始化語句 int new in...