Пример #1
0
    def test_coerce(self):
        class O(object):
            def __coerce__(self, other):
                return self, other

        o = Proxy(O)
        self.assertTrue(o.__coerce__(3))
Пример #2
0
    def test_reduce(self):
        class X(object):
            def __reduce__(self):
                return 123

        x = Proxy(lambda: X())
        self.assertEqual(x.__reduce__(), 123)
Пример #3
0
    def test_index(self):
        class O(object):
            def __index__(self):
                return 1

        o = Proxy(O)
        self.assertEqual(o.__index__(), 1)
Пример #4
0
    def test_reduce(self):
        class X(object):
            def __reduce__(self):
                return 123

        x = Proxy(lambda: X())
        assert x.__reduce__() == 123
Пример #5
0
    def test_complex_cast(self):
        class O(object):
            def __complex__(self):
                return complex(10.333)

        o = Proxy(O)
        assert o.__complex__() == complex(10.333)
Пример #6
0
    def test_index(self):
        class O(object):
            def __index__(self):
                return 1

        o = Proxy(O)
        assert o.__index__() == 1
Пример #7
0
    def test_index(self):
        class O(object):
            def __index__(self):
                return 1

        o = Proxy(O)
        self.assertEqual(o.__index__(), 1)
Пример #8
0
    def test_complex_cast(self):
        class O(object):
            def __complex__(self):
                return complex(10.333)

        o = Proxy(O)
        self.assertEqual(o.__complex__(), complex(10.333))
Пример #9
0
    def test_reduce(self):
        class X(object):
            def __reduce__(self):
                return 123

        x = Proxy(lambda: X())
        self.assertEqual(x.__reduce__(), 123)
Пример #10
0
    def test_coerce(self):
        class O(object):
            def __coerce__(self, other):
                return self, other

        o = Proxy(O)
        assert o.__coerce__(3)
Пример #11
0
    def test_complex_cast(self):
        class O(object):
            def __complex__(self):
                return complex(10.333)

        o = Proxy(O)
        self.assertEqual(o.__complex__(), complex(10.333))
Пример #12
0
    def test_reduce(self):

        class X(object):

            def __reduce__(self):
                return 123

        x = Proxy(lambda: X())
        assert x.__reduce__() == 123
Пример #13
0
    def test_index(self):

        class O(object):

            def __index__(self):
                return 1

        o = Proxy(O)
        assert o.__index__() == 1
Пример #14
0
    def test_complex_cast(self):

        class O(object):

            def __complex__(self):
                return complex(10.333)

        o = Proxy(O)
        assert o.__complex__() == complex(10.333)
Пример #15
0
    def test_dir(self):
        class X(object):
            def __dir__(self):
                return ['a', 'b', 'c']

        x = Proxy(lambda: X())
        self.assertListEqual(dir(x), ['a', 'b', 'c'])

        class Y(object):
            def __dir__(self):
                raise RuntimeError()

        y = Proxy(lambda: Y())
        self.assertListEqual(dir(y), [])
Пример #16
0
    def test_dir(self):
        class X(object):
            def __dir__(self):
                return ['a', 'b', 'c']

        x = Proxy(lambda: X())
        assert dir(x) == ['a', 'b', 'c']

        class Y(object):
            def __dir__(self):
                raise RuntimeError()

        y = Proxy(lambda: Y())
        assert dir(y) == []
Пример #17
0
 def test_listproxy(self):
     v = []
     x = Proxy(lambda: v)
     x.append(1)
     x.extend([2, 3, 4])
     self.assertEqual(x[0], 1)
     self.assertEqual(x[:-1], [1, 2, 3])
     del(x[-1])
     self.assertEqual(x[:-1], [1, 2])
     x[0] = 10
     self.assertEqual(x[0], 10)
     self.assertIn(10, x)
     self.assertEqual(len(x), 3)
     self.assertTrue(iter(x))
Пример #18
0
    def test_getsetdel_attr(self):
        if sys.version_info < (2, 6):
            raise SkipTest('Not relevant for Py2.5')

        class X(object):
            a = 1
            b = 2
            c = 3

            def __dir__(self):
                return ['a', 'b', 'c']

        v = X()

        x = Proxy(lambda: v)
        self.assertListEqual(x.__members__, ['a', 'b', 'c'])
        self.assertEqual(x.a, 1)
        self.assertEqual(x.b, 2)
        self.assertEqual(x.c, 3)

        setattr(x, 'a', 10)
        self.assertEqual(x.a, 10)

        del(x.a)
        self.assertEqual(x.a, 1)
Пример #19
0
    def test_name(self):
        def real():
            """real function"""
            return 'REAL'

        x = Proxy(lambda: real, name='xyz')
        self.assertEqual(x.__name__, 'xyz')

        y = Proxy(lambda: real)
        self.assertEqual(y.__name__, 'real')

        self.assertEqual(x.__doc__, 'real function')

        self.assertEqual(x.__class__, type(real))
        self.assertEqual(x.__dict__, real.__dict__)
        self.assertEqual(repr(x), repr(real))
Пример #20
0
    def test_slots(self):
        class X(object):
            __slots__ = ()

        x = Proxy(X)
        with pytest.raises(AttributeError):
            x.__dict__
Пример #21
0
 def __call__(self):
     def _lookup():
         rv = self.top
         if rv is None:
             raise RuntimeError('object unbound')
         return rv
     return Proxy(_lookup)
Пример #22
0
    def test_nonzero(self):
        class X(object):
            def __nonzero__(self):
                return False

        x = Proxy(lambda: X())
        self.assertFalse(x)
Пример #23
0
    def test_call(self):

        class X:

            def __call__(self):
                return 1234

        assert Proxy(lambda: X())() == 1234
Пример #24
0
    def test_hash(self):

        class X:

            def __hash__(self):
                return 1234

        assert hash(Proxy(lambda: X())) == 1234
Пример #25
0
    def test_dir(self):
        if sys.version_info < (2, 6):
            raise SkipTest('Not relevant for Py2.5')

        class X(object):
            def __dir__(self):
                return ['a', 'b', 'c']

        x = Proxy(lambda: X())
        self.assertListEqual(dir(x), ['a', 'b', 'c'])

        class Y(object):
            def __dir__(self):
                raise RuntimeError()

        y = Proxy(lambda: Y())
        self.assertListEqual(dir(y), [])
Пример #26
0
    def test_name(self):

        def real():
            """real function"""
            return "REAL"

        x = Proxy(lambda: real, name="xyz")
        self.assertEqual(x.__name__, "xyz")

        y = Proxy(lambda: real)
        self.assertEqual(y.__name__, "real")

        self.assertEqual(x.__doc__, "real function")

        self.assertEqual(x.__class__, type(real))
        self.assertEqual(x.__dict__, real.__dict__)
        self.assertEqual(repr(x), repr(real))
Пример #27
0
    def test_name(self):
        def real():
            """real function"""
            return 'REAL'

        x = Proxy(lambda: real, name='xyz')
        assert x.__name__ == 'xyz'

        y = Proxy(lambda: real)
        assert y.__name__ == 'real'

        assert x.__doc__ == 'real function'

        assert x.__class__ == type(real)
        assert x.__dict__ == real.__dict__
        assert repr(x) == repr(real)
        assert x.__module__
Пример #28
0
    def test_hash(self):

        class X(object):

            def __hash__(self):
                return 1234

        self.assertEqual(hash(Proxy(lambda: X())), 1234)
Пример #29
0
    def test_call(self):

        class X(object):

            def __call__(self):
                return 1234

        self.assertEqual(Proxy(lambda: X())(), 1234)
Пример #30
0
    def test_bool(self):
        class X(object):
            def __bool__(self):
                return False

            __nonzero__ = __bool__

        x = Proxy(lambda: X())
        assert not x
Пример #31
0
 def test_dictproxy(self):
     v = {}
     x = Proxy(lambda: v)
     x["foo"] = 42
     self.assertEqual(x["foo"], 42)
     self.assertEqual(len(x), 1)
     self.assertIn("foo", x)
     del(x["foo"])
     with self.assertRaises(KeyError):
         x["foo"]
     self.assertTrue(iter(x))
Пример #32
0
 def test_dictproxy(self):
     v = {}
     x = Proxy(lambda: v)
     x['foo'] = 42
     assert x['foo'] == 42
     assert len(x) == 1
     assert 'foo' in x
     del (x['foo'])
     with pytest.raises(KeyError):
         x['foo']
     assert iter(x)
Пример #33
0
    def test_unicode(self):
        class X(object):
            def __unicode__(self):
                return u'UNICODE'

            def __repr__(self):
                return 'REPR'

        x = Proxy(lambda: X())
        self.assertEqual(unicode(x), u'UNICODE')
        del (X.__unicode__)
        self.assertEqual(unicode(x), 'REPR')
Пример #34
0
    def test_unicode(self):

        class X(object):

            def __unicode__(self):
                return u"UNICODE"

            def __repr__(self):
                return "REPR"

        x = Proxy(lambda: X())
        self.assertEqual(unicode(x), u"UNICODE")
        del(X.__unicode__)
        self.assertEqual(unicode(x), "REPR")
Пример #35
0
def get_connection(name, **kw):
    cnf = xconf.get_connection(name)
    # print(cnf)
    if cnf:
        cnf = cnf.copy()
    else:
        cnf = {}
    dsn = cnf.pop('dsn', None) if cnf else None
    if not dsn:
        raise ConfigError(u'不存在的连接 %s 设置' % name)

    pd = urlparse(dsn)
    scheme = pd.scheme
    if scheme == 'redis':
        return Proxy(functools.partial(get_redis, dsn))
    elif scheme == 'mongodb':
        return Proxy(functools.partial(get_mongodb, dsn, **cnf))
    elif scheme == 'mysql':
        return Proxy(functools.partial(get_mysql, dsn, **cnf))
    elif scheme == 'influxdb':
        return Proxy(functools.partial(get_influxdb, dsn))
    else:
        cnf['dsn'] = dsn
        raise ConfigError(u'连接 %s 设置错误 %s' % (name, cnf))
Пример #36
0
    def test_unicode(self):
        class X(object):
            def __unicode__(self):
                return 'UNICODE'

            __str__ = __unicode__

            def __repr__(self):
                return 'REPR'

        x = Proxy(lambda: X())
        self.assertEqual(string(x), 'UNICODE')
        del (X.__unicode__)
        del (X.__str__)
        self.assertEqual(string(x), 'REPR')
Пример #37
0
    def test_unicode(self):
        @python_2_unicode_compatible
        class X(object):
            def __unicode__(self):
                return 'UNICODE'

            __str__ = __unicode__

            def __repr__(self):
                return 'REPR'

        x = Proxy(lambda: X())
        assert string(x) == 'UNICODE'
        del (X.__unicode__)
        del (X.__str__)
        assert string(x) == 'REPR'
Пример #38
0
    def test_context(self):
        class X(object):
            entered = exited = False

            def __enter__(self):
                self.entered = True
                return 1234

            def __exit__(self, *exc_info):
                self.exited = True

        v = X()
        x = Proxy(lambda: v)
        with x as val:
            assert val == 1234
        assert x.entered
        assert x.exited
Пример #39
0
 def test_listproxy(self):
     v = []
     x = Proxy(lambda: v)
     x.append(1)
     x.extend([2, 3, 4])
     self.assertEqual(x[0], 1)
     self.assertEqual(x[:-1], [1, 2, 3])
     del(x[-1])
     self.assertEqual(x[:-1], [1, 2])
     x[0] = 10
     self.assertEqual(x[0], 10)
     self.assertIn(10, x)
     self.assertEqual(len(x), 3)
     self.assertTrue(iter(x))
     x[0:2] = [1, 2]
     del(x[0:2])
     self.assertTrue(str(x))
     if sys.version_info[0] < 3:
         self.assertEqual(x.__cmp__(object()), -1)
Пример #40
0
 def test_listproxy(self):
     v = []
     x = Proxy(lambda: v)
     x.append(1)
     x.extend([2, 3, 4])
     assert x[0] == 1
     assert x[:-1] == [1, 2, 3]
     del(x[-1])
     assert x[:-1] == [1, 2]
     x[0] = 10
     assert x[0] == 10
     assert 10 in x
     assert len(x) == 3
     assert iter(x)
     x[0:2] = [1, 2]
     del(x[0:2])
     assert str(x)
     if sys.version_info[0] < 3:
         assert x.__cmp__(object()) == -1
Пример #41
0
 def test_get_current_local(self):
     x = Proxy(lambda: 10)
     object.__setattr__(x, '_Proxy_local', Mock())
     self.assertTrue(x._get_current_object())
Пример #42
0
    def test_int(self):
        self.assertEqual(Proxy(lambda: 10) + 1, Proxy(lambda: 11))
        self.assertEqual(Proxy(lambda: 10) - 1, Proxy(lambda: 9))
        self.assertEqual(Proxy(lambda: 10) * 2, Proxy(lambda: 20))
        self.assertEqual(Proxy(lambda: 10) ** 2, Proxy(lambda: 100))
        self.assertEqual(Proxy(lambda: 20) / 2, Proxy(lambda: 10))
        self.assertEqual(Proxy(lambda: 20) // 2, Proxy(lambda: 10))
        self.assertEqual(Proxy(lambda: 11) % 2, Proxy(lambda: 1))
        self.assertEqual(Proxy(lambda: 10) << 2, Proxy(lambda: 40))
        self.assertEqual(Proxy(lambda: 10) >> 2, Proxy(lambda: 2))
        self.assertEqual(Proxy(lambda: 10) ^ 7, Proxy(lambda: 13))
        self.assertEqual(Proxy(lambda: 10) | 40, Proxy(lambda: 42))
        self.assertEqual(~Proxy(lambda: 10), Proxy(lambda: -11))
        self.assertEqual(-Proxy(lambda: 10), Proxy(lambda: -10))
        self.assertEqual(+Proxy(lambda: -10), Proxy(lambda: -10))
        self.assertTrue(Proxy(lambda: 10) < Proxy(lambda: 20))
        self.assertTrue(Proxy(lambda: 20) > Proxy(lambda: 10))
        self.assertTrue(Proxy(lambda: 10) >= Proxy(lambda: 10))
        self.assertTrue(Proxy(lambda: 10) <= Proxy(lambda: 10))
        self.assertTrue(Proxy(lambda: 10) == Proxy(lambda: 10))
        self.assertTrue(Proxy(lambda: 20) != Proxy(lambda: 10))

        x = Proxy(lambda: 10)
        x -= 1
        self.assertEqual(x, 9)
        x = Proxy(lambda: 9)
        x += 1
        self.assertEqual(x, 10)
        x = Proxy(lambda: 10)
        x *= 2
        self.assertEqual(x, 20)
        x = Proxy(lambda: 20)
        x /= 2
        self.assertEqual(x, 10)
        x = Proxy(lambda: 10)
        x %= 2
        self.assertEqual(x, 0)
        x = Proxy(lambda: 10)
        x <<= 3
        self.assertEqual(x, 80)
        x = Proxy(lambda: 80)
        x >>= 4
        self.assertEqual(x, 5)
        x = Proxy(lambda: 5)
        x ^= 1
        self.assertEqual(x, 4)
        x = Proxy(lambda: 4)
        x **= 4
        self.assertEqual(x, 256)
        x = Proxy(lambda: 256)
        x //= 2
        self.assertEqual(x, 128)
        x = Proxy(lambda: 128)
        x |= 2
        self.assertEqual(x, 130)
        x = Proxy(lambda: 130)
        x &= 10
        self.assertEqual(x, 2)

        x = Proxy(lambda: 10)
        self.assertEqual(type(x.__float__()), float)
        self.assertEqual(type(x.__int__()), int)
        self.assertEqual(type(x.__long__()), long)
        self.assertTrue(hex(x))
        self.assertTrue(oct(x))
Пример #43
0
    def test_int(self):
        assert Proxy(lambda: 10) + 1 == Proxy(lambda: 11)
        assert Proxy(lambda: 10) - 1 == Proxy(lambda: 9)
        assert Proxy(lambda: 10) * 2 == Proxy(lambda: 20)
        assert Proxy(lambda: 10) ** 2 == Proxy(lambda: 100)
        assert Proxy(lambda: 20) / 2 == Proxy(lambda: 10)
        assert Proxy(lambda: 20) // 2 == Proxy(lambda: 10)
        assert Proxy(lambda: 11) % 2 == Proxy(lambda: 1)
        assert Proxy(lambda: 10) << 2 == Proxy(lambda: 40)
        assert Proxy(lambda: 10) >> 2 == Proxy(lambda: 2)
        assert Proxy(lambda: 10) ^ 7 == Proxy(lambda: 13)
        assert Proxy(lambda: 10) | 40 == Proxy(lambda: 42)
        assert Proxy(lambda: 10) != Proxy(lambda: -11)
        assert Proxy(lambda: 10) != Proxy(lambda: -10)
        assert Proxy(lambda: -10) == Proxy(lambda: -10)

        assert Proxy(lambda: 10) < Proxy(lambda: 20)
        assert Proxy(lambda: 20) > Proxy(lambda: 10)
        assert Proxy(lambda: 10) >= Proxy(lambda: 10)
        assert Proxy(lambda: 10) <= Proxy(lambda: 10)
        assert Proxy(lambda: 10) == Proxy(lambda: 10)
        assert Proxy(lambda: 20) != Proxy(lambda: 10)
        assert Proxy(lambda: 100).__divmod__(30)
        assert Proxy(lambda: 100).__truediv__(30)
        assert abs(Proxy(lambda: -100))

        x = Proxy(lambda: 10)
        x -= 1
        assert x == 9
        x = Proxy(lambda: 9)
        x += 1
        assert x == 10
        x = Proxy(lambda: 10)
        x *= 2
        assert x == 20
        x = Proxy(lambda: 20)
        x /= 2
        assert x == 10
        x = Proxy(lambda: 10)
        x %= 2
        assert x == 0
        x = Proxy(lambda: 10)
        x <<= 3
        assert x == 80
        x = Proxy(lambda: 80)
        x >>= 4
        assert x == 5
        x = Proxy(lambda: 5)
        x ^= 1
        assert x == 4
        x = Proxy(lambda: 4)
        x **= 4
        assert x == 256
        x = Proxy(lambda: 256)
        x //= 2
        assert x == 128
        x = Proxy(lambda: 128)
        x |= 2
        assert x == 130
        x = Proxy(lambda: 130)
        x &= 10
        assert x == 2

        x = Proxy(lambda: 10)
        assert type(x.__float__()) == float
        assert type(x.__int__()) == int
        if not PY3:
            assert type(x.__long__()) == long_t
        assert hex(x)
        assert oct(x)