Пример #1
0
                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):
Пример #2
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):
Пример #3
0
        >>> 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)
Пример #4
0
        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
Пример #5
0
    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)
Пример #6
0
        undefined.
        """
        item = self.resolve_or_missing(key)
        if item is missing:
            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."""
Пример #7
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)
Пример #8
0
        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]