jvonrad commited on
Commit
d18b2fd
·
verified ·
1 Parent(s): 4613cab

Upload src/xscript/_yaml.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. src/xscript/_yaml.py +35 -0
src/xscript/_yaml.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """YAML loading with a corrected float resolver.
2
+
3
+ PyYAML's default implicit float resolver requires a signed exponent, so plain
4
+ scientific notation like `1.0e15` or `2.0e9` is loaded as a *string*. Our
5
+ configs are full of token budgets in that form, so we install the fixed
6
+ resolver everywhere configs are read.
7
+ """
8
+ import re
9
+
10
+ import yaml
11
+
12
+
13
+ class _Loader(yaml.SafeLoader):
14
+ pass
15
+
16
+
17
+ _Loader.add_implicit_resolver(
18
+ "tag:yaml.org,2002:float",
19
+ re.compile(r"""^(?:
20
+ [-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+]?[0-9]+)?
21
+ |[-+]?\.[0-9_]+(?:[eE][-+]?[0-9]+)?
22
+ |[-+]?[0-9][0-9_]*(?:[eE][-+]?[0-9]+)
23
+ |[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*
24
+ |[-+]?\.(?:inf|Inf|INF)
25
+ |\.(?:nan|NaN|NAN))$""", re.X),
26
+ list("-+0123456789."))
27
+
28
+
29
+ def load(path) -> dict:
30
+ from pathlib import Path
31
+ return yaml.load(Path(path).read_text(), Loader=_Loader)
32
+
33
+
34
+ def loads(text: str) -> dict:
35
+ return yaml.load(text, Loader=_Loader)