| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,6 +2,11 @@ | |||
| 2 | 2 | ||
| 3 | 3 | from types import ModuleType | |
| 4 | 4 | ||
| 5 | + try: | ||
| 6 | + from collections.abc import Mapping | ||
| 7 | + except ImportError: | ||
| 8 | + from collections import Mapping | ||
| 9 | + | ||
| 5 | 10 | from six import text_type | |
| 6 | 11 | ||
| 7 | 12 | try: | |
@@ -47,9 +52,6 @@ class MethodDispatcher(dict): | |||
| 47 | 52 | """ | |
| 48 | 53 | ||
| 49 | 54 | def __init__(self, items=()): | |
| 50 | - # Using _dictEntries instead of directly assigning to self is about | ||
| 51 | - # twice as fast. Please do careful performance testing before changing | ||
| 52 | - # anything here. | ||
| 53 | 55 | _dictEntries = [] | |
| 54 | 56 | for name, value in items: | |
| 55 | 57 | if isinstance(name, (list, tuple, frozenset, set)): | |
@@ -64,6 +66,36 @@ def __init__(self, items=()): | |||
| 64 | 66 | def __getitem__(self, key): | |
| 65 | 67 | return dict.get(self, key, self.default) | |
| 66 | 68 | ||
| 69 | + def __get__(self, instance, owner=None): | ||
| 70 | + return BoundMethodDispatcher(instance, self) | ||
| 71 | + | ||
| 72 | + | ||
| 73 | + class BoundMethodDispatcher(Mapping): | ||
| 74 | + """Wraps a MethodDispatcher, binding its return values to `instance`""" | ||
| 75 | + def __init__(self, instance, dispatcher): | ||
| 76 | + self.instance = instance | ||
| 77 | + self.dispatcher = dispatcher | ||
| 78 | + | ||
| 79 | + def __getitem__(self, key): | ||
| 80 | + # see https://docs.python.org/3/reference/datamodel.html#object.__get__ | ||
| 81 | + # on a function, __get__ is used to bind a function to an instance as a bound method | ||
| 82 | + return self.dispatcher[key].__get__(self.instance) | ||
| 83 | + | ||
| 84 | + def get(self, key, default): | ||
| 85 | + if key in self.dispatcher: | ||
| 86 | + return self[key] | ||
| 87 | + else: | ||
| 88 | + return default | ||
| 89 | + | ||
| 90 | + def __iter__(self): | ||
| 91 | + return iter(self.dispatcher) | ||
| 92 | + | ||
| 93 | + def __len__(self): | ||
| 94 | + return len(self.dispatcher) | ||
| 95 | + | ||
| 96 | + def __contains__(self, key): | ||
| 97 | + return key in self.dispatcher | ||
| 98 | + | ||
| 67 | 99 | ||
| 68 | 100 | # Some utility functions to deal with weirdness around UCS2 vs UCS4 | |
| 69 | 101 | # python builds | |
| Back | FazBrowse Home | New Git URL |
0 commit comments