-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_auditing.py
More file actions
63 lines (37 loc) · 1.32 KB
/
code_auditing.py
File metadata and controls
63 lines (37 loc) · 1.32 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
import inspect
import binascii
jpeg_signatures = [
binascii.unhexlify(b'FFD8FFD8'),
binascii.unhexlify(b'FFD8FFE0'),
binascii.unhexlify(b'FFD8FFE1')
]
# JPEG Testing Example (https://www.geeksforgeeks.org/working-with-binary-data-in-python/)
# with open('food.jpeg', 'rb') as file:
# first_four_bytes = file.read(4)
#
# if first_four_bytes in jpeg_signatures:
# print("JPEG detected.")
# else:
# print("File does not look like a JPEG.")
def called_func():
return inspect.stack()[1][3]
def calling_func():
return inspect.stack()[2][3]
import time
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class"""
class Timer:
def __init__(self):
self._start_time = None
def start(self):
"""Start a new timer"""
if self._start_time is not None:
raise TimerError(f"Timer is running. Use .stop() to stop it")
self._start_time = time.perf_counter()
def stop(self):
"""Stop the timer, and report the elapsed time"""
if self._start_time is None:
raise TimerError(f"Timer is not running. Use .start() to start it")
elapsed_time = time.perf_counter() - self._start_time
self._start_time = None
print(f"Elapsed time: {elapsed_time:0.4f} seconds")