Skip to content

Commit

Permalink
format with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrd committed Oct 30, 2023
1 parent 2097a5b commit 112f0b2
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 244 deletions.
2 changes: 1 addition & 1 deletion path_dict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . path_dict import PathDict
from .path_dict import PathDict

pd = PathDict

Expand Down
20 changes: 4 additions & 16 deletions path_dict/multi_path_dict.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
from __future__ import annotations

from typing import Any, Callable
from . path import Path
from . path_dict import PathDict

from .path import Path
from .path_dict import PathDict


class MultiPathDict:
path_handle: Path
root_data: dict | list


def __init__(self, data: dict | list, path: Path):
self.path_handle = path
self.root_data = data


def __repr__(self) -> str:
return f"MultiPathDict({self.root_data = }, {self.path_handle = })"


############################################################################
# Setters
# Setters ALWAYS return a value
############################################################################


def gather(self, as_type="list", include_paths=False) -> dict | list:
"""
Get the actual value at the given path.
Expand All @@ -49,7 +47,6 @@ def gather(self, as_type="list", include_paths=False) -> dict | list:
res[tuple(path.path)] = handle.at(path.path).get()
return res


def gather_pd(self, as_type="list", include_paths=False) -> PathDict:
data = self.gather(as_type=as_type, include_paths=include_paths)
return PathDict.from_data_and_path(data, self.path_handle.copy(replace_path=[]))
Expand All @@ -59,7 +56,6 @@ def gather_pd(self, as_type="list", include_paths=False) -> PathDict:
# Setters ALWAYS return a handle, not the value.
############################################################################


def map(self, f: Callable) -> PathDict:
"""
Map the result of f to the value at path previously set by ".at(path)".
Expand All @@ -70,49 +66,41 @@ def map(self, f: Callable) -> PathDict:
PathDict.from_data_and_path(self.root_data, path).map(f)
return PathDict.from_data_and_path(self.root_data, self.path_handle)


def reduce(self, f: Callable, aggregate: Any, as_type="list", include_paths=False) -> Any:
"""
Get all values of the given multi-path, and reduce them using f.
"""
return self.gather_pd(as_type=as_type, include_paths=include_paths).reduce(f, aggregate)


############################################################################
#### Filter
############################################################################


def filter(self, f: Callable, as_type="list", include_paths=False) -> PathDict:
"""
At the current path only keep the elements for which f(key, value)
is True for dicts, or f(value) is True for lists.
"""
return self.gather_pd(as_type=as_type, include_paths=include_paths).filter(f)


# def filtered(self, f: Callable[[Any], bool], as_type="list", include_paths=False) -> PathDict:
# raise NotImplementedError


############################################################################
#### Useful shorthands
############################################################################


def sum(self) -> Any:
"""
Sum all values at the given multi-path.
"""
return sum(self.gather())


def set(self, value: Any) -> PathDict:
for path in self.path_handle.expand(self.root_data):
PathDict.from_data_and_path(self.root_data, path).set(value)
return self


############################################################################
#### Standard dict methods
############################################################################
13 changes: 3 additions & 10 deletions path_dict/path.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations
from . utils import get_nested_keys_or_indices

from .utils import get_nested_keys_or_indices


class Path:
path: list[str]
raw: bool


def __init__(self, *path, raw=False):
# Careful, if the kwargs are passed as positional agrs, they are part of the path
self.raw = raw
Expand All @@ -22,35 +22,28 @@ def __init__(self, *path, raw=False):
# Clean up empty strings
self.path = [x for x in self.path if x != ""]


def __repr__(self) -> str:
return f"Path(path={self.path}, raw={self.raw})"


@property
def has_wildcards(self):
return "*" in self.path


def __iter__(self):
""" Iterate over path keys using a for in loop """
"""Iterate over path keys using a for in loop"""
return iter(self.path)


def __len__(self):
return len(self.path)


def __getitem__(self, key):
return self.path[key]


def copy(self, replace_path=None, replace_raw=None) -> Path:
path_copy = list(self.path) if replace_path is None else replace_path
raw_copy = self.raw if replace_raw is None else replace_raw
return Path(path_copy, raw=raw_copy)


def expand(self, ref: dict | list) -> list[Path]:
"""
Expand the path to list[Path], using the ref as a reference.
Expand Down
Loading

0 comments on commit 112f0b2

Please sign in to comment.