Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

utilities.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
66
67
68
69
  1. from pathlib import Path
  2. from typing import Union
  3. import yaml
  4. from yaml import Loader
  5. from collections.abc import Iterable
  6. def read_yaml(
  7. file: Union[str, Path], key: str = None, default: Union[str, dict] = None
  8. ) -> dict:
  9. """
  10. Read yaml file and return `dict`.
  11. Args:
  12. file: `str` or `Path`. Yaml file path.
  13. key: `str`. Yaml key you want to read.
  14. default: `str` or `dict`. Yaml key or default dict to use as default values.
  15. Returns:
  16. Yaml file content as `dict` object.
  17. """
  18. with open(file, "r") as fp:
  19. params = yaml.load(fp, Loader)
  20. default = (
  21. default
  22. if isinstance(default, dict)
  23. else (params[default] if isinstance(default, str) else dict())
  24. )
  25. result = params[key] if key else params
  26. return {**default, **result}
  27. def dump_yaml(
  28. obj: dict, file_path: Union[str, Path], key: str = None, norm: bool = True
  29. ) -> Path:
  30. """
  31. Write yaml file and return `Path`.
  32. Args:
  33. obj: `dict` to write to yaml file.
  34. file: `str` or `Path`. Yaml file path.
  35. key: `str`. dict key you want to write.
  36. norm: `bool`. flag to normalize float values or not.
  37. Returns:
  38. `Path` of yaml file after writing.
  39. """
  40. obj = obj[key] if key else obj
  41. if norm:
  42. obj = normalize(obj)
  43. with open(file_path, "w+") as file:
  44. yaml.dump(obj, file)
  45. return Path(file_path)
  46. def normalize(obj: dict, ndigits: int = 4) -> dict:
  47. """Normalizes float values to `ndigits` decimal places"""
  48. if isinstance(obj, (float,)):
  49. return round(obj, ndigits)
  50. if isinstance(obj, (str,)):
  51. return obj
  52. if isinstance(obj, dict):
  53. for key, value in obj.items():
  54. obj[key] = normalize(value, ndigits)
  55. return obj
  56. if isinstance(obj, Iterable):
  57. return [normalize(x, ndigits) for x in obj]
  58. return obj
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...