1 """Classes for handling high score tables.
6 def High(fname,limit=10):
7 """Create a Highs object and returns the default high score table.
10 fname -- filename to store high scores in
11 limit -- limit of scores to be recorded, defaults to 10
14 return Highs(fname,limit)['default']
17 def __init__(self,score,name,data=None):
18 self.score,self.name,self.data=score,name,data
21 """A high score table. These objects are passed to the user, but should
22 not be created directly.
26 print(e.score,e.name,e.data)
29 myhigh[0].name = 'Cuzco'
31 You can find out their length:
35 def __init__(self,highs,limit=10):
41 """Save the high scores."""
44 def submit(self,score,name,data=None):
45 """Submit a high score to this table.
47 Return -- the position in the table that the score attained. None if
48 the score did not attain a position in the table.
54 self._list.insert(n,_Score(score,name,data))
55 self._list = self._list[0:self.limit]
58 if len(self._list) < self.limit:
59 self._list.append(_Score(score,name,data))
60 return len(self._list)-1
62 def check(self,score):
63 """Check if a score will attain a position in the table.
65 Return -- the position the score will attain, else None
73 if len(self._list) < self.limit:
74 return len(self._list)
78 return self._list.__iter__()
80 def __getitem__(self,key):
81 return self._list[key]
84 return self._list.__len__()
88 """The high score object.
91 fname -- filename to store high scores in
92 limit -- limit of scores to be recorded, defaults to 10
94 You may access _High objects through this object:
96 my_easy_hs = highs['easy']
97 my_hard_hs = highs['hard']
100 def __init__(self,fname,limit=10):
106 """Re-load the high scores."""
111 for line in f.readlines():
112 key,score,name,data = line.strip().split("\t")
113 if key not in self._dict:
114 self._dict[key] = _High(self,self.limit)
115 high = self._dict[key]
116 high.submit(int(score),name,data)
122 """Save the high scores."""
124 f = open(self.fname,"w")
125 for key,high in self._dict.items():
127 f.write("%s\t%d\t%s\t%s\n"%(key,e.score,e.name,str(e.data)))
130 def __getitem__(self,key):
131 if key not in self._dict:
132 self._dict[key] = _High(self,self.limit)
133 return self._dict[key]