Skip to content

Commit

Permalink
数据库导入示例
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Feb 23, 2020
1 parent db7981b commit 9b4a988
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 90 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Django + MySQL + Pug + JS

### 我还缺什么

- 缺单词库啊,还用说吗!
- 缺单词库啊,还用说吗!
不过网络这么发达,我要的单词书基本都能找到电子清单(๑•̀ㅂ•́)و✧
<!-- - 有些人可能会说缺了图,像百次斩那种 -->
- ~~缺背单词的毅力~~
Expand Down
2 changes: 1 addition & 1 deletion apps/review/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Review(models.Model):
total_num = models.IntegerField('复习总次数', default=0)
forget_num = models.IntegerField('忘记次数', default=0)
rate = models.FloatField(
'单词遗忘率', default=None, null=True)
'单词遗忘率', default=-1, null=False)
LIST = models.IntegerField('list', default=0)
UNIT = models.IntegerField('unit', default=0)
INDEX = models.IntegerField('index', default=0)
Expand Down
66 changes: 0 additions & 66 deletions apps/review/src/import_excel.py

This file was deleted.

68 changes: 51 additions & 17 deletions apps/review/src/init_db.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
import pandas as pd

bookName = 'aBook' # 请用英文
List_begin_num = 0 # 或 1,看你的 List 是从 0 开始还是 1 开始

'''
如果是 excel/csv,你应该有以下列
word 单词
mean 中文
List
Unit
Index
'''


def import_word(Review, BookList, Words):
path = 'path/to/word.xlsx'
df = pd.read_excel(path)
for i in range(0, (len(df))):
dr = df.iloc[i]
review_db = {
'word': dr['word'],
'LIST': dr['List'],
'UNIT': dr['Unit'],
'INDEX': dr['Index'],
'BOOK': bookName,
}
print(i, dr['word'], dr['mean'])

new_word = Review.objects.create(**review_db)
new_word.save()

# init_db_booklist(BookList, Review)
# init_db_word(Review, Words)


def init_db_booklist(BookList, Review):
for BOOK in ['GRE3000', 'qugen10000']:
for l in range(len(set(Review.objects.filter(BOOK=BOOK).values_list('LIST')))):
ld = Review.objects.filter(BOOK=BOOK, LIST=l) # list data
print(l)
rate = sum([r[0] if r[0] is not None else 1 for r in ld.values_list('rate')
]) / len(ld) * 1
rate = 1 - rate if rate != 0.0 else 0
data = {
'LIST': l,
'BOOK': BOOK,
'list_rate': rate,
'word_num': len(ld),
'review_word_counts': ';'.join(
set([str(t[0]) for t in ld.values_list('total_num')])),
}
BookList.objects.create(**data)
for l in range(List_begin_num, len(set(Review.objects.filter(BOOK=bookName).values_list('LIST')))):
ld = Review.objects.filter(BOOK=bookName, LIST=l) # list data
print(l)
rate = sum([r[0] if r[0] is not None else 1 for r in ld.values_list('rate')
]) / len(ld) * 1
rate = 1 - rate if rate != 0.0 else 0
data = {
'LIST': l,
'BOOK': bookName,
'list_rate': rate,
'word_num': len(ld),
'review_word_counts': ';'.join(
set([str(t[0]) for t in ld.values_list('total_num')])),
}
BookList.objects.create(**data)


def init_db_word(Review, Words):
Expand Down Expand Up @@ -43,4 +78,3 @@ def init_db_word(Review, Words):
word.rate = word.forget_num / word.total_num
print(word.word)
word.save()
# break
8 changes: 3 additions & 5 deletions apps/review/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

from apps.src.util import ormToJson, valueList

# from apps.review.src.import_excel import import_qugen
from apps.review.src.init_db import init_db_word
# from apps.review.src.init_db import import_word, init_db_word, init_db_booklist(BookList, Review)

from datetime import datetime, timedelta

Expand All @@ -21,10 +20,9 @@ def index(request):


def temp(request):
# import_word(Review, BookList, Words)
# init_db_booklist(BookList, Review)
# init_db_word(Review, Words)
# import_qugen(Review)
# rev = Review.objects.filter(BOOK='GRE3000')
# print(len(rev))
return render(request, "calendar.pug")


Expand Down

0 comments on commit 9b4a988

Please sign in to comment.