Created
August 31, 2011 23:19
-
-
Save wraithan/1185025 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def phasing(phaser_attr): | |
def phaser(f): | |
@wraps(f) | |
def wrapper(self, *args, **kwargs): | |
state = eval("self.%s" % phaser_attr) | |
if hasattr(f, "phases") and state in f.phases: | |
return f.phases[state](self, *args, **kwargs) | |
elif hasattr(f, "phase_points"): | |
points = [pt for pt in f.phase_points if pt <= state] | |
if len(points) > 0: | |
return f.phase_points[max(points)](self, *args, **kwargs) | |
else: | |
return f(self, *args, **kwargs) | |
else: | |
return f(self, *args, **kwargs) | |
wrapper.f = f | |
def phase_to(*states): | |
def phase_to_er(this_phase_f): | |
if not hasattr(wrapper.f, "phases"): | |
wrapper.f.phases = {} | |
for state in states: | |
wrapper.f.phases[state] = this_phase_f | |
return wrapper | |
return phase_to_er | |
wrapper.phase_to = phase_to | |
def phase_after(state): | |
def phase_after_er(this_phase_f): | |
if not hasattr(wrapper.f, "phase_points"): | |
wrapper.f.phase_points = {} | |
wrapper.f.phase_points[state] = this_phase_f | |
return wrapper | |
return phase_after_er | |
wrapper.phase_after = phase_after | |
return wrapper | |
return phaser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment