Exemple #1
0
def test_full_funcname():
    from syn.base_utils import full_funcname, REPL, this_module

    class Foo(object):
        @classmethod
        def bar(cls):
            pass

        @staticmethod
        def baz():
            pass

    Foo.bar()
    Foo.baz()

    r = REPL()
    s = set([1, 2, 3])
    modname = this_module().__name__

    assert full_funcname(full_funcname) == 'syn.base_utils.py.full_funcname'
    assert full_funcname(r.eval) == 'syn.base_utils.repl.REPL.eval'
    if six.PY2:
        assert_raises(TypeError, full_funcname, REPL.eval)
    else:
        assert full_funcname(REPL.eval) == 'syn.base_utils.repl.eval'
    assert full_funcname(Foo.bar) == '{}.Foo.bar'.format(modname)
    assert full_funcname(Foo.baz) == '{}.baz'.format(modname)

    if six.PY2:
        assert full_funcname(s.__contains__) == '__builtin__.set.__contains__'
    else:
        assert full_funcname(s.__contains__) == 'builtins.set.__contains__'
Exemple #2
0
def test_this_module():
    from syn.base_utils import this_module

    this = this_module()
    assert hasattr(this, 'test_this_module')
    assert hasattr(this, 'test_import_module')
    assert this.test_this_module is test_this_module
Exemple #3
0
def test_this_module():
    from syn.base_utils import this_module

    this = this_module()
    assert hasattr(this, 'test_this_module')
    assert hasattr(this, 'test_import_module')
    assert this.test_this_module is test_this_module
Exemple #4
0
def test_full_funcname():
    from syn.base_utils import full_funcname, REPL, this_module
    
    class Foo(object):
        @classmethod
        def bar(cls):
            pass
        @staticmethod
        def baz():
            pass

    Foo.bar()
    Foo.baz()

    r = REPL()
    s = set([1, 2, 3])
    modname = this_module().__name__

    assert full_funcname(full_funcname) == 'syn.base_utils.py.full_funcname'
    assert full_funcname(r.eval) == 'syn.base_utils.repl.REPL.eval'
    if six.PY2:
        assert_raises(TypeError, full_funcname, REPL.eval)
    else:
        assert full_funcname(REPL.eval) == 'syn.base_utils.repl.eval'
    assert full_funcname(Foo.bar) == '{}.Foo.bar'.format(modname)
    assert full_funcname(Foo.baz) == '{}.baz'.format(modname)

    if six.PY2:
        assert full_funcname(s.__contains__) == '__builtin__.set.__contains__'
    else:
        assert full_funcname(s.__contains__) == 'builtins.set.__contains__'
Exemple #5
0
def test_elog():
    from syn.base_utils import elog, this_module
    import syn.base_utils.py as sp

    modname = this_module().__name__

    class ElogTest(Exception): pass

    msgs = []
    class FakeLogger(object):
        def error(self, msg):
            msgs.append(msg)
        @classmethod
        def cmeth(cls):
            pass
        @staticmethod
        def smeth():
            pass
            
    
    FakeLogger.cmeth()
    FakeLogger.smeth()

    logger = FakeLogger()
    with assign(sp, 'elogger', logger):
        elog(ElogTest('msg 1'), elog, (1, 1.2, 'abc'))
        assert msgs[-1] == ('***ElogTest***: "msg 1" --- '
                            'syn.base_utils.py.elog(1, 1.2, abc)')

        elog(ElogTest('msg 2'), message, (1, 1.2, 'abc'), dict(a=2, b=3.4))
        assert msgs[-1] == ('***ElogTest***: "msg 2" --- '
                            'syn.base_utils.py.message(1, 1.2, abc, a=2, b=3.4)')

        elog(ElogTest('msg 3'), test_elog, (1, 1.2, 'abc'), dict(a=2), 
             pretty=False)
        assert msgs[-1] == ('***ElogTest***: "msg 3" --- '
                            '{}.test_elog'.format(modname) + 
                            '(args=(1, 1.2, \'abc\'), '
                            'kwargs={\'a\': 2})')

        elog(ElogTest('msg4'), logger.error, ('abc',))
        assert msgs[-1] == ('***ElogTest***: "msg4" --- '
                            '{}.FakeLogger.error(abc)'.format(modname))
                            
        elog(ElogTest('msg5'), FakeLogger.cmeth)
        assert msgs[-1] == ('***ElogTest***: "msg5" --- '
                            '{}.FakeLogger.cmeth()'.format(modname))

        elog(ElogTest('msg6'), FakeLogger.smeth)
        assert msgs[-1] == ('***ElogTest***: "msg6" --- '
                            '{}.smeth()'.format(modname))

        elog(ElogTest('msg7'), FakeLogger.smeth, name='FakeLogger.smeth')
        assert msgs[-1] == ('***ElogTest***: "msg7" --- '
                            '{}.FakeLogger.smeth()'.format(modname))

        elog(ElogTest('msg8'), elog, (1, 1.2, u'ab\u2013cd'))
Exemple #6
0
def delete(*args):
    '''For using then deleting objects.'''
    from syn.base_utils import this_module
    mod = this_module(npop=3)
    yield
    for arg in args:
        name = arg
        if not isinstance(name, STR):
            name = arg.__name__
        delattr(mod, name)
Exemple #7
0
def delete(*args):
    '''For using then deleting objects.'''
    from syn.base_utils import this_module
    mod = this_module(npop=3)
    yield
    for arg in args:
        name = arg
        if not isinstance(name, STR):
            name = arg.__name__
        delattr(mod, name)
Exemple #8
0
def test_ngzwarn():
    from syn.base_utils import ngzwarn, this_module
    import syn.base_utils.py as sp

    mod = this_module()

    msgs = []
    class FakeLogger(object):
        def warning(self, msg):
            msgs.append(msg)

    logger = FakeLogger()
    with assign(sp, 'test_logger', logger):
        ngzwarn(1, 'FOO')
        assert msgs == []

        ngzwarn(0, 'Test value 1')
        assert msgs[-1] == ('Test value 1 set to value <= 0 (0) in {}'
                            .format(mod.__name__))

        ngzwarn(-1, 'Test value 2')
        assert msgs[-1] == ('Test value 2 set to value <= 0 (-1) in {}'
                            .format(mod.__name__))
Exemple #9
0
def test_ngzwarn():
    from syn.base_utils import ngzwarn, this_module
    import syn.base_utils.py as sp

    mod = this_module()

    msgs = []

    class FakeLogger(object):
        def warning(self, msg):
            msgs.append(msg)

    logger = FakeLogger()
    with assign(sp, 'test_logger', logger):
        ngzwarn(1, 'FOO')
        assert msgs == []

        ngzwarn(0, 'Test value 1')
        assert msgs[-1] == ('Test value 1 set to value <= 0 (0) in {}'.format(
            mod.__name__))

        ngzwarn(-1, 'Test value 2')
        assert msgs[-1] == ('Test value 2 set to value <= 0 (-1) in {}'.format(
            mod.__name__))
Exemple #10
0
def test_elog():
    from syn.base_utils import elog, this_module
    import syn.base_utils.py as sp

    modname = this_module().__name__

    class ElogTest(Exception):
        pass

    msgs = []

    class FakeLogger(object):
        def error(self, msg):
            msgs.append(msg)

        @classmethod
        def cmeth(cls):
            pass

        @staticmethod
        def smeth():
            pass

    FakeLogger.cmeth()
    FakeLogger.smeth()

    logger = FakeLogger()
    with assign(sp, 'elogger', logger):
        elog(ElogTest('msg 1'), elog, (1, 1.2, 'abc'))
        assert msgs[-1] == ('***ElogTest***: "msg 1" --- '
                            'syn.base_utils.py.elog(1, 1.2, abc)')

        elog(ElogTest('msg 2'), message, (1, 1.2, 'abc'), dict(a=2, b=3.4))
        assert msgs[-1] == (
            '***ElogTest***: "msg 2" --- '
            'syn.base_utils.py.message(1, 1.2, abc, a=2, b=3.4)')

        elog(ElogTest('msg 3'),
             test_elog, (1, 1.2, 'abc'),
             dict(a=2),
             pretty=False)
        assert msgs[-1] == ('***ElogTest***: "msg 3" --- '
                            '{}.test_elog'.format(modname) +
                            '(args=(1, 1.2, \'abc\'), '
                            'kwargs={\'a\': 2})')

        elog(ElogTest('msg4'), logger.error, ('abc', ))
        assert msgs[-1] == ('***ElogTest***: "msg4" --- '
                            '{}.FakeLogger.error(abc)'.format(modname))

        elog(ElogTest('msg5'), FakeLogger.cmeth)
        assert msgs[-1] == ('***ElogTest***: "msg5" --- '
                            '{}.FakeLogger.cmeth()'.format(modname))

        elog(ElogTest('msg6'), FakeLogger.smeth)
        assert msgs[-1] == ('***ElogTest***: "msg6" --- '
                            '{}.smeth()'.format(modname))

        elog(ElogTest('msg7'), FakeLogger.smeth, name='FakeLogger.smeth')
        assert msgs[-1] == ('***ElogTest***: "msg7" --- '
                            '{}.FakeLogger.smeth()'.format(modname))

        elog(ElogTest('msg8'), elog, (1, 1.2, u'ab\u2013cd'))
Exemple #11
0
 def serialize_type(cls, typ, **kwargs):
     dct = {SER_KEYS.name: 'NoneType',
            SER_KEYS.mod: this_module().__name__,
            SER_KEYS.is_type: True}
     return dct
Exemple #12
0
def test_repr_template():
    r = REPR1(a = 1, b = 2.3)
    assert repr(r) == '<{}.REPR1 a=1 b=2.3>'.format(this_module().__name__)

    r._opts.repr_template = '{b}'
    assert repr(r) == '2.3'
Exemple #13
0
def test_repr_template():
    r = REPR1(a=1, b=2.3)
    assert repr(r) == '<{}.REPR1 a=1 b=2.3>'.format(this_module().__name__)

    r._opts.repr_template = '{b}'
    assert repr(r) == '2.3'