本文整理汇总了Python中model.connect_db函数的典型用法代码示例。如果您正苦于以下问题:Python connect_db函数的具体用法?Python connect_db怎么用?Python connect_db使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了connect_db函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, params):
self.params = params
# load configuration
model.load_config(params.config, basepath=params.path)
# connect to database
model.connect_db()
# clear db
model.ue.UE.drop_collection()
model.location.Location.drop_collection()
model.accesspoint.AccessPoint.drop_collection()
# setup zero mq receiver
self.setup_zmq()
# load access point definitions from configuration file
# and from remote AP manager component.
# Deletes all current AP definitions at first.
# If connection to remote AP manage API is not possible,
# the system will run without any AP object and thus
# no APs are assigned to UEs.
model.accesspoint.AccessPoint.refresh(
model.CONFIG["accesspoints"], ap_manager_client.get_accesspoints())
# load management algorithm
plugin.load_algorithms(
model.CONFIG["algorithm"]["available"],
model.CONFIG["algorithm"]["default"])
# kick of AP state fetcher thread
self.apFetcherThread = updater.AccessPointStateFetcher()
self.apFetcherThread.start()
# enable "always_on_macs" on all access points
if "always_on_macs" in model.CONFIG:
for mac in model.CONFIG["always_on_macs"].itervalues():
ap_manager_client.set_mac_list(
mac, [ap.uuid for ap in model.accesspoint.AccessPoint.objects], [])
logging.info("Enabled MAC %s on all APs." % str(mac))
示例2: save_task
def save_task():
title = request.form['title']
db = model.connect_db()
#Assume that all tasks are attached to user Christian Fernandez
#task ID below
task_id = model.new_task(db, title, 'Christian', 'Fernandez')
return redirect("/tasks")
示例3: populate_sets_fans
def populate_sets_fans(batch):
db = model.connect_db()
for set_id, seo_title in batch:
model.enter_new_sets_fans(db, set_id)
示例4: list_of_unique_items
def list_of_unique_items(batch):
# Make a tuple of the relevant sets to pass into the query
relevant_sets = tuple([ b[0] for b in batch ])
question_marks = ["?"] * len(relevant_sets)
question_marks_string = ", ".join(question_marks)
# Connecting to database
db = model.connect_db()
# Format and execute the SQL query
query_template = """SELECT * FROM Sets_Items WHERE set_id IN (%s)"""
query = query_template % (question_marks_string)
result = db.execute(query, relevant_sets)
# Get a list of records, where each item is a fan name
list_of_item_ids = []
list_of_item_seo_titles = []
for row in result.fetchall():
if not row[2] in list_of_item_ids: # Check for duplicates
# We need to delay zipping until the end b/c of this.
list_of_item_ids.append(row[2])
list_of_item_seo_titles.append(row[3])
list_of_items = zip(list_of_item_ids, list_of_item_seo_titles)
return list_of_items
示例5: list_of_unique_fans
def list_of_unique_fans(batch):
# Make a tuple of the relevant sets to pass into the query
relevant_sets = tuple([ b[0] for b in batch ])
question_marks = ["?"] * len(relevant_sets)
question_marks_string = ", ".join(question_marks)
# Connecting to database
db = model.connect_db()
# Format and execute the SQL query
query_template = """SELECT * FROM Sets_Fans WHERE set_id IN (%s)"""
query = query_template % (question_marks_string)
result = db.execute(query, relevant_sets)
# Get a list of records, where each item is a fan name
list_of_fans = []
for row in result.fetchall():
if not row[3] in list_of_fans: # Check for duplicates
if not row[2] == "0": # If fan_id is 0, means they don't have an actual account
list_of_fans.append(row[3])
return list_of_fans
示例6: save_task
def save_task():
task_title = request.form['task_title']
user_id = request.form['user_id']
db = model.connect_db()
# assume all tasks are attached to user 1 :(
task_id = model.new_task(db, task_title, user_id)
return redirect("/tasks")
示例7: login
def login():
email = request.form['email']
password = request.form['password']
db = model.connect_db()
auth_user = model.authenticate(db, email, password)
if auth_user != None:
return redirect("/Tasks")
示例8: save_participant
def save_participant():
participant_name = request.form['participant_name']
number_entries = request.form['number_entries']
db = model.connect_db()
participant_id = model.new_participant(db, participant_name, number_entries)
print participant_id
return redirect("/the_lottery")
示例9: winners
def winners():
db = model.connect_db()
participants_from_db = model.get_participants(db)
games_from_db = model.get_games(db)
print participants_from_db
entries = []
for item in participants_from_db:
entry = item['number_entries']
person = item['participant_name']
for i in range(entry):
entries.append(person)
random.shuffle(entries)
games_list = []
for game in games_from_db:
games_list.append(game["game"])
random.shuffle(games_list)
decisions = {}
a = 0
while a < len(entries):
if entries[a] in decisions:
decisions[entries[a]].append(games_list[a])
else:
decisions[entries[a]] = [games_list[a]]
a +=1
print decisions
return render_template("winners.html", winners = decisions)
示例10: sets_per_item
def sets_per_item():
db = model.connect_db()
query = """SELECT COUNT(set_id) from Items_Sets GROUP BY item_id"""
result = db.execute(query)
result_list = [ row[0] for row in result.fetchall() ]
# Create a histogram
histogram = [0] * 23
print histogram
for num in result_list:
if num <= 22:
histogram[num] += 1
elif num > 22:
histogram[22] += 1
print histogram
# Print out the histogram
for bucket in range(1, len(histogram)):
if bucket <= 21:
print "%d items <-----> belong to %d sets" % (histogram[bucket], bucket)
elif bucket == 22:
print "%d items <-----> belong to 22 or more sets" % histogram[bucket]
# Check against total # of Items_Sets records by doing SumProduct
sum_p = 0
for bucket in range(1, len(histogram)):
sum_p += bucket * histogram[bucket]
print sum_p
db.close()
示例11: new_user
def new_user():
user_name= request.form['user_name']
email= request.form['user_email']
db = model.connect_db()
#new_user(db, email, password, name)
user_id = model.new_user(db, email, None, user_name)
return redirect("/new_task")
示例12: print_overlap
def print_overlap():
db = model.connect_db()
# Creates a table with 2 columns -- (a) fan_name (b) num out of 5 sets in batch_1 that they fanned
cursor = db.execute("SELECT fan_name, COUNT(fan_name) FROM Sets_Fans GROUP BY fan_name")
# Doing the rest in Python instead of SQL for now --> need to look up how to query the result of a query
total_fans = 0.0
sets_rated_by_each_fan = ["Not Applicable", 0.0, 0.0, 0.0, 0.0, 0.0]
for row in cursor.fetchall():
number_rated = row[1]
sets_rated_by_each_fan[number_rated] += 1
total_fans += 1
# Transform frequency counts into percentages
print "LEVEL OF OVERLAP"
print "Out of " + str(int(total_fans)) + " total fans who rated 5 'Popular' sets"
print "--------------------------------------------------"
for x in range(1,6):
percentage = int(round(sets_rated_by_each_fan[x]/total_fans*100))
print str(int(sets_rated_by_each_fan[x])) + " out of " + str(int(total_fans)) + " -- (" + str(percentage) + "%) -- rated " + str(x) + " out of 5 sets"
db.close()
示例13: populate_items
def populate_items(item_list, level):
db = model.connect_db()
successfully_entered = 0.0
no_file_error = 0.0
for item_id, item_seo_title in item_list:
try:
model.enter_new_item(db, item_id, level)
print item_id
successfully_entered += 1
except:
print "++++++++++++++++++++++++++"
print "couldn't find file"
print item_id
print item_seo_title
print "++++++++++++++++++++++++++"
no_file_error += 1
print "Successfully entered: " + str(successfully_entered)
print "Errors: " + str(no_file_error)
print "Error rate: " + str(round(no_file_error / successfully_entered * 100, 2)) + "%"
db.close()
示例14: list_tasks
def list_tasks():
db = model.connect_db()
tasks_from_db = model.get_tasks(db, None)
completed_items = request.form.getlist("completed_t_f", type=int)
if completed_items:
for task in completed_items:
model.complete_task(db, task)
return render_template("tasks.html", tasks_field = tasks_from_db)
示例15: list_tasks
def list_tasks():
db = model.connect_db()
tasks_from_db = model.get_tasks(db, None)
user_id_list = []
for dictionary in tasks_from_db:
user_id_list.append(dictionary["user_id"])
for user_id in user_id_list:
users_from_db = model.get_user(db, user_id)
return render_template("list_tasks.html", tasks=tasks_from_db,users=users_from_db)