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.
9 <pre>High(fname,limit=10)</pre>
12 <dt>fname <dd>filename to store high scores in
13 <dt>limit <dd>limit of scores to be recorded, defaults to 10
16 return Highs(fname
,limit
)['default']
19 def __init__(self
,score
,name
,data
=None):
20 self
.score
,self
.name
,self
.data
=score
,name
,data
23 """A high score table. These objects are passed to the user, but should not be created directly.
25 <p>You can iterate them:</p>
28 print e.score,e.name,e.data
31 <p>You can modify them:</p>
33 myhigh[0].name = 'Cuzco'
36 <p>You can find out their length:</p>
42 def __init__(self
,highs
,limit
=10):
48 """Save the high scores.
50 <pre>_High.save()</pre>
54 def submit(self
,score
,name
,data
=None):
55 """Submit a high score to this table.
57 <pre>_High.submit(score,name,data=None)</pre>
59 <p>return -- the position in the table that the score attained. None if the score did not attain a position in the table.</p>
64 self
._list
.insert(n
,_Score(score
,name
,data
))
65 self
._list
= self
._list
[0:self
.limit
]
68 if len(self
._list
) < self
.limit
:
69 self
._list
.append(_Score(score
,name
,data
))
70 return len(self
._list
)-1
72 def check(self
,score
):
73 """Check if a score will attain a position in the table.
75 <pre>_High.check(score)</pre>
77 <p>return -- the position the score will attain, else None</p>
84 if len(self
._list
) < self
.limit
:
85 return len(self
._list
)
89 return self
._list
.__iter
__()
91 def __getitem__(self
,key
):
92 return self
._list
[key
]
95 return self
._list
.__len
__()
99 """The high score object.
101 <pre>Highs(fname,limit=10)</pre>
103 <dt>fname <dd>filename to store high scores in
104 <dt>limit <dd>limit of scores to be recorded, defaults to 10
107 <p>You may access _High objects through this object:</p>
110 my_easy_hs = highs['easy']
111 my_hard_hs = highs['hard']
115 def __init__(self
,fname
,limit
=10):
121 """Re-load the high scores.
123 <pre>Highs.load()</pre>
129 for line
in f
.readlines():
130 key
,score
,name
,data
= line
.strip().split("\t")
131 if key
not in self
._dict
:
132 self
._dict
[key
] = _High(self
,self
.limit
)
133 high
= self
._dict
[key
]
134 high
.submit(int(score
),name
,data
)
140 """Save the high scores.
142 <pre>Highs.save()</pre>
145 f
= open(self
.fname
,"w")
146 for key
,high
in self
._dict
.items():
148 f
.write("%s\t%d\t%s\t%s\n"%(key
,e
.score
,e
.name
,str(e
.data
)))
151 def __getitem__(self
,key
):
152 if key
not in self
._dict
:
153 self
._dict
[key
] = _High(self
,self
.limit
)
154 return self
._dict
[key
]