This package simplifies the fundamental software engineering practices such as bottleneck analysis, exception handling, logging etc.
pip install pysimpler
You have to set PYSIMPLER environment variable as 1 to activate pysimpler. If you want to deactivate it, you should set as 0.
#activate
export PYSIMPLER=1
#deactivate
export PYSIMPLER=0
You can add the following command at the end of your code for reporting.
pysimpler.reporter.report()
This feature aims to log duration of the execution time of method. We use timer keyword for using this feature. It help you analysing your bottlenecks in function level.
Example:
import pysimpler
@pysimpler.timer.actual()
def counter(count):
x = 1
for i in range(count):
y = x*i
if __name__ == '__main__':
print("Process 1")
result = counter(10)
print("Process 3")
pysimpler.reporter.report()
# output
# ------------------------------
# Process 1
# 2023-12-25T11:55:38.474173+0300 | INFO | File: app.py | Function : counter | Duration : 0.0005151670000032027 sec
# Process 3
import pysimpler
import gc
@pysimpler.cache.clear()
def memory(count):
mem = []
for i in range(count):
mem.append("data")
if __name__ == '__main__':
# print(gc.get_count()) lines are only for printing of the memory state.
# You can remove it.
print("Process 1")
print("Process 2")
print(gc.get_count())
result = memory(1000)
print(gc.get_count())
print("Process 4")
# output
#------------------------------
# Process 1
# Process 2
# (351, 10, 1)
# (0, 0, 0)
# Process 4
import pysimpler
import gc
@pysimpler.cache.clear(pysimpler.MLFrameworks.PYTORCH)
def memory_pytorch(device = "cuda"):
print("=> memory_pytorch")
var = torch.ones(1,3,1024,1024)
if torch.cuda.is_available():
var = var.to(device)
if __name__ == '__main__':
print("Process 1")
print("Process 2")
result = memory_pytorch()
print("Process 4")
# output
# ------------------------------
# Process 1
# Process 2
# => memory_pytorch
# Process 4
import pysimpler
@pysimpler.error.catch(raise_exception=False)
def zero_devision(x):
print(f"func : zero devision => {x}/0")
return x/0
if __name__ == '__main__':
print("Process 1")
print("Process 2")
result = zero_devision(x = 10)
print("Process 4")
# output
------------------------------
Process 1
Process 2
func : zero devision => 10/0
2023-08-20T03:11:50.412347+0300 | ERROR | "/Users/mp/Desktop/Github/simpler/app.py", line 8, in zero_devision
return x/0
ZeroDivisionError: division by zero
Process 4