Skip to content

Commit 8619247

Browse files
committedApr 3, 2020
Code update
1 parent f231b92 commit 8619247

File tree

7 files changed

+723
-1
lines changed

7 files changed

+723
-1
lines changed
 

‎Python Files/BackupHelper.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os, platform, ez, shutil
2+
3+
def check(src, dst, removeFile = False, removeFolder = False):
4+
src_list = os.listdir(src)
5+
dst_list = os.listdir(dst)
6+
for item in src_list:
7+
src_item = os.path.join(src, item)
8+
dst_item = os.path.join(dst, item)
9+
if os.path.isdir(src_item):
10+
if item in dst_list:
11+
check(src_item, dst_item, removeFile, removeFolder)
12+
else:
13+
shutil.copytree(src_item, dst_item)
14+
print(f'{src_item} is copied.')
15+
else:
16+
if item in dst_list:
17+
src_time = os.path.getmtime(src_item)
18+
dst_time = os.path.getmtime(dst_item)
19+
if src_time > dst_time:
20+
shutil.copy2(src_item, dst_item)
21+
print(f'{dst_item} is updated.')
22+
## elif src_time < dst_time:
23+
## print(f'{src_item} is outdated.')
24+
else:
25+
shutil.copy2(src_item, dst)
26+
print(f'{src_item} is copied.')
27+
for item in set(dst_list) - set(src_list):
28+
path = os.path.join(dst, item)
29+
if os.path.isdir(path):
30+
if removeFolder:
31+
shutil.rmtree(path)
32+
print(f'{path} is deleted.')
33+
else:
34+
print(f'{path} doesn\'t exists in source.')
35+
else:
36+
print(removeFile)
37+
if removeFile:
38+
os.remove(path)
39+
print(f'{path} is deleted.')
40+
else:
41+
print(f'{path} doesn\'t exists in source')
42+
43+
if __name__ == '__main__':
44+
## ez.cddt()
45+
check(r'C:\Users\Seaky\AppData\Local\Programs\Python\Python38\Python Files', \
46+
r'E:\Python\Python Files',
47+
True,
48+
False)

‎Python Files/FlappyBird.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from random import *
2+
from tkinter import *
3+
from tkinter import messagebox
4+
5+
root=Tk()
6+
class World(Frame):
7+
def __init__(self,parent,x=600,y=800,size=25):
8+
Frame.__init__(self,parent)
9+
self.canvas=Canvas(self,height=y,width=x,relief=SUNKEN,borderwidth=3)
10+
self.width,self.height=x,y
11+
self.pixel_size=size
12+
self.canvas.pack()
13+
self.birdX,self.birdY=200,400
14+
self.canvas.create_rectangle(self.birdX,self.birdY,self.birdX+self.pixel_size,self.birdY+self.pixel_size,fill='red',tags='bird')
15+
self.isFalling=True
16+
self.time=0
17+
self.interval=6*size
18+
self.points=0
19+
self.canvas.create_text(self.width//2,self.height//10,text=str(self.points),fill='black',font=('Helvetica',self.pixel_size*2,'bold italic'),tags='points')
20+
self.columns=[]
21+
self.column_size=2*self.pixel_size
22+
self.isAlive=True
23+
self.canvas.bind_all('<KeyRelease>',self.rise)
24+
self.generate_columns()
25+
self.move_columns()
26+
self.fall()
27+
28+
def rise(self,event):
29+
if not self.isAlive:
30+
return
31+
if event.keysym=='Up':
32+
self.isFalling=False
33+
self.time=0
34+
self.up()
35+
self.isFalling=True
36+
37+
def up(self):
38+
if self.time<15:
39+
self.time+=1
40+
self.birdY-=15-self.time
41+
self.canvas.delete('bird')
42+
self.canvas.create_rectangle(self.birdX,self.birdY,self.birdX+self.pixel_size,self.birdY+self.pixel_size,tags='bird',fill='red')
43+
self.judge()
44+
self.after(25,self.up)
45+
46+
def fall(self):
47+
if self.isFalling:
48+
self.birdY+=7.5
49+
self.canvas.delete('bird')
50+
self.canvas.create_rectangle(self.birdX,self.birdY,self.birdX+self.pixel_size,self.birdY+self.pixel_size,tags='bird',fill='red')
51+
self.judge()
52+
if not self.isAlive and self.birdY+self.pixel_size>=self.height:
53+
return
54+
self.after(25,self.fall)
55+
56+
def generate_columns(self):
57+
colX,colY=self.width,randrange(self.interval,self.height-self.interval,self.pixel_size)
58+
self.canvas.create_rectangle(colX,0,colX+self.column_size,colY,tags='col',fill='green') #upper tube
59+
self.canvas.create_rectangle(colX,colY+self.interval,colX+self.column_size,self.height,tags='col',fill='green') #lower tube
60+
self.columns.append((colX,colY))
61+
62+
def move_columns(self):
63+
if not self.isAlive:
64+
return
65+
self.canvas.delete('col')
66+
for i in range(len(self.columns)):
67+
x,y=self.columns.pop(0)
68+
if x+2*self.pixel_size<0:
69+
continue
70+
elif x<200+3*self.pixel_size and len(self.columns)==0:
71+
self.generate_columns()
72+
elif x+2*self.pixel_size==self.birdX:
73+
self.canvas.delete('points')
74+
self.points+=1
75+
self.canvas.create_text(self.width//2,self.height//10,text=str(self.points),fill='black',font=('Helvetica',self.pixel_size*2,'bold italic'),tags='points')
76+
x-=5
77+
self.columns.append((x,y))
78+
self.canvas.create_rectangle(x,0,x+self.column_size,y,tags='col',fill='green') #upper tube
79+
self.canvas.create_rectangle(x,y+self.interval,x+self.column_size,self.height,tags='col',fill='green') #lower tube
80+
self.canvas.after(40,self.move_columns)
81+
82+
def judge(self):
83+
x,y=self.columns[-1]
84+
if self.birdY+self.pixel_size>self.height or \
85+
(x+self.column_size>self.birdX+self.pixel_size>x and self.birdY+self.pixel_size>y+self.interval) or \
86+
(x<self.birdX<x+self.column_size and self.birdY+self.pixel_size>y+self.interval) or\
87+
(x+self.column_size>self.birdX+self.pixel_size>x and self.birdY<y) or\
88+
(x<self.birdX<x+self.column_size and self.birdY<y):
89+
messagebox.showinfo('You Are Lost!',f'You got {self.points} points.')
90+
self.isAlive=False
91+
92+
game=World(root)
93+
game.pack()
94+
game.master.title("Flappy Bird")
95+
root.mainloop()

‎Python Files/diary.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ def search(word, groupByYear = False, Print = True, printText = True):
126126

127127
if __name__ == '__main__':
128128
## main(2021)
129-
search('', 1, 1, 0)
129+
search('崇仁', 0, 1, 1)

‎Python Files/学习强国/test.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import time
2+
3+
for i in range(10):
4+
print('Loading' + i * '.', end='\r')
5+
time.sleep(1)

‎Python Files/学习强国/utils.py

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import traceback
2+
from selenium.common.exceptions import NoSuchElementException, WebDriverException, TimeoutException
3+
from PIL import ImageChops, Image
4+
5+
#############################################################
6+
###### Find Elements
7+
#############################################################
8+
9+
def find_elements(driver, id_):
10+
'''
11+
Only supports id, xpath, class_name and android_uiautomator
12+
@params:
13+
driver: a webdriver object
14+
id_: can be any of the id, xpath, class_name and android_uiautomator
15+
@returns:
16+
a list of found elements
17+
'''
18+
# __handle_system_dialogs(driver)
19+
if ':id/' in id_:
20+
return driver.find_elements_by_id(id_)
21+
elif id_.startswith('android.widget'):
22+
return driver.find_elements_by_class_name(id_)
23+
elif id_.startswith('new '):
24+
return driver.find_elements_by_android_uiautomator(id_)
25+
else: # elif id_.startswith('//android.widget') or id_.startswith('//*[contains'):
26+
return driver.find_elements_by_xpath(id_)
27+
28+
def find_element(driver, id_: str, index: int = 0):
29+
'''
30+
This function calls find_elements and returns the indexed element from it.
31+
@params:
32+
driver: a webdriver object
33+
id_ (str): can be any of the id, xpath, class_name and android_uiautomator
34+
index (int): the index of element found, default is 0
35+
@returns:
36+
the indexed element if found, otherwise False
37+
'''
38+
elements = find_elements(driver, id_)
39+
return elements[index] if elements else False
40+
41+
def find_and_do(driver, id_: str, operation, index: int = 0):
42+
'''
43+
Find the indexed element and do an operation on it.
44+
@returns:
45+
True if element is found otherwise False
46+
'''
47+
try:
48+
elements = find_elements(driver, id_)
49+
if elements:
50+
operation(elements[index])
51+
return True
52+
return False
53+
except:
54+
return False
55+
56+
def click(driver, id_: str, index: int = 0):
57+
'''Find the indexed element and click it.'''
58+
return find_and_do(driver, id_, lambda x: x.click(), index)
59+
60+
def set_text(driver, id_: str, text: str, index: int = 0):
61+
'''Find the indexed element and set its text.'''
62+
return find_and_do(driver, id_, lambda x: x.set_text(text), index)
63+
64+
def get_text(driver, id_: str, index: int = 0):
65+
'''
66+
Find the indexed element and return its text. If not found, return ''.
67+
'''
68+
element = find_element(driver, id_, index)
69+
return element.text if element else ''
70+
71+
def get_attribute(driver, id_: str, attribute: str, index: int = 0):
72+
'''Find the indexed element and get its attribute.'''
73+
element = find_element(driver, id_, index)
74+
return element.get_attribute(attribute) if element else None
75+
76+
def is_selected(driver, id_: str, index: int = 0):
77+
'''Find if the indexed element has the attribute 'selected' equal to true.'''
78+
return get_attribute(driver, id_, 'selected', index) == 'true'
79+
80+
def is_checked(driver, id_: str, index: int = 0):
81+
'''Find if the indexed element has the attribute 'checked' equal to true.'''
82+
return get_attribute(driver, id_, 'checked', index) == 'true'
83+
84+
#############################################################
85+
###### SWIPE
86+
#############################################################
87+
88+
def __swipe(driver, fromX, fromY, toX, toY, duration) -> bool:
89+
size = driver.get_window_size()
90+
width = size['width']
91+
height = size['height']
92+
try:
93+
driver.swipe(width * fromX, height * fromY, width * toX, height * toY, 500)
94+
return True
95+
except (NoSuchElementException, TimeoutException, WebDriverException):
96+
print(traceback.format_exc())
97+
return False
98+
99+
def swipe(driver, start_x_ratio, start_y_ratio, end_x_ratio, end_y_ratio, duration=500):
100+
'''
101+
Swipe from (start_x_ratio * window_width, start_y_ratio * window * height) to (end_x_ratio * window_width, end_y_ratio * height)
102+
@params:
103+
driver: a webdriver object
104+
start_x_ratio, start_y_ratio, end_x_ratio, end_y_ratio: Swipe from (start_x_ratio * window_width, start_y_ratio * window * height)
105+
to (end_x_ratio * window_width, end_y_ratio * height)
106+
duration: swipe time in milliseconds
107+
@returns:
108+
returns True if swipeable otherwise False
109+
@raises:
110+
AssertionException if start_x_ratio, start_y_ratio, end_x_ratio, end_y_ratio don't fall in range [0, 1]
111+
'''
112+
assert 0 <= start_x_ratio <= 1 and 0 <= start_y_ratio <= 1 and 0 <= end_x_ratio <= 1 and 0 <= end_y_ratio <= 1, "Ratio doesn't fall in range [0, 1]"
113+
return __swipe(driver, start_x_ratio, start_y_ratio, end_x_ratio, end_y_ratio, duration)
114+
115+
def swipe_left(driver, x_ratio=7/8, y_ratio=1/2) -> bool:
116+
'''
117+
@params:
118+
driver: a webdriver object
119+
x_ratio, y_ratio: swipe from (x_ratio * window_width, y_ratio * window_height)
120+
to ((1 - x_ratio) * window_width, y_ratio * window_height)
121+
@returns:
122+
returns True if swipeable otherwise False
123+
@raises:
124+
AssertionException if x_ratio doesn't fall in range (1/2, 1], y_ratio doesn't fall in range [0, 1]
125+
'''
126+
assert 1/2 < x_ratio <= 1, f"x_ratio ({x_ratio}) doesn't fall in range (1/2, 1]"
127+
assert 0 <= y_ratio <= 1, f"y_ratio ({y_ratio}) doesn't fall in range [0, 1]"
128+
return swipe(driver, x_ratio, y_ratio, 1 - x_ratio, y_ratio)
129+
130+
def swipe_right(driver, x_ratio=1/8, y_ratio=1/2) -> bool:
131+
'''
132+
@params:
133+
driver: a webdriver object
134+
x_ratio, y_ratio: swipe from (x_ratio * window_width, y_ratio * window_height)
135+
to ((1 - x_ratio) * window_width, y_ratio * window_height)
136+
@returns:
137+
returns True if swipeable otherwise False
138+
@raises:
139+
AssertionException if x_ratio doesn't fall in range [0, 1/2), y_ratio doesn't fall in range [0, 1]
140+
'''
141+
assert 0 <= x_ratio < 1/2, f"x_ratio ({x_ratio}) doesn't fall in range [0, 1/2)"
142+
assert 0 <= y_ratio <= 1, f"y_ratio ({y_ratio}) doesn't fall in range [0, 1]"
143+
return swipe(driver, x_ratio, y_ratio, 1 - x_ratio, y_ratio)
144+
145+
def swipe_up(driver, x_ratio=1/2, y_ratio=3/5) -> bool:
146+
'''
147+
@params:
148+
driver: a webdriver object
149+
x_ratio, y_ratio: swipe from (x_ratio * window_width, y_ratio * window_height)
150+
to (x_ratio * window_width, (1 - y_ratio) * window_height).
151+
@returns:
152+
returns True if swipeable otherwise False
153+
@raises:
154+
AssertionException if x_ratio doesn't fall in range [0, 1], y_ratio doesn't fall in range (1/2, 1]
155+
'''
156+
assert 0 <= x_ratio <= 1, f"x_ratio ({x_ratio}) doesn't fall in range [0, 1]"
157+
assert 1/2 < y_ratio <= 1, f"y_ratio ({y_ratio}) doesn't fall in range (1/2, 1]"
158+
return swipe(driver, x_ratio, y_ratio, x_ratio, 1 - y_ratio)
159+
160+
def swipe_down(driver, x_ratio=1/2, y_ratio=2/5) -> bool:
161+
'''
162+
@params:
163+
driver: a webdriver object
164+
x_ratio, y_ratio: swipe from (x_ratio * window_width, y_ratio * window_height)
165+
to (x_ratio * window_width, (1 - y_ratio) * window_height).
166+
@returns:
167+
returns True if swipeable otherwise False
168+
@raises:
169+
AssertionException if x_ratio doesn't fall in range [0, 1], y_ratio doesn't fall in range [0, 1/2)
170+
'''
171+
assert 0 <= x_ratio <= 1, f"x_ratio ({x_ratio}) doesn't fall in range [0, 1]"
172+
assert 0 <= y_ratio < 1/2, f"y_ratio ({y_ratio}) doesn't fall in range [0, 1/2)"
173+
return swipe(driver, x_ratio, y_ratio, x_ratio, 1 - y_ratio)
174+
175+
def compare_images(image1: str, image2: str) -> bool:
176+
'''
177+
@params:
178+
image1 (str): The path of image1.
179+
image2 (str): The path of image2.
180+
@returns:
181+
True if two images are identical otherwise False.
182+
'''
183+
return ImageChops.difference(Image.open(image1), Image.open(image2)).getbbox() is None

0 commit comments

Comments
 (0)
Please sign in to comment.