當前位置: 首頁>>代碼示例>>Python>>正文


Python db.DB類代碼示例

本文整理匯總了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
開發者ID:daoli,項目名稱:ZhihuHelp__Python,代碼行數:27,代碼來源:initialbook.py

示例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
開發者ID:LichAmnesia,項目名稱:ZhihuHelp,代碼行數:59,代碼來源:login.py

示例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
開發者ID:FengWenPei,項目名稱:ZhihuHelp,代碼行數:9,代碼來源:worker.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:9,代碼來源:task_result.py

示例5: __init__

 def __init__(self):
     #   初始化目錄結構
     Path.init_base_path()
     Path.init_work_directory()
     #   初始化數據庫鏈接
     DB.init_database()
     #   初始化配置
     Config.init_config()
     return
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:9,代碼來源:main.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:9,代碼來源:worker.py

示例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
開發者ID:daoli,項目名稱:ZhihuHelp__Python,代碼行數:11,代碼來源:initialbook.py

示例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
開發者ID:gitter-badger,項目名稱:EE-Book,代碼行數:25,代碼來源:initialbook.py

示例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
開發者ID:hmilyfyj,項目名稱:ZhihuHelp__Python,代碼行數:7,代碼來源:book.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:7,代碼來源:task_result.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:7,代碼來源:task_result.py

示例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
開發者ID:daoli,項目名稱:ZhihuHelp__Python,代碼行數:7,代碼來源:initialbook.py

示例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
開發者ID:gitter-badger,項目名稱:EE-Book,代碼行數:7,代碼來源:initialbook.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:7,代碼來源:task_result.py

示例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
開發者ID:EleVenPerfect,項目名稱:OTHERS,代碼行數:8,代碼來源:task_result.py


注:本文中的src.tools.db.DB類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。