[ [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