本文整理汇总了Python中pymongo.connection.Connection.add_son_manipulator方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.add_son_manipulator方法的具体用法?Python Connection.add_son_manipulator怎么用?Python Connection.add_son_manipulator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymongo.connection.Connection
的用法示例。
在下文中一共展示了Connection.add_son_manipulator方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Database
# 需要导入模块: from pymongo.connection import Connection [as 别名]
# 或者: from pymongo.connection.Connection import add_son_manipulator [as 别名]
class Database(object):
'''
# TODO: rewrite
Database provides access to its collections by collection class name and
collection alias. It automatically gather all imported classes inherited
of "Document" and classes derived from "Document" and make references to
them if they belongs to this database (determined by __db__ attribute,
if it missing it is consider that collection belongs to first created db).
It should be remembered that class inherited of "Document" or of the class
derived from "Document" and containing at least one instance of "Field"
class as attribute is collection!
Database automatically setup missing aliases if possible by make plural
in lower case. Also it automatically assign cross-references.
To create you must specify database name, host and port
Host is '127.0.0.1' by default
Port is 27017 by default
Optionally you can pass three additional arguments:
gen_ids - to generate object id at client side. True by default
quiet_output - to hide warning. False by default
safe_mode - to set safe quering mode. May be bool or dict (safe options).
False by default
To see full database info just type
>>> db = Database(name, host, port)
>>> print(db)
'''
@property
def name(self):
return self._name
@property
def host(self):
return self._host
@property
def port(self):
return self._port
def __init__(self, db_name='test', host='127.0.0.1', port=27017,
gen_ids=True, quiet_output=False, safe_mode=False, schemes=()):
# set quiet output if need
Selector.quiet_output = quiet_output
# set read-only attrs
self._name = db_name
self._host = host
self._port = port
# set attrs
self.gen_ids = gen_ids
self.quiet_output = quiet_output
self.safe_mode = bool(safe_mode)
self.safe_opts = safe_mode \
if safe_mode and isinstance(safe_mode, dict) else {}
# connect to mongo
self._connection = Connection(host, port)[db_name]
# add converter
self._connection.add_son_manipulator(ConvertToObject(self))
# declare vars
self._collections = {}
collections = set()
# take the collections that belong to this database
# or do not belong to anything
for scheme in schemes:
if len(scheme._meta.fields) and \
getattr(scheme._meta, 'db', self.name) == self.name:
collections.add(scheme)
# create plural maker
plural = Plural()
# set aliases and references for each collection
for coll in collections:
# occupy collection and set reference to this database
setattr(coll._meta, 'db', self._name)
setattr(coll._meta, 'db_ref', self)
if getattr(coll._meta, 'alias', '').startswith('_'):
raise SchemaError.metadata_naming_exc(
coll._meta.alias, coll.__module__, coll.__name__)
if not hasattr(coll._meta, 'alias'):
# no alias was given
coll._meta.alias = plural.make(coll.__name__)
if not hasattr(self, coll.__name__):
# collection name is free
setattr(self, coll.__name__, coll)
elif hasattr(self, coll._meta.alias):
# collection name or alias must be free
#.........这里部分代码省略.........