01:
02:
03: class EnumEntry(tuple):
04:
05:
06:
07:
08:
09:
10:
11:
12: def __str__(self):
13:
14: if self[1]:
15: return str(self[2])
16: else:
17: return str(self[0].sequence[self[2]])
18:
19: def __repr__(self):
20:
21: if self[1]:
22: return repr(self[2])
23: else:
24: return repr(self[0].sequence[self[2]])
25:
26: def __getattr__(self,attr):
27:
28: if self[1]:
29: return getattr(self[2],attr)
30: else:
31: return getattr(self[0].sequence[self[2]],attr)
32:
33: def canonical_tuple(self):
34: return self[1:]
35:
36: class Enum(object):
37:
38:
39: def __init__(
40: self,sequence=(),casesensitive=False,refilter=None,strict=False
41: ):
42: self.sequence=sequence
43: self.casesensitive=casesensitive
44: self.refilter=refilter
45: self.strict=strict
46: self.hashseed=reduce(lambda x,y: x^hash(y),sequence,0)
47: self.dictlowered=self.dictenum={}
48:
49: def create_dict(self):
50: for (i,e) in enumerate(self.sequence):
51: self.dictenum[e]=i
52: if not self.casesensitive:
53: for (i,e) in enumerate(self.sequence):
54: self.dictenum[e.lower()]=i
55:
56: class Enumeration(EnumEntry):
57:
58:
59: def __new__(Self,s,strict=False):
60: assert isinstance(Self.enum,Enum)
61: i=None
62: if isinstance(s,str) and Self.enum.refilter:
63: for (i,f) in Self.enum.refilter:
64: if f.match(s):
65: break
66: else:
67: i=None
68: elif isinstance(s,str) and Self.enum.dictlowered:
69: l=s.lower()
70: if l in Self.enum.dictlowered:
71: i=Self.enum.dictlowered[l]
72: elif Self.enum.dictenum:
73: if s in Self.enum.dictenum:
74: i=Self.enum.dictenum[s]
75: else:
76: try:
77: i=Self.enum.sequence.index(s)
78: except ValueError:
79: pass
80: if i<>None:
81: try:
82: Self.enumcache
83: except AttributeError:
84: Self.enumcache=[None]*len(Self.enum.sequence)
85: if not Self.enumcache[i]:
86: Self.enumcache[i]=EnumEntry.__new__(Self,(Self.enum,0,i))
87:
88: return Self.enumcache[i]
89: else:
90: if strict or Self.enum.strict:
91: raise ValueError
92: return EnumEntry.__new__(Self,(Self.enum,1,s))
93:
94:
PyScore
A badly written regatta scoring programme in Python and PyGtk.
register
relational
standing
tabulate
timesheet
treemodel
utility
Copyright 2004, M.E.J.Draisey
This file is part of pyscore.
pyscore is free software; you can redistribute it and/or modify it under
the terms of the GNU General
Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
pyscore is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyscore; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Formatted with
GNU source-highlight:
http:// www.gnu.org/ software/ src-highlite