当前位置: 首页>>代码示例>>Python>>正文


Python util.identity_key方法代码示例

本文整理汇总了Python中sqlalchemy.orm.util.identity_key方法的典型用法代码示例。如果您正苦于以下问题:Python util.identity_key方法的具体用法?Python util.identity_key怎么用?Python util.identity_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sqlalchemy.orm.util的用法示例。


在下文中一共展示了util.identity_key方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def __init__(self, label=None, validators=None, query_factory=None,
                 get_pk=None, get_label=None, allow_blank=False,
                 blank_text='', **kwargs):
        super(QuerySelectField, self).__init__(label, validators, **kwargs)
        self.query_factory = query_factory

        if get_pk is None:
            if not has_identity_key:
                raise Exception('The sqlalchemy identity_key function could not be imported.')
            self.get_pk = get_pk_from_identity
        else:
            self.get_pk = get_pk

        if get_label is None:
            self.get_label = lambda x: x
        elif isinstance(get_label, string_types):
            self.get_label = operator.attrgetter(get_label)
        else:
            self.get_label = get_label

        self.allow_blank = allow_blank
        self.blank_text = blank_text
        self.query = None
        self._object_list = None 
开发者ID:jpush,项目名称:jbox,代码行数:26,代码来源:fields.py

示例2: get

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def get(cls, pk, force=False):
        if not force:
            ident_key = identity_key(cls, pk)
            if cls._db_session.identity_map and \
                    ident_key in cls._db_session.identity_map:
                return cls._db_session.identity_map[ident_key]

            try:
                cached_val = cls._cache_client.get(cls.gen_raw_key(pk))
                if cached_val:
                    cls._statsd_incr('hit')
                    return cls.from_cache(cached_val)
            except redis.ConnectionError as e:
                logger.error(e)
            except TypeError as e:
                logger.error(e)

        cls._statsd_incr('miss')

        obj = cls._db_session().query(cls).get(pk)
        if obj is not None:
            cls.set_raw(obj.__rawdata__)
        return obj 
开发者ID:MrKiven,项目名称:ECache,代码行数:25,代码来源:core.py

示例3: test_key_replaced_by_oob_insert

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_key_replaced_by_oob_insert(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)

        u1 = User(name="u1")

        s = Session()
        s.add(u1)
        s.commit()

        s.delete(u1)
        s.flush()

        s.execute(users.insert().values(name="u1"))
        u2 = s.query(User).get("u1")

        assert u1 not in s
        s.rollback()

        assert u1 in s
        assert u2 not in s

        assert s.identity_map[identity_key(User, ("u1",))] is u1 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:26,代码来源:test_transaction.py

示例4: get_pk_from_identity

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def get_pk_from_identity(obj):
    cls, key = identity_key(instance=obj)
    return ':'.join(text_type(x) for x in key) 
开发者ID:jpush,项目名称:jbox,代码行数:5,代码来源:fields.py

示例5: get

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def get(cls, _id, force=False):
        """Query object by pk.

        :func:`~get` will use db slave and store the value to cache.

        Use ``force=True`` to force load from db.

        :param _id: primary key value
        :param force: whether force to load from db
        :return: object from db or cache
        """
        if not force:
            # try load from session
            ident_key = identity_key(cls, _id)
            if cls._db_session.identity_map and \
                    ident_key in cls._db_session.identity_map:
                return cls._db_session.identity_map[ident_key]

            try:
                cached_val = cls._cache_client.get(cls.gen_raw_key(_id))
                if cached_val:
                    cls._statsd_incr("hit")

                    # load from cache
                    return cls.from_cache(cached_val)
            except redis.ConnectionError as e:
                logger.error(e)
            except TypeError as e:
                logger.error(e)

        cls._statsd_incr("miss")

        obj = cls._db_session().query(cls).get(_id)
        if obj is not None:
            cls.set_raw(obj.__rawdata__, nx=True)
        return obj 
开发者ID:huskar-org,项目名称:huskar,代码行数:38,代码来源:__init__.py

示例6: test_identity_key

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_identity_key(self):
        User = self.classes.User
        u1 = User(name="ed")
        s = Session(testing.db)
        s.add(u1)
        s.flush()
        insp = inspect(u1)
        eq_(insp.identity_key, identity_key(User, (u1.id,))) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:10,代码来源:test_inspect.py

示例7: test_identity_key_1

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_identity_key_1(self):
        User, users = self.classes.User, self.tables.users

        mapper(User, users)

        key = orm_util.identity_key(User, [1])
        eq_(key, (User, (1,), None))
        key = orm_util.identity_key(User, ident=[1])
        eq_(key, (User, (1,), None)) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:11,代码来源:test_utils.py

示例8: test_identity_key_scalar

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_identity_key_scalar(self):
        User, users = self.classes.User, self.tables.users

        mapper(User, users)

        key = orm_util.identity_key(User, 1)
        eq_(key, (User, (1,), None))
        key = orm_util.identity_key(User, ident=1)
        eq_(key, (User, (1,), None)) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:11,代码来源:test_utils.py

示例9: test_identity_key_2

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_identity_key_2(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)
        s = create_session()
        u = User(name="u1")
        s.add(u)
        s.flush()
        key = orm_util.identity_key(instance=u)
        eq_(key, (User, (u.id,), None)) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:12,代码来源:test_utils.py

示例10: test_identity_key_3

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_identity_key_3(self):
        User, users = self.classes.User, self.tables.users

        mapper(User, users)

        row = {users.c.id: 1, users.c.name: "Frank"}
        key = orm_util.identity_key(User, row=row)
        eq_(key, (User, (1,), None)) 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:10,代码来源:test_utils.py

示例11: test_key_replaced_by_update

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_key_replaced_by_update(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)

        u1 = User(name="u1")
        u2 = User(name="u2")

        s = Session()
        s.add_all([u1, u2])
        s.commit()

        s.delete(u1)
        s.flush()

        u2.name = "u1"
        s.flush()

        assert u1 not in s
        s.rollback()

        assert u1 in s
        assert u2 in s

        assert s.identity_map[identity_key(User, ("u1",))] is u1
        assert s.identity_map[identity_key(User, ("u2",))] is u2 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:28,代码来源:test_transaction.py

示例12: test_multiple_key_replaced_by_update

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def test_multiple_key_replaced_by_update(self):
        users, User = self.tables.users, self.classes.User

        mapper(User, users)

        u1 = User(name="u1")
        u2 = User(name="u2")
        u3 = User(name="u3")

        s = Session()
        s.add_all([u1, u2, u3])
        s.commit()

        s.delete(u1)
        s.delete(u2)
        s.flush()

        u3.name = "u1"
        s.flush()

        u3.name = "u2"
        s.flush()

        s.rollback()

        assert u1 in s
        assert u2 in s
        assert u3 in s

        assert s.identity_map[identity_key(User, ("u1",))] is u1
        assert s.identity_map[identity_key(User, ("u2",))] is u2
        assert s.identity_map[identity_key(User, ("u3",))] is u3 
开发者ID:sqlalchemy,项目名称:sqlalchemy,代码行数:34,代码来源:test_transaction.py

示例13: mget

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def mget(cls, pks, force=False, as_dict=False):
        if not pks:
            return {} if as_dict else []

        objs = {}
        if not force:
            if cls._db_session.identity_map:
                for pk in pks:
                    ident_key = identity_key(cls, pk)
                    if ident_key in cls._db_session.identity_map:
                        objs[pk] = cls._db_session.identity_map[identity_key]

            if len(pks) > len(objs):
                missed_pks = list(set(pks) - set(objs))
                vals = cls._cache_client.mget(cls.gen_raw_key(pk)
                                              for pk in missed_pks)
                if vals:
                    cached = {
                        k: cls.from_cache(v)
                        for k, v in zip(missed_pks, vals)
                        if v is not None
                    }
                    _hit_counts = len(cached)
                    cls._statsd_incr('hit', _hit_counts)
                    objs.update(cached)

        lack_pks = set(pks) - set(objs)
        if lack_pks:
            pk = cls.pk_attribute()
            if pk:
                lack_objs = cls._db_session().query(cls).\
                    filter(pk.in_(lack_pks)).all()
                if lack_objs:
                    cls.mset(lack_objs)

                cls._statsd_incr('miss', len(lack_objs))

                objs.update({obj.pk: obj} for obj in lack_objs)
            else:
                logger.warn("No pk found for %s, skip %s" %
                            cls.__tablename__, lack_pks)
        return objs if as_dict else _dict2list(pks, objs) 
开发者ID:MrKiven,项目名称:ECache,代码行数:44,代码来源:core.py

示例14: mget

# 需要导入模块: from sqlalchemy.orm import util [as 别名]
# 或者: from sqlalchemy.orm.util import identity_key [as 别名]
def mget(cls, _ids, force=False, as_dict=False):
        """Query a list of objects by pks.

        :func:`~mget` will always use db slave, values will be stored
        to cache if cache misses.

        Use ``force=True`` to force load from db.

        :param _ids: a list of pks
        :param force: whether force to load from db
        :param as_dict: return dict or list
        :return: dict or list of objects
        """
        if not _ids:
            return {} if as_dict else []

        objs = {}
        if not force:
            # load from session
            if cls._db_session.identity_map:
                for i in _ids:
                    ident_key = identity_key(cls, i)
                    if ident_key in cls._db_session.identity_map:
                        objs[i] = cls._db_session.identity_map[ident_key]

            # load from cache
            if len(_ids) > len(objs):
                missed_ids = list(set(_ids) - set(objs))
                _objs = cls._from_cache(missed_ids, from_raw=True)
                objs.update(_objs)

        lack_ids = set(_ids) - set(objs)
        if lack_ids:
            pk = cls.pk_attribute()
            # we assume CacheMixin have pk, if not, bypass it.
            if pk:
                lack_objs = cls._db_session().using_bind('master').\
                    query(cls).filter(pk.in_(lack_ids)).all()
                if lack_objs:
                    cls.mset(lack_objs, nx=True)

                cls._statsd_incr("miss", len(lack_ids))

                objs.update({obj.pk: obj for obj in lack_objs})
            else:  # pragma: no cover
                logger.warn("No pk found for %s, skip %s" %
                            cls.__tablename__, lack_ids)

        # TODO hack to make mget return list
        return objs if as_dict else _dict2list(_ids, objs) 
开发者ID:huskar-org,项目名称:huskar,代码行数:52,代码来源:__init__.py


注:本文中的sqlalchemy.orm.util.identity_key方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。