Reputation: 14317
For an existing Python project, I need to introduce the "cross cutting" non-functional concern of FLOP counting so I can compute the performance in GFlop per second for that application.
In Scala, I could solve this by having an implicit parameter e.g. flopTracker
and have it accessible wherever needed flopTracker.count(N)
adding to a total sum. Then at the end of the program do flopTracker.getTotalCount()
.
In Python I could use a context manager like:
with FlopTracker as flop_tracker:
# call other modules having flop_tracker accessible ...
# ...
logger.info(f"my total flop count is {flop_tracker.get_total_count()}")
What's the Pythonic way to do this? how can I have any module "see" this flop_tracker
when available and ideally in a decoupled fashion?
Upvotes: 1
Views: 405