2b50c0a934906506797fc1cef1b6e50f819212a4
[matches/honours.git] / research / transmission_spectroscopy / simulator / pgu-0.18 / build / lib / pgu / high.py
1 """Classes for handling high score tables.
2 """
3
4 import os
5
6 def High(fname,limit=10):
7     """Create a Highs object and returns the default high score table.
8
9     Arguments:    
10         fname -- filename to store high scores in
11         limit -- limit of scores to be recorded, defaults to 10
12
13     """
14     return Highs(fname,limit)['default']
15     
16 class _Score:
17     def __init__(self,score,name,data=None):
18         self.score,self.name,self.data=score,name,data
19     
20 class _High:
21     """A high score table.  These objects are passed to the user, but should 
22     not be created directly.
23     
24     You can iterate them:
25         for e in myhigh:
26             print(e.score,e.name,e.data)
27         
28     You can modify them:
29         myhigh[0].name = 'Cuzco'
30     
31     You can find out their length:
32         print(len(myhigh))
33     """
34     
35     def __init__(self,highs,limit=10):
36         self.highs = highs
37         self._list = []
38         self.limit = limit
39         
40     def save(self):
41         """Save the high scores."""
42         self.highs.save()
43         
44     def submit(self,score,name,data=None):
45         """Submit a high score to this table.
46
47         Return -- the position in the table that the score attained.  None if
48         the score did not attain a position in the table.
49
50         """
51         n = 0
52         for e in self._list:
53             if score > e.score:
54                 self._list.insert(n,_Score(score,name,data))
55                 self._list = self._list[0:self.limit]
56                 return n
57             n += 1
58         if len(self._list) < self.limit:
59             self._list.append(_Score(score,name,data))
60             return len(self._list)-1
61     
62     def check(self,score):
63         """Check if a score will attain a position in the table.
64         
65         Return -- the position the score will attain, else None
66
67         """
68         n = 0
69         for e in self._list:
70             if score > e.score:
71                 return n
72             n += 1
73         if len(self._list) < self.limit:
74             return len(self._list)
75         
76         
77     def __iter__(self):
78         return self._list.__iter__()
79         
80     def __getitem__(self,key):
81         return self._list[key]
82         
83     def __len__(self):
84         return self._list.__len__()
85         
86
87 class Highs:
88     """The high score object.
89
90     Arguments:    
91         fname -- filename to store high scores in
92         limit -- limit of scores to be recorded, defaults to 10
93     
94     You may access _High objects through this object:
95
96         my_easy_hs = highs['easy']
97         my_hard_hs = highs['hard']
98     
99     """
100     def __init__(self,fname,limit=10):
101         self.fname = fname
102         self.limit = limit
103         self.load()
104         
105     def load(self):
106         """Re-load the high scores."""
107         
108         self._dict = {}
109         try:
110             f = open(self.fname)
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)
117             f.close()
118         except:
119             pass
120     
121     def save(self):
122         """Save the high scores."""
123         
124         f = open(self.fname,"w")
125         for key,high in self._dict.items():
126             for e in high:
127                 f.write("%s\t%d\t%s\t%s\n"%(key,e.score,e.name,str(e.data)))
128         f.close()
129         
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]
134

UCC git Repository :: git.ucc.asn.au