Python sqlalchemy.orm 模块,object_mapper() 实例源码

我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用sqlalchemy.orm.object_mapper()

项目:Trusted-Platform-Module-nova    作者:BU-NU-CLOUD-SP16    | 项目源码 | 文件源码
def name(self):
        try:
            base_name = CONF.instance_name_template % self.id
        except TypeError:
            # Support templates like "uuid-%(uuid)s", etc.
            info = {}
            # NOTE(russellb): Don't use self.iteritems() here, as it will
            # result in infinite recursion on the name property.
            for column in iter(orm.object_mapper(self).columns):
                key = column.name
                # prevent recursion if someone specifies %(name)s
                # %(name)s will not be valid.
                if key == 'name':
                    continue
                info[key] = self[key]
            try:
                base_name = CONF.instance_name_template % info
            except KeyError:
                base_name = self.uuid
        return base_name
项目:craton    作者:openstack    | 项目源码 | 文件源码
def __repr__(self):
        mapper = object_mapper(self)
        cols = getattr(self, '_repr_columns', mapper.primary_key)
        items = [(p.key, getattr(self, p.key))
                 for p in [
                     mapper.get_property_by_column(c) for c in cols]]
        return "{0}({1})".format(
            self.__class__.__name__,
            ', '.join(['{0}={1!r}'.format(*item) for item in items]))
项目:weibo    作者:windskyer    | 项目源码 | 文件源码
def __iter__(self):
        columns = dict(object_mapper(self).columns).keys()
        # NOTE(russellb): Allow models to specify other keys that can be looked
        # up, beyond the actual db columns.  An example would be the 'name'
        # property for an Instance.
        if hasattr(self, '_extra_keys'):
            columns.extend(self._extra_keys())
            self._i = iter(columns)
            return self
项目:blacklib    作者:halfss    | 项目源码 | 文件源码
def __iter__(self):
        self._i = iter(object_mapper(self).columns)
        return self
项目:rucio    作者:rucio01    | 项目源码 | 文件源码
def __iter__(self):
        self._i = iter(object_mapper(self).columns)
        return self
项目:ranger-agent    作者:openstack    | 项目源码 | 文件源码
def __iter__(self):
        self._i = iter(orm.object_mapper(self).columns)
        return self
项目:weasyl    作者:Weasyl    | 项目源码 | 文件源码
def to_dict(self):
        return {col.name: getattr(self, col.name)
                for col in object_mapper(self).mapped_table.c}
项目:chrononaut    作者:onecodex    | 项目源码 | 文件源码
def diff(self, from_model, to=None, include_hidden=False):
        """Enumerate the changes from a prior history model to a later history model or the current model's
        state (if ``to`` is ``None``).

        :param from_model: A history model to diff from.
        :param to: A history model or ``None``.
        :return: A dict of column names and ``(from, to)`` value tuples
        """
        to_model = to or self
        untracked_cols = set(getattr(self, '__chrononaut_untracked__', []))

        for k in self.__history_mapper__.primary_key:
            if k.key == 'version':
                continue
            if getattr(from_model, k.key) != getattr(to_model, k.key):
                raise ChrononautException('You can only diff models with the same primary keys.')

        if not isinstance(from_model, self.__history_mapper__.class_):
            raise ChrononautException('Cannot diff from a non-history model.')

        if to_model is not self and from_model.changed > to_model.changed:
            raise ChrononautException('Diffs must be chronological. Your from_model '
                                      'post-dates your to.')

        # TODO: Refactor this and `create_version` so some of the object mapper
        #       iteration is not duplicated twice
        diff = {}
        obj_mapper = object_mapper(from_model)
        for om in obj_mapper.iterate_to_root():
            for obj_col in om.local_table.c:
                if 'version_meta' in obj_col.info or obj_col.key in untracked_cols:
                    continue
                try:
                    prop = obj_mapper.get_property_by_column(obj_col)
                except UnmappedColumnError:
                    continue

                # First check the history model's columns
                from_val = getattr(from_model, prop.key)
                to_val = getattr(to_model, prop.key)
                if from_val != to_val:
                    diff[prop.key] = (from_val, to_val)

        # If `include_hidden` we need to enumerate through every
        # model *since* the from_model and see if `change_info` includes
        # hidden columns. We only need to do this for non-history instances.
        if include_hidden and isinstance(to_model, self.__class__):
            from_versions = self.versions(after=from_model.changed)
            for from_version in from_versions:
                if 'hidden_cols_changed' in from_version.change_info:
                    for hidden_col in from_version.change_info['hidden_cols_changed']:
                        diff[hidden_col] = (None, getattr(to_model, hidden_col))
                    break

        return diff