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


Python Dataset.find方法代码示例

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


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

示例1: __init__

# 需要导入模块: from openspending.model import Dataset [as 别名]
# 或者: from openspending.model.Dataset import find [as 别名]
    def __init__(self, dataset_name, unique_keys, label, description=u'',
                 metadata=None, currency=u'gbp', time_axis='time.from.year',
                 changeset=None):
        '''\
        Constructs a Loader for the :class:`openspending.model.Dataset`
        `dataset_name`. Calling the constructor creates or updates the
        `Dataset` object with `dataset_name`, `label`, `description`,
        `metadata` and `currency`. The Loader instance can only be used
        to create :class:`openspending.model.Entry` objects with the same set
        of `unique_keys`. If you need to create another type of
        ``Entry`` objects instantiate another ``Loader``.

        ``dataset_name``
            The unique name for the dataset.
        ``unique_keys``
            The keys for which all entries in the dataset are unique.
            For example if you have a entries with payments that have
            are identifiable by a *department* and a *consecutive number*
            that is unique within the *department*, you would pass in
            a list with the keys ``['department', 'consecutive_number']``.
        ``label``
            A label for the dataset that can be presented to the user
        ``description``
            A description for the dataset taht can be presented
            to the user.
        ``metadata``
            A ``dict`` with metadata that will be saved on the dataset.
        ``currency``
            The default currency for the entries in the dataset. An
            individual currency can be set in :meth:`create_entry`.
            The currenty is stored in upper case.
        ``time_axis``
            The time axis of the dataset. This is the time range for which
            all entries in the dataset can be analized. The default is
            'time.from.year' and should not be changed.
            fixme: add details and move possible values into constants in
            model.dataset.
         ``changeset``
            A :class:`openspending.model.Changeset` object. This is only required
            if you use load a dataset with more than one loader. If you
            want to add manual changes to the changeset of your loader
            you can retrive the changeset with *.changeset*.

        Raises:
            ``AssertionError`` if more than one dataset with the name
                ``dataset_name`` exists already.
            ``ValueError``
                If and duplicated :class:`openspending.model.Entry` object
                is found (The entry has the same values for the
                ``unique_keys``) or two :class:`model.class.Entity`
                objects are found with the same name.
        '''
        assert isinstance(dataset_name, unicode)
        assert isinstance(unique_keys, list)
        check_rest_suffix(dataset_name)

        # create a changeset:
        if changeset is None:
            name = dataset_name
            if label:
                name = "%s (%s)" % (name, label)
            message = ('Load dataset %s. currency: %s, time axis: %s' %
                       (name, currency, time_axis))
            changeset = Changeset()
            changeset.author = 'system'
            changeset.message = message
            changeset.save()
        self.changeset = changeset

        # get the dataset
        q = {'name': dataset_name}
        dataset_count = Dataset.find(q).count()
        if dataset_count == 0:
            operation = CREATE
        elif dataset_count == 1:
            operation = UPDATE
        else:
            raise AssertionError("Ambiguous dataset name: %s" % dataset_name)
        data = {"label": label,
                "currency": currency.upper(),
                "description": description,
                "time_axis": time_axis}
        if metadata is not None:
            data.update(metadata)
        Dataset.c.update(q, {"$set": data}, upsert=True)
        self.dataset = Dataset.find_one(q)
        self._add_changeobj(Dataset.c.name, self.dataset.id, self.dataset,
                            operation)
        self.base_query = {"dataset._id": self.dataset.id}

        # caches
        self.entity_cache = {}
        self.classifier_cache = {}
        self.unique_keys = unique_keys

        # We need indexes to speed up lookups and updates
        self.ensure_index(Entry, ['dataset._id'])
        self.ensure_index(Entry, ['dataset.name'])
        self.ensure_index(Entry, ['classifiers'])
        self.ensure_index(Entry, ['entities'])
#.........这里部分代码省略.........
开发者ID:pombredanne,项目名称:openspending.etl,代码行数:103,代码来源:loader.py


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