| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """This module defines an interface of a meta structure -- ``MetaObject``. |
| 6 |
| 7 We can consider ``MetaObject`` as tree node, and it has ``is_element`` property |
| 8 for us to tell whether it is a leaf or not. We can think ``MetaObject`` is an |
| 9 effective way to group elements, basically each ``MetaObject``(tree root) |
| 10 controls all the ``Element``s (leaf) under it. |
| 11 |
| 12 There are 2 kinds of ``MetaObject``s: |
| 13 (1) ``Element``: The basic class (leaf node). |
| 14 (2) ``Meta*``: A collection of ``MetaObject``s. It can be ``MetaDict``, |
| 15 ``MetaList`` or ``MetaSet``...etc. (non-leaf node) |
| 16 |
| 17 N.B. Except self-awareness of that itself is not an ``Element`` (since |
| 18 the ``is_element`` property is False), the ``Meta*`` acts the same way as |
| 19 whatever container it is. (list, dict, set...etc.) |
| 20 |
| 21 An easy example for ``Meta*``, say we have a ``MetaDict`` as below: |
| 22 {'a': e(1), 'b': {'c': e(2), 'd': e(3)}}, it is a dict of ``MetaObject``s, |
| 23 The e(1) is an ``Element`` and the {'c': e(2), 'd': e(3)} is a |
| 24 ``MetaDict``. |
| 25 |
| 26 An usecase is in ``Predator``, ``Feature`` inherits ``Element`` and |
| 27 ``MetaFeature`` inherits ``MetaDict``, so for some relevant features, we can |
| 28 group them together to get a ``MetaFeature``.. |
| 29 """ |
| 30 |
| 31 import copy |
| 32 |
| 33 |
| 34 class MetaObject(object): |
| 35 """Class that can be either one element or a collection of elements.""" |
| 36 |
| 37 def IsElement(self): # pragma: no cover |
| 38 return NotImplementedError() |
| 39 |
| 40 |
| 41 class Element(MetaObject): |
| 42 """Element class that cannot be divided anymore.""" |
| 43 |
| 44 @property |
| 45 def is_element(self): |
| 46 return True |
| 47 |
| 48 |
| 49 class MetaDict(MetaObject): |
| 50 """Dict-like object containing a collection of ``MetaObject``s.""" |
| 51 |
| 52 def __init__(self, value): |
| 53 """Construct a meta dict from a dict of meta objects. |
| 54 |
| 55 Args: |
| 56 value (dict): Dict of meta objects. |
| 57 """ |
| 58 self._value = copy.deepcopy(value) |
| 59 |
| 60 @property |
| 61 def is_element(self): |
| 62 return False |
| 63 |
| 64 def __getitem__(self, key): |
| 65 return self._value[key] |
| 66 |
| 67 def __setitem__(self, key, val): |
| 68 self._value[key] = val |
| 69 |
| 70 def get(self, key, default=None): |
| 71 return self._value.get(key, default) |
| 72 |
| 73 def __iter__(self): |
| 74 return iter(self._value) |
| 75 |
| 76 def __eq__(self, other): |
| 77 return self._value == other._value |
| 78 |
| 79 def iteritems(self): |
| 80 return self._value.iteritems() |
| 81 |
| 82 def itervalues(self): |
| 83 return self._value.itervalues() |
| 84 |
| 85 def keys(self): |
| 86 return self._value.keys() |
| 87 |
| 88 def values(self): |
| 89 return self._value.values() |
| OLD | NEW |