コード例 #1
0
def test_abc_type_is_a_lie_in_python():
    # YOLO
    from collections import Mapping

    class TotallyNotAMapping:
        pass

    Mapping.register(TotallyNotAMapping)
    assert isinstance(TotallyNotAMapping(), Mapping)
コード例 #2
0
 def json_to_value(mcs, json: collections.Mapping):
     key = json.get('__type__', None)
     value = json.get('value', None)
     if key is None:
         raise ValueError(f"Can not convert with missing type key!")
     if value is None:
         raise ValueError(f"Can not convert with missing value!")
     converter_cls = mcs.__key_to_converter__.get(key, None)
     if converter_cls is None:
         raise ValueError(f"Can not convert object of type key {key}!")
     return converter_cls.json_to_value(value)
コード例 #3
0
ファイル: DataBase.py プロジェクト: mingotang/libdata
    def __init__(self, db_path: str, writeback=False):
        Mapping.__init__(self)
        if '.' in db_path:
            if db_path.split('.')[-1] == 'db':
                path = db_path
            else:
                path = '.'.join([db_path, 'db'])
        else:
            path = '.'.join([db_path, 'db'])

        self.__path__ = path
        self.__db__ = shelve.open(self.__path__,
                                  writeback=writeback,
                                  protocol=None)
        self.__closed__ = False  # tag whether db is closed
        logging.info('Connected to shelve database {}'.format(path))
コード例 #4
0
    def kl_divergence(self, q: collections.Mapping):
        """
        Return the Kullback-Leibler divergence with probability distribution.

        This is given by the formula:

            $KL = \sum_i p_i \ln \frac {p_i} {q_i},$

        in which p_i comes from the probability object and q_i comes from the
        argument.
        """

        prob = self._data.get
        divergence = 0.0
        visited = 0

        for k, q in q.items():
            visited += 1
            p = prob(k, 0.0)
            if p:
                try:
                    divergence += p and p * log(p / q)
                except ZeroDivisionError:
                    return float('inf')

        if len(self._data) != visited:
            return float('inf')

        return divergence
コード例 #5
0
ファイル: sequence_rebatch.py プロジェクト: civodlu/trw
def split_in_2_batches(batch: collections.Mapping, first_batch_size: int):
    """
    Split a single batch into 2 batches. The first batch will have a fixed size.

    If there is not enough sample to split the batch, return (batch, None)

    Args:
        batch: the batch to split
        first_batch_size: the batch size of the first batch. The remaining samples will be in the second batch

    Returns:
        a tuple (first batch, second batch)
    """
    batch_size = len_batch(batch)
    if batch_size <= first_batch_size:
        return batch, None

    # actually, split!
    batch_1 = type(batch)()
    batch_2 = type(batch)()

    for name, value in batch.items():
        if isinstance(value, (np.ndarray, torch.Tensor, list)):
            # split the array/list
            batch_1[name] = value[:first_batch_size]
            batch_2[name] = value[first_batch_size:]
        else:
            # for other types, simply duplicate
            batch_1[name] = value
            batch_2[name] = value

    return batch_1, batch_2
コード例 #6
0
def extract_labels_from_mask(mask: np.ndarray, color_id_to_obj_class: collections.Mapping) -> list:
    """
    Extract multiclass instances from grayscale mask and save it to labels list.
    Args:
        mask: multiclass grayscale mask
        color_id_to_obj_class: dict of objects classes assigned to color id (e.g. {1: ObjClass('cat), ...})
    Returns:
        list of labels with bitmap geometry
    """
    zero_offset = 1 if 0 in color_id_to_obj_class else 0
    if zero_offset > 0:
        mask = mask + zero_offset

    labeled, labels_count = measure.label(mask, connectivity=1, return_num=True)
    objects_slices = ndimage.find_objects(labeled)
    labels = []

    for object_index, slices in enumerate(objects_slices, start=1):
        crop = mask[slices]
        sub_mask = crop * (labeled[slices] == object_index).astype(np.int)

        class_index = np.max(sub_mask) - zero_offset

        if class_index in color_id_to_obj_class:
            bitmap = Bitmap(data=sub_mask.astype(np.bool), origin=PointLocation(slices[0].start, slices[1].start))
            label = Label(geometry=bitmap, obj_class=color_id_to_obj_class.get(class_index))
            labels.append(label)
    return labels
コード例 #7
0
ファイル: analysis_plots.py プロジェクト: civodlu/trw
def list_classes_from_mapping(mappinginv: collections.Mapping,
                              default_name='unknown'):
    """
    Create a contiguous list of label names ordered from 0..N from the class mapping

    :param mappinginv: a dictionary like structure encoded as (class id, class_name)
    :param default_name: if there is no class name, use this as default
    :return: a list of class names ordered from class id = 0 to class id = N. If `mappinginv` is None,
        returns None
    """
    if mappinginv is None:
        return None
    nb_classes = max(mappinginv.keys()) + 1
    classes = [default_name] * nb_classes
    for class_id, name in mappinginv.items():
        classes[class_id] = name
    return classes
コード例 #8
0
def nested_dict_contains_key(ndict: collections.Mapping, key):
    if key in ndict:
        return True
    else:
        for k, v in ndict.items():
            if isinstance(v, collections.Mapping):
                if nested_dict_contains_key(v, key):
                    return True
        return False
コード例 #9
0
def set_value(package: collections.Mapping, keypath: str, value: JSONValue):
    paths = keypath.split('.')
    target = package
    for path in paths[:-1]:
        target = package.setdefault(path, {})
        assert isinstance(target, collections.Mapping), "Invalid keypath"
    assert isinstance(target, collections.Mapping), "Invalid keypath"
    path = paths[-1]
    target[path] = value
コード例 #10
0
def _get_nested(value: collections.Mapping, keys: deque, default=None):
    if len(keys) > 0:
        attr = keys.popleft()
        value = value.get(attr)
        if len(keys) > 0 and value is not None:
            if isinstance(value, collections.Mapping):
                return _get_nested(value, keys, default)
            if isinstance(value, object):
                return _get_nested(vars(value), keys, default)
    return value or default
コード例 #11
0
def flatten_dict_by_joining(
    d: collections.Mapping, parent_key: Optional[str] = "", sep: Optional[str] = "-"
) -> collections.Mapping:
    items = []
    for k, v in d.items():
        new_k = sep.join([parent_key, k]) if parent_key else k
        if isinstance(v, collections.Mapping):
            items.extend(flatten_dict_by_joining(v, new_k, sep=sep).items())
        else:
            items.append((new_k, v))
    return dict(items)
コード例 #12
0
ファイル: _ordered.py プロジェクト: IuryAlves/bidict
 def __eq__(self, other):
     if isinstance(other, (OrderedBidirectionalMapping, OrderedDict)):
         if len(self) != len(other):
             return False
         for i, j in izip(iteritems(self), iteritems(other)):
             if i != j:
                 return False
         return True
     if isinstance(other, Mapping):
         return Mapping.__eq__(self, other)
     return False
コード例 #13
0
ファイル: group.py プロジェクト: JGoutin/skio
    def __new__(cls, *args, **kwargs):
        """
        Group Instanciation.
        """
        # Create instance
        self = Mapping.__new__(cls)

        # Set default Parent value: in case not called by another Group
        self._parent = None
        self._values = {}
        self._iterdefault = False
        self._name = cls.__name__

        # Performance: Alias dotted names
        clsdict = cls.__dict__
        clsdtype = cls._dtype
        clsdoc = cls._doc
        values = self._values

        # Instantiate attributes-classes
        module = ("{0}['{1}']".format(cls.__module__, cls.__name__))
        for key in clsdict.keys():
            attr = clsdict[key]

            # Only for classes starting with '_Key'
            if not isclass(attr):
                continue
            name = attr.__name__
            if not name.startswith('_Key'):
                continue

            # Update attribute-class information
            name = name[len('_Key'):]
            attr.__name__ = name
            attr.__module__ = module

            # Instantiate attribute-class
            instance = attr()

            # If attribute-class is Group, add information
            if isinstance(instance, Group):
                setattr(instance, '_parent', self)
                if name not in clsdtype:
                    clsdtype[name] = Group

            # Add doc to current class _doc
            if name not in clsdoc:
                clsdoc[name] = attr.__doc__

            # Add instance in default value dict
            values[name] = instance

        # Return instance
        return self
コード例 #14
0
 def _check_event_handlers(self, value: collections.Mapping, level: int):
     if level > 0:
         for k, v in value.items():
             if k.startswith('_on_'):
                 assert 'name' in v, f'Missing name for the event handler \'{k}\'.'
                 assert type(
                     v['name']
                 ) == str, f'The name of event handler \'{k}\' MUST be a string.'
                 assert 'args' in v, f'Missing args for the event handler \'{k}\'.'
                 assert isinstance(
                     v, collections.Mapping
                 ), f'The args of event handler \'{k}\' MUST be an object.'
             if isinstance(v, collections.Mapping):
                 self._check_event_handlers(v, level - 1)
コード例 #15
0
ファイル: config.py プロジェクト: thomasjosif/botbase
    def nested_update(self,
                      current: collections.Mapping,
                      defaults: Dict[str, Any] = ...) -> Dict[str, Any]:
        """Robust updater for nested dictionaries

        If no defaults are passed, then the instance attribute 'defaults'
        will be used.
        """
        if defaults is ...:
            defaults = self.defaults

        for key, value in current.items():
            if isinstance(value, collections.Mapping):
                result = self.nested_update(value, defaults.get(key, {}))
                defaults[key] = result
            else:
                defaults[key] = deepcopy(current[key])
        return defaults
コード例 #16
0
        def __eq__(self, other):
            """
            Generated by @autodict.
            In the case the other is of the same type, use the dict comparison. Otherwise, falls back to super.

            :param self:
            :param other:
            :return:
            """
            # in the case the other is of the same type, use the dict comparison, that relies on the appropriate fields
            if isinstance(other, cls):
                return dict(self) == dict(other)
            elif isinstance(other, Mapping):
                return dict(self) == other
            else:
                # else fallback to inherited behaviour, whatever it is
                try:
                    f = super(cls, self).__eq__
                except AttributeError:
                    # can happen in python 2 when adding Mapping inheritance failed
                    return Mapping.__eq__(dict(self), other)
                else:
                    return f(other)
コード例 #17
0
ファイル: operation.py プロジェクト: deztructor/pycor
 def prepare_field(self, field_name: str, values: collections.Mapping):
     return values.get(field_name)
コード例 #18
0
        undefined.
        """
        item = self.resolve(key)
        if isinstance(item, Undefined):
            raise KeyError(key)
        return item

    def __repr__(self):
        return '<%s %s of %r>' % (self.__class__.__name__, repr(
            self.get_all()), self.name)


# register the context as mapping if possible
try:
    from collections import Mapping
    Mapping.register(Context)
except ImportError:
    pass


class BlockReference(object):
    """One block on a template reference."""
    def __init__(self, name, context, stack, depth):
        self.name = name
        self._context = context
        self._stack = stack
        self._depth = depth

    @property
    def super(self):
        """Super the block."""
コード例 #19
0
ファイル: runtime.py プロジェクト: paulfitz/blender2haxe
                DeprecationWarning(
                    "variables set in a base template "
                    "will no longer leak into the child "
                    "context in future versions.  Happened "
                    "when " + detail
                )
            )
            return base_rv
        return self_rv


# register the context as mapping if possible
try:
    from collections import Mapping

    Mapping.register(Context)
except ImportError:
    pass


class BlockReference(object):
    """One block on a template reference."""

    def __init__(self, name, context, stack, depth):
        self.name = name
        self._context = context
        self._stack = stack
        self._depth = depth

    @property
    def super(self):
コード例 #20
0
ファイル: listControllers.py プロジェクト: oserve/PyNMR
 def __init__(self):
     """
     """
     Mapping.__init__(self)
     self.atomTypes = defaultdict(list)
コード例 #21
0
ファイル: listControllers.py プロジェクト: oserve/PyNMR
 def __init__(self):
     """
     """
     Mapping.__init__(self)
     self.resiNumbers = defaultdict(list)
コード例 #22
0
ファイル: containers.py プロジェクト: keheliyax/diofant
    def values(self):
        """D.values() -> list of D's values."""
        return self._dict.values()

    def __iter__(self):
        """x.__iter__() <==> iter(x)."""
        return iter(self._dict)

    def __len__(self):
        """x.__len__() <==> len(x)."""
        return self._dict.__len__()

    def get(self, key, default=None):
        """D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None."""
        return self._dict.get(sympify(key), default)

    def __contains__(self, key):
        """D.__contains__(k) -> True if D has a key k, else False."""
        return sympify(key) in self._dict

    def __lt__(self, other):
        return sympify(self.args < other.args)

    @property
    def _sorted_args(self):
        from ..utilities import default_sort_key
        return tuple(sorted(self.args, key=default_sort_key))


Mapping.register(Dict)
コード例 #23
0
    # until we can update we have to do it this way.
    def __iter__(self):
        """Iterator through db columns."""
        return ((column.name, getattr(self, column.name)) for column in self.__table__.columns
                if column.name not in self.__excluded_fields__)

    def __getitem__(self, item):
        """Get specific column value."""
        if item not in self.columns:
            raise KeyError("Invalid attribute name: %s" % item)
        return getattr(self, item)

    def __len__(self):
        """Returns number of db columns."""
        return len(self.columns)
Mapping.register(DictMixin)  # pylint: disable=no-member


# pylint: disable=too-few-public-methods
class JSONMixin(DictMixin):
    """
    JSON serialisation mixin for DB tables.

    A mixin class which provides basic serialisation
    for SQLAlchemy based table classes.
    """

    def encode_for_json(self):
        """Return an object that can be encoded with the default JSON encoder."""
        ret = {}
        for key, value in self:
コード例 #24
0
    def __init__(self, path: str):
        Mapping.__init__(self)
        Sized.__init__(self)

        os.makedirs(path, exist_ok=True)
        self.__path__ = path
コード例 #25
0
ファイル: containers.py プロジェクト: skirpichev/diofant
    def values(self):
        """D.values() -> list of D's values."""
        return self._dict.values()

    def __iter__(self):
        """x.__iter__() <==> iter(x)."""
        return iter(self._dict)

    def __len__(self):
        """x.__len__() <==> len(x)."""
        return self._dict.__len__()

    def get(self, key, default=None):
        """D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None."""
        return self._dict.get(sympify(key), default)

    def __contains__(self, key):
        """D.__contains__(k) -> True if D has a key k, else False."""
        return sympify(key) in self._dict

    def __lt__(self, other):
        return sympify(self.args < other.args)

    @property
    def _sorted_args(self):
        from ..utilities import default_sort_key
        return tuple(sorted(self.args, key=default_sort_key))


Mapping.register(Dict)
コード例 #26
0
ファイル: _pmap.py プロジェクト: zhangxuan1340/odooX64
        >>> m1
        pmap({'a': 1, 'b': 2})

        The changes are kept in the evolver. An updated pmap can be created using the
        persistent() function on the evolver.

        >>> m2 = e.persistent()
        >>> m2
        pmap({'c': 3, 'b': 2})

        The new pmap will share data with the original pmap in the same way that would have
        been done if only using operations on the pmap.
        """
        return self._Evolver(self)

Mapping.register(PMap)
Hashable.register(PMap)


def _turbo_mapping(initial, pre_size):
    if pre_size:
        size = pre_size
    else:
        try:
            size = 2 * len(initial) or 8
        except Exception:
            # Guess we can't figure out the length. Give up on length hinting,
            # we can always reallocate later.
            size = 8

    buckets = size * [None]
コード例 #27
0
ファイル: context.py プロジェクト: edelvalle/xoutil
        return 0

    def __iter__(self):
        return ()

    def __getitem__(self, key):
        raise KeyError(key)

    def __nonzero__(self):
        return False
    __bool__ = __nonzero__

    def __enter__(self):
        return _null_context

    def __exit__(self, exc_type, exc_value, traceback):
        return False

    def get(self, name, default=None):
        return default


_null_context = NullContext()


from collections import Mapping

Mapping.register(NullContext)

del Mapping
コード例 #28
0
        for key in list(other.keys()):
          self[key] = other[key]
      else:
        for key, value in other:
          self[key] = value
      for key, value in list(kwds.items()):
        self[key] = value

    def setdefault(self, key, default=None):
      try:
        return self[key]
      except KeyError:
        self[key] = default
      return default

  _Mapping.register(Mapping)

else:
  # In Python 3 we can just use MutableMapping directly, because it defines
  # __slots__.
  from collections import MutableMapping


class BaseContainer(object):

  """Base container class."""

  # Minimizes memory usage and disallows assignment to other attributes.
  __slots__ = ['_message_listener', '_values']

  def __init__(self, message_listener):
コード例 #29
0
                for key in other.keys():
                    self[key] = other[key]
            else:
                for key, value in other:
                    self[key] = value
            for key, value in kwds.items():
                self[key] = value

        def setdefault(self, key, default=None):
            try:
                return self[key]
            except KeyError:
                self[key] = default
            return default

    _Mapping.register(Mapping)

else:
    # In Python 3 we can just use MutableMapping directly, because it defines
    # __slots__.
    from collections import MutableMapping


class BaseContainer(object):
    """Base container class."""

    # Minimizes memory usage and disallows assignment to other attributes.
    __slots__ = ['_message_listener', '_values']

    def __init__(self, message_listener):
        """
コード例 #30
0
ファイル: _pmap.py プロジェクト: alx-/pyrsistent
        >>> m1
        pmap({'a': 1, 'b': 2})

        The changes are kept in the evolver. An updated pmap can be created using the
        persistent() function on the evolver.

        >>> m2 = e.persistent()
        >>> m2
        pmap({'c': 3, 'b': 2})

        The new pmap will share data with the original pmap in the same way that would have
        been done if only using operations on the pmap.
        """
        return self._Evolver(self)

Mapping.register(PMap)
Hashable.register(PMap)


def _turbo_mapping(initial, pre_size):
    size = pre_size or (2 * len(initial)) or 8
    buckets = size * [None]

    if not isinstance(initial, Mapping):
        # Make a dictionary of the initial data if it isn't already,
        # that will save us some job further down since we can assume no
        # key collisions
        initial = dict(initial)

    for k, v in six.iteritems(initial):
        h = hash(k)
コード例 #31
0
    """
    Interface to access latest cached value for a Property.
    """
    def get(self):
        """
        Get the most recent value of the property.
        :return: The most recent value of the property.
        """
        raise NotImplementedError()

    def on_updated(self, func):
        """
        Add a new callback for updated event.
        It can also be used as decorator.
        :param func: The callback.
        """
        raise NotImplementedError()

    @property
    def updated(self):
        """
        Get the updated event handler.
        :return utils.EventHandler: The event handler.
        """
        raise NotImplementedError()


# register Config as a compatible Mapping class
# so that isinstance(config, Mapping) returns True.
Mapping.register(Config)