本文整理汇总了Python中sqlalchemy.ext.serializer.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python serializer.dumps方法的具体用法?Python serializer.dumps怎么用?Python serializer.dumps使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.serializer
的用法示例。
在下文中一共展示了serializer.dumps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_query_three
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_query_three(self):
ua = aliased(User)
q = (
Session.query(ua)
.join(ua.addresses)
.filter(Address.email.like("%fred%"))
)
for prot in pickle_protocols():
q2 = serializer.loads(
serializer.dumps(q, prot), users.metadata, Session
)
eq_(q2.all(), [User(name="fred")])
# try to pull out the aliased entity here...
ua_2 = q2._compile_state()._entities[0].entity_zero.entity
eq_(list(q2.with_entities(ua_2.id, ua_2.name)), [(9, "fred")])
示例2: test_aliases
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_aliases(self):
u7, u8, u9, u10 = Session.query(User).order_by(User.id).all()
ualias = aliased(User)
q = (
Session.query(User, ualias)
.join(ualias, User.id < ualias.id)
.filter(User.id < 9)
.order_by(User.id, ualias.id)
)
eq_(
list(q.all()), [(u7, u8), (u7, u9), (u7, u10), (u8, u9), (u8, u10)]
)
q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session)
eq_(
list(q2.all()),
[(u7, u8), (u7, u9), (u7, u10), (u8, u9), (u8, u10)],
)
示例3: test_unicode
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_unicode(self):
m = MetaData()
t = Table(
ue("\u6e2c\u8a66"), m, Column(ue("\u6e2c\u8a66_id"), Integer)
)
expr = select([t]).where(t.c[ue("\u6e2c\u8a66_id")] == 5)
expr2 = serializer.loads(serializer.dumps(expr, -1), m)
self.assert_compile(
expr2,
ue(
'SELECT "\u6e2c\u8a66"."\u6e2c\u8a66_id" FROM "\u6e2c\u8a66" '
'WHERE "\u6e2c\u8a66"."\u6e2c\u8a66_id" = :\u6e2c\u8a66_id_1'
),
dialect="default",
)
示例4: Serializer
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def Serializer(*args, **kw):
pickler = pickle.Pickler(*args, **kw)
def persistent_id(obj):
# print "serializing:", repr(obj)
if isinstance(obj, QueryableAttribute):
cls = obj.impl.class_
key = obj.impl.key
id = "attribute:" + key + ":" + b64encode(pickle.dumps(cls))
elif isinstance(obj, Mapper) and not obj.non_primary:
id = "mapper:" + b64encode(pickle.dumps(obj.class_))
elif isinstance(obj, MapperProperty) and not obj.parent.non_primary:
id = "mapperprop:" + b64encode(pickle.dumps(obj.parent.class_)) + \
":" + obj.key
elif isinstance(obj, Table):
id = "table:" + text_type(obj.key)
elif isinstance(obj, Column) and isinstance(obj.table, Table):
id = "column:" + \
text_type(obj.table.key) + ":" + text_type(obj.key)
elif isinstance(obj, Session):
id = "session:"
elif isinstance(obj, Engine):
id = "engine:"
else:
return None
return id
pickler.persistent_id = persistent_id
return pickler
示例5: dumps
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def dumps(obj, protocol=0):
buf = byte_buffer()
pickler = Serializer(buf, protocol)
pickler.dump(obj)
return buf.getvalue()
示例6: dumps
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def dumps(obj, protocol=pickle.HIGHEST_PROTOCOL):
buf = byte_buffer()
pickler = Serializer(buf, protocol)
pickler.dump(obj)
return buf.getvalue()
示例7: cache
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def cache(key_pattern, expire=None):
def deco(f):
arg_names, varargs, varkw, defaults = inspect.getargspec(f)
if varargs or varkw:
raise Exception("do not support varargs")
gen_key = gen_key_factory(key_pattern, arg_names, defaults)
@functools.wraps(f)
def _(*a, **kw):
if not current_app.config["USE_REDIS"]:
return f(*a, **kw)
key, args = gen_key(*a, **kw)
if not key:
return f(*a, **kw)
force = kw.pop("force", False)
r = rdb.get(key) if not force else None
if r is None:
r = f(*a, **kw)
if r is not None:
if not isinstance(r, BUILTIN_TYPES):
r = dumps(r)
rdb.set(key, r, expire)
else:
r = dumps(empty)
rdb.set(key, r, expire)
try:
r = loads(r)
except (TypeError, UnpicklingError):
pass
if isinstance(r, Empty):
r = None
if isinstance(r, bytes):
r = r.decode()
return r
_.original_function = f
return _
return deco
示例8: cache_by_args
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def cache_by_args(key_pattern, expire=None):
def deco(f):
arg_names, varargs, varkw, defaults = inspect.getargspec(f)
if varargs or varkw:
raise Exception("do not support varargs")
gen_key = gen_key_factory(key_pattern, arg_names, defaults)
@functools.wraps(f)
def _(*a, **kw):
if not current_app.config["USE_REDIS"]:
return f(*a, **kw)
key, args = gen_key(*a, **kw)
if not key:
return f(*a, **kw)
key = key + ":" + request.query_string.decode()
force = kw.pop("force", False)
r = rdb.get(key) if not force else None
if r is None:
r = f(*a, **kw)
if r is not None:
if not isinstance(r, BUILTIN_TYPES):
r = dumps(r)
rdb.set(key, r, expire)
else:
r = dumps(empty)
rdb.set(key, r, expire)
try:
r = loads(r)
except (TypeError, UnpicklingError):
pass
if isinstance(r, Empty):
r = None
return r
_.original_function = f
return _
return deco
示例9: get_data
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def get_data(self):
"""Go through every mapped class and dumps the data"""
db = self.db()
data = dict()
for model in self.get_mapped_classes():
query = db.session.query(model)
data[model.__name__] = dumps(query.all())
return data
示例10: Serializer
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def Serializer(*args, **kw):
pickler = pickle.Pickler(*args, **kw)
def persistent_id(obj):
# print "serializing:", repr(obj)
if isinstance(obj, Mapper) and not obj.non_primary:
id_ = "mapper:" + b64encode(pickle.dumps(obj.class_))
elif isinstance(obj, MapperProperty) and not obj.parent.non_primary:
id_ = (
"mapperprop:"
+ b64encode(pickle.dumps(obj.parent.class_))
+ ":"
+ obj.key
)
elif isinstance(obj, Table):
if "parententity" in obj._annotations:
id_ = "mapper_selectable:" + b64encode(
pickle.dumps(obj._annotations["parententity"].class_)
)
else:
id_ = "table:" + text_type(obj.key)
elif isinstance(obj, Column) and isinstance(obj.table, Table):
id_ = (
"column:" + text_type(obj.table.key) + ":" + text_type(obj.key)
)
elif isinstance(obj, Session):
id_ = "session:"
elif isinstance(obj, Engine):
id_ = "engine:"
else:
return None
return id_
pickler.persistent_id = persistent_id
return pickler
示例11: test_tables
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_tables(self):
assert (
serializer.loads(
serializer.dumps(users, -1), users.metadata, Session
)
is users
)
示例12: test_columns
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_columns(self):
assert (
serializer.loads(
serializer.dumps(users.c.name, -1), users.metadata, Session
)
is users.c.name
)
示例13: test_mapper
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_mapper(self):
user_mapper = class_mapper(User)
assert (
serializer.loads(serializer.dumps(user_mapper, -1), None, None)
is user_mapper
)
示例14: test_expression
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_expression(self):
expr = select([users]).select_from(users.join(addresses)).limit(5)
re_expr = serializer.loads(
serializer.dumps(expr, -1), users.metadata, None
)
eq_(str(expr), str(re_expr))
assert re_expr.bind is testing.db
eq_(
re_expr.execute().fetchall(),
[(7, "jack"), (8, "ed"), (8, "ed"), (8, "ed"), (9, "fred")],
)
示例15: test_query_two
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import dumps [as 别名]
def test_query_two(self):
q = (
Session.query(User)
.join(User.addresses)
.filter(Address.email.like("%fred%"))
)
q2 = serializer.loads(serializer.dumps(q, -1), users.metadata, Session)
eq_(q2.all(), [User(name="fred")])
eq_(list(q2.with_entities(User.id, User.name)), [(9, "fred")])