-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some minor-impact utilities, rework types in a backward-compatiblβ¦
β¦e way. Signed-off-by: Emanuele Ballarin <[email protected]>
- Loading branch information
1 parent
1dc09c7
commit 9c5fcd4
Showing
7 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ~~ Imports ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
from collections.abc import Iterable | ||
from functools import partial | ||
from typing import List | ||
from typing import Optional | ||
from typing import Tuple | ||
from typing import Union | ||
|
||
# ~~ Exports ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
__all__: List[str] = ["variadic_attrs"] | ||
|
||
|
||
# ~~ Utilities ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
def _str_to_bool(s: str, onesym: bool = False) -> bool: | ||
osl: List[str] = ["t", "y", "1"] | ||
if onesym: | ||
return s.lower() in osl | ||
return s.lower() in (osl + ["true", "yes"]) | ||
|
||
|
||
def _any_to_bool(x, onesym: bool = False) -> bool: | ||
if isinstance(x, str): | ||
return _str_to_bool(x, onesym) | ||
return bool(x) | ||
|
||
|
||
def _str_to_booltuple(s: str, sep: Optional[str] = None) -> Tuple[bool, ...]: | ||
if sep is not None: | ||
return tuple(map(_str_to_bool, s.split(sep))) | ||
return tuple(map(partial(_str_to_bool, onesym=True), [*s])) | ||
|
||
|
||
def _any_to_booltuple( | ||
x: Union[str, Iterable[Union[str, bool]]], sep: Optional[str] = None | ||
) -> Tuple[bool, ...]: | ||
if isinstance(x, str): | ||
return _str_to_booltuple(x, sep) | ||
return tuple(map(_any_to_bool, x)) | ||
|
||
|
||
def variadic_attrs( | ||
selfobj, | ||
varsel: Optional[Iterable[Union[str, bool]]] = None, | ||
insep: Optional[str] = None, | ||
outsep: str = "_", | ||
): | ||
odict: dict = selfobj.__getstate__() | ||
odkeys: Tuple[str, ...] = tuple(odict.keys()) | ||
lodk: int = len(odkeys) | ||
varsel: Iterable = varsel if varsel is not None else ([True] * lodk) | ||
bvsel: Tuple[bool, ...] = _any_to_booltuple(varsel, insep) | ||
strtuple: Tuple[str, ...] = tuple( | ||
str(odict[odkeys[i]]) if bvsel[i] else "" for i in range(lodk) | ||
) | ||
return (outsep.join(strtuple)).strip().strip(outsep) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ~~ Imports ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
from collections.abc import Callable | ||
from typing import List | ||
from typing import Optional | ||
from typing import Tuple | ||
|
||
# ~~ Exports ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
__all__: List[str] = ["fromcache"] | ||
|
||
|
||
# ~~ Utilities ~~ ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
def _normargs(args, kwargs, kwpos: Tuple[str, ...], kwdef: tuple) -> tuple: | ||
largs: int = len(args) | ||
n_args: list = list(args) + [None] * (len(kwpos) - largs) | ||
for i, argname in enumerate(kwpos[largs:], start=largs): | ||
if argname in kwargs: | ||
n_args[i] = kwargs[argname] | ||
else: | ||
n_args[i] = kwdef[i] | ||
return tuple(n_args) | ||
|
||
|
||
def _args2keyer(kwpos: Tuple[str, ...], kwdef: tuple) -> Callable: | ||
def _args2key(*args, **kwargs) -> tuple: | ||
return _normargs(args, kwargs, kwpos, kwdef) | ||
|
||
return _args2key | ||
|
||
|
||
def _retlookup(key: tuple, dictionary: dict) -> Optional: | ||
return dictionary[key] if key in dictionary else None | ||
|
||
|
||
# ~~ Cache retrieval function ~~ βββββββββββββββββββββββββββββββββββββββββββββββ | ||
def fromcache( | ||
func: Callable, | ||
*, | ||
kwpos: Tuple[str, ...], | ||
kwdef: tuple, | ||
cache: dict, | ||
updateable: bool = True, | ||
) -> Callable: | ||
def _cached_func(*args, **kwargs): | ||
key: tuple = _args2keyer(kwpos, kwdef)(*args, **kwargs) | ||
if (rlk := _retlookup(key, cache)) is not None: | ||
return rlk | ||
else: | ||
if updateable: | ||
funcret = func(*args, **kwargs) | ||
cache[key] = funcret | ||
return funcret | ||
else: | ||
raise ValueError("Cache miss, but cache is not updateable.") | ||
|
||
return _cached_func |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
from typing import List | ||
from typing import Optional | ||
|
||
import matplotlib.pyplot as plt | ||
|
||
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
__all__: List[str] = ["custom_plot_setup", "plot_out"] | ||
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
|
||
|
||
def custom_plot_setup() -> None: | ||
plt.rcParams["text.usetex"] = True | ||
plt.style.use("ggplot") | ||
plt.rcParams["axes.facecolor"] = "white" | ||
plt.rcParams["axes.edgecolor"] = "black" | ||
plt.rcParams["axes.spines.top"] = False | ||
plt.rcParams["axes.spines.right"] = False | ||
plt.rcParams["xtick.color"] = "black" | ||
plt.rcParams["ytick.color"] = "black" | ||
plt.rcParams["axes.labelcolor"] = "black" | ||
plt.rcParams["grid.color"] = "gainsboro" | ||
|
||
|
||
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | ||
|
||
|
||
def plot_out(savepath: Optional[str] = None) -> None: | ||
if savepath: | ||
plt.savefig(savepath, dpi=400, bbox_inches="tight") | ||
else: | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ def read(fname): | |
|
||
setup( | ||
name=PACKAGENAME, | ||
version="0.28.2", | ||
version="0.28.3", | ||
author="Emanuele Ballarin", | ||
author_email="[email protected]", | ||
url="https://github.com/emaballarin/ebtorch", | ||
|