Permalink
Browse files

CLOSURES MOTHERFUCKER

  • Loading branch information...
jandre committed Dec 6, 2013
1 parent 13c8c69 commit d873d1d61dd584860f62965e01d324ba9d4b50aa
Showing with 18 additions and 58 deletions.
  1. +18 −58 midas/modules/lib/decorators.py
@@ -1,106 +1,66 @@
#!/usr/bin/env python
"""
-This is the declaration file for a set of useful decorators. The "DRY" rule is
-intentionally violated here in an effort to provide nice syntactic sugar
+This is the declaration file for a set of useful decorators.
+
+NOW WITH CLOSURES
"""
from time import strftime, gmtime, time
-
-def run_every_5(func):
+def run_every_n_minutes(n, func):
"""
- a decorator for running functions every 5 minutes
+ a decorator for running functions every n minutes
"""
def decorated(*args, **kwargs):
"""
internal decorator method
"""
minute = int(strftime("%M", gmtime()))
ret = None
- if minute % 5 == 0:
+ if minute % n == 0:
ret = func(*args, **kwargs)
return ret
return decorated
+
+
+def run_every_5(func):
+ """
+ a decorator for running functions every 5 minutes
+ """
+ return run_every_n_minutes(5, func)
def run_every_10(func):
"""
a decorator for running functions every 10 minutes
"""
- def decorated(*args, **kwargs):
- """
- internal decorator method
- """
- minute = int(strftime("%M", gmtime()))
- ret = None
- if minute % 10 == 0:
- ret = func(*args, **kwargs)
- return ret
- return decorated
+ return run_every_n_minutes(10, func)
def run_every_15(func):
"""
a decorator for running functions every 15 minutes
"""
- def decorated(*args, **kwargs):
- """
- internal decorator method
- """
- minute = int(strftime("%M", gmtime()))
- ret = None
- if minute % 15 == 0:
- ret = func(*args, **kwargs)
- return ret
- return decorated
-
+ return run_every_n_minutes(15, func)
def run_every_20(func):
"""
a decorator for running functions every 20 minutes
"""
- def decorated(*args, **kwargs):
- """
- internal decorator method
- """
- minute = int(strftime("%M", gmtime()))
- ret = None
- if minute % 20 == 0:
- ret = func(*args, **kwargs)
- return ret
- return decorated
+ return run_every_n_minutes(20, func)
def run_every_30(func):
"""
a decorator for running functions every 30 minutes
"""
- def decorated(*args, **kwargs):
- """
- internal decorator method
- """
- minute = int(strftime("%M", gmtime()))
- ret = None
- if minute % 30 == 0:
- ret = func(*args, **kwargs)
- return ret
- return decorated
-
+ return run_every_n_minutes(30, func)
def run_every_60(func):
"""
a decorator for running functions every 60 minutes
"""
- def decorated(*args, **kwargs):
- """
- internal decorator method
- """
- minute = int(strftime("%M", gmtime()))
- ret = None
- if minute == 0:
- ret = func(*args, **kwargs)
- return ret
- return decorated
+ return run_every_n_minutes(60, func)
def timer(func):

0 comments on commit d873d1d

Please sign in to comment.