segram.utils.resources module

Utilities for accessing package resources.

class segram.utils.resources.Resource(path: str | bytes | PathLike, *, compression: Literal['.gz', '.bz2', '.xz'] | None = None)[source]

Bases: object

Resource handler class.

path

Path to the resource file.

compression

Compression being used. Deduced from the filename extension if not specified directly.

classmethod from_package(package: str, filename: str) Self[source]

Construct from a package/filename specification.

Parameters:
  • package – Package/module name using standard dot notation.

  • filename – Name of the resource file. Compression extension can be omitted. However, if it is specified then only files with a given extension are considered.

static get_path(package: str, filename: str | bytes | PathLike) PathLike[source]

Get file path from package and file names.

open(path: PathLike, mode: str, **kwds: Any) Any[source]

Open resource file.

Parameters:
  • path – File path.

  • mode – File opening mode.

  • **kwds – Other keyword arguments passed to an appropriate open function depending on the compression method.

get(**kwds: Any) str | bytes[source]

Get resource data.

Parameters:

**kwds – Passed to open(), gzip.open(), bz2.open() or lzma.open() depending on file compression.

write(data: str | bytes, mode: str = 'x', **kwds: Any) None[source]

Write resource.

class segram.utils.resources.JSONResource(path: str | bytes | PathLike, *, compression: Literal['.gz', '.bz2', '.xz'] | None = None)[source]

Bases: Resource

JSON resource handler class.

See also

Resource

base resource accessor class.

get(*, json_kws: Mapping | None = None, **kwds: Any) dict | list[source]

Get JSON resource.

Parameters:
  • json_kws – Dictionary with keyword arguments passed to json.loads().

  • **kwds – Passed to Resource.get().

write(data: str | list | dict, mode: str = 'w', json_kws: Mapping | None = None, **kwds: Any) None[source]

Write JSON resource.

static obj_hook(obj: Mapping) dict[source]

Custom deserialization of JSON objects.

class segram.utils.resources.JSONLinesResource(path: str | bytes | PathLike, *, compression: Literal['.gz', '.bz2', '.xz'] | None = None)[source]

Bases: JSONResource

JSON lines resource handler class.

See also

JSONResource

JSON resource handler.

iter(*, json_kws: Mapping | None, **kwds: Any) Iterable[str | list | dict][source]

Get resource data line by line.

Parameters:
  • json_kws – Dictionary with keyword arguments passed to json.loads().

  • **kwds – Passed to Resource.get().

get(*, json_kws: Mapping | None, **kwds: Any) Iterable[str | list | dict][source]

Get resource data line by line.

Parameters:
  • json_kws – Dictionary with keyword arguments passed to json.loads().

  • **kwds – Passed to Resource.get().

write(data: Iterable[str | list | dict], mode: str = 'x', json_kws: Mapping | None = None, **kwds: Any) None[source]

Write JSON resource.