Skip to content

Instantly share code, notes, and snippets.

@dchentech
Created December 5, 2014 02:47
Show Gist options
  • Save dchentech/ea5e5f31068967f62680 to your computer and use it in GitHub Desktop.
Save dchentech/ea5e5f31068967f62680 to your computer and use it in GitHub Desktop.
Lazy reference between tasks, include recursive references.
# Lazy reference between tasks, include recursive references.
# 1. `eval` version
def ref_tasks(*tasks):
def wrap(task1):
def _ref_task(self):
return eval(task1)
return _ref_task
def func(cls):
for task1 in tasks:
setattr(cls, task1, property(wrap(task1)))
return cls
return func
# Ref before TaskB, TaskC defined, so it's really lazy.
@ref_tasks("TaskB", "TaskC")
class TaskA(object): pass
class TaskB(object): pass
class TaskC(object): pass
# Check it.
assert TaskA().TaskB == TaskB
assert TaskA().TaskC == TaskC
# Even itself.
ref_tasks("TaskA")(TaskA)
assert TaskA().TaskA == TaskA
# 2. `import` version
"""
lib1 = importlib.import_module("tasks." + Inflector().underscore(task_name1))
task1 = getattr(lib1, task_name1)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment