Skip to content

Commit ed442e1

Browse files
committed
初始化代码
0 parents  commit ed442e1

File tree

185 files changed

+131742
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

185 files changed

+131742
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__/
2+
.DS_Store
3+
.idea/

chapter_10/QASystem/Pipfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[[source]]
2+
3+
url = "https://pypi.python.org/simple"
4+
verify_ssl = true
5+
name = "pypi"
6+
7+
8+
[packages]
9+
10+
flask = "*"
11+
redis = "*"
12+
pymongo = "*"
13+
14+
15+
[dev-packages]
16+

chapter_10/QASystem/Pipfile.lock

Lines changed: 131 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import pymongo
2+
import json
3+
from bson import ObjectId
4+
5+
class MongoUtil(object):
6+
def __init__(self):
7+
db = pymongo.MongoClient().qa_system
8+
self.question = db.question
9+
self.answer = db.answer
10+
11+
def query_question(self):
12+
question_iter_obj = self.question.aggregate([
13+
{'$lookup': {
14+
'from': 'answer',
15+
'localField': '_id',
16+
'foreignField': 'question_id',
17+
'as': 'answer_list'}}])
18+
19+
question_list = []
20+
for question in question_iter_obj:
21+
question_list.append(
22+
{'title': question['title'],
23+
'detail': question['detail'],
24+
'author': question['author'],
25+
'vote_up': question['vote_up'] - question['vote_down'],
26+
'answer_number': len(question['answer_list']),
27+
'question_id': str(question['_id'])
28+
}
29+
)
30+
return question_list
31+
32+
def query_answer(self, question_id):
33+
answer_iter_obj = self.question.aggregate([
34+
{'$match': {'_id': ObjectId(question_id)}},
35+
{'$lookup': {
36+
'from': 'answer',
37+
'localField': '_id',
38+
'foreignField': 'question_id',
39+
'as': 'answer_list'}}])
40+
question_answer = list(answer_iter_obj)[0]
41+
question_answer_dict = {
42+
'question_id': str(question_answer['_id']),
43+
'question_title': question_answer['title'],
44+
'question_detail': question_answer['detail'],
45+
'question_author': question_answer['author'],
46+
'answer_num': len(question_answer['answer_list'])
47+
}
48+
answer_list = []
49+
for answer in question_answer['answer_list']:
50+
answer_list.append(
51+
{'answer_detail': answer['answer'],
52+
'answer_author': answer['author'],
53+
'answer_id': str(answer['_id']),
54+
'answer_vote': answer['vote_up'] - answer['vote_down']})
55+
question_answer_dict['answer_list'] = answer_list
56+
return question_answer_dict
57+
58+
def insert_answer(self, question_id, answer, author, now, vote_up=0, vote_down=0):
59+
data_to_insert = {
60+
'author': author,
61+
'question_id': ObjectId(question_id),
62+
'answer': answer,
63+
'answer_time': now,
64+
'vote_up': vote_up,
65+
'vote_down': vote_down
66+
}
67+
self.answer.insert_one(data_to_insert)
68+
return True
69+
70+
def insert_question(self, title, detail, author, now, vote_up=0, vote_down=0):
71+
data_to_insert = {
72+
'title': title,
73+
'detail': detail,
74+
'author': author,
75+
'ask_time': now,
76+
'vote_up': vote_up,
77+
'vote_down': vote_down
78+
}
79+
self.question.insert_one(data_to_insert)
80+
return True
81+
82+
def vote_for_question(self, object_id, value):
83+
self.question.update_one({'_id': ObjectId(object_id)}, {'$inc': {value: 1}})
84+
return True
85+
86+
def vote_for_answer(self, object_id, value):
87+
self.answer.update_one({'_id': ObjectId(object_id)}, {'$inc': {value: 1}})

chapter_10/QASystem/answer/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.
2+
├── Pipfile
3+
├── Pipfile.lock
4+
├── answer
5+
│   ├── MongoUtil.py
6+
│   └── __init__.py
7+
├── generate_answer.py
8+
├── generate_question.py
9+
├── main.py
10+
├── static
11+
│   ├── css
12+
│   │   ├── spectre-icons.css
13+
│   │   └── spectre.min.css
14+
│   ├── img
15+
│   │   └── avatar.png
16+
│   └── js
17+
│   ├── jquery-3.3.1.min.js
18+
│   └── post_question_and_answer.js
19+
├── templates
20+
│   ├── answer_list.html
21+
│   ├── base.html
22+
│   └── index.html
23+
├── util
24+
│   ├── __init__.py
25+
│   └── utils.py
26+
└── your_code_here
27+
├── MongoUtil.py
28+
└── __init__.py
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import pymongo
2+
from bson import ObjectId
3+
4+
handler = pymongo.MongoClient().qa_system.answer
5+
answer = [
6+
{'author': '王小一',
7+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc23"),
8+
'answer': '毫无疑问,必定为2',
9+
'answer_time': '2018-07-23 12:18:11',
10+
'vote_up': 10,
11+
'vote_down': 200},
12+
{'author': '钱小九',
13+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc23"),
14+
'answer': '楼上的,自问自答有意思吗?',
15+
'answer_time': '2018-07-23 12:18:11',
16+
'vote_up': 100,
17+
'vote_down': 20},
18+
{'author': '孙小十',
19+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc23"),
20+
'answer': '在某些情况下可能等于三。题主应该补充必要信息。',
21+
'answer_time': '2018-07-23 12:18:11',
22+
'vote_up': 0,
23+
'vote_down': 100},
24+
{'author': '张若虚',
25+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc26"),
26+
'answer': '这是我写的诗,下一句是:愿逐月华流照君。诗名为:《春江花月夜》',
27+
'answer_time': '2018-07-23 12:18:11',
28+
'vote_up': 1000,
29+
'vote_down': 0},
30+
{'author': '阿东莫夫',
31+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc24"),
32+
'answer': '这是阿西莫夫的《银河系漫游指南》里面的',
33+
'answer_time': '2018-07-23 12:18:11',
34+
'vote_up': 20,
35+
'vote_down': 21},
36+
{'author': '道格拉斯',
37+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc24"),
38+
'answer': '楼上的别乱说啊,《银河系漫游指南》是英国作家道格拉斯·亚当斯写的。',
39+
'answer_time': '2018-07-23 12:18:11',
40+
'vote_up': 60,
41+
'vote_down': 10},
42+
{'author': '张起灵',
43+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc24"),
44+
'answer': '是英国作家道格拉斯·亚当斯写的《银河系漫游指南》里面的原话。原因是42在ASCII码中对应了*号,而*号在通配符中代表一切',
45+
'answer_time': '2018-07-23 12:18:11',
46+
'vote_up': 200,
47+
'vote_down': 1},
48+
{'author': '老司机',
49+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc29"),
50+
'answer': '楼主,这不是去幼儿园的车!我只说作者是兰陵笑笑生,至于书名嘛,哈哈哈哈哈。',
51+
'answer_time': '2018-07-23 12:18:11',
52+
'vote_up': 0,
53+
'vote_down': 100},
54+
{'author': '小文青',
55+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc29"),
56+
'answer': '警察叔叔,这里有人在飙车。我认为兰陵笑笑生应该说王世贞。至于书名嘛,哈哈哈哈哈。',
57+
'answer_time': '2018-07-23 12:18:11',
58+
'vote_up': 30,
59+
'vote_down': 0},
60+
{'author': '欧阳掏粪',
61+
'question_id': ObjectId("5b8b8fb9d3a25054b7a0dc29"),
62+
'answer': '《明朝那些事儿》里面讲了的,你去看看。',
63+
'answer_time': '2018-07-23 12:18:11',
64+
'vote_up': 40,
65+
'vote_down': 3},
66+
]
67+
68+
handler.insert_many(answer)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import pymongo
2+
3+
4+
handler = pymongo.MongoClient().qa_system.question
5+
question = [
6+
{'author': '王小一',
7+
'title': '1+1=?',
8+
'detail': '请问1+1等于几?',
9+
'ask_time': '2018-07-23 12:18:11',
10+
'vote_up': 0,
11+
'vote_down': 100},
12+
{'author': '张小二',
13+
'title': '为什么说42 is the answer of all?',
14+
'detail': '这句话出自哪里?',
15+
'ask_time': '2018-07-23 12:18:11',
16+
'vote_up': 100,
17+
'vote_down': 0},
18+
{'author': '刘小三',
19+
'title': '明天天气如何?',
20+
'detail': '明天会下雨吗?',
21+
'ask_time': '2018-07-23 12:18:11',
22+
'vote_up': 10,
23+
'vote_down': 10},
24+
{'author': '旺小四',
25+
'title': '此时相忘不相闻下一句是什么?',
26+
'detail': '还有,这是谁写的诗?',
27+
'ask_time': '2018-07-23 12:18:11',
28+
'vote_up': 100,
29+
'vote_down': 3},
30+
{'author': '赵小五',
31+
'title': '把微波炉温度调低一些,可以孵鸡蛋吗?',
32+
'detail': '孵蛋除了温度还需要什么?',
33+
'ask_time': '2018-07-23 12:18:11',
34+
'vote_up': 23,
35+
'vote_down': 3},
36+
{'author': '朱小六',
37+
'title': '四大名著你喜欢哪一本?',
38+
'detail': '请回答具体原因。',
39+
'ask_time': '2018-07-23 12:18:11',
40+
'vote_up': 70,
41+
'vote_down': 2},
42+
{'author': '马小七',
43+
'title': '你知道明朝时期的四大名著,除了《西游记》《水浒传》和《三国演义》还有一本是什么吗?',
44+
'detail': '这本书的作者又是是呢?',
45+
'ask_time': '2018-07-23 12:18:11',
46+
'vote_up': 120,
47+
'vote_down': 16},
48+
]
49+
50+
handler.insert_many(question)

0 commit comments

Comments
 (0)