-
Notifications
You must be signed in to change notification settings - Fork 0
/
rw_method.py
81 lines (66 loc) · 2.58 KB
/
rw_method.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import csv
import numpy as np
class QA:
def __init__(self, filename):
self.answers = []
self.questions = []
self.index = []
self.read_csv = None
self.filename = filename
self.load()
def add(self, question, answer):
self.index = np.append(self.index, str(int(self.index[-1]) + 1))
self.questions = np.append(self.questions, question)
self.answers = np.append(self.answers, answer)
def edit(self, index, question, answer):
v = np.where(self.index == index)[0]
self.questions[v] = question
self.answers[v] = answer
def delete(self, index):
v = np.where(self.index == index)[0]
self.index = np.delete(self.index, v)
self.questions = np.delete(self.questions, v)
self.answers = np.delete(self.answers, v)
def renumber(self):
m = 0
while self.index[m] == '0':
m += 1
self.index[m:] = np.array([str(i) for i in range(m, len(self.index) + 1)])
def list_copy(self):
return self.index[4:].copy(), self.questions[4:].copy(), self.answers[4:].copy()
def load(self):
with open(self.filename) as qa:
self.read_csv = np.array(list(csv.reader(qa)))
self.index = self.read_csv[1:, 0]
self.questions = self.read_csv[1:, 1]
self.answers = self.read_csv[1:, 2]
def save(self):
np.savetxt(self.filename, np.array([self.index, self.questions, self.answers]).T, delimiter=",",
header='序号,问,答',
comments="", encoding='utf-8', fmt='%s')
class User:
def __init__(self, filename):
self.filename = filename
self.index = None
self.users = None
self.permissions = None
self.read_csv = None
self.load()
def load(self):
with open(self.filename) as user:
self.read_csv = np.array(list(csv.reader(user)))
self.index = self.read_csv[1:, 0]
self.users = self.read_csv[1:, 1]
self.permissions = self.read_csv[1:, 2]
def get_permission(self, user):
if user in self.users:
return self.permissions[self.users == user]
else:
return '0'
def change_permission(self, user, permission):
if user not in self.users:
self.index = np.append(self.index, str(int(self.index[-1]) + 1))
self.users = np.append(self.users, user)
self.permissions = np.append(self.permissions, permission)
else:
self.permissions[self.users == user] = permission