Python sqlalchemy.ext.serializer.dumps() Examples

The following are 30 code examples of sqlalchemy.ext.serializer.dumps(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy.ext.serializer , or try the search function .
Example #1
Source File: test_serializer.py    From sqlalchemy with MIT License 6 votes vote down vote up
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")]) 
Example #2
Source File: test_serializer.py    From sqlalchemy with MIT License 6 votes vote down vote up
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)],
        ) 
Example #3
Source File: test_serializer.py    From sqlalchemy with MIT License 6 votes vote down vote up
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",
        ) 
Example #4
Source File: serializer.py    From jbox with MIT License 5 votes vote down vote up
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 
Example #5
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_columns(self):
        assert (
            serializer.loads(
                serializer.dumps(users.c.name, -1), users.metadata, Session
            )
            is users.c.name
        ) 
Example #6
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_mapper(self):
        user_mapper = class_mapper(User)
        assert (
            serializer.loads(serializer.dumps(user_mapper, -1), None, None)
            is user_mapper
        ) 
Example #7
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
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")],
        ) 
Example #8
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
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")]) 
Example #9
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
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) 
Example #10
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_any(self):
        r = User.addresses.any(Address.email == "x")
        ser = serializer.dumps(r, -1)
        x = serializer.loads(ser, users.metadata)
        eq_(str(r), str(x)) 
Example #11
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_deserailize_colprop(self):
        TestTable = self.classes.TestTable

        s = scoped_session(sessionmaker())

        expr = s.query(TestTable).filter(TestTable.some_primary_id == 123456)

        expr2 = serializer.loads(serializer.dumps(expr), TestTable.metadata, s)

        # note in the original, the same bound parameter is used twice
        self.assert_compile(
            expr,
            "SELECT test.some_id AS test_some_id, "
            "CAST(left(test.some_id, :left_1) AS INTEGER) AS anon_1, "
            "test.id AS test_id FROM test WHERE "
            "CAST(left(test.some_id, :left_1) AS INTEGER) = :param_1",
            checkparams={"left_1": 6, "param_1": 123456},
        )

        # in the deserialized, it's two separate parameter objects which
        # need to have different anonymous names.  they still have
        # the same value however
        self.assert_compile(
            expr2,
            "SELECT test.some_id AS test_some_id, "
            "CAST(left(test.some_id, :left_1) AS INTEGER) AS anon_1, "
            "test.id AS test_id FROM test WHERE "
            "CAST(left(test.some_id, :left_2) AS INTEGER) = :param_1",
            checkparams={"left_1": 6, "left_2": 6, "param_1": 123456},
        ) 
Example #12
Source File: manage.py    From flask-project-template with MIT License 5 votes vote down vote up
def dump(destination):
    dump_models = []  # List of models you want to dump
    serialized = list()
    for model in dump_models:
        print('Dumping {}'.format(model))
        serialized.append(unicode(dumps(db.session.query(model).all()), errors='ignore'))
    with open(destination, 'w') as f:
        f.writelines(json.dumps(serialized))
    print('Done.') 
Example #13
Source File: serializer.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
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 
Example #14
Source File: serializer.py    From jarvis with GNU General Public License v2.0 5 votes vote down vote up
def dumps(obj, protocol=pickle.HIGHEST_PROTOCOL):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #15
Source File: serializer.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #16
Source File: serializer.py    From moviegrabber with GNU General Public License v3.0 5 votes vote down vote up
def dumps(obj, protocol=0):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #17
Source File: serializer.py    From android_universal with MIT License 5 votes vote down vote up
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 
Example #18
Source File: serializer.py    From android_universal with MIT License 5 votes vote down vote up
def dumps(obj, protocol=pickle.HIGHEST_PROTOCOL):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #19
Source File: serializer.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def dumps(obj, protocol=0):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #20
Source File: serializer.py    From jbox with MIT License 5 votes vote down vote up
def dumps(obj, protocol=0):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #21
Source File: serializer.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #22
Source File: serializer.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def dumps(obj, protocol=0):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #23
Source File: serializer.py    From planespotter with MIT License 5 votes vote down vote up
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 
Example #24
Source File: serializer.py    From planespotter with MIT License 5 votes vote down vote up
def dumps(obj, protocol=pickle.HIGHEST_PROTOCOL):
    buf = byte_buffer()
    pickler = Serializer(buf, protocol)
    pickler.dump(obj)
    return buf.getvalue() 
Example #25
Source File: mc.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #26
Source File: mc.py    From flask-shop with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #27
Source File: serializer.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #28
Source File: test_serializer.py    From sqlalchemy with MIT License 5 votes vote down vote up
def test_tables(self):
        assert (
            serializer.loads(
                serializer.dumps(users, -1), users.metadata, Session
            )
            is users
        ) 
Example #29
Source File: database.py    From alchemydumps with MIT License 5 votes vote down vote up
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 
Example #30
Source File: serializer.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
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