forked from plazar/Ratings2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rating_value.py
65 lines (53 loc) · 1.99 KB
/
rating_value.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
import re
class RatingValue(object):
ratval_re = re.compile(r"Rating name \(version\): (?P<name>.*?) " \
r"\(v(?P<version>\d+?)\)\n" \
r"Description: (?P<descr>.*?)\n" \
r"Value: (?P<value>.*?)$", \
flags=re.MULTILINE | re.DOTALL)
def __init__(self, name, version, description, value):
self.name = name
self.version = version
self.description = description
self.value = value
if self.value is None:
self.valuestr = "%s" % self.value
else:
self.valuestr = "%.12g" % self.value
def __str__(self):
text = "Rating name (version): %s (v%d)\n" % (self.name, self.version)
text += "Description: %s\n" % self.description
text += "Value: %s" % self.valuestr
return text
def get_short_string(self):
return "%s (v%d): %s" % (self.name, self.version, self.valuestr)
def parse_string(string):
"""Parse a string for rating values and return a list
of RatingValue objects.
Input:
string: The string to parse.
Output:
ratvals: A list of RatingValue objects gleaned from the string.
"""
matches = RatingValue.ratval_re.finditer(string)
ratvals = []
for match in matches:
grps = match.groupdict()
if grps['value'] == "None":
value = None
else:
value = float(grps['value'])
ratvals.append(RatingValue(grps['name'], int(grps['version']), \
grps['descr'], value))
return ratvals
def read_file(ratfilenm):
"""Read a rating file, and return the rating values parsed from it.
Input:
ratfilenm: Name of the *.rat file.
Output:
ratvals: A list of RatingValue objects parsed from the file.
"""
f = open(ratfilenm, 'r')
contents = f.read()
f.close()
return parse_string(contents)