-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_utilities.py
More file actions
51 lines (41 loc) · 1.61 KB
/
format_utilities.py
File metadata and controls
51 lines (41 loc) · 1.61 KB
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
"""
This module contains functions which are used by the output_formats tool.
"""
import os
import yaml
import grade_reflect_loop
from functools import lru_cache
@lru_cache(maxsize=None)
def load_output_formats_yaml():
with open( 'output_formats.yaml', encoding='utf-8' ) as f:
return yaml.load(f, Loader=yaml.FullLoader)
def get_config_for( file ):
"""
Returns the config for the given file for the output_formats tool.
"""
output_formats_yaml = load_output_formats_yaml()
if os.path.splitext(file)[0] in output_formats_yaml['configs']:
return output_formats_yaml['configs'][os.path.splitext(file)[0]]
return None
def get_sorted_verses( translation_data, reference_key, sort_on_first = False ):
"""Returns the next verse as sorted by grades"""
fake_config_for_grade_reflect_loop = {
'reference_key': reference_key,
'grades_per_reflection_loop': float('inf'),
}
def get_grade( verse ):
if sort_on_first:
reflection_loops = verse.get('reflection_loops', [])
if reflection_loops:
first_loop = reflection_loops[0]
grades = first_loop.get('grades', [])
if grades:
grade = sum( [grade['grade'] for grade in grades] ) / len(grades)
return grade
else:
grade = grade_reflect_loop.compute_verse_grade( verse, fake_config_for_grade_reflect_loop )
if grade is not None:
return grade
return float('inf')
sorted_verses = sorted( translation_data, key=get_grade )
return sorted_verses, get_grade