|
13 | 13 | desktop = os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + '\\'
|
14 | 14 | DataTypeError = TypeError('Unsupported data type.')
|
15 | 15 |
|
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 | + ''' |
21 | 59 | if type(pattern) == str:
|
22 | 60 | pattern = re.compile(pattern)
|
23 | 61 | if type(pattern) != re.Pattern:
|
@@ -55,7 +93,7 @@ def getlnk(path: str):
|
55 | 93 | shortcut = shell.CreateShortCut(path)
|
56 | 94 | return shortcut.Targetpath
|
57 | 95 |
|
58 |
| -def random_pop(sequence: list): |
| 96 | +def rpop(sequence: list): |
59 | 97 | '''Randomly remove and return an item from the sequence'''
|
60 | 98 | index = random.randrange(0, len(sequence))
|
61 | 99 | return sequence.pop(index)
|
@@ -335,12 +373,17 @@ def findFilePath(filename: str, path: str = ''):
|
335 | 373 | return result
|
336 | 374 | return False
|
337 | 375 |
|
| 376 | +def timeof(func, args: tuple = ()): |
| 377 | +## from functools import partial |
| 378 | +## partial(func, args) |
| 379 | + return timer(func, 1, args) |
| 380 | + |
338 | 381 | def timer(func, iterations: int = 1000, args: tuple = ()):
|
339 | 382 | '''If func has arguments, put them into args.'''
|
340 |
| - t = time.time() |
| 383 | + t = time.perf_counter() |
341 | 384 | for i in range(iterations):
|
342 | 385 | func(*args)
|
343 |
| - return time.time() - t |
| 386 | + return time.perf_counter() - t |
344 | 387 |
|
345 | 388 | def fread(filename: str, evaluate: bool = True, coding: str = 'utf8'):
|
346 | 389 | '''Read the file that has the filename.
|
|
0 commit comments