Download pool/shard001/Python/0009145_mode.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard001/Python/0009145_mode.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard001/Python/0009145_mode.py
-
curl -L -o 0009145_mode.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard001/Python/0009145_mode.py
1.89 kB
| from traceback import print_exc as debug | |
| class Mode(object): | |
| def __init__(self): | |
| self.base = dict() | |
| def drive(self, event, *args): | |
| """ Spreads an event """ | |
| try: | |
| item = self.base[event] | |
| except KeyError: | |
| pass | |
| else: | |
| for handle, data in item[:]: | |
| try: | |
| # We call the handle associated to event. | |
| # if it returns an iterator we just chain it. | |
| seq = handle(self, *(args + data)) | |
| # In case of not having an iterator we go on | |
| # processing. | |
| if not seq: | |
| continue | |
| # It chains the iterator. | |
| point = next(seq) | |
| point(self, seq) | |
| # It stops processing handles linked to event. | |
| except Stop: | |
| break | |
| # It stops all the sequence of events. | |
| except Kill, Root: | |
| raise | |
| except StopIteration: | |
| pass | |
| # It prints the exception. | |
| except Exception: | |
| debug() | |
| def link(self, event, handle, *args): | |
| """ Used to link a handle to an event """ | |
| item = self.base.setdefault(event, list()) | |
| item.append((handle, args)) | |
| def unlink(self, event, handle, *args): | |
| """ Use args to unmap the handle """ | |
| # It shouldnt have printed exceptions if the event | |
| # doesnt exist since a call to spin.destroy() | |
| # would clear all the events and some handles | |
| # would try to unlinke themselves from the spin | |
| # it wouldnt be like a real problem. | |
| try: | |
| self.base[event].remove((handle, args)) | |
| except KeyError: | |
| pass | |
| class Stop(Exception): | |
| pass | |
| class Root(Exception): | |
| pass | |
| class Kill(Exception): | |
| pass | |