1
1
from pathlib import Path
2
- from typing import Union , Optional , Tuple , Dict , Any
2
+ from typing import Any
3
3
4
- PathLike = Union [ str , Path ]
4
+ PathLike = str | Path
5
5
6
6
7
7
# Copied from odc.io.text
8
8
9
9
10
- def read_int (path : PathLike , default = None , base = 10 ) -> Optional [ int ] :
10
+ def read_int (path : PathLike , default = None , base = 10 ) -> int | None :
11
11
"""
12
12
Read single integer from a text file.
13
13
14
14
Useful for things like parsing content of /sys/ or /proc.
15
15
"""
16
16
try :
17
- with open (path , "rt" , encoding = "utf8" ) as f :
17
+ with open (path , encoding = "utf8" ) as f :
18
18
return int (f .read (), base )
19
19
except (FileNotFoundError , ValueError ):
20
20
return default
21
21
22
22
23
23
def split_and_check (
24
- s : str , separator : str , n : Union [ int , Tuple [int , ...] ]
25
- ) -> Tuple [str , ...]:
24
+ s : str , separator : str , n : int | tuple [int , ...]
25
+ ) -> tuple [str , ...]:
26
26
"""Turn string into tuple, checking that there are exactly as many parts as expected.
27
27
:param s: String to parse
28
28
:param separator: Separator character
@@ -44,7 +44,7 @@ def parse_slice(s: str) -> slice:
44
44
Examples "::4", "2:5", "2::10", "3:100:5"
45
45
"""
46
46
47
- def parse (part : str ) -> Optional [ int ] :
47
+ def parse (part : str ) -> int | None :
48
48
if part == "" :
49
49
return None
50
50
return int (part )
@@ -57,32 +57,32 @@ def parse(part: str) -> Optional[int]:
57
57
return slice (* parts )
58
58
59
59
60
- def parse_yaml (s : str ) -> Dict [str , Any ]:
60
+ def parse_yaml (s : str ) -> dict [str , Any ]:
61
61
# pylint: disable=import-outside-toplevel
62
62
import yaml
63
63
64
64
return yaml .load (s , Loader = getattr (yaml , "CSafeLoader" , yaml .SafeLoader ))
65
65
66
66
67
- def parse_yaml_file_or_inline (s : str ) -> Dict [str , Any ]:
67
+ def parse_yaml_file_or_inline (s : str ) -> dict [str , Any ]:
68
68
"""
69
69
Accept on input either a path to yaml file or yaml text, return parsed yaml document.
70
70
"""
71
71
try :
72
72
# if file
73
73
path = Path (s )
74
- with open (path , "rt" , encoding = "utf8" ) as f :
74
+ with open (path , encoding = "utf8" ) as f :
75
75
txt = f .read ()
76
76
assert isinstance (txt , str )
77
- except (FileNotFoundError , IOError , ValueError ):
77
+ except (FileNotFoundError , OSError , ValueError ):
78
78
txt = s
79
79
result = parse_yaml (txt )
80
80
if isinstance (result , str ):
81
- raise IOError (f"No such file: { s } " )
81
+ raise OSError (f"No such file: { s } " )
82
82
return result
83
83
84
84
85
- def load_yaml_remote (yaml_url : str ) -> Dict [str , Any ]:
85
+ def load_yaml_remote (yaml_url : str ) -> dict [str , Any ]:
86
86
"""
87
87
Open a yaml file remotely and return the parsed yaml document
88
88
"""
@@ -97,7 +97,7 @@ def load_yaml_remote(yaml_url: str) -> Dict[str, Any]:
97
97
raise
98
98
99
99
100
- def parse_range2d_int (s : str ) -> Tuple [ Tuple [int , int ], Tuple [int , int ]]:
100
+ def parse_range2d_int (s : str ) -> tuple [ tuple [int , int ], tuple [int , int ]]:
101
101
"""Parse string like "0:3,4:5" -> ((0,3), (4,5))"""
102
102
try :
103
103
return tuple (
0 commit comments