本文整理汇总了Python中mongokit.Connection.close方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.close方法的具体用法?Python Connection.close怎么用?Python Connection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongokit.Connection
的用法示例。
在下文中一共展示了Connection.close方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DBConnection
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import close [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: DBConnection
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import close [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
示例3: __init__
# 需要导入模块: from mongokit import Connection [as 别名]
# 或者: from mongokit.Connection import close [as 别名]
class Database:
def __init__(self):
self.database = None
self.connection = None
def connect(self, env=None):
section_name = "Mongo"
if env or (current_app and current_app.config["TESTING"]):
section_name += " Test"
mongodb_uri = "mongodb://"
if config.has_option(section_name, "user"):
mongodb_uri += config.get(section_name, "user") + ":" + config.get(section_name, "password") + "@"
mongodb_uri += config.get(section_name, "host") + ":" + config.get(section_name, "port")
db_name = config.get(section_name, "database")
try:
self.connection = Connection(mongodb_uri)
self.database = self.connection[db_name]
except ConnectionError:
print('Error: Unable to connect to database.')
sys.exit(1)
def close(self):
self.connection.close()
def clean(self):
for collection_name in self.database.collection_names():
if collection_name not in ["system.indexes"]:
self.database[collection_name].remove({})
@staticmethod
def dump_object(json_object):
return json.dumps(json_object, default=json_util.default)
@staticmethod
def create_bson_id_from_object(data):
try:
data["_id"] = ObjectId(str(data["id"]))
del data["id"]
except KeyError:
# This just means it is a new object, not an existing one
pass
return data
@staticmethod
def create_bson_id_from_string(string):
try:
string = ObjectId(string)
except KeyError:
# This just means it is a new object, not an existing one
pass
return string
@staticmethod
def extract_bson_id(data):
try:
data["id"] = str(data["_id"])
del data["_id"]
except KeyError:
# This just means it is a new
pass
return data
def get_collection(self, collection_name):
return self.database[collection_name]
def get_data(self, collection_name, dump=True, criteria=None, selection=None, sort=None, extract_id=True):
collection = self.get_collection(collection_name)
if not sort:
sort = [('_id', 1)]
if not criteria:
criteria = {}
data_list = []
for entry in collection.find(criteria, selection).sort(sort):
if extract_id:
entry = self.extract_bson_id(entry)
data_list.append(entry)
if dump:
return self.dump_object
else:
return data_list
def get_single_data(self, collection_name, dump=True, criteria=None, selection=None, sort=None, extract_id=True):
data = self.get_data(collection_name,
dump=False,
criteria=criteria,
selection=selection,
sort=sort,
extract_id=extract_id)
if len(data) > 0:
data = data[0]
#.........这里部分代码省略.........