[ [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ] python
下面的代碼是根據當前日期和模板對應的值來進行提交,用於定時任務天天執行。能夠直接部署到服務器,經過後面的命令設置定時任務。git
main.py#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import time import datetime def calculate_date(start, end): # 計算日期相差天數 start_sec = time.mktime(time.strptime(start, '%Y-%m-%d')) end_sec = time.mktime(time.strptime(end, '%Y-%m-%d')) days = int((end_sec - start_sec) / (24 * 60 * 60)) return days def commit(flag): if flag: for n in range(49): # 設置commit次數 with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') else: # 天天推一條 with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') os.system('git pull && git push origin master') with open('./model.json') as f: # 加載模型 PATTEN = json.loads(f.read()) f.close() PERIOD = len(PATTEN[0]) # 週期(圖案列數) START_DATE = '2017-7-16' # 開始日期,很重要,左上角提一格的日期,本身手動修改 now = datetime.datetime.now().strftime('%Y-%m-%d') row = calculate_date(START_DATE, now) % 7 col = int(calculate_date(START_DATE, now) / 7) % PERIOD commit(PATTEN[row][col])json
開啓定時任務數組
crontab -e # 輸入如下代碼,前兩個參數分別是分鐘和小時,該任務爲天天12:00定時執行 # 00 12 * * * cd /home/git_heart && git pull && /usr/bin/python main.py
服務器
二、定時任務只能幫我完成今天及之後的事情,以前的也須要寫個腳本跑一下。
運維
loop.pyoop
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import time import datetime def calculate_date(start, end): # 計算日期相差天數 start_sec = time.mktime(time.strptime(start, '%Y-%m-%d')) end_sec = time.mktime(time.strptime(end, '%Y-%m-%d')) days = int((end_sec - start_sec) / (24 * 60 * 60)) return days def add_days(d, num): # 日期遞增 sec = num * 24 * 60 * 60 now_sec = time.mktime(time.strptime(d, '%Y-%m-%d')) + sec return time.strftime("%Y-%m-%d", time.localtime(now_sec)) def commit(flag): if flag: for n in range(49): with open('./record.txt', 'a') as record: record.write('.') record.close() os.system('git commit -a -m \"HeartBeat\"') with open('./record.txt', 'a') as record: record.write('\n') record.close() os.system('git commit -a -m \"HeartBeat\"') else: with open('./record.txt', 'a') as record: record.write(now + '\n') record.close() os.system('git commit -a -m \"HeartBeat\"') with open('./model.json') as f: # 加載模型 PATTEN = json.loads(f.read()) f.close() PERIOD = len(PATTEN[0]) # 週期(圖案列數) START_DATE = '2017-7-16' # 開始日期, 碼雲和git顯示不同, 建議從最左上角開始 now = datetime.datetime.now().strftime('%Y-%m-%d') os.system('timedatectl set-ntp false') # 關閉時間自動同步 while calculate_date(START_DATE, now) >= 0: row = calculate_date(START_DATE, now) % 7 col = int(calculate_date(START_DATE, now) / 7) % PERIOD commit(PATTEN[row][col]) now = add_days(now, -1) os.system('timedatectl set-time ' + now) # 復原時間 os.system('timedatectl set-ntp 1 && timedatectl set-local-rtc 1')學習
到這裏基本就結束了,第三個目標實際上在第一步就已經完成了,下面上一下測試結果。測試
效果展現
一、當心心設計
[ [0, 1, 1, 0, 0, 0, 1, 1, 0, 0], [1, 1, 1, 1, 0, 1, 1, 1, 1, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 0, 0], [0, 0, 1, 1, 1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0] ]
二、X
[ [0, 0, 0, 0, 0], [1, 0, 0, 0, 1], [0, 1, 0, 1, 0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 0], [1, 0, 0, 0, 1], [0, 0, 0, 0, 0] ]
想提升技術獲取乾貨的朋友能夠加下哦 Python技術交流羣733736235