Skip to content

Commit bff23b9

Browse files
committed
rpassword
1 parent 3d1680b commit bff23b9

File tree

5 files changed

+60
-16
lines changed

5 files changed

+60
-16
lines changed

Python Files/Sudoku/ocr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def find_puzzle(image):
9292
return warp
9393
return None
9494

95-
def image_to_matrix(image, intermediatesPath, entries) -> dict:
95+
def image_to_matrix(image, intermediatesPath) -> dict:
9696
'''
9797
DESCRIPTION
9898
-----------

Python Files/Sudoku/sudoku.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def ocr_event(self, filename):
7777
numbers = image_to_matrix(filename, self.intermediates)
7878
assert numbers != {}
7979
self.setNumbers(numbers)
80-
except:
80+
except Exception as e:
81+
print(e)
8182
messagebox.showerror(title='错误', message='无法识别图片')
8283

8384
def edit(self):

Python Files/Sudoku/sudoku.pyw

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class sudoku:
7777
numbers = image_to_matrix(filename, self.intermediates)
7878
assert numbers != {}
7979
self.setNumbers(numbers)
80-
except:
80+
except Exception as e:
81+
print(e)
8182
messagebox.showerror(title='错误', message='无法识别图片')
8283

8384
def edit(self):

ez.py

+51-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,49 @@
1313
desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + '\\'
1414
DataTypeError = TypeError('Unsupported data type.')
1515

16-
def replacePattern(pattern: str, string: str, substr: str = ''):
17-
'''Replace the substring that follows a pattern from a string.
18-
pattern: a Regex string or a re.Pattern object.
19-
string: the target string.
20-
substr: default is empty, which removes the pattern.'''
16+
def rpassword(length: tuple = (8, 20), capital: bool = True, numbers: bool = True, punctuations: bool = False) -> str:
17+
'''
18+
Randomly generate a password. Full clexicon is list(map(chr, range(32, 126))).
19+
@params:
20+
length: a tuple of with the format of (minLength, maxLength),
21+
or an integer that specifies the length.
22+
Default: (8, 20).
23+
capital: a bool that specifies the inclusion of capital letters.
24+
Default: True.
25+
numbers: a bool that specifies the inclusion of numbers.
26+
Default: True.
27+
punctuations: a bool that specifies the inclusion of punctuations, which will be selected from [` -=[]\;',./~!@#$%^&*()_+{}|:"<>?].
28+
Default: False.
29+
@return:
30+
password: a string. If no argument is set to True, a lower-case string will be returned.
31+
'''
32+
lexicon = list(map(chr, range(32, 126)))
33+
if not capital:
34+
lexicon = filter(lambda x: not x.isupper(), lexicon)
35+
if not numbers:
36+
lexicon = filter(lambda x: not x.isdigit(), lexicon)
37+
if not punctuations:
38+
lexicon = filter(lambda x: x.isalnum(), lexicon)
39+
lexicon = list(lexicon)
40+
if isinstance(length, (tuple, list)):
41+
assert len(length) == 2, 'length object must have exactly 2 elements!'
42+
mi, ma = length
43+
iterations = random.randint(mi, ma)
44+
else:
45+
iterations = length
46+
password = ''.join([ random.choice(lexicon) for _ in range(iterations) ])
47+
return password
48+
49+
def replacePattern(pattern: str, string: str, substr: str = '') -> str:
50+
'''
51+
Replace the substring that follows a pattern from a string.
52+
@params:
53+
pattern: a Regex string or a re.Pattern object.
54+
string: the target string.
55+
substr: default is empty, which removes the pattern.
56+
@return:
57+
new: the final result string
58+
'''
2159
if type(pattern) == str:
2260
pattern = re.compile(pattern)
2361
if type(pattern) != re.Pattern:
@@ -55,7 +93,7 @@ def getlnk(path: str):
5593
shortcut = shell.CreateShortCut(path)
5694
return shortcut.Targetpath
5795

58-
def random_pop(sequence: list):
96+
def rpop(sequence: list):
5997
'''Randomly remove and return an item from the sequence'''
6098
index = random.randrange(0, len(sequence))
6199
return sequence.pop(index)
@@ -335,12 +373,17 @@ def findFilePath(filename: str, path: str = ''):
335373
return result
336374
return False
337375

376+
def timeof(func, args: tuple = ()):
377+
## from functools import partial
378+
## partial(func, args)
379+
return timer(func, 1, args)
380+
338381
def timer(func, iterations: int = 1000, args: tuple = ()):
339382
'''If func has arguments, put them into args.'''
340-
t = time.time()
383+
t = time.perf_counter()
341384
for i in range(iterations):
342385
func(*args)
343-
return time.time() - t
386+
return time.perf_counter() - t
344387

345388
def fread(filename: str, evaluate: bool = True, coding: str = 'utf8'):
346389
'''Read the file that has the filename.

eztk.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ def focusOut(event):
3434

3535
class LoadDialog(Toplevel):
3636
'''A simple LoadDialog'''
37-
def __init__(self, master, load_message = 'Loading', maxDots = 5):
38-
assert isinstance(load_message, str)
39-
assert isinstance(maxDots, int)
40-
assert maxDots > 0
37+
def __init__(self, master, load_message = 'Loading', maxDots = 6):
38+
assert isinstance(load_message, str) and isinstance(maxDots, int) and maxDots > 0
4139
Toplevel.__init__(self, master)
4240
self.transient(master)
4341
self.geometry(f"+{master.winfo_rootx() + 50}+{master.winfo_rooty() + 50}")
@@ -65,7 +63,7 @@ def dontClose(self):
6563
def update(self):
6664
dots = 0
6765
while not self.isClose:
68-
dots = (dots + 1) % self.maxDots
66+
dots = (dots + 1) % (self.maxDots + 1)
6967
time.sleep(0.5)
7068
self.label['text'] = self.load_message + dots * '.'
7169
self.destroy()
@@ -76,6 +74,7 @@ def close(self):
7674
class ProgressDialog(Toplevel):
7775
'''A simple ProgressDialog with progress from 0 to 100'''
7876
def __init__(self, master, load_message = 'Progress: 0%'):
77+
assert isinstance(load_message, str)
7978
Toplevel.__init__(self, master)
8079
self.master = master
8180
self.transient(master)

0 commit comments

Comments
 (0)