本文整理汇总了Python中bson.SON属性的典型用法代码示例。如果您正苦于以下问题:Python bson.SON属性的具体用法?Python bson.SON怎么用?Python bson.SON使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类bson
的用法示例。
在下文中一共展示了bson.SON属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: objify
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def objify(self, doc, columns=None):
"""
Decode a Pymongo SON object into an Pandas DataFrame
"""
cols = columns or doc[METADATA][COLUMNS]
data = {}
for col in cols:
# if there is missing data in a chunk, we can default to NaN
# and pandas will autofill the missing values to the correct length
if col not in doc[METADATA][LENGTHS]:
d = [np.nan]
else:
d = decompress(doc[DATA][doc[METADATA][LENGTHS][col][0]: doc[METADATA][LENGTHS][col][1] + 1])
# d is ready-only but that's not an issue since DataFrame will copy the data anyway.
d = np.frombuffer(d, doc[METADATA][DTYPE][col])
if MASK in doc[METADATA] and col in doc[METADATA][MASK]:
mask_data = decompress(doc[METADATA][MASK][col])
mask = np.frombuffer(mask_data, 'bool')
d = ma.masked_array(d, mask)
data[col] = d
# Copy into
return pd.DataFrame(data, columns=cols, copy=True)[cols]
示例2: dumps
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :func:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
:Parameters:
- `json_options`: A :class:`JSONOptions` instance used to modify the
encoding of MongoDB Extended JSON types. Defaults to
:const:`DEFAULT_JSON_OPTIONS`.
.. versionchanged:: 3.4
Accepts optional parameter `json_options`. See :class:`JSONOptions`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances.
"""
json_options = kwargs.pop("json_options", DEFAULT_JSON_OPTIONS)
return json.dumps(_json_convert(obj, json_options), *args, **kwargs)
示例3: _match_map
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def _match_map(self, doc, other_doc):
for key, val in doc.items():
if val is absent:
if key in other_doc:
return False
elif not self._match_val(val, other_doc.get(key, None)):
return False
if isinstance(doc, (OrderedDict, bson.SON)):
if not isinstance(other_doc, (OrderedDict, bson.SON)):
raise TypeError(
"Can't compare ordered and unordered document types:"
" %r, %r" % (doc, other_doc))
keys = [key for key, val in doc.items()
if val is not absent]
if not seq_match(keys, list(other_doc.keys())):
return False
return True
示例4: count_by_state_unsynced
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def count_by_state_unsynced(self, arg):
exp_key = self._exp_key
# TODO: consider searching by SON rather than dict
if isinstance(arg, int):
if arg not in JOB_STATES:
raise ValueError('invalid state', arg)
query = dict(state=arg)
else:
assert hasattr(arg, '__iter__')
states = list(arg)
assert all([x in JOB_STATES for x in states])
query = dict(state={'$in': states})
if exp_key != None:
query['exp_key'] = exp_key
rval = self.handle.jobs.find(query).count()
return rval
示例5: deserialize
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def deserialize(self, data, columns=None):
"""
Deserializes SON to a DataFrame
Parameters
----------
data: SON data
columns: None, or list of strings
optionally you can deserialize a subset of the data in the SON. Index
columns are ALWAYS deserialized, and should not be specified
Returns
-------
pandas dataframe or series
"""
if not data:
return pd.DataFrame()
meta = data[0][METADATA] if isinstance(data, list) else data[METADATA]
index = INDEX in meta
if columns:
if index:
columns = columns[:]
columns.extend(meta[INDEX])
if len(columns) > len(set(columns)):
raise Exception("Duplicate columns specified, cannot de-serialize")
if not isinstance(data, list):
df = self.converter.objify(data, columns)
else:
df = pd.concat([self.converter.objify(d, columns) for d in data], ignore_index=not index)
if index:
df = df.set_index(meta[INDEX])
if meta[TYPE] == 'series':
return df[df.columns[0]]
return df
示例6: dumps
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :class:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances. (But not in Python 2.4.)
"""
if not json_lib:
raise Exception("No json library available")
return json.dumps(_json_convert(obj), *args, **kwargs)
示例7: _json_convert
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def _json_convert(obj):
"""Recursive helper method that converts BSON types so they can be
converted into json.
"""
if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support
return SON(((k, _json_convert(v)) for k, v in obj.items()))
elif hasattr(obj, '__iter__') and not isinstance(obj, string_types):
return list((_json_convert(v) for v in obj))
try:
return default(obj)
except TypeError:
return obj
示例8: validate_document_class
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def validate_document_class(option, value):
"""Validate the document_class option."""
if not issubclass(value, (abc.MutableMapping, RawBSONDocument)):
raise TypeError("%s must be dict, bson.son.SON, "
"bson.raw_bson.RawBSONDocument, or a "
"sublass of collections.MutableMapping" % (option,))
return value
示例9: validate_is_mapping
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def validate_is_mapping(option, value):
"""Validate the type of method arguments that expect a document."""
if not isinstance(value, abc.Mapping):
raise TypeError("%s must be an instance of dict, bson.son.SON, or "
"other type that inherits from "
"collections.Mapping" % (option,))
示例10: validate_is_document_type
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def validate_is_document_type(option, value):
"""Validate the type of method arguments that expect a MongoDB document."""
if not isinstance(value, (abc.MutableMapping, RawBSONDocument)):
raise TypeError("%s must be an instance of dict, bson.son.SON, "
"bson.raw_bson.RawBSONDocument, or "
"a type that inherits from "
"collections.MutableMapping" % (option,))
示例11: _json_convert
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def _json_convert(obj, json_options=DEFAULT_JSON_OPTIONS):
"""Recursive helper method that converts BSON types so they can be
converted into json.
"""
if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support
return SON(((k, _json_convert(v, json_options))
for k, v in iteritems(obj)))
elif hasattr(obj, '__iter__') and not isinstance(obj, (text_type, bytes)):
return list((_json_convert(v, json_options) for v in obj))
try:
return default(obj, json_options)
except TypeError:
return obj
示例12: to_mongo
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def to_mongo(self, *args, **kwargs) -> SON:
"""Wrap to_mongo."""
raw = kwargs.pop("raw", False)
son = super().to_mongo(*args, **kwargs)
if all([not raw, "_id" in son, "id" not in son]):
son["id"] = son.pop("_id")
return son
示例13: dumps
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def dumps(obj, *args, **kwargs):
"""Helper function that wraps :class:`json.dumps`.
Recursive function that handles all BSON types including
:class:`~bson.binary.Binary` and :class:`~bson.code.Code`.
.. versionchanged:: 2.7
Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
instances.
"""
return json.dumps(_json_convert(obj), *args, **kwargs)
示例14: _json_convert
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def _json_convert(obj):
"""Recursive helper method that converts BSON types so they can be
converted into json.
"""
if hasattr(obj, 'iteritems') or hasattr(obj, 'items'): # PY3 support
return SON(((k, _json_convert(v)) for k, v in iteritems(obj)))
elif hasattr(obj, '__iter__') and not isinstance(obj, (text_type, bytes)):
return list((_json_convert(v) for v in obj))
try:
return default(obj)
except TypeError:
return obj
示例15: validate_list_or_mapping
# 需要导入模块: import bson [as 别名]
# 或者: from bson import SON [as 别名]
def validate_list_or_mapping(option, value):
"""Validates that 'value' is a list or a document."""
if not isinstance(value, (abc.Mapping, list)):
raise TypeError("%s must either be a list or an instance of dict, "
"bson.son.SON, or any other type that inherits from "
"collections.Mapping" % (option,))