Working with scores¶
A tested document¶
This is a tested document. The following instructions are used for initialization:
>>> import lino
>>> lino.startup('lino_prima.projects.prima1.settings')
>>> from lino.api.doctest import *
>>> settings.SITE.site_locale
'de_BE.UTF-8'
>>> from lino_prima.lib.ratings.utils import ScoreValue, RatingCollector, format_score
The format_score()
function prints a number with zero or one decimal place
as needed, and respecting the site_locale
.
>>> print(format_score(1))
1
>>> print(format_score(1.2))
1,2
>>> print(format_score(1.23))
1,2
>>> print(format_score(1.26))
1,3
>>> print(format_score(1.01))
1
>>> print(format_score(1.06))
1,1
>>> v1 = ScoreValue(8, 10)
>>> print(v1)
8/10
>>> print(v1.absolute)
8/10
>>> print(v1.relative)
80,0 %
>>> print(v1.rebase(20))
16/20
>>> v2 = ScoreValue(5, 20)
>>> print(f"{v1} + {v2} = {v1+v2}")
8/10 + 5/20 = 13/30
>>> tot = v1 + v2
>>> print(tot.relative)
43,3 %
>>> round(100*13/30, 1)
43.3
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
'en_US.UTF-8'
>>> print(tot.relative)
43.3 %
>>> print(tot)
13/30
>>> print(ScoreValue(5.29, 8))
5.3/8
>>> v3 = ScoreValue(5.24, 8)
>>> print(v3)
5.2/8
>>> print(v3*3)
15.7/24
>>> print(ScoreValue(1,0))
Traceback (most recent call last):
...
ValueError: score is higher than max_score!
>>> print(ScoreValue(-1,10))
Traceback (most recent call last):
...
ValueError: Negative scores aren't allowed!
>>> print(ScoreValue(None,10))
☐/10
>>> print(ScoreValue(None,10).rebase(20))
☐/20
In a boolean context, a ScoreValue
>>> print("yes" if ScoreValue(1,10) else "no")
yes
>>> print("yes" if ScoreValue() else "no")
no
>>> print("yes" if ScoreValue(None,10) else "no")
no
>>> print("yes" if ScoreValue(0,10) else "no")
yes
>>> tot = RatingCollector()
>>> tot.collect(8, 10)
>>> tot.collect(5, 20)
>>> print(tot)
13/30
>>> scores = []
>>> def add(score, max_score):
... scores.append(ScoreValue(score, max_score))
>>> add(9, 10)
>>> add(3, 12.5)
>>> add(4.5, 4.5)
>>> add(4, 4)
>>> add(8, 9)
>>> add(40.5, 50)
>>> print("; ".join(map(str, scores)))
9/10; 3/12.5; 4.5/4.5; 4/4; 8/9; 40.5/50
>>> total = sum(scores)
>>> print(total)
69/90
>>> print(total.relative)
76.7 %
>>> print(f"{total} = {total:%}")
69/90 = 76.7 %
>>> total = total.rebase(20)
>>> print(f"{total} = {total:%}")
15.3/20 = 76.5 %
Same story with another series of scores:
>>> scores = [ScoreValue(sc, ms) for sc, ms in (
... (23.5, 40), (6.5, 10), (10.5, 15), (6, 10), (14, 20),
... (6, 10), (6.5, 10), (4, 10))]
>>> total = sum(scores)
>>> print(f"{total} = {total:%}")
77/125 = 61.6 %
>>> total = total.rebase(20)
>>> print(f"{total} = {total:%}")
12.3/20 = 61.5 %
Don’t read this¶
The following feature has been removed:
>>> # tot.ratings
[<ScoreValue(8, 10)>, <ScoreValue(5, 20)>]
>>> # " + ".join(map(str, tot.ratings))
'8/10 + 5/20'