Skip to content

Commit

Permalink
Use Python 3.10 typing feature, black styling indentations (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
teutoburg authored Sep 7, 2024
2 parents 730c2b2 + 72dce11 commit cc3c996
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
32 changes: 22 additions & 10 deletions astar_utils/nested_mapping.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Contains NestedMapping class."""

from typing import TextIO, Optional, Union, Any
from typing import TextIO, Any
from io import StringIO
from collections import abc, ChainMap

Expand All @@ -16,8 +16,11 @@ class NestedMapping(abc.MutableMapping):
# TODO: improve docstring
"""Dictionary-like structure that supports nested !-bang string keys."""

def __init__(self, new_dict: Optional[abc.Iterable] = None,
title: Optional[str] = None):
def __init__(
self,
new_dict: abc.Iterable | None = None,
title: str | None = None,
):
self.dic: abc.MutableMapping[str, Any] = {}
self._title = title
if isinstance(new_dict, abc.MutableMapping):
Expand Down Expand Up @@ -131,8 +134,10 @@ def _guard_submapping(entry, key_chunks, kind: str = "get") -> None:
f"self['!{'.'.join(key_chunks)}']`` first and then optionally "
"re-assign a new sub-mapping to the key.")

def _staggered_items(self, key: Union[str, None],
value: abc.Mapping) -> abc.Iterator[tuple[str, Any]]:
def _staggered_items(
self, key: str | None,
value: abc.Mapping,
) -> abc.Iterator[tuple[str, Any]]:
simple = []
for subkey, subvalue in value.items():
new_key = self._join_subkey(key, subkey)
Expand All @@ -157,9 +162,12 @@ def _write_subkey(key: str, pre: str, final: bool, stream: TextIO) -> str:
stream.write(f"{newpre}{key}: ")
return newpre

def _write_subitems(self, items: abc.Collection[tuple[str, Any]], pre: str,
stream: TextIO, nested: bool = False
) -> list[tuple[str, Any]]:
def _write_subitems(
self, items: abc.Collection[tuple[str, Any]],
pre: str,
stream: TextIO,
nested: bool = False,
) -> list[tuple[str, Any]]:
# TODO: could this (and _write_subdict) use _staggered_items instead??
n_items = len(items)
simple: list[tuple[str, Any]] = []
Expand All @@ -180,8 +188,12 @@ def _write_subitems(self, items: abc.Collection[tuple[str, Any]], pre: str,

return simple

def _write_subdict(self, subdict: abc.Mapping, stream: TextIO,
pad: str = "") -> None:
def _write_subdict(
self,
subdict: abc.Mapping,
stream: TextIO,
pad: str = "",
) -> None:
pre = pad.replace("├─", "│ ").replace("└─", " ")
simple = self._write_subitems(subdict.items(), pre, stream, True)
self._write_subitems(simple, pre, stream)
Expand Down
4 changes: 2 additions & 2 deletions astar_utils/unique_list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Contains UniqueList class."""

from typing import Optional, Any
from typing import Any
from collections.abc import Iterable, MutableSequence


Expand All @@ -19,7 +19,7 @@ class UniqueList(MutableSequence):
items.
"""

def __init__(self, initial: Optional[Iterable[Any]] = None):
def __init__(self, initial: Iterable[Any] | None = None):
self._set: set[Any] = set() # For uniqueness
self._list: list[Any] = [] # For order

Expand Down

0 comments on commit cc3c996

Please sign in to comment.