Exemplo n.º 1
0
class Cleaner(object):
    def __init__(self):
        self.initLogger()
        self.db = QuestionsDb('XFQuestionsLib.db')

    def initLogger(self):
        self.logger = logging.getLogger('Assistant')
        self.logger.setLevel(level=logging.INFO)
        handler = logging.FileHandler("log.txt")
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        handler.setFormatter(formatter)
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        console.setFormatter(formatter)
        self.logger.addHandler(console)
        self.logger.addHandler(handler)

    def deleteAQuestion(self, ques_id):
        self.logger.info('delete question id {}!'.format(ques_id))
        self.db.deleteAQues(ques_id)

    def quesRecord(self, ques_id):
        return self.db.questionRecords(ques_id)
Exemplo n.º 2
0
 def __init__(self):
     self.initLogger()
     self.examiner = Examiner()
     self.sec = Secretary()
     self.clr = Cleaner()
     self.questionsDb = QuestionsDb('XFQuestionsLib.db')
     self.login()
     self.init()
Exemplo n.º 3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : AddQuestions.py
# @Author: Donglai Chen
# @Date  : 2018/9/23
# @Desc  : 批量增加问题

import csv
from questionsDbMbms import QuestionsDb
import pprint

if __name__ == '__main__':
    db = QuestionsDb('XFQuestionsLib.db')
    # db.addTables()
    with open("quesInput.csv", "r") as csvFile:
        dict_reader = csv.DictReader(csvFile)
        for i in dict_reader:
            if '' != i['question'] and '' != i['answer']:
                print('input question {} answer {}'.format(
                    i['question'], i['answer']))
                db.insertAQues(i['question'], i['answer'])
    print('Questions:')
    pprint.pprint(db.tableData('Questions'))
    # print('\nTests:')
    # pprint.pprint(db.tableData('Tests'))
Exemplo n.º 4
0
 def __init__(self):
     self.initLogger()
     self.db = QuestionsDb('XFQuestionsLib.db')
Exemplo n.º 5
0
class Secretary(object):
    def __init__(self):
        self.initLogger()
        self.db = QuestionsDb('XFQuestionsLib.db')
        self.clear()

    def initLogger(self):
        self.logger = logging.getLogger('Assistant')
        self.logger.setLevel(level=logging.INFO)
        handler = logging.FileHandler("log.txt", encoding='utf-8')
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        handler.setFormatter(formatter)
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        console.setFormatter(formatter)
        self.logger.addHandler(console)
        self.logger.addHandler(handler)

    def recMsg(self, msg):
        self.logger.info('Status {} rec {}'.format(self.status, msg))
        ret = ''
        if self.status == 'WaitQues':
            self.inputQuestion(msg)
            ret = 'Please input answer.'
        elif self.status == 'WaitAnsw':
            self.inputAnswer(msg)
            ret = self.questionContent(
            ) + '\nPress c to clear, press any other key to input next question!'
        elif self.status == 'WaitCommit':
            if 'c' != msg:
                self.commit()
                ret += 'Question commited! '
            self.clear()
            ret += 'Please input next question:'
        return ret

    def clear(self):
        self.logger.info('clear')
        self.question = {}
        self.status = 'WaitQues'

    def inputQuestion(self, ques):
        self.logger.info('input ques: {}'.format(ques))
        self.question['ques'] = ques
        self.status = 'WaitAnsw'

    def inputAnswer(self, answ):
        self.logger.info('input answ: {}'.format(answ))
        self.question['answ'] = answ
        self.status = 'WaitCommit'

    def commit(self):
        self.logger.info('commit')
        self.db.insertAQues(self.question['ques'], self.question['answ'])

    def questionContent(self):
        return str(self.question)
Exemplo n.º 6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : AddQuestions.py
# @Author: Donglai Chen
# @Date  : 2018/9/23
# @Desc  : 批量增加问题

import csv
from questionsDbMbms import QuestionsDb
import pprint

if __name__ == '__main__':
    db = QuestionsDb('test.db')
    db.addTables()
    with open("quesInput.csv", "r") as csvFile:
        dict_reader = csv.DictReader(csvFile)
        for i in dict_reader:
            print('input question {} answer {}'.format(i['question'],i['answer']))
            db.insertAQues(i['question'],i['answer'])
    print('Questions:')
    pprint.pprint(db.tableData('Questions'))
    print('\nTests:')
    pprint.pprint(db.tableData('Tests'))
Exemplo n.º 7
0
class Examiner(object):
    ''''
    考官的类
    '''
    def __init__(self):
        self.initLogger()
        self.db = QuestionsDb('XFQuestionsLib.db')
        self.init()

    def init(self):
        self.status = 'NULL'

    def initLogger(self):
        self.logger = logging.getLogger('Assistant')
        self.logger.setLevel(level=logging.INFO)
        handler = logging.FileHandler("log.txt", encoding='utf-8')
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        handler.setFormatter(formatter)
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        console.setFormatter(formatter)
        self.logger.addHandler(console)
        self.logger.addHandler(handler)

    def ask(self):
        '''
        推送一条新的问题给用户
        :return:
        '''
        self.question = self.db.question()
        if None == self.question:
            self.logger.info('There is NO question to ask!')
            self.status = 'NULL'
            return 'Examiner: There is NO question to ask!'
        else:
            self.status = 'WaitAnsw'
            return 'No.'+str(self.question[0])+' '+self.question[1] + '\n\nThere are still '\
                   +str(self.db.examQuestionNum())+' questions.'

    def correct(self, answ):
        reply = ''
        if answ != self.question[2]:
            self.logger.info('Correct Wrong!')
            reply += ('Wrong! The correct answer is: \n' + self.question[2])
            self.db.updateQuestion(self.question[0], 'FAIL', self.question[4])
        else:
            self.logger.info('Correct Right!')
            reply += 'Right! Congratulation!'
            self.db.updateQuestion(self.question[0], 'SUCC', self.question[4])
        reply += ('\n\n' + self.ask())
        return reply

    def recMsg(self, msg):
        self.logger.info('Status {} rec {}'.format(self.status, msg))
        ret = ''
        if self.status == 'WaitAnsw' and msg != '':
            ret = self.correct(msg)
        return ret
Exemplo n.º 8
0
class Assistant(object):
    def __init__(self):
        self.initLogger()
        self.examiner = Examiner()
        self.sec = Secretary()
        self.clr = Cleaner()
        self.questionsDb = QuestionsDb('XFQuestionsLib.db')
        self.login()
        self.init()

    def init(self):
        self.proc = 'ProcNull'
        self.examiner.init()
        self.sec.clear()
        self.sendYq('System init!')

    def login(self):
        itchat.auto_login()
        #itchat.auto_login(hotReload=True)
        self.yq = itchat.search_friends(name='陈有钱')[0]

    def initLogger(self):
        self.logger = logging.getLogger('Assistant')
        self.logger.setLevel(level=logging.INFO)
        handler = logging.FileHandler("log.txt", encoding='utf-8')
        handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        handler.setFormatter(formatter)
        console = logging.StreamHandler()
        console.setLevel(logging.INFO)
        formatter = logging.Formatter(
            '%(asctime)s - %(name)s - %(filename)s - %(levelname)s - %(funcName)s - %(message)s'
        )
        console.setFormatter(formatter)
        self.logger.addHandler(console)
        self.logger.addHandler(handler)

    def sendYq(self, msg):
        if str(msg) != '':
            self.logger.info('sendYq: ' + str(msg))
            self.yq.send(str(msg))

    def command(self, cmd):
        self.logger.info('rec cmd: ' + cmd)
        if cmd == 'exam':
            self.proc = 'ProcExam'  #开始测试
            self.sendYq('Begin exam:')
            self.sendYq(self.examiner.ask())
        elif cmd == 'createQuestions':
            self.sendYq('Begin creating questions')
            self.proc = 'ProcCreateQuestions'  #编辑问题
        elif cmd == 'init':
            self.init()
        elif cmd.startswith('deleteQuestions '):
            self.clr.deleteAQuestion(cmd.replace('deleteQuestions ', ''))
            self.sendYq('question deleted.')
        elif cmd.startswith('questionRecord '):
            self.sendYq(self.clr.quesRecord(cmd.replace('questionRecord ',
                                                        '')))
        elif cmd == 'questionCount':
            self.sendYq(self.questionsDb.questionCount())
        else:
            self.logger.error('Sorry, I don`t known the meaning of: ' + cmd)
            self.yq.send('Sorry, I don`t known the meaning of: ' + cmd)

    def exam(self):
        q = self.examiner.ask()
        if 'Examiner: There is NO question to ask!' != q:
            self.sendYq(self.examiner.ask())
            self.status = 'EXAM'
        else:
            self.status = 'NULL'

    def process(self, msg):
        if self.proc == 'ProcExam':
            self.sendYq(self.examiner.recMsg(msg))
        elif self.proc == 'ProcCreateQuestions':
            self.sendYq(self.sec.recMsg(msg))

    def receiveMsg(self, msg):
        self.logger.info('Received {} from {}: '.format(
            msg.text, msg['User']['NickName']))
        if msg['User']['NickName'] == '陈有钱':
            if msg.text.startswith('cmd:'):
                self.command(msg.text.replace('cmd:', ''))
            else:
                self.process(msg.text)