Example #1
0
def _t_impl_of_ABCMeta():
    class C(__error_ABC):
        @abstractmethod
        def f(sf, /):pass
    assert inspect.isabstract(C)
    assert {'f'} == getattr(C, '__abstractmethods__', ())
    assert True is getattr(C.__dict__['f'], '__isabstractmethod__', False)
    assert expectError(TypeError, C)
    del C.__abstractmethods__
    assert not expectError(TypeError, C)
    C()
    assert inspect.isabstract(C)
Example #2
0
def _test__DataDescriptor(Descriptor, /):
    class C:
        @Descriptor
        def echo(*args):
            return args

    c = C()
    assert expectError(AttributeError, lambda: setattr(c, 'echo', 1))
    c.__dict__['echo'] = ...
    if c.echo is ...: raise logic - err
Example #3
0
def _test__using_slots(Descriptor, /):
    descriptor = Descriptor(id)
    assert expectError(AttributeError, lambda: setattr(descriptor, 'x', 1))
    try:
        descriptor.x = 1
    except AttributeError as e:
        assert repr(
            e
        ) == fr'''AttributeError("'{Descriptor.__name__!s}' object has no attribute 'x'")'''

    else:
        raise logic - err
Example #4
0

class _(ABC4check_slots_setting__no_slots):
    pass


assert _.___no_slots_ok___


def _t():
    class _(ABC4check_slots_setting):
        pass


#_t()
assert expectError(TypeError, _t)

ABCMeta = ABCMeta4check_slots_setting
ABC = ABC4check_slots_setting
ABC__no_slots = ABC4check_slots_setting__no_slots


def override(f):
    return f


def define(f):
    return f


def final(f):
Example #5
0
    '''.split()

from seed.tiny_.echo_key import echo_key
from seed.tiny_.funcs import echo_args
from seed.debug.expectError import expectError

def range2triple(range_, /):
    'range -> (.start, .stop, .step)  #range is both immutable and hashable, diff slice'
    #check_type_is(range_, range)
    assert type(range_) is range
    return range_.start, range_.stop, range_.step
assert range2triple(range(3, 9, 2)) == (3, 9, 2)


hash(range(5))
assert expectError(TypeError, lambda:hash(slice(1,5)))


def slice2triple(slice_, /):
    'slice -> (.start, .stop, .step)  #slice is immutable but unhashable, to be used as special-key for __getitem__'
    #check_type_is(slice_, slice)
    assert type(slice_) is slice
    return slice_.start, slice_.stop, slice_.step
assert slice2triple(slice(3, 9, 2)) == (3, 9, 2)


def slice2item(kv, /):
    if not type(kv) is slice: raise TypeError
    k, v, xxx = slice2triple(kv)
    if not xxx is None: raise TypeError
    return k, v
        if not (_v is v):
            print(v)
            print(_v)
            raise ValueError
    else:
        if not (_v is v): raise ValueError
    return


def dict_add__eq(d, k, v, /):
    _v = d.setdefault(k, v)
    if not (_v is v or _v == v): raise ValueError
    return


assert expectError(ValueError, lambda: dict_add__eq({1: 2}, 1, 3))
assert not expectError(ValueError, lambda: dict_add__eq({1: []}, 1, []))

assert expectError(ValueError, lambda: dict_add__is({1: 2}, 1, 3))
assert expectError(ValueError, lambda: dict_add__is({1: []}, 1, []))
assert not expectError(ValueError, lambda: dict_add__is({1: True}, 1, True))


def group4dict_value(f, d, /):
    #partition4dict_value??
    g2k2v = group2part = {}
    for k, v in d.items():
        g = f(v)
        k2v = g2k2v.setdefault(g, {})
        k2v[k] = v
    return g2k2v
Example #7
0
check_type_le(object, '')
assert 1 == icheck_uint(1)
assert (0, ) == icheck_tmay((0, ))
assert (0, 0) == icheck_pair((0, 0))
assert '' == icheck_type_is(str, '')
assert '' == icheck_type_le(object, '')

assert 'class'.isidentifier()
assert 'def'.isidentifier()
check_pseudo_identifier('def')
check_smay_pseudo_qual_name('')
check_smay_pseudo_qual_name('x')
check_smay_pseudo_qual_name('x.def')
check_pseudo_qual_name('x')
check_pseudo_qual_name('x.def')
assert expectError(TypeError, lambda: check_pseudo_identifier(''))
assert expectError(TypeError, lambda: check_pseudo_identifier(' '))
assert expectError(TypeError, lambda: check_pseudo_identifier('.x'))

assert expectError(TypeError, lambda: check_smay_pseudo_qual_name(' '))
assert expectError(TypeError, lambda: check_smay_pseudo_qual_name('.x'))

assert expectError(TypeError, lambda: check_pseudo_qual_name(''))

from seed.tiny_.containers import null_str, null_bytes, null_int, null_tuple, null_frozenset, null_mapping_view, null_iter, mk_frozenset, mk_tuple
assert mk_tuple([]) is null_tuple
assert mk_frozenset([]) is null_frozenset
assert null_str == ''
assert null_bytes == b''
assert null_int == 0
assert null_tuple == ()
assert None is lookup_may_PermanentSymbol(__name__, '_T', 'PermanentSymbol')
register_new_PermanentSymbol(__name__, '_T', 'PermanentSymbol',
                             'PermanentSymbol class')
register_new_PermanentSymbol(__name__, '_T', 'PermanentSymbol.__dir__',
                             'PermanentSymbol class::__dir__')
register_new_PermanentSymbol(__name__, '_T', 'PermanentSymbol.uuuuu',
                             'PermanentSymbol class::uuuuu-nonexist')
assert not None is lookup_may_PermanentSymbol(__name__, '_T',
                                              'PermanentSymbol')
assert PermanentSymbol is type(
    lookup_may_PermanentSymbol(__name__, '_T', 'PermanentSymbol'))
assert PermanentSymbol is type(
    lookup_PermanentSymbol(__name__, '_T', 'PermanentSymbol'))

assert expectError(
    AttributeError,
    lambda: fill_module_with_registered_permanent_symbols(__name__))


class _T:
    pass


assert expectError(
    AttributeError,
    lambda: get_PermanentSymbol(__name__, '_T', 'PermanentSymbol'))
fill_module_with_registered_permanent_symbols(__name__)
assert _T.PermanentSymbol is get_PermanentSymbol(
    __name__, '_T', 'PermanentSymbol') is lookup_PermanentSymbol(
        __name__, '_T', 'PermanentSymbol')
assert _T.PermanentSymbol is eval(repr(_T.PermanentSymbol))
    #Mapping.__eq__: return dict(self.items()) == dict(other.items())
    #return len(ot) == len(sf) and Mapping.__eq__(sf, ot)
    f = mapping_to_key_id2value
    return len(ot) == len(sf) and f(ot) == f(sf)


def set_to_key_id_set(s, /):
    return {*map(id, s)}


def set_eq4key_eq_by_id(sf, ot, /):
    f = set_to_key_id_set
    return len(ot) == len(sf) and f(ot) == f(sf)


assert expectError(AttributeError,
                   lambda: setattr(PermanentKeyRefDict(), 'x', 1))


# view ~/../usr/lib/python3.8/abc.py
# view ~/../usr/lib/python3.8/_collections_abc.py
#Mapping ok: has '__slots__ = ()'
#ABC ok: has '__slots__ = ()'
class PermanentKeyRefSet(MutableSet):
    r'''
    PermanentKeyRefSet<key>
        via dict<id(key), key>
        alternative impl:
            via set<AddrAsHashWrapper(key)>
    #'''
    __slots__ = '_s'
    __slots__ = ('_s', '__weakref__')
    str_split__list_nonemty

    str_join__entry_nonemty
    str_split__entry_nonemty

    str_join__both_list_and_entry_may_be_emty
    str_split__both_list_and_entry_may_be_emty
    '''.split()

from seed.debug.expectError import expectError
from itertools import chain

assert ','.join([]) == '' == ','.join([''])
# 碰撞,歧义!
assert ''.split(',') == ['']
assert expectError(ValueError, lambda: ''.split(''))
# ''.split('')
#   ==>> ValueError: empty separator


def str_join__list_nonemty(sep, iterable, /):
    if not sep: raise TypeError
    ls = [*iterable] if type(iterable) is not list else iterable
    if not ls: raise TypeError
    s = sep.join(ls)
    if not ls == str_split__list_nonemty(sep, s): raise logic - err
    return s


def str_split__list_nonemty(sep, s, /):
    if not sep: raise TypeError