hpotk.util package

hpotk.util.looks_like_url(file: str) bool[source]

Checks if the file looks like a URL.

Parameters:

file – file to check.

Returns:

True if the file starts with http:// or https://.

hpotk.util.looks_gzipped(file: str) bool[source]

Checks file suffix to determine if it looks gzipped.

Parameters:

file – file path to check.

Returns:

True if the file ends with .gz.

hpotk.util.open_text_io_handle_for_reading(fh: IO | str, timeout: int = 30, encoding: str | None = None) TextIO[source]

Open a io.TextIO file handle based on fh.

Parameters:
  • fh – a str or typing.IO to read from. If str, then it should be a path to a local file or a URL of a remote resource. Either http or https protocols are supported. The content will be uncompressed on the fly if the file name ends with .gz. If fh is an IO wrapper, the function ensures we get a text wrapper that uses given encoding.

  • timeout – timeout in seconds used when accessing a remote resource.

  • encoding – encoding used to decode the input or the system preferred encoding if unset.

Returns:

the io.TextIO wrapper.

hpotk.util.open_text_io_handle_for_writing(fh: str | IO, encoding: str | None = None) TextIO[source]

Open a io.TextIO file handle based on fpath.

Parameters:
  • fh – a str with a path to a local file The content will be compressed on the fly if the file name ends with .gz.

  • encoding – encoding used to encode the output or the system preferred encoding if unset.

Returns:

a io.TextIO wrapper.

hpotk.util.setup_logging(level: int = 20, log_fmt: str = '%(asctime)s %(name)-20s %(levelname)-3s : %(message)s', stream: TextIO | None = None)[source]

Create a basic configuration for the logging library. Set up console and file handler using provided log_fmt.

The level signifies the desired verbosity. Use:

  • 10 for DEBUG

  • 20 for INFO

  • 30 for WARNING

  • 40 for ERROR

  • 50 for CRITICAL

Parameters:
  • level – the verbosity to use, INFO by default.

  • log_fmt – format string for logging.

  • stream – stream to write to. Will default to sys.stderr if None.

hpotk.util.validate_instance(obj: T, clz: type, param_name: str | None = None) T[source]

Validate that obj is instance of clz or raise ValueError otherwise.

Parameters:
  • obj – and instance for validation.

  • clz – the target class.

  • param_name – name of the object to include in the error message.

Returns:

the obj if the validation passes.

Raises:

ValueError – if the instance check fails.

hpotk.util.validate_optional_instance(obj: T | None, clz: type, param_name: str | None = None) T | None[source]

Validate that obj is instance of clz or None, or raise ValueError otherwise.

Parameters:
  • obj – and instance for validation or None.

  • clz – the target class.

  • param_name – name of the object to include in the error message.

Returns:

the obj if the validation passes.

Raises:

ValueError – if obj is not None and the instance check fails.

Subpackages