io
thkit.io
¶
Input/output utilities.
Classes:
-
DotDict–Dictionary supporting dot notation (attribute access) as well as standard dictionary access.
Functions:
-
read_yaml–Read data from a YAML file.
-
write_yaml–Write data to a YAML file.
-
combine_text_files–Combine text files into a single file in a memory-efficient.
-
unpack_dict–Unpack one level of nested dictionary.
-
download_rawtext–Download raw text from a URL.
-
txt2str–Convert a text file to a string.
-
str2txt–Convert a string to a text file.
-
txt2list–Convert a text file to a list of lines (without newline characters).
-
list2txt–Convert a list of lines to a text file.
-
float2str–Convert float number to str.
DotDict(dct=None)
¶
Bases: dict
Dictionary supporting dot notation (attribute access) as well as standard dictionary access. Nested dicts and sequences (list/tuple/set) are converted recursively.
Parameters:
-
dct(dict, default:None) –Initial dictionary to populate the DotDict. Defaults to empty dict.
Usage
d = DotDict({'a': 1, 'b': {'c': 2, 'd': [3, {'e': 4}]}}) print(d.b.c) # 2 print(d['b']['c']) # 2 d.b.d[1].e = 42 print(d.b.d[1].e) # 42 print(d.to_dict()) # plain dict
Methods:
-
__setitem__–Set item using dot notation or standard dict syntax.
-
__setattr__– -
to_dict–Recursively convert DotDict back to plain dict.
Attributes:
__getattr__ = dict.__getitem__
class-attribute
instance-attribute
¶
__delattr__ = dict.__delitem__
class-attribute
instance-attribute
¶
__setitem__(key, value)
¶
Set item using dot notation or standard dict syntax.
__setattr__(key, value)
¶
to_dict()
¶
Recursively convert DotDict back to plain dict.
read_yaml(filename: str | Path) -> Any
¶
Read data from a YAML file.
write_yaml(jdata: dict[Any, Any] | list[Any], filename: str | Path)
¶
Write data to a YAML file.
combine_text_files(files: list[str], output_file: str, chunk_size: int = 1024)
¶
Combine text files into a single file in a memory-efficient.
Read and write in chunks to avoid loading large files into memory
Parameters:
unpack_dict(nested_dict: dict) -> dict
¶
Unpack one level of nested dictionary.
download_rawtext(url: str, outfile: str | None = None) -> str
¶
Download raw text from a URL.
txt2str(file_path: str | Path) -> str
¶
Convert a text file to a string.
str2txt(text: str, file_path: str | Path) -> None
¶
Convert a string to a text file.
txt2list(file_path: str | Path) -> list[str]
¶
Convert a text file to a list of lines (without newline characters).
list2txt(lines: list[str], file_path: str | Path) -> None
¶
Convert a list of lines to a text file.