import re class Conditional(object): """Default conditional, always says True.""" def __init__(self, target): self.target = target def __call__(self, comm): return True class RegexConditional(Conditional): def __init__(self, regex, *args, **kwargs): super(RegexConditional, self).__init__(*args, **kwargs) self.regex = regex def __call__(self, target, comm): return re.match(self.regex, comm) class Actor(object): """Default actor. Does nothing, contains the default conditional.""" def __init__(self): pass def act(self, comm): pass condition = Conditional(Actor) class ComplicatedActor(object): def __init__(self): self.my_regex = RegexConditional('hi there') self.cond2 = Conditional() condition = lambda comm: self.my_regex(comm) and self.cond2(comm) a = Actor() print a.condition("hi") if a.condition("hi"): a.act("hi")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question