本文整理匯總了Python中bson.dbref.DBRef方法的典型用法代碼示例。如果您正苦於以下問題:Python dbref.DBRef方法的具體用法?Python dbref.DBRef怎麽用?Python dbref.DBRef使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bson.dbref
的用法示例。
在下文中一共展示了dbref.DBRef方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dereference
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def dereference(self, dbref, **kwargs):
"""Dereference a :class:`~bson.dbref.DBRef`, getting the
document it points to.
Raises :class:`TypeError` if `dbref` is not an instance of
:class:`~bson.dbref.DBRef`. Returns a document, or ``None`` if
the reference does not point to a valid document. Raises
:class:`ValueError` if `dbref` has a database specified that
is different from the current database.
:Parameters:
- `dbref`: the reference
- `**kwargs` (optional): any additional keyword arguments
are the same as the arguments to
:meth:`~pymongo.collection.Collection.find`.
"""
if not isinstance(dbref, DBRef):
raise TypeError("cannot dereference a %s" % type(dbref))
if dbref.database is not None and dbref.database != self.__name:
raise ValueError("trying to dereference a DBRef that points to "
"another database (%r not %r)" % (dbref.database,
self.__name))
return self[dbref.collection].find_one({"_id": dbref.id}, **kwargs)
示例2: transform_incoming
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_incoming(self, son, collection):
"""Replace embedded documents with DBRefs.
"""
def transform_value(value):
if isinstance(value, dict):
if "_id" in value and "_ns" in value:
return DBRef(value["_ns"], transform_value(value["_id"]))
else:
return transform_dict(SON(value))
elif isinstance(value, list):
return [transform_value(v) for v in value]
return value
def transform_dict(object):
for (key, value) in list(object.items()):
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
示例3: transform_outgoing
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_outgoing(self, son, collection):
"""Replace DBRefs with embedded documents.
"""
def transform_value(value):
if isinstance(value, DBRef):
return self.database.dereference(value)
elif isinstance(value, list):
return [transform_value(v) for v in value]
elif isinstance(value, dict):
return transform_dict(SON(value))
return value
def transform_dict(object):
for (key, value) in list(object.items()):
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
# TODO make a generic translator for custom types. Take encode, decode,
# should_encode and should_decode functions and just encode and decode where
# necessary. See examples/custom_type.py for where this would be useful.
# Alternatively it could take a should_encode, to_binary, from_binary and
# binary subtype.
示例4: transform_incoming
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_incoming(self, son, collection):
"""Replace embedded documents with DBRefs.
"""
def transform_value(value):
if isinstance(value, abc.MutableMapping):
if "_id" in value and "_ns" in value:
return DBRef(value["_ns"], transform_value(value["_id"]))
else:
return transform_dict(SON(value))
elif isinstance(value, list):
return [transform_value(v) for v in value]
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
示例5: transform_outgoing
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_outgoing(self, son, collection):
"""Replace DBRefs with embedded documents.
"""
def transform_value(value):
if isinstance(value, DBRef):
return self.database.dereference(value)
elif isinstance(value, list):
return [transform_value(v) for v in value]
elif isinstance(value, abc.MutableMapping):
return transform_dict(SON(value))
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
示例6: _get_object
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def _get_object(data, position, obj_end, opts, dummy):
"""Decode a BSON subdocument to opts.document_class or bson.dbref.DBRef."""
obj_size = _UNPACK_INT(data[position:position + 4])[0]
end = position + obj_size - 1
if data[end:position + obj_size] != b"\x00":
raise InvalidBSON("bad eoo")
if end >= obj_end:
raise InvalidBSON("invalid object length")
if _raw_document_class(opts.document_class):
return (opts.document_class(data[position:end + 1], opts),
position + obj_size)
obj = _elements_to_dict(data, position + 4, end, opts)
position += obj_size
if "$ref" in obj:
return (DBRef(obj.pop("$ref"), obj.pop("$id", None),
obj.pop("$db", None), obj), position)
return obj, position
示例7: dumps
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [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)
示例8: _parse_canonical_dbpointer
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def _parse_canonical_dbpointer(doc):
"""Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef."""
dbref = doc['$dbPointer']
if len(doc) != 1:
raise TypeError('Bad $dbPointer, extra field(s): %s' % (doc,))
if isinstance(dbref, DBRef):
dbref_doc = dbref.as_doc()
# DBPointer must not contain $db in its value.
if dbref.database is not None:
raise TypeError(
'Bad $dbPointer, extra field $db: %s' % (dbref_doc,))
if not isinstance(dbref.id, ObjectId):
raise TypeError(
'Bad $dbPointer, $id must be an ObjectId: %s' % (dbref_doc,))
if len(dbref_doc) != 2:
raise TypeError(
'Bad $dbPointer, extra field(s) in DBRef: %s' % (dbref_doc,))
return dbref
else:
raise TypeError('Bad $dbPointer, expected a DBRef: %s' % (doc,))
示例9: transform_incoming
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_incoming(self, son, collection):
"""Replace embedded documents with DBRefs.
"""
def transform_value(value):
if isinstance(value, collections.MutableMapping):
if "_id" in value and "_ns" in value:
return DBRef(value["_ns"], transform_value(value["_id"]))
else:
return transform_dict(SON(value))
elif isinstance(value, list):
return [transform_value(v) for v in value]
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
示例10: transform_outgoing
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def transform_outgoing(self, son, collection):
"""Replace DBRefs with embedded documents.
"""
def transform_value(value):
if isinstance(value, DBRef):
return self.database.dereference(value)
elif isinstance(value, list):
return [transform_value(v) for v in value]
elif isinstance(value, collections.MutableMapping):
return transform_dict(SON(value))
return value
def transform_dict(object):
for (key, value) in object.items():
object[key] = transform_value(value)
return object
return transform_dict(SON(son))
# TODO make a generic translator for custom types. Take encode, decode,
# should_encode and should_decode functions and just encode and decode where
# necessary. See examples/custom_type.py for where this would be useful.
# Alternatively it could take a should_encode, to_binary, from_binary and
# binary subtype.
示例11: _node_changes
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def _node_changes(self, kind, method, links, nodes):
event = InvestigationEvent(kind=kind)
for link in links:
link = InvestigationLink.build(link)
if method('links', link.to_mongo()):
event.links.append(link)
for node in nodes:
if not isinstance(node, DBRef):
node = node.to_dbref()
if method('nodes', node):
event.nodes.append(node)
if len(event.nodes) > 0 or len(event.links) > 0:
self.modify(push__events=event, updated=datetime.utcnow())
示例12: cleanup_links
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def cleanup_links(self):
links = self.get_links(resolve=True)
back_links = self.get_back_links(resolve=True)
if len(back_links) > 0:
self.log.error(("{} is a dependency for the objects:"
.format(self._name)))
for elem in back_links:
self.log.error("[{}/{}]".format(elem['collection'],
elem['name']))
return False
for link in links:
self.unlink(link['DBRef'])
return True
示例13: _get_object
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def _get_object(data, position, as_class, tz_aware, uuid_subtype, compile_re):
obj_size = struct.unpack("<i", data[position:position + 4])[0]
if data[position + obj_size - 1:position + obj_size] != ZERO:
raise InvalidBSON("bad eoo")
encoded = data[position + 4:position + obj_size - 1]
object = _elements_to_dict(
encoded, as_class, tz_aware, uuid_subtype, compile_re)
position += obj_size
if "$ref" in object:
return (DBRef(object.pop("$ref"), object.pop("$id", None),
object.pop("$db", None), object), position)
return object, position
示例14: _get_ref
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def _get_ref(data, position, as_class, tz_aware, uuid_subtype, compile_re):
collection, position = _get_string(data, position, as_class, tz_aware,
uuid_subtype, compile_re)
oid, position = _get_oid(data, position)
return DBRef(collection, oid), position
示例15: dereference
# 需要導入模塊: from bson import dbref [as 別名]
# 或者: from bson.dbref import DBRef [as 別名]
def dereference(self, dbref, session=None, **kwargs):
"""Dereference a :class:`~bson.dbref.DBRef`, getting the
document it points to.
Raises :class:`TypeError` if `dbref` is not an instance of
:class:`~bson.dbref.DBRef`. Returns a document, or ``None`` if
the reference does not point to a valid document. Raises
:class:`ValueError` if `dbref` has a database specified that
is different from the current database.
:Parameters:
- `dbref`: the reference
- `session` (optional): a
:class:`~pymongo.client_session.ClientSession`.
- `**kwargs` (optional): any additional keyword arguments
are the same as the arguments to
:meth:`~pymongo.collection.Collection.find`.
.. versionchanged:: 3.6
Added ``session`` parameter.
"""
if not isinstance(dbref, DBRef):
raise TypeError("cannot dereference a %s" % type(dbref))
if dbref.database is not None and dbref.database != self.__name:
raise ValueError("trying to dereference a DBRef that points to "
"another database (%r not %r)" % (dbref.database,
self.__name))
return self[dbref.collection].find_one(
{"_id": dbref.id}, session=session, **kwargs)