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


Python SON.get方法代码示例

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


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

示例1: MRsimple

# 需要导入模块: from bson import SON [as 别名]
# 或者: from bson.SON import get [as 别名]
def MRsimple(collection, FunMap, FunReduce=None, query={}, out={"replace": 'mr_tmp'}, finalize=None,
             scope={}, sort=None, jsMode=False, verbose=1):
    """ simplified generic Map Reduce
        see: http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce/
        returns (MR response object, results collection or results list if out={"inline":1})
        Reduce function defaults to one that increments value count
        optimize by sorting on emit fields
        see: http://edgystuff.tumblr.com/post/7624019777/optimizing-map-reduce-with-mongodb
        docs.mongodb.org/manual/reference/method/db.collection.mapReduce/#db.collection.mapReduce
        sort      i.e: sort= { "_id":1 }
        jsMode    should be False if we expect more than 500K dictinct ids
    """
    if len(out.viewkeys()) > 1:
        command = MRCommand_(out)
        out = SON([(command, out[command]), ('db', out.get('db')),
                   ('nonAtomic', out.get('nonAtomic', False))])
        #nonAtomic not allowed on replace
    FunMap = Code(FunMap, {})
    if FunReduce is None:
        FunReduce = u"""function (key, values) {var total = 0; for (var i = 0;
                        i < values.length; i++) { total += values[i]; } return total;}
                     """
    FunReduce = Code(FunReduce, {})
    if verbose > 2:
        print "Start MRsimple collection = %s"\
              "query = %s\nMap=\n%s\nReduce=\n%s\nFinalize=%s\nscope=%s sort=%s" \
              % tuple(map(str, (out, query, FunMap, FunReduce, finalize, scope, sort)))
    if sort:
        sort = SON(sort)
    r = collection.map_reduce(FunMap, FunReduce, out=out, query=query,
                              finalize=finalize, scope=scope, sort=sort, full_response=True)
    if verbose > 1:
        print  "End MRsimple collection=%s, query=%s\nresulsts=\n %s"\
                % (collection.name, str(query), str(r))
    if 'db' in out.viewkeys():
        #@note:  can be dict or SON, either way it has property viewkeys
        results = collection.database.connection[r['result']['db']][r['result']['collection']]
    else:
        results = r['results'] if  out.keys()[0] == 'inline' else collection.database[r['result']]
        #@note:  results is a list if inline else a collection
    return r, results
开发者ID:nickmilon,项目名称:pymongo_ext,代码行数:43,代码来源:pymongo_ext.py

示例2: BaseDocument

# 需要导入模块: from bson import SON [as 别名]
# 或者: from bson.SON import get [as 别名]
class BaseDocument(object):
    __slots__ = ('_changed_fields', '_initialised', '_created', '_data',
                 '_dynamic_fields', '_auto_id_field', '_db_field_map',
                 '__weakref__')

    _dynamic = False
    _dynamic_lock = True
    STRICT = False

    def __init__(self, *args, **values):
        """
        Initialise a document or embedded document

        :param __auto_convert: Try and will cast python objects to Object types
        :param values: A dictionary of values for the document
        """
        self._initialised = False
        self._created = True
        if args:
            # Combine positional arguments with named arguments.
            # We only want named arguments.
            field = iter(self._fields_ordered)
            # If its an automatic id field then skip to the first defined field
            if getattr(self, '_auto_id_field', False):
                next(field)
            for value in args:
                name = next(field)
                if name in values:
                    raise TypeError(
                        'Multiple values for keyword argument "%s"' % name)
                values[name] = value

        __auto_convert = values.pop('__auto_convert', True)

        # 399: set default values only to fields loaded from DB
        __only_fields = set(values.pop('__only_fields', values))

        _created = values.pop('_created', True)

        signals.pre_init.send(self.__class__, document=self, values=values)

        # Check if there are undefined fields supplied to the constructor,
        # if so raise an Exception.
        if not self._dynamic and (self._meta.get('strict', True) or _created):
            _undefined_fields = set(values.keys()) - set(
                self._fields.keys() + ['id', 'pk', '_cls', '_text_score'])
            if _undefined_fields:
                msg = (
                    'The fields "{0}" do not exist on the document "{1}"'
                ).format(_undefined_fields, self._class_name)
                raise FieldDoesNotExist(msg)

        if self.STRICT and not self._dynamic:
            self._data = StrictDict.create(allowed_keys=self._fields_ordered)()
        else:
            self._data = {}

        self._dynamic_fields = SON()

        # Assign default values to instance
        for key, field in iteritems(self._fields):
            if self._db_field_map.get(key, key) in __only_fields:
                continue
            value = getattr(self, key, None)
            setattr(self, key, value)

        if '_cls' not in values:
            self._cls = self._class_name

        # Set passed values after initialisation
        if self._dynamic:
            dynamic_data = {}
            for key, value in iteritems(values):
                if key in self._fields or key == '_id':
                    setattr(self, key, value)
                else:
                    dynamic_data[key] = value
        else:
            FileField = _import_class('FileField')
            for key, value in iteritems(values):
                key = self._reverse_db_field_map.get(key, key)
                if key in self._fields or key in ('id', 'pk', '_cls'):
                    if __auto_convert and value is not None:
                        field = self._fields.get(key)
                        if field and not isinstance(field, FileField):
                            value = field.to_python(value)
                    setattr(self, key, value)
                else:
                    self._data[key] = value

        # Set any get_<field>_display methods
        self.__set_field_display()

        if self._dynamic:
            self._dynamic_lock = False
            for key, value in iteritems(dynamic_data):
                setattr(self, key, value)

        # Flag initialised
        self._initialised = True
#.........这里部分代码省略.........
开发者ID:MongoEngine,项目名称:mongoengine,代码行数:103,代码来源:document.py

示例3: mr

# 需要导入模块: from bson import SON [as 别名]
# 或者: from bson.SON import get [as 别名]
def mr(
    coll,                       # a pymongo collection instance
    fun_map,                    # js function used for map
    fun_reduce=None,            # js function used for reduce defaults to one counting values
    query={},                   # a pymongo query dictionary to query coll defaults to {}
    out={"replace": 'mr_tmp'},  # output dict {'replace'|'merge'|'reduce'|'inline':collection_name|'database':db_name}
    fun_finalize=None,          # js function to run on finalize
    scope={},                   # vars available during map-reduce
    sort=None,                  # i.e: sort= { "_id":1 } short dict to sort before map
    jsMode=False,               # True|False (don't convert to Bson between map & reduce if True)
    verbose=1                   # if 1 includes timing info on output if 2,3  more details
        ):
    """simplified generic Map Reduce
    `see MongoDB Map Reduce <http://docs.mongodb.org/manual/reference/command/mapReduce/>`_

    :Parameters:
      - coll (object) a pymongo collection instance
      - fun_map js function used for map
      - fun_reduce js function used for reduce defaults to a function that increments value count
      - query a pymongo query dictionary to query collection, defaults to {}
      - out  a dictionary for output specification  {replace|merge|reduce|:collection_name|db:db_name}
        also can specify {'inline':1} for in memory operation (with some limitations)
        defaults to {"replace": 'mr_tmp'}
      - scope vars available during map-reduce-finalize
      - sort dictionary to sort before map  i.e: sort= { "_id":1 }
      - jsMode True|False (don't convert to Bson between map & reduce if True)
        should be False if we expect more than 500K distinct results
      - db' (optional): database_name
        if no db is specified output collection will be in same db as input coll

    :Returns: tuple (results collection or results list if out={"inline" :1}, MR response statistics)
    :Example: see :func:`group_counts` function
    """
    def mr_cmd():
        """returns the actual command from output parameter"""
        return [i for i in ['replace', 'merge', 'reduce', 'inline']
                if i in list(out.keys())][0]
    command = mr_cmd()
    out_db = out.get('db', None)
    out = SON([(command, out[command]), ('nonAtomic', out.get('nonAtomic', False))])
    if out_db:
        out.update(SON([('db', out_db)]))
#     out = SON([(command, out[command]), ('db', out.get('db')),
#                    ('nonAtomic', out.get('nonAtomic', False))])
        # nonAtomic not allowed on replace
    fun_map = Code(fun_map, {})
    if fun_reduce is None:
        fun_reduce = parse_js_default('MapReduce.js', 'GroupCountsReduce')
    fun_reduce = Code(fun_reduce, {})

    if sort:
        sort = SON(sort)
    if verbose > 1:
        frmt = "Map Reduce {}\n\
                collection={coll!s:}\n\
                query=     {query!s:}\n\
                sort=      {sort!s:}\n\
                scope=     {scope!s}\n\
                out=       {out!s:}\n\
                jsMode=    {jsMode!s:}\n\
                map=       {fun_map!s:}\n\
                reduce=    {fun_reduce!s:}\n\
                finalize=  {fun_finalize!s:}\n"
        print(frmt.format('Starting...', **locals()))
    r = coll.map_reduce(fun_map, fun_reduce, out=out, query=query,
                        finalize=fun_finalize, scope=scope, sort=sort,
                        full_response=True, jsMode=jsMode)
    if verbose > 0:
        frmt = "Map Reduce {}\n\
                ok=        {ok:}\n\
                millisecs = {timeMillis:,d}\n\
                counts=    {counts!s:}\n\
                out=       {!s:}\n"
        print(frmt.format('End', out, **r))

    if command == 'inline':
        # if 'results' in list(r.keys()):              # @note:  results is a list if inline else a coll
        results = r['results']
        del r['results']
    else:
        db_out = out.get('db', coll.database.name)     # if db not specified it is the collection_db
        results = coll.database.client[db_out][out[command]]
    return results, r
开发者ID:danpetrikin,项目名称:mongoUtils,代码行数:85,代码来源:mapreduce.py


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