Esempio n. 1
0
    def estr(self, **kwargs):
        '''Should return a string that can eval into an equivalent object'''
        if hasmethod(self.obj, '_estr'):
            return escape_for_eval(self.obj._estr(**kwargs))

        objstr = escape_for_eval(quote_string(str(self.obj)))
        return '{}({})'.format(get_typename(self.obj), objstr)
Esempio n. 2
0
    def visit(self, k, **kwargs):
        step = kwargs.get('step', 1)
        enum = kwargs.get('enumerate', False)

        self._attrs = self.attrs(**kwargs)
        self.is_primitive = not bool(self._attrs)
        N = self.visit_len(**kwargs)

        count = 0
        limit = iteration_length(N, k, step)
        while True:
            if count >= limit:
                raise StopIteration

            if hasmethod(self.obj, '_visit'):
                item = self.obj._visit(k, **kwargs)
            else:
                item = self._visit(k, **kwargs)

            if enum:
                yield k, item
            else:
                yield item

            k += step
            count += 1
Esempio n. 3
0
    def find_ne(self, other, func=op.eq, **kwargs):
        if func(self.obj, other):
            return

        if type(self.obj) is not type(other):
            from .ne import DifferentTypes
            return DifferentTypes(self.obj, other)

        if hasmethod(self.obj, '_find_ne'):
            return self.obj._find_ne(other, func, **kwargs)
        return self._find_ne(other, func, **kwargs)
Esempio n. 4
0
    def serialize(self, **kwargs):
        # TODO: option for custom idempotent types (may be different
        # for different serialization methods)
        if type(self.obj) in SER_IDEMPOTENT:
            return self.obj

        dct = self._serialize_dict(type(self.obj), **kwargs)
        if hasmethod(self.obj, '_serialize'):
            return self.obj._serialize(dct, **kwargs)

        self._serialize(dct, **kwargs)
        return dct
Esempio n. 5
0
def test_hasmethod():
    Foo = Methods

    f = Foo()
    f.bar()
    Foo.cbar()
    Foo.sbar()

    assert hasmethod(f, 'bar')
    assert hasmethod(Foo, 'bar')

    assert not hasmethod(f, 'a')
    assert not hasmethod(f, 'foo')

    assert hasmethod(f, 'cbar')
    assert hasmethod(Foo, 'cbar')

    assert not hasmethod(f, 'sbar')
    if six.PY2:
        assert not hasmethod(Foo, 'sbar')
    else:
        assert hasmethod(Foo, 'sbar')
Esempio n. 6
0
def test_hasmethod():
    Foo = Methods

    f = Foo()
    f.bar()
    Foo.cbar()
    Foo.sbar()

    assert hasmethod(f, 'bar')
    assert hasmethod(Foo, 'bar')

    assert not hasmethod(f, 'a')
    assert not hasmethod(f, 'foo')

    assert hasmethod(f, 'cbar')
    assert hasmethod(Foo, 'cbar')

    assert not hasmethod(f, 'sbar')
    if six.PY2:
        assert not hasmethod(Foo, 'sbar')
    else:
        assert hasmethod(Foo, 'sbar')
Esempio n. 7
0
    def _populate_data(self):
        self._data = Data()
        opt = Meta._get_opt

        # Generate attr display order
        self._data.attr_display_order = sorted(self._attrs.keys())

        # Gather persistent pre-create hooks
        self._data.pre_create_hooks = \
            {getfunc(f) for f in callables(self).values()
             if getattr(f, 'is_pre_create_hook', None) is _PreCreateHook
             and getattr(f, 'persist', False)}

        # Generate attr documentation order
        tmp = []
        attrs = list(self._data.attr_display_order)
        for attr in opt(self, 'args', default=()):
            tmp.append(attr)
            attrs.remove(attr)
        tmp += attrs
        self._data.kw_attrs = attrs
        self._data.attr_documentation_order = tmp

        # Process metaclass_lookup
        sopt = partial(opt, opts='_seq_opts', default=list)
        for attr in sopt(self, 'metaclass_lookup'):
            attrs = sopt(self, attr)
            values = [getattr(self, attr_) for attr_ in attrs]
            values = type(attrs)(values)
            setattr(self._data, attr, values)

        # Register subclasses
        reg = partial(opt, name='register_subclasses', default=False)
        if reg(self):
            for c in self.mro():
                if hasmethod(c, '_get_opt'):
                    if reg(c):
                        if issubclass(self, c):
                            lst = rgetattr(c, '_data.subclasses')
                            if self not in lst:
                                lst.append(self)
                            c._data.subclasses = lst
Esempio n. 8
0
    def deserialize(cls, dct, **kwargs_):
        if not isinstance(dct, dict):
            return dct

        name = dct[SER_KEYS.name]
        mod = import_module(dct[SER_KEYS.mod])
        args = dct.get(SER_KEYS.args, [])
        kwargs = dct.get(SER_KEYS.kwargs, {})
        attrs = dct.get(SER_KEYS.attrs, {})

        if args:
            args = deserialize(args, **kwargs_)
        if kwargs:
            kwargs = deserialize(kwargs, **kwargs_)
        if attrs:
            attrs = deserialize(attrs, **kwargs_)

        typ = getattr(mod, name)
        if dct.get(SER_KEYS.is_type, False):
            return typ

        if args and kwargs:
            obj = typ(*args, **kwargs)
        elif args:
            obj = typ(*args)
        elif kwargs:
            obj = typ(**kwargs)
        else:
            obj = typ()

        for attr, val in attrs.items():
            setattr(obj, attr, val)

        if hasmethod(obj, '_deserialize'):
            obj._deserialize(dct)
        return obj
Esempio n. 9
0
 def __init__(self, typ):
     super(TypeType, self).__init__()
     self.type = typ
     self.call_coerce = hasmethod(self.type, 'coerce')
     self.call_validate = hasmethod(self.type, 'validate')
Esempio n. 10
0
 def collect(self, func, **kwargs):
     if hasmethod(self.obj, '_collect'):
         return self.obj._collect(func, **kwargs)
     return self._collect(func, **kwargs)
Esempio n. 11
0
 def visit_len(self, **kwargs):
     if hasmethod(self.obj, '_visit_len'):
         return self.obj._visit_len(**kwargs)
     return self._visit_len(**kwargs)
Esempio n. 12
0
 def rstr(self, **kwargs):
     '''The idea is somethinig like a recursive str().'''
     if hasmethod(self.obj, '_rstr'):
         return self.obj._rstr(**kwargs)
     return self._rstr(**kwargs)
Esempio n. 13
0
 def hashable(self, **kwargs):
     if hasmethod(self.obj, '_hashable'):
         return self.obj._hashable(**kwargs)
     return self._hashable(**kwargs)
Esempio n. 14
0
 def _generate(cls, **kwargs):
     if hasmethod(cls.type, '_generate'):
         return cls.type._generate(**kwargs)
     raise NotImplementedError
Esempio n. 15
0
 def enumeration_value(cls, x, **kwargs):
     if hasmethod(cls.type, '_enumeration_value'):
         return cls.type._enumeration_value(x, **kwargs)
     return cls._enumeration_value(x, **kwargs)