-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
64 lines (51 loc) · 1.81 KB
/
utils.py
File metadata and controls
64 lines (51 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from random import randrange
from time import gmtime, process_time, strftime
def randstr(charlist, length):
"""Generates a string with characters chosen at random from the string
received as first argument. If an empty string is received, lower
and upper case letters and decimal digits are used by default.
Positional arguments:
charlist -- Characters than can appear in the result.
length -- Desidred length of the resulting string.
"""
res = ""
chars = ""
if not charlist:
chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
else:
chars = charlist
l = len(chars)
for _ in range(length):
nextchar = randrange(l)
res = res + chars[nextchar]
return res
def show_process_time(original_function):
"""Decorator that prints the process time elapsed during the execution
of the function decorated, up to seconds precision.
https://docs.python.org/dev/library/time.html#time.process_time
"""
def timed_function(*args, **kwargs):
start = process_time()
res = original_function(*args, **kwargs)
end = process_time()
print(strftime("CPU time elapsed: %H:%M:%S", gmtime(end - start)))
return res
return timed_function
def subsequences(sequence):
"""Returns a generator of all the sub-sequences of target sequence,
that preserve the order of the elements. Each sub-sequence is represented
as a tuple.
Example:
for i, e in enumerate(subsequences([1, 2])):
print(i, e)
--> 0 ()
--> 1 (1,)
--> 2 (1, 2)
--> 3 (2,)
"""
elems = len(sequence)
yield ()
for startidx in range(elems):
for endidx in range(startidx, elems):
subsequence = tuple(sequence[startidx : endidx + 1])
yield subsequence