本文整理汇总了Python中sqlalchemy.ext.serializer.loads方法的典型用法代码示例。如果您正苦于以下问题:Python serializer.loads方法的具体用法?Python serializer.loads怎么用?Python serializer.loads使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sqlalchemy.ext.serializer
的用法示例。
在下文中一共展示了serializer.loads方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_query_three
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [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 loads [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 loads [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: restore
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def restore(source='data/serialized_dump.txt'):
print("Start importing data")
with open(source, 'rb') as f:
data = json.loads(f.readline())
for model_data in data:
try:
restored = loads(model_data, db.metadata, db.session)
except AttributeError as e:
print('Table does not exist: {}'.format(e))
continue
if restored:
print('Importing {} table...'.format(restored[0].__table__.name))
for item in restored:
db.session.merge(item)
db.session.commit()
print('Done')
示例5: loads
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def loads(data, metadata=None, scoped_session=None, engine=None):
buf = byte_buffer(data)
unpickler = Deserializer(buf, metadata, scoped_session, engine)
return unpickler.load()
示例6: cache
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [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
示例7: cache_by_args
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [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
示例8: restore
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def restore(location):
import zipfile
zip_ref = zipfile.ZipFile(location.get_filename(), 'r')
zip_ref.extractall(location.get_filename().split(".zip")[0])
zip_ref.close()
backupfolder = location.get_filename().split(".zip")[0]
dbname = os.path.split(backupfolder)
dbname = dbname[len(dbname)-1]
metadata = MetaData(share.config.db.engine)
folder = os.path.join(backupfolder, dbname)
a = (Bill, Config, Notebook, BankNames, BankAccounts, ProductGroups, Products, Users, Permissions, Subject, Factors,
CustGroups, Customers, Cheque, ChequeHistory)
for table in a:
file = open(os.path.join(folder, str(table.__name__)), "r")
serialized_data = file.read()
restore_q = loads(serialized_data, metadata, share.config.db.session)
share.config.db.session.query(table).delete()
for row in restore_q:
share.config.db.session.merge(row)
share.config.db.session.commit()
file.close()
rmtree(backupfolder, ignore_errors=True)
share.mainwin.silent_daialog(_("Backup restored successfully"))
示例9: parse_data
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def parse_data(self, contents):
"""Loads a dump and convert it into rows """
db = self.db()
return loads(contents, db.metadata, db.session)
示例10: test_tables
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def test_tables(self):
assert (
serializer.loads(
serializer.dumps(users, -1), users.metadata, Session
)
is users
)
示例11: test_columns
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def test_columns(self):
assert (
serializer.loads(
serializer.dumps(users.c.name, -1), users.metadata, Session
)
is users.c.name
)
示例12: test_mapper
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def test_mapper(self):
user_mapper = class_mapper(User)
assert (
serializer.loads(serializer.dumps(user_mapper, -1), None, None)
is user_mapper
)
示例13: test_expression
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [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")],
)
示例14: test_query_two
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [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")])
示例15: test_annotated_one
# 需要导入模块: from sqlalchemy.ext import serializer [as 别名]
# 或者: from sqlalchemy.ext.serializer import loads [as 别名]
def test_annotated_one(self):
j = join(users, addresses)._annotate({"foo": "bar"})
query = select([addresses]).select_from(j)
str(query)
for prot in pickle_protocols():
pickled_failing = serializer.dumps(j, prot)
serializer.loads(pickled_failing, users.metadata, None)