Skip to content

Commit

Permalink
#22 | add sub parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mxndtaylor committed Jun 10, 2024
1 parent 4e08d06 commit a6eac17
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "aliasing"
version = "0.5.2"
version = "0.5.3"
description = "A utility for duplicating class members to other names or \"aliases\""
authors = [
{ name = "mxt", email = "[email protected]" },
Expand Down
34 changes: 30 additions & 4 deletions src/aliasing/virtual_alias.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import itertools
import warnings
from typing import List, Optional, Any, Union, Sequence, Iterable

Expand Down Expand Up @@ -77,6 +78,7 @@ def __init__(
self,
*,
short: Optional[Union[int, Sequence[int], bool]] = None,
sub: Optional[Union[int, Sequence[int]]] = None,
trample_ok: Optional[List[str]] = None,
):
self.trample_ok = trample_ok or []
Expand All @@ -90,16 +92,40 @@ def __init__(
else:
self._short = bool(short)

def _generate_short(self, name: str) -> list[str]:
self._sub = None
if isinstance(sub, Iterable):
self._sub = list(sub)
elif isinstance(sub, int):
self._sub = list(range(1, int(sub) + 1))

@classmethod
def _generate_substring(cls, name: str, *, indices: list[int], strip_underscores=False) -> list[str]:
results: list[str] = []

for i in range(1, len(name)):
if self._short or i in self._short_indices:
if strip_underscores and len(name) > 1:
name = name.lstrip('_')

for i in range(1, len(name) + 1):
if i in indices:
results.append(name[:i])

return results

def _generate_sub(self, name: str) -> list[str]:
if not self._sub:
return []
return self._generate_substring(name, indices=self._sub)

def _generate_short(self, name: str) -> list[str]:
if not self._short and not self._short_indices:
return []
indices = self._short_indices if not self._short else list(range(1, len(name) + 1))
return self._generate_substring(name, indices=indices, strip_underscores=True)

def __call__(self, func: Any) -> valiased:
name = func.__name__
aliases = self._generate_short(name=name)
aliases = itertools.chain(
self._generate_short(name=name),
self._generate_sub(name=name),
)
return valiased(func, *aliases, trample_ok=self.trample_ok)

0 comments on commit a6eac17

Please sign in to comment.