本文整理汇总了Python中src.tools.db.DB类的典型用法代码示例。如果您正苦于以下问题:Python DB类的具体用法?Python DB怎么用?Python DB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DB类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __get_question_list
def __get_question_list(self):
question_list = [DB.wrap('question', x) for x in DB.get_result_list(self.sql.question)]
answer_list = [DB.wrap('answer', x) for x in DB.get_result_list(self.sql.get_answer_sql())]
def merge_answer_into_question():
question_dict = {x['question_id']: {'question': x.copy(), 'answer_list': [], 'agree': 0} for x in
question_list}
for answer in answer_list:
question_dict[answer['question_id']]['answer_list'].append(answer)
return question_dict.values()
def add_property(question):
agree_count = 0
char_count = 0
for answer in question['answer_list']:
answer['char_count'] = len(answer['content'])
answer['agree_count'] = answer['agree']
answer['update_date'] = answer['edit_date']
agree_count += answer['agree']
char_count += answer['char_count']
question['answer_count'] = len(question['answer_list'])
question['agree_count'] = agree_count
question['char_count'] = char_count
return question
question_list = [add_property(x) for x in merge_answer_into_question() if len(x['answer_list'])]
return question_list
示例2: login
def login(self, account, password, captcha=''):
content = Http.get_content('https://www.zhihu.com/')
xsrf = Match.xsrf(content)
if not xsrf:
Debug.logger.info(u'登陆失败')
Debug.logger.info(u'敲击回车重新发送登陆请求')
return False
xsrf = xsrf.split('=')[1]
# add xsrf as cookie into cookieJar,
cookie = Http.make_cookie(name='_xsrf', value=xsrf, domain='www.zhihu.com')
self.cookieJar.set_cookie(cookie)
if captcha:
post_data = {'_xsrf': xsrf, 'email': account, 'password': password, 'remember_me': True,
'captcha': captcha}
else:
post_data = {'_xsrf': xsrf, 'email': account, 'password': password, 'remember_me': True}
header = {
'Accept': '*/*',
'Accept-Encoding': 'gzip,deflate', # 主要属性,只要有此项知乎即认为来源非脚本
'Accept-Language': 'zh,zh-CN;q=0.8,en-GB;q=0.6,en;q=0.4',
'Host': 'www.zhihu.com',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36(KHTML, like Gecko)Chrome/34.0.1847.116 Safari/537.36',
'Connection': 'keep-alive',
'X-Requested-With': 'XMLHttpRequest',
'Origin': 'https://www.zhihu.com',
'Referer': 'https://www.zhihu.com/',
}
result = Http.get_content(url=r'https://www.zhihu.com/login/email', data=post_data, extra_header=header)
if not result:
Debug.logger.info(u'登陆失败,请敲击回车重新登陆')
return False
response = json.loads(result)
if response['r'] == 0:
print u'登陆成功!'
print u'登陆账号:', account
print u'请问是否需要记住帐号密码?输入yes记住,输入其它任意字符跳过,回车确认'
if raw_input() == 'yes':
Config.account, Config.password, Config.remember_account = account, password, True
print u'帐号密码已保存,可通过修改config.json修改设置'
else:
Config.account, Config.password, Config.remember_account = '', '', False
print u'跳过保存环节,进入下一流程'
Config._save()
cookie = self.get_cookie()
data = {}
data['account'] = account
data['password'] = password
data['recordDate'] = ExtraTools.get_today()
data['cookieStr'] = cookie
DB.save(data, 'LoginRecord')
DB.commit()
return True
else:
print u'登陆失败'
Debug.print_dict(response)
return False
示例3: save
def save(self):
self.clear_index()
save_config = self.create_save_config()
for key in save_config:
for item in save_config[key]:
if item:
DB.save(item, key)
DB.commit()
return
示例4: query_answer_list_by_author_page_id
def query_answer_list_by_author_page_id(self, author_page_id):
# 需要先查出来对应的author_id
author_info = DB.query_row(u'select author_id from Author where author_page_id="{author_page_id}"'.format(author_page_id=author_page_id))
author_id = author_info[u'author_id']
raw_answer_list = DB.query_all(u'select * from Answer where author_id="{author_id}" {order_by} '.format(author_id=author_id, order_by=Config.answer_order_by))
answer_list = []
for raw_answer in raw_answer_list:
answer_list.append(self.format_answer(raw_answer))
return answer_list
示例5: __init__
def __init__(self):
# 初始化目录结构
Path.init_base_path()
Path.init_work_directory()
# 初始化数据库链接
DB.init_database()
# 初始化配置
Config.init_config()
return
示例6: save_record_list
def save_record_list(table_name, record_list):
"""
将数据保存到数据库中
:return:
"""
for record in record_list:
DB.save(record, table_name)
DB.commit()
return
示例7: __get_article_list
def __get_article_list(self):
def add_property(article):
article['char_count'] = len(article['content'])
article['agree_count'] = article['agree']
article['update_date'] = article['publish_date']
article['answer_count'] = 1
return article
article_list = [DB.wrap(Type.article, x) for x in DB.get_result_list(self.sql.get_answer_sql())]
article_list = [add_property(x) for x in article_list]
return article_list
示例8: __get_article_list
def __get_article_list(self):
def add_property(article):
article['char_count'] = len(article['content'])
article['answer_count'] = 1
# TODO
if self.kind in [Type.jianshu_author, Type.jianshu_collection, Type.jianshu_notebooks,
Type.sinablog_author, Type.csdnblog_author]:
article['agree_count'] = "没有赞同数" # article['agree']
else:
article['agree_count'] = article['agree']
article['update_date'] = article['publish_date']
return article
if self.kind in [Type.jianshu_author, Type.jianshu_collection, Type.jianshu_notebooks]:
article_list = [DB.wrap(Type.jianshu_article, x) for x in DB.get_result_list(self.sql.get_answer_sql())]
elif self.kind == Type.sinablog_author:
article_list = [DB.wrap(Type.sinablog_article, x) for x in DB.get_result_list(self.sql.get_answer_sql())]
elif self.kind == Type.csdnblog_author:
article_list = [DB.wrap(Type.csdnblog_article, x) for x in DB.get_result_list(self.sql.get_answer_sql())]
else:
article_list = [DB.wrap(Type.article, x) for x in DB.get_result_list(self.sql.get_answer_sql())]
article_list = [add_property(x) for x in article_list]
return article_list
示例9: catch_info
def catch_info(self):
info = {}
if self.property.sql.info:
info = DB.cursor.execute(self.property.sql.info).fetchone()
info = DB.wrap(Type.info_table[self.kind], info)
self.set_info(info)
return
示例10: query_question_list
def query_question_list(self, question_id_list):
raw_question_list = DB.query_all(
u'select * from Question where question_id in ({question_id_list})'.format(question_id_list=','.join(question_id_list)))
question_list = []
for raw_question in raw_question_list:
question_list.append(self.format_question(raw_question))
return question_list
示例11: query_question
def query_question(self, question_id):
"""
:rtype: Question_Info
"""
question = DB.query_row(u'select * from Question where question_id in ({question_id})'.format(question_id=question_id))
question = self.format_question(question) # 包装成标准的信息格式
return question
示例12: catch_article_book_info
def catch_article_book_info(self, sql):
info_list = DB.cursor.execute(self.sql.info).fetchall()
info_list = [DB.wrap(Type.article, item) for item in info_list]
info = {}
info['title'] = '_'.join([str(item['title']) for item in info_list])
info['id'] = '_'.join([str(item['article_id']) for item in info_list])
return info
示例13: catch_question_book_info
def catch_question_book_info(self, sql):
info_list = DB.cursor.execute(self.sql.info).fetchall()
info_list = [DB.wrap(Type.question, item) for item in info_list]
info = dict()
info['title'] = '_'.join([str(item['title']) for item in info_list]) # 可以是多个问题, 多个id联系在一起
info['id'] = '_'.join([str(item['question_id']) for item in info_list])
return info
示例14: query_answer_list_by_question_id
def query_answer_list_by_question_id(self, question_id):
raw_answer_list = DB.query_all(
u'select * from Answer where question_id="{question_id}" {order_by} '.format(question_id=question_id, order_by=Config.answer_order_by))
answer_list = []
for raw_answer in raw_answer_list:
answer_list.append(self.format_answer(raw_answer))
return answer_list
示例15: query_article_list_by_column_id
def query_article_list_by_column_id(self, column_id):
# 根据发表时间正序获取文章列表,方便浏览
raw_article_list = DB.query_all(u'select * from Article where column_id="{column_id}" {order_by}'.format(column_id=column_id, order_by=Config.article_order_by))
article_list = []
for raw_article in raw_article_list:
article = self.format_article(raw_article)
article_list.append(article)
return article_list