本文整理匯總了Python中db.DB.create_all方法的典型用法代碼示例。如果您正苦於以下問題:Python DB.create_all方法的具體用法?Python DB.create_all怎麽用?Python DB.create_all使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類db.DB
的用法示例。
在下文中一共展示了DB.create_all方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ChromaTest
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import create_all [as 別名]
class ChromaTest(unittest.TestCase):
def setUp(self):
logging.basicConfig(level=logging.DEBUG)
conf = MockConf(dbstring="sqlite://")
self.db = DB(conf)
self.db.create_all()
self.sess = self.db.session()
self.sess.add_all(Region.create_from_json(TEST_LANDS))
self.sess.commit()
# Create some users
self.alice = self.create_user("alice", 0)
self.bob = self.create_user("bob", 1)
def create_user(self, name, team):
newbie = User(name=name, team=team, loyalists=100, leader=True)
self.sess.add(newbie)
cap = Region.capital_for(team, self.sess)
newbie.region = cap
self.sess.commit()
return newbie
def get_region(self, name):
name = name.lower()
region = self.sess.query(Region).filter_by(name=name).first()
return region
示例2: Bot
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import create_all [as 別名]
class Bot(object):
def __init__(self, config, reddit):
self.config = config
self.reddit = reddit
self.db = DB(config)
self.db.create_all()
self.session = self.db.session()
@failable
def check_battles(self):
session = self.session
battles = session.query(Battle).all()
for battle in battles:
post = self.reddit.get_submission(
comment_limit=None,
submission_id=name_to_id(battle.submission_id))
if post:
self.process_post_for_battle(post, battle, session)
@failable
def check_hq(self):
hq = self.reddit.get_subreddit(self.config.headquarters)
submissions = hq.get_new()
for submission in submissions:
if "[recruitment]" in submission.title.lower():
self.recruit_from_post(submission)
break # Only recruit from the first one
@failable
def check_messages(self):
unread = reddit.get_unread(True, True)
session = self.session
for comment in unread:
# Only PMs, we deal with comment replies in process_post_for_battle
if not comment.was_comment:
seen = (session.query(Processed).
filter_by(id36=comment.name).
count())
if seen:
continue
player = self.find_player(comment, session)
if player:
cmds = extract_command(comment.body)
if not cmds:
cmds = [comment.body]
context = Context(player, self.config, session,
comment, self.reddit)
for cmd in cmds:
self.command(cmd, context)
session.add(Processed(id36=comment.name))
session.commit()
comment.mark_as_read()
@failable
def command(self, text, context):
text = text.lower()
logging.info("Processing command: '%s' by %s" %
(text, context.player.name))
try:
parsed = parse(text)
parsed.execute(context)
except ParseException as pe:
result = (
"I'm sorry, I couldn't understand your command:"
"\n\n"
"> %s\n"
"\nThe parsing error is below:\n\n"
" %s") % (text, pe)
context.reply(result)
def find_player(self, comment, session):
if comment.author: # Some messages (mod invites) don't have authors
player = session.query(User).filter_by(
name=comment.author.name.lower()).first()
if not player and getattr(comment, 'was_comment', None):
comment.reply(Command.FAIL_NOT_PLAYER %
self.config.headquarters)
return player
return None
@failable
def generate_markdown_report(self, loop_start):
"""
Separate from the others as this logs to a sidebar rather than
a file
"""
s = self.session
land_report = StatusCommand.lands_status_for(s, self.config)
hq = self.reddit.get_subreddit(self.config.headquarters)
cur = now()
elapsed = (cur - loop_start) + self.config["bot"]["sleep"]
version_str = version(self.config)
bot_report = ("Bot Status:\n\n"
"* Last run at %s\n\n"
"* Seconds per Frame: %d\n\n"
"* Version: %s") % (timestr(cur), elapsed, version_str)
#.........這裏部分代碼省略.........
示例3: DB
# 需要導入模塊: from db import DB [as 別名]
# 或者: from db.DB import create_all [as 別名]
import bottle
import os
app = bottle.default_app()
app.config['basedir'] = os.path.abspath(os.path.dirname(__file__))
app.config['db_path'] = os.path.join(app.config['basedir'], 'db\\university.sqlite')
app.config['static'] = os.path.join(app.config['basedir'], 'static')
from db import DB
db = DB(app)
if not os.path.exists(app.config['db_path']):
print("Created new database: " + app.config['db_path'])
db.connect()
db.create_all()
else:
db.connect()
app.config['db'] = db
from controller import Controller
mainController = Controller(app)
app.run(host='localhost', port=8080, debug=True)