Skip to content

Instantly share code, notes, and snippets.

@Endika
Created July 14, 2016 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Endika/3d1d4696fe24d426e4e416e00ecc005d to your computer and use it in GitHub Desktop.
Save Endika/3d1d4696fe24d426e4e416e00ecc005d to your computer and use it in GitHub Desktop.
Lazy decorators
import datetime
def bucle(loop=1, debug=False):
"""Simple decorator only use to repeat func."""
def bucle_decorator(func):
def func_wrapper(*args, **kwargs):
c = loop
i = 0
while c > 0:
start = False
if debug:
start = datetime.datetime.now()
func(*args, **kwargs)
if debug:
end = datetime.datetime.now()
total = end - start
print "[{}] {}".format(i,total)
i += 1
c -= 1
return func_wrapper
return bucle_decorator
# Simple example
@bucle(5,True)
def demo_01():
print "Hello"
# Class example
class demo02:
@bucle(6)
def test(self, text):
print "{} <= Text".format(text)
# demo_01 execute
demo_01()
# Hello
# [0] 0:00:00.000023
# Hello
# [1] 0:00:00.000008
# Hello
# [2] 0:00:00.000007
# Hello
# [3] 0:00:00.000006
# Hello
# [4] 0:00:00.000006
# demo02 execute
t=demo02()
t.test("I'm")
# I'm <= Text
# I'm <= Text
# I'm <= Text
# # I'm <= Text
# I'm <= Text
# I'm <= Text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment