本文整理汇总了Python中mongokit.Connection.disconnect方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.disconnect方法的具体用法?Python Connection.disconnect怎么用?Python Connection.disconnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongokit.Connection
的用法示例。
在下文中一共展示了Connection.disconnect方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DBConnection
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import disconnect [as 别名]
class DBConnection(object):
def __init__(self):
#mongodb_uri = "mongodb://13.76.244.22:27017"
#mongolab_uri = "mongodb://shahid:[email protected]:62818/MongoLab-25"
#connectionString = 'mongodb://MongoLab-25:[email protected]:62818/MongoLab-25'
#self.client = MongoClient(mongolab_uri,
# connectTimeoutMS=30000,
# socketTimeoutMS=None,
# socketKeepAlive=True)
#self.db = self.client.get_default_database()
#self.con = Connection(mongodb_uri)
self.con = Connection()
self.con.register([Route])
self.con.register([Stop])
self.routes = self.con.Route
self.stops = self.con.Stop
def __del__(self):
self.con.close()
self.con.disconnect()
def connect():
con = Connection()
routes = con.amdoraft.routes
stops = con.amdoraft.stops
return
def disconnect():
return
示例2: MongoFest
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import disconnect [as 别名]
class MongoFest(BaseDB):
def __init__(self,app):
self.conn = Connection(app.config['MONGODB_HOST'],app.config['MONGODB_PORT'])
self.conn.register(Host)
self.logger = app.logger
def find(self,hostname='',tags=''):
if hostname == '':
return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find().sort("hostname", pymongo.ASCENDING) ]
elif hostname != '':
regx = re.compile(hostname)
return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find({"$or":[{'hostname':regx},{'homes.hostnames.val':regx}]}).sort("hostname", pymongo.ASCENDING) ]
def search(self,query):
tag_pattern = re.compile('tag:(\w+)')
host_pattern = re.compile('host:([\w\.-]+)')
tags = tag_pattern.findall(query)
hosts = host_pattern.findall(query)
query = tag_pattern.sub('',query)
query = host_pattern.sub('',query)
key_words = query.split(' ')
terms = [ {"description":re.compile(x,flags=re.I)} for x in key_words]
if hosts:
terms = terms + [{"$or":[{'hostname':re.compile(x)},{'homes.hostnames.val':re.compile(x)}]} for x in hosts]
if tags: #pythonic (moronic) way to check if an array is empty
terms = terms + [{"tags":{"$all":tags}}]
return [ self._clean_oid(h) for h in self.conn.sysfest.Host.find({"$and":terms}).sort("hostname", pymongo.ASCENDING) ]
def find_one(self,host_id):
if isinstance(host_id, basestring):
host_id=bson.objectid.ObjectId(host_id)
host = self._clean_oid(self.conn.sysfest.Host.find_one({'_id':host_id}))
return host
def update(self,host_id,values):
self.conn.sysfest.hosts.update({"_id":bson.objectid.ObjectId(host_id)},{"$set":values})
return self.find_one(host_id)
def create(self,values):
oid = self.conn.sysfest.hosts.insert(values)
return self.find_one(host_id=oid)
def delete(self,host_id):
return self.conn.sysfest.hosts.remove({'_id':bson.objectid.ObjectId(host_id)})
def close(self):
self.conn.disconnect()
def _clean_oid(self,host):
if host is not None and isinstance(host["_id"], bson.objectid.ObjectId):
host["_id"]=str(host["_id"])
return host
示例3: DBConnection
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import disconnect [as 别名]
class DBConnection(object):
def __init__(self):
#mongodb_uri = "mongodb://13.76.244.22:27017"
#mongolab_uri = "mongodb://shahid:[email protected]:62818/MongoLab-25"
connectionString = 'insert mongodb uri here'
#self.client = MongoClient(mongolab_uri,
# connectTimeoutMS=30000,
# socketTimeoutMS=None,
# socketKeepAlive=True)
#self.db = self.client.get_default_database()
#self.con = Connection(mongodb_uri)
self.con = Connection()
self.con.register([Project])
self.con.register([Files])
self.con.register([Job])
self.con.register([User])
self.con.register([Admin])
self.con.register([Log])
self.con.register([Server])
self.projects = self.con.Project
self.files = self.con.Files
self.jobs = self.con.Job
self.users = self.con.User
self.admins = self.con.Admin
self.logs = self.con.Log
self.server = self.con.Server
def __del__(self):
self.con.close()
self.con.disconnect()
def connect():
con = Connection()
projects = con.vcad.projects
admins = con.vcad.admin
con.register([Project])
return
def disconnect():
return
示例4: MongoKit
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import disconnect [as 别名]
#.........这里部分代码省略.........
:class:`flask.ext.mongokit.BSONObjectIdConverter`
as a converter with the key word **ObjectId**.
:param app: The Flask application will be bound to this MongoKit
instance.
"""
app.config.setdefault('MONGODB_HOST', '127.0.0.1')
app.config.setdefault('MONGODB_PORT', 27017)
app.config.setdefault('MONGODB_DATABASE', 'flask')
app.config.setdefault('MONGODB_SLAVE_OKAY', False)
app.config.setdefault('MONGODB_USERNAME', None)
app.config.setdefault('MONGODB_PASSWORD', None)
app.config.setdefault('MONGODB_MAX_POOL_SIZE', 10)
# register extension with app
app.extensions = getattr(app, 'extensions', {})
app.extensions['mongokit'] = self
app.url_map.converters['ObjectId'] = BSONObjectIdConverter
self.app = app
def register(self, documents):
"""Register one or more :class:`mongokit.Document` instances to the
connection.
Can be also used as a decorator on documents:
.. code-block:: python
db = MongoKit(app)
@db.register
class Task(Document):
structure = {
'title': unicode,
'text': unicode,
'creation': datetime,
}
:param documents: A :class:`list` of :class:`mongokit.Document`.
"""
#enable decorator usage as in mongokit.Connection
decorator = None
if not isinstance(documents, (list, tuple, set, frozenset)):
# we assume that the user used this as a decorator
# using @register syntax or using db.register(SomeDoc)
# we stock the class object in order to return it later
decorator = documents
documents = [documents]
for document in documents:
if document not in self.registered_documents:
self.registered_documents.append(document)
if decorator is None:
return self.registered_documents
else:
return decorator
def connect(self):
"""Connect to the MongoDB server and register the documents from
:attr:`registered_documents`. If you set ``MONGODB_USERNAME`` and
``MONGODB_PASSWORD`` then you will be authenticated at the
``MONGODB_DATABASE``.
"""
self.mongokit_connection = Connection(
host=self.app.config.get('MONGODB_HOST'),
port=self.app.config.get('MONGODB_PORT'),
max_pool_size=self.app.config.get('MONGODB_MAX_POOL_SIZE'),
slave_okay=self.app.config.get('MONGODB_SLAVE_OKAY')
)
self.mongokit_connection.register(self.registered_documents)
if self.database:
self.mongokit_db = Database(self.mongokit_connection, self.database)
else:
self.mongokit_db = Database(self.mongokit_connection, self.app.config.get('MONGODB_DATABASE'))
if self.app.config.get('MONGODB_USERNAME') is not None:
self.mongokit_db.authenticate(
self.app.config.get('MONGODB_USERNAME'),
self.app.config.get('MONGODB_PASSWORD')
)
@property
def connected(self):
"""Connection status to your MongoDB."""
return self.mongokit_connection is not None
def disconnect(self):
"""Close the connection to your MongoDB."""
if self.connected:
self.mongokit_connection.disconnect()
self.mongokit_connection = None
del self.mongokit_db
def __getattr__(self, name, **kwargs):
if not self.connected:
self.connect()
return getattr(self.mongokit_db, name)