| 2 | |
| 3 | |
| 4 | class Event(list): |
| 5 | |
| 6 | def __call__(self, *args, **kwargs): |
| 7 | for f in self[:]: |
| 8 | if "once" in dir(f) and f in self: |
| 9 | self.remove(f) |
| 10 | f(*args, **kwargs) |
| 11 | |
| 12 | def __repr__(self): |
| 13 | return "Event(%s)" % list.__repr__(self) |
| 14 | |
| 15 | def once(self, func, name=None): |
| 16 | func.once = True |
| 17 | func.name = None |
| 18 | if name: # Dont function with same name twice |
| 19 | names = [f.name for f in self if "once" in dir(f)] |
| 20 | if name not in names: |
| 21 | func.name = name |
| 22 | self.append(func) |
| 23 | else: |
| 24 | self.append(func) |
| 25 | return self |
| 26 | |
| 27 | |
| 28 | if __name__ == "__main__": |
no outgoing calls
no test coverage detected