Beispiel #1
0
    def set_size(self, size):
        """See `storm.store.Cache.set_size`.

        After calling this, the cache may still contain more than `size`
        objects, but no more than twice that number.
        """
        self._size = size
        cache = itertools.islice(
            itertools.chain(iter_items(self._new_cache),
                            iter_items(self._old_cache)), 0, size)
        self._new_cache = dict(cache)
        self._old_cache.clear()
Beispiel #2
0
 def _detect_attr_name(self, used_cls):
     self_id = id(self)
     for cls in used_cls.__mro__:
         for attr, prop in iter_items(cls.__dict__):
             if id(prop) == self_id:
                 return attr
     raise RuntimeError("Property used in an unknown class")
Beispiel #3
0
def assert_methods_match(first, second):
    """Assert that public methods in C{first} are present in C{second}.

    This helper function asserts that all public methods found in C{first} are
    also present in C{second} and accept the same arguments.  C{first} may have
    its own private methods, though, and may not have all methods found in
    C{second}.  Note that if a private method in C{first} matches the name of
    one in C{second}, their specification is still compared.

    This is useful to verify if a fake or stub class have the same API as the
    real class being simulated.
    """
    first_methods = dict(inspect.getmembers(first, inspect.ismethod))
    second_methods = dict(inspect.getmembers(second, inspect.ismethod))
    for name, first_method in iter_items(first_methods):
        # First may have its own private methods.
        if name.startswith("_"):
            continue

        first_argspec = inspect.getargspec(first_method)
        first_formatted = inspect.formatargspec(*first_argspec)

        assert name in second_methods
        second_method = second_methods.get(name)
        second_argspec = inspect.getargspec(second_method)
        assert first_argspec == second_argspec
Beispiel #4
0
 def __str__(self):
     tokens = [self.scheme, ":"]
     append = tokens.append
     if (self.username is not None or self.password is not None
             or self.host is not None or self.port is not None):
         append("//")
         if self.username is not None or self.password is not None:
             if self.username is not None:
                 append(escape(self.username))
             if self.password is not None:
                 append(":")
                 append(escape(self.password))
             append("@")
         if self.host is not None:
             append(escape(self.host))
         if self.port is not None:
             append(":")
             append(str(self.port))
         append("/")
     if self.database is not None:
         append(escape(self.database, "/"))
     if self.options:
         options = [
             "%s=%s" % (escape(key), escape(value))
             for key, value in sorted(iter_items(self.options))
         ]
         append("?")
         append("&".join(options))
     return "".join(tokens)
Beispiel #5
0
    def __init__(self, name=None, primary=False, **kwargs):
        set_map = dict(kwargs.pop("map"))
        get_map = {value: key for key, value in iter_items(set_map)}
        if "set_map" in kwargs:
            set_map = dict(kwargs.pop("set_map"))

        kwargs["get_map"] = get_map
        kwargs["set_map"] = set_map
        SimpleProperty.__init__(self, name, primary, **kwargs)
Beispiel #6
0
def _find_descriptor_obj(used_cls, descr):
    for cls in used_cls.__mro__:
        for attr, _descr in iter_items(cls.__dict__):
            if _descr is descr:
                return getattr(cls, attr)
    raise RuntimeError("Reference used in an unknown class")