Skip to content

io

thkit.io

Classes:

  • DotDict

    Dictionary supporting dot notation (attribute access) as well as standard dictionary access.

Functions:

  • write_yaml

    Write data to a YAML file.

  • read_yaml

    Read data from a YAML file.

  • combine_text_files

    Combine text files into a single file in a memory-efficient. Read and write in chunks to avoid loading large files into memory

  • 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.

write_yaml(jdata: dict, filename: str | Path)

Write data to a YAML file.

read_yaml(filename: str | Path) -> dict

Read data from 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:

  • files (list[str]) –

    List of file paths to combine.

  • output_file (str) –

    Path to the output file.

  • chunk_size (int, default: 1024 ) –

    Size of each chunk in KB to read/write. Defaults to 1024 KB.

unpack_dict(nested_dict: dict) -> dict

Unpack one level of nested dictionary.

download_rawtext(url: str, outfile: str = 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

float2str(number: float, decimals=6)

convert float number to str REF: https://stackoverflow.com/questions/2440692/formatting-floats-without-trailing-zeros

Parameters:

  • number (float) –

    float number

  • fmt (str) –

    format of the output string

Returns:

  • s ( str ) –

    string of the float number