Adds decorators
authorScott Gasch <[email protected]>
Sat, 28 Nov 2020 21:25:53 +0000 (13:25 -0800)
committerScott Gasch <[email protected]>
Sat, 28 Nov 2020 21:25:53 +0000 (13:25 -0800)
decorators.py [new file with mode: 0644]

diff --git a/decorators.py b/decorators.py
new file mode 100644 (file)
index 0000000..7a08879
--- /dev/null
@@ -0,0 +1,24 @@
+from datetime import datetime
+import functools
+
+def invokation_logged(func):
+    @functools.wraps(func)
+    def wrapper(*args, **kwargs):
+        now = datetime.now()
+        timestamp = now.strftime("%d-%b-%Y (%H:%M:%S.%f)")
+        print("%s(%s): Entered function" % (func.__name__, timestamp))
+        ret = func(*args, **kwargs)
+        now = datetime.now()
+        timestamp = now.strftime("%d-%b-%Y (%H:%M:%S.%f)")
+        print("%s(%s): Exited function" % (func.__name__, timestamp))
+        return ret
+    return wrapper
+
+# Test
+#@invokation_logged
+#def f(x):
+#    print(x * x)
+#    return x * x
+#
+#q = f(10)
+#print(q)