From ba2c56a885b3710e6432ae4d342e46292400ddac Mon Sep 17 00:00:00 2001 From: "i.nueske" <i.nueske@indiscale.com> Date: Tue, 5 Nov 2024 11:59:15 +0100 Subject: [PATCH] STY: Docstring indentation --- .../table_json_conversion/convert.py | 199 +++++++++--------- 1 file changed, 99 insertions(+), 100 deletions(-) diff --git a/src/caosadvancedtools/table_json_conversion/convert.py b/src/caosadvancedtools/table_json_conversion/convert.py index 09882f96..48a0f676 100644 --- a/src/caosadvancedtools/table_json_conversion/convert.py +++ b/src/caosadvancedtools/table_json_conversion/convert.py @@ -55,10 +55,9 @@ class ForeignError(KeyError): class XLSXConverter: """Class for conversion from XLSX to JSON. -For a detailed description of the required formatting of the XLSX files, see ``specs.md`` in the -documentation. + For a detailed description of the required formatting of the XLSX files, see ``specs.md`` in the + documentation. """ - PARSER: dict[str, Callable] = { "string": str, "number": float, @@ -69,17 +68,17 @@ documentation. def __init__(self, xlsx: Union[str, BinaryIO], schema: Union[dict, str, TextIO], strict: bool = False): """ -Parameters ----------- -xlsx: Union[str, BinaryIO] - Path to the XLSX file or opened file object. + Parameters + ---------- + xlsx: Union[str, BinaryIO] + Path to the XLSX file or opened file object. -schema: Union[dict, str, TextIO] - Schema for validation of XLSX content. + schema: Union[dict, str, TextIO] + Schema for validation of XLSX content. -strict: bool, optional - If True, fail faster. -""" + strict: bool, optional + If True, fail faster. + """ self._workbook = load_workbook(xlsx) self._schema = read_or_dict(schema) self._defining_path_index = xlsx_utils.get_defining_paths(self._workbook) @@ -91,20 +90,20 @@ strict: bool, optional def to_dict(self, validate: bool = False, collect_errors: bool = True) -> dict: """Convert the xlsx contents to a dict. -Parameters ----------- -validate: bool, optional - If True, validate the result against the schema. + Parameters + ---------- + validate: bool, optional + If True, validate the result against the schema. -collect_errors: bool, optional - If True, do not fail at the first error, but try to collect as many errors as possible. After an - Exception is raised, the errors can be collected with ``get_errors()`` and printed with - ``get_error_str()``. + collect_errors: bool, optional + If True, do not fail at the first error, but try to collect as many errors as possible. After an + Exception is raised, the errors can be collected with ``get_errors()`` and printed with + ``get_error_str()``. -Returns -------- -out: dict - A dict representing the JSON with the extracted data. + Returns + ------- + out: dict + A dict representing the JSON with the extracted data. """ self._handled_sheets = set() self._result = {} @@ -177,17 +176,17 @@ out: dict def _handle_sheet(self, sheet: Worksheet, fail_later: bool = False) -> None: """Add the contents of the sheet to the result (stored in ``self._result``). -Each row in the sheet corresponds to one entry in an array in the result. Which array exactly is -defined by the sheet's "proper name" and the content of the foreign columns. + Each row in the sheet corresponds to one entry in an array in the result. Which array exactly is + defined by the sheet's "proper name" and the content of the foreign columns. -Look at ``xlsx_utils.get_path_position`` for the specification of the "proper name". + Look at ``xlsx_utils.get_path_position`` for the specification of the "proper name". -Parameters ----------- -fail_later: bool, optional - If True, do not fail with unresolvable foreign definitions, but collect all errors. -""" + Parameters + ---------- + fail_later: bool, optional + If True, do not fail with unresolvable foreign definitions, but collect all errors. + """ row_type_column = xlsx_utils.get_row_type_column_index(sheet) foreign_columns = xlsx_utils.get_foreign_key_columns(sheet) foreign_column_paths = {col.index: col.path for col in foreign_columns.values()} @@ -267,9 +266,9 @@ fail_later: bool, optional def _get_parent_dict(self, parent_path: list[str], foreign: list[list]) -> dict: """Return the dict into which values can be inserted. -This method returns, from the current result-in-making, the entry at ``parent_path`` which matches -the values given in the ``foreign`` specification. -""" + This method returns, from the current result-in-making, the entry at ``parent_path`` which matches + the values given in the ``foreign`` specification. + """ foreign_groups = _group_foreign_paths(foreign, common=parent_path) current_object = self._result @@ -296,9 +295,9 @@ the values given in the ``foreign`` specification. def _validate_and_convert(self, value: Any, path: list[str]): """Apply some basic validation and conversion steps. -This includes: -- Validation against the type given in the schema -- List typed values are split at semicolons and validated individually + This includes: + - Validation against the type given in the schema + - List typed values are split at semicolons and validated individually """ if value is None: return value @@ -340,29 +339,29 @@ This includes: def _group_foreign_paths(foreign: list[list], common: list[str]) -> list[SimpleNamespace]: """Group the foreign keys by their base paths. -Parameters ----------- -foreign: list[list] - A list of foreign definitions, consisting of path components, property and possibly value. - -common: list[list[str]] - A common path which defines the final target of the foreign definitions. This helps to understand - where the ``foreign`` paths shall be split. - -Returns -------- -out: list[dict[str, list[list]]] - - A list of foreign path segments, grouped by their common segments. Each element is a namespace - with detailed information of all those elements which form the group. The namespace has the - following attributes: - - - ``path``: The full path to this path segment. This is always the previous segment's ``path`` - plus this segment's ``subpath``. - - ``stringpath``: The stringified ``path``, might be useful for comparison or sorting. - - ``subpath``: The path, relative from the previous segment. - - ``definitions``: A list of the foreign definitions for this segment, but stripped of the - ``path`` components. + Parameters + ---------- + foreign: list[list] + A list of foreign definitions, consisting of path components, property and possibly value. + + common: list[list[str]] + A common path which defines the final target of the foreign definitions. This helps to understand + where the ``foreign`` paths shall be split. + + Returns + ------- + out: list[dict[str, list[list]]] + + A list of foreign path segments, grouped by their common segments. Each element is a namespace + with detailed information of all those elements which form the group. The namespace has the + following attributes: + + - ``path``: The full path to this path segment. This is always the previous segment's ``path`` + plus this segment's ``subpath``. + - ``stringpath``: The stringified ``path``, might be useful for comparison or sorting. + - ``subpath``: The path, relative from the previous segment. + - ``definitions``: A list of the foreign definitions for this segment, but stripped of the + ``path`` components. """ # Build a simple dict first, without subpath. results = {} @@ -405,31 +404,31 @@ def _set_in_nested(mydict: dict, path: list, value: Any, prefix: list = [], skip overwrite: bool = False, append_to_list: bool = False) -> dict: """Set a value in a nested dict. -Parameters ----------- -mydict: dict - The dict into which the ``value`` shall be inserted. -path: list - A list of keys, denoting the location of the value. -value - The value which shall be set inside the dict. -prefix: list - A list of keys which shall be removed from ``path``. A KeyError is raised if ``path`` does not - start with the elements of ``prefix``. -skip: int = 0 - Remove this many additional levels from the path, *after* removing the prefix. -overwrite: bool = False - If True, allow overwriting existing content. Otherwise, attempting to overwrite existing values - leads to an exception. -append_to_list: bool = False - If True, assume that the element at ``path`` is a list and append the value to it. If the list - does not exist, create it. If there is a non-list at ``path`` already, overwrite it with a new - list, if ``overwrite`` is True, otherwise raise a ValueError. - -Returns -------- -mydict: dict - The same dictionary that was given as a parameter, but modified. + Parameters + ---------- + mydict: dict + The dict into which the ``value`` shall be inserted. + path: list + A list of keys, denoting the location of the value. + value + The value which shall be set inside the dict. + prefix: list + A list of keys which shall be removed from ``path``. A KeyError is raised if ``path`` does not + start with the elements of ``prefix``. + skip: int = 0 + Remove this many additional levels from the path, *after* removing the prefix. + overwrite: bool = False + If True, allow overwriting existing content. Otherwise, attempting to overwrite existing values + leads to an exception. + append_to_list: bool = False + If True, assume that the element at ``path`` is a list and append the value to it. If the list + does not exist, create it. If there is a non-list at ``path`` already, overwrite it with a new + list, if ``overwrite`` is True, otherwise raise a ValueError. + + Returns + ------- + mydict: dict + The same dictionary that was given as a parameter, but modified. """ for idx, el in enumerate(prefix): if path[idx] != el: @@ -473,25 +472,25 @@ def to_dict(xlsx: Union[str, BinaryIO], schema: Union[dict, str, TextIO], validate: bool = None, strict: bool = False) -> dict: """Convert the xlsx contents to a dict, it must follow a schema. -Parameters ----------- -xlsx: Union[str, BinaryIO] - Path to the XLSX file or opened file object. + Parameters + ---------- + xlsx: Union[str, BinaryIO] + Path to the XLSX file or opened file object. -schema: Union[dict, str, TextIO] - Schema for validation of XLSX content. + schema: Union[dict, str, TextIO] + Schema for validation of XLSX content. -validate: bool, optional - If True, validate the result against the schema. + validate: bool, optional + If True, validate the result against the schema. -strict: bool, optional - If True, fail faster. + strict: bool, optional + If True, fail faster. -Returns -------- -out: dict - A dict representing the JSON with the extracted data. + Returns + ------- + out: dict + A dict representing the JSON with the extracted data. """ converter = XLSXConverter(xlsx, schema, strict=strict) return converter.to_dict() -- GitLab