-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_io.py
41 lines (32 loc) · 1.31 KB
/
config_io.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
# -*- coding: utf-8 -*-
import json
import os.path
import time
from typing import Union
import numpy as np
import ruamel_yaml as yaml
def float_representer(dumper, value):
text = '{0:.3f}'.format(value)
return dumper.represent_scalar(u'tag:yaml.org,2002:float', text)
def save_room_config(room_config, room_directory, filename, prevent_repitition=False):
if prevent_repitition:
filename = filename + '-' + '-'.join('{:.7f}'.format(time.time()).split('.'))
with open(os.path.join(room_directory, filename + '.yml'), 'w') as f:
yaml.dump(room_config, f)
return filename
def save_track_info(track_info, track_directory, filename, prevent_repitition=False):
if prevent_repitition:
filename = filename + '-' + '-'.join('{:.7f}'.format(time.time()).split('.'))
with open(os.path.join(track_directory, filename + '.json'), 'w') as f:
json.dump(track_info, f)
return filename
def conf_to_range(config: Union[dict, list, int, float]):
if type(config) == dict:
result = np.linspace(config['min'], config['max'], config['steps'])
elif type(config) == list:
result = config.copy()
elif type(config) == float or type(config) == int:
result = [config]
else:
raise ValueError('value must be dict, list, int or float')
return result