-
Notifications
You must be signed in to change notification settings - Fork 0
/
rankHelperDb.py
111 lines (91 loc) · 2.58 KB
/
rankHelperDb.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import string
class rankHelperDb:
def __init__(self):
self.ranks = list()
self.nets = list()
self.rankBuddies = list()
def loadNets(self, netsFile):
file = open(netsFile)
netsLines = file.readlines()
file.close()
for line in netsLines:
self.rankBuddies.append(set())
thisNet = list()
line = line.rstrip().split(",")
for nickname in line:
thisNet.append(nickname.lower())
self.nets.append(thisNet)
def loadRanks(self, ranksFile):
file = open(ranksFile)
ranksLines = file.readlines()
file.close()
rankSet = set()
for rank in ranksLines:
rank = rank.rstrip().split(",")
rank = [ int(stringNum) for stringNum in rank ]
self.addRankToRanks(rank)
self.addRankToRankBuddies(rank)
def getNetId(self, name):
name = name.lower()
index = None
for i in range(0, len(self.nets)):
net = self.nets[i]
if name in net:
index = i
return index
def checkValidName(self, name):
numOccurrences = 0
name = name.lower()
for i in range(0, len(self.nets)):
net = self.nets[i]
if name in net:
numOccurrences += 1
if numOccurrences == 1:
return True
else:
return False
def newRank(self, args):
rankInNums = self.convertRankToNums(args)
self.addRankToRanks(rankInNums)
self.addRankToRankBuddies(rankInNums)
print self.ranks
print self.rankBuddies
def convertRankToNums(self, args):
rankInNums = set()
for name in args:
rankInNums.add(self.getNetId(name))
return rankInNums
def addRankToRanks(self, rankInNums):
thisRank = set()
for netId in rankInNums:
thisRank.add(netId)
self.ranks.append(thisRank)
def addRankToRankBuddies(self, rankInNums):
for netId in rankInNums:
self.rankBuddies[netId].update(rankInNums)
def writeRanksToFile(self, ranksFilename):
ranksFile = open(ranksFilename, 'w') #Currently overwrites the entire file each time
for rank in self.ranks:
rankString = ','.join(map(str, rank)) #Make into a comma-separated string
ranksFile.write(rankString + "\n")
ranksFile.close()
def printRanks(self):
for rank in self.ranks:
rankString = ','.join(map(str, rank))
print rankString
def printNets(self):
for net in self.nets:
netString = ", ".join(map(str, net))
print netString
def checkRank(self, potentialRank):
potentialRankInNums = self.convertRankToNums(potentialRank)
for net1 in potentialRankInNums:
for net2 in potentialRankInNums:
if net1 != net2 and net2 in self.rankBuddies[net1]:
return False
return True
if __name__ == '__main__':
db = rankHelperDb()
db.loadNets("netFile.csv")
if db.checkValidName("jen"):
print db.getNetId("jen")