-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathD_module.py
More file actions
65 lines (53 loc) · 1.82 KB
/
D_module.py
File metadata and controls
65 lines (53 loc) · 1.82 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
65
"""
File name: Decorator_module.py
Author: Mihai Jianu, Daniele La Prova, Lorenzo Mei
Python version: 3.x
Tale modulo contiene le definizioni di profiler e include_stripped.
"""
from time import time, ctime
def include_stripped(decorator):
"""
Decoratore di decoratori:
Mantiene il nome originale di func, così che non venga sostituito da quello della
funzione di wrap.
"""
def wrapping_decorator(func):
"""
Nucleo di include_stripped.
"""
wrapped = decorator(func)
wrapped_stripped = func
return wrapped
return wrapping_decorator
@include_stripped
def profiler(func):
"""
Decoratore di funzioni.
Esegue un profiling di func e lo annota nel file log.txt, registrando la data,
il nome di func, il numero di vertici e archi del grafo di input, il tempo di
esecuzione e il valore di ritorno.
"""
def wrapFunction(*args, **kwargs):
"""
Nucleo di profiler.
"""
try:
pathToOutput = kwargs["pathLog"]
except KeyError:
pathToOutput = "log.txt"
startTime = time()
try:
rValue = func(args[0], kwargs["debug"])
except KeyError:
rValue = func(args[0])
elapsedTime = time() - startTime
appendedLine = f"[{ctime(time())}] name: {func.__name__} ; nodes: {len(args[0].adj)} ; edges: {args[0].numEdges()} ; elapsed: {'%.3f' % elapsedTime}s ; return: {rValue}\n"
with open(pathToOutput, "a") as fOutput:
fOutput.write(appendedLine)
try:
if kwargs["showProfile"]:
print(appendedLine)
except KeyError:
pass
return rValue
return wrapFunction