Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # Copyright 2013, Big Switch Networks, Inc. |
| 2 | # |
| 3 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | # the following special exception: |
| 5 | # |
| 6 | # LOXI Exception |
| 7 | # |
| 8 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | # |
| 15 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | # |
| 17 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | # a copy of the EPL at: |
| 19 | # |
| 20 | # http://www.eclipse.org/legal/epl-v10.html |
| 21 | # |
| 22 | # Unless required by applicable law or agreed to in writing, software |
| 23 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | # EPL for the specific language governing permissions and limitations |
| 26 | # under the EPL. |
| 27 | |
| 28 | """ |
| 29 | @brief Generic utilities |
| 30 | |
| 31 | Intended to be imported into another namespace |
| 32 | """ |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 33 | import logging |
Andreas Wundsam | 662779f | 2013-07-30 10:38:53 -0700 | [diff] [blame] | 34 | import collections |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 35 | import functools |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 36 | import sys |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 37 | |
| 38 | ################################################################ |
| 39 | # |
| 40 | # Debug |
| 41 | # |
| 42 | ################################################################ |
| 43 | |
| 44 | def debug(obj): |
| 45 | """ |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 46 | Legacy logging method. Delegate to logging.debug. |
Andreas Wundsam | 55ecc3c | 2013-11-15 16:11:52 -0800 | [diff] [blame] | 47 | Use logging.debug directly in the future. |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 48 | """ |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 49 | logging.debug(obj) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 50 | |
| 51 | def log(obj): |
| 52 | """ |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 53 | Legacy logging method. Delegate to logging.info. |
| 54 | Use logging.info directly in the future.S |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 55 | """ |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 56 | logging.info(obj) |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 57 | |
| 58 | ################################################################ |
| 59 | # |
| 60 | # Memoize |
| 61 | # |
| 62 | ################################################################ |
| 63 | |
| 64 | def memoize(obj): |
| 65 | """ A function/method decorator that memoizes the result""" |
| 66 | cache = obj.cache = {} |
| 67 | |
| 68 | @functools.wraps(obj) |
| 69 | def memoizer(*args, **kwargs): |
Andreas Wundsam | f35ec81 | 2013-07-16 13:49:18 -0700 | [diff] [blame] | 70 | key = args + tuple(kwargs.items()) |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 71 | if key not in cache: |
| 72 | cache[key] = obj(*args, **kwargs) |
| 73 | return cache[key] |
| 74 | return memoizer |
| 75 | |
| 76 | ################################################################ |
| 77 | # |
| 78 | # OrderedSet |
| 79 | # |
| 80 | ################################################################ |
| 81 | |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 82 | class OrderedSet(collections.MutableSet): |
| 83 | """ |
| 84 | A set implementations that retains insertion order. From the receipe |
| 85 | http://code.activestate.com/recipes/576694/ |
| 86 | as referred to in the python documentation |
| 87 | """ |
| 88 | |
| 89 | def __init__(self, iterable=None): |
| 90 | self.end = end = [] |
| 91 | end += [None, end, end] # sentinel node for doubly linked list |
| 92 | self.map = {} # key --> [key, prev, next] |
| 93 | if iterable is not None: |
| 94 | self |= iterable |
| 95 | |
| 96 | def __len__(self): |
| 97 | return len(self.map) |
| 98 | |
| 99 | def __contains__(self, key): |
| 100 | return key in self.map |
| 101 | |
| 102 | def add(self, key): |
| 103 | if key not in self.map: |
| 104 | end = self.end |
| 105 | curr = end[1] |
| 106 | curr[2] = end[1] = self.map[key] = [key, curr, end] |
| 107 | |
| 108 | def discard(self, key): |
| 109 | if key in self.map: |
| 110 | key, prev, next = self.map.pop(key) |
| 111 | prev[2] = next |
| 112 | next[1] = prev |
| 113 | |
| 114 | def __iter__(self): |
| 115 | end = self.end |
| 116 | curr = end[2] |
| 117 | while curr is not end: |
| 118 | yield curr[0] |
| 119 | curr = curr[2] |
| 120 | |
| 121 | def __reversed__(self): |
| 122 | end = self.end |
| 123 | curr = end[1] |
| 124 | while curr is not end: |
| 125 | yield curr[0] |
| 126 | curr = curr[1] |
| 127 | |
| 128 | def pop(self, last=True): |
| 129 | if not self: |
| 130 | raise KeyError('set is empty') |
| 131 | key = self.end[1][0] if last else self.end[2][0] |
| 132 | self.discard(key) |
| 133 | return key |
| 134 | |
| 135 | def __repr__(self): |
| 136 | if not self: |
| 137 | return '%s()' % (self.__class__.__name__,) |
| 138 | return '%s(%r)' % (self.__class__.__name__, list(self)) |
| 139 | |
| 140 | def __eq__(self, other): |
| 141 | if isinstance(other, OrderedSet): |
| 142 | return len(self) == len(other) and list(self) == list(other) |
| 143 | return set(self) == set(other) |
| 144 | |
Andreas Wundsam | 662779f | 2013-07-30 10:38:53 -0700 | [diff] [blame] | 145 | ################################################################ |
| 146 | # |
| 147 | # OrderedDefaultDict |
| 148 | # |
| 149 | ################################################################ |
| 150 | |
| 151 | class OrderedDefaultDict(collections.OrderedDict): |
| 152 | """ |
| 153 | A Dictionary that maintains insertion order where missing values |
| 154 | are provided by a factory function, i.e., a combination of |
| 155 | the semantics of collections.defaultdict and collections.OrderedDict. |
| 156 | """ |
| 157 | def __init__(self, default_factory=None, *a, **kw): |
| 158 | if (default_factory is not None and |
| 159 | not callable(default_factory)): |
| 160 | raise TypeError('first argument must be callable') |
| 161 | collections.OrderedDict.__init__(self, *a, **kw) |
| 162 | self.default_factory = default_factory |
| 163 | |
| 164 | def __getitem__(self, key): |
| 165 | try: |
| 166 | return collections.OrderedDict.__getitem__(self, key) |
| 167 | except KeyError: |
| 168 | return self.__missing__(key) |
| 169 | |
| 170 | def __missing__(self, key): |
| 171 | if self.default_factory is None: |
| 172 | raise KeyError(key) |
| 173 | self[key] = value = self.default_factory() |
| 174 | return value |
| 175 | |
| 176 | def __reduce__(self): |
| 177 | if self.default_factory is None: |
| 178 | args = tuple() |
| 179 | else: |
| 180 | args = self.default_factory, |
| 181 | return type(self), args, None, None, self.items() |
| 182 | |
| 183 | def copy(self): |
| 184 | return self.__copy__() |
| 185 | |
| 186 | def __copy__(self): |
| 187 | return type(self)(self.default_factory, self) |
| 188 | |
| 189 | def __deepcopy__(self, memo): |
| 190 | import copy |
| 191 | return type(self)(self.default_factory, |
| 192 | copy.deepcopy(self.items())) |
| 193 | def __repr__(self): |
| 194 | return 'OrderedDefaultDict(%s, %s)' % (self.default_factory, |
| 195 | collections.OrderedDict.__repr__(self)) |
| 196 | |
| 197 | |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 198 | def find(func, iterable): |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 199 | """ |
| 200 | find the first item in iterable for which func returns something true'ish. |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 201 | @returns None if no item in iterable fulfills the condition |
Andreas Wundsam | 2d29c07 | 2013-07-16 12:44:03 -0700 | [diff] [blame] | 202 | """ |
| 203 | for i in iterable: |
| 204 | if func(i): |
| 205 | return i |
Andreas Wundsam | 1f0d880 | 2013-08-02 22:20:14 -0700 | [diff] [blame] | 206 | return None |
| 207 | |
| 208 | def count(func, iteratable): |
| 209 | """ |
| 210 | count how the number of items in iterable for which func returns something true'ish. |
| 211 | """ |
| 212 | c = 0 |
| 213 | for i in iterable: |
| 214 | if func(i): |
| 215 | c +=1 |
| 216 | return c |