Skip to content

Commit

Permalink
Merge branch 'release/1.2.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Aug 22, 2023
2 parents f15e087 + a8eb9a4 commit 7ce6f42
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 17 deletions.
2 changes: 1 addition & 1 deletion warg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__project__ = "Warg"

__author__ = "Christian Heider Nielsen"
__version__ = "1.2.3"
__version__ = "1.2.5"
__doc__ = r"""
Created on 27/04/2019
Expand Down
2 changes: 1 addition & 1 deletion warg/business.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def busy_indicator(
stream: Callable = print,
indicator_interval: int = 1,
phases: Iterable[str] = ("◑", "◒", "◐", "◓"),
) -> int:
) -> Iterable[int]:
"""
You can choose arbitrary phases like ['|','/','-','\\']
Expand Down
7 changes: 5 additions & 2 deletions warg/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
__all__ = ["NoData", "IncompatiblePackageVersions"]

import types
import typing
from importlib.resources import Package
from typing import Iterable, MutableMapping, Union


Expand All @@ -23,7 +25,7 @@ def __init__(self, msg: str = "No Data Available"):
class IncompatiblePackageVersions(Exception):
"""description"""

def __init__(self, *packages: Iterable[Union[str, object]], **versions: MutableMapping):
def __init__(self, *packages: Iterable[Union[str, types.ModuleType]], **versions):
str_o = ", "
str_l = []

Expand Down Expand Up @@ -69,4 +71,5 @@ def main2() -> None:

raise IncompatiblePackageVersions(numpy, scipy)

main()
# main()
main2()
45 changes: 43 additions & 2 deletions warg/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
"list_keys",
"first_key",
"last_key",
"to_list",
"to_tuple",
]

import operator
Expand All @@ -39,6 +41,7 @@
Sequence,
Tuple,
List,
Union,
)

from warg import Number, drop_unused_kws
Expand Down Expand Up @@ -117,7 +120,7 @@ def sink(*args, **kwargs) -> None:

def prod(iterable: Iterable[Number]) -> Number:
"""
Calculate the product of the a Iterable of int or floats
Calculate the product of an Iterable, of int or floats
:param iterable:
:return:"""
return reduce(operator.mul, iterable, 1)
Expand Down Expand Up @@ -244,6 +247,34 @@ def chain_apply(it: Iterable, *callables: Callable) -> Iterable[Any]:
return it


def to_list(x: Union[Iterable, Any]) -> Union[List, Any]:
if False:
if x is None:
return []
if not isinstance(x, Iterable):
return x
i = iter(x)
try:
val = next(i)
except StopIteration:
return []
return [to_list(val)] + to_list(i)


def to_tuple(x: Union[Iterable, Any]) -> Union[Tuple, Any]:
if False:
if x is None:
return ()
if not isinstance(x, Iterable):
return x
i = iter(x)
try:
val = next(i)
except StopIteration:
return ()
return (to_tuple(val), *to_tuple(i))


if __name__ == "__main__":

def asud() -> None:
Expand Down Expand Up @@ -274,6 +305,16 @@ def asidj() -> None:
result = swap_mapping_order(test_dict, [2, 0, 1])
print(result)

def i8jsadij():
ij = ([10, 29], [(2, 3, 4), [[12, 4, 5]], ((2, 92, 90))], [])
print(to_list(ij))

def i8jsadi2j():
ij = ([10, 29], [(2, 3, 4), [[12, 4, 5]], ((2, 92, 90))], [])
print(to_tuple(ij))

i8jsadij()
i8jsadi2j()
# asud()
asidj()
# asidj()
# asjdnasid()
1 change: 1 addition & 0 deletions warg/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
from .mapping_generator import *
from .zipping_generator import *
from .numbers import *
from .testing import *
4 changes: 2 additions & 2 deletions warg/generators/numbers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import random
from typing import Iterable, Tuple, Callable
from typing import Iterable, Tuple, Callable, List

from warg import Number

__all__ = ["n_uint_mix", "n_uint_mix_generator_builder", "n_uint_mix_generator"]


def n_uint_mix(mix: Iterable[Number]) -> Tuple[Number, ...]:
def n_uint_mix(mix: Iterable[Number]) -> List[Number]:
return [random.randrange(0, m) for m in mix]


Expand Down
16 changes: 16 additions & 0 deletions warg/generators/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import itertools
from typing import Optional, Iterator

__all__ = ["peek"]


def peek(generator: Iterator) -> Optional[itertools.chain]:
try:
return itertools.chain((next(generator),), generator)
except StopIteration:
return None


if __name__ == "__main__":
print(peek(iter(range(0))))
print(peek(iter(range(1))))
6 changes: 3 additions & 3 deletions warg/generators/zipping_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def check_next_iter(iterable_: Any) -> Any:
"""description"""
if isinstance(iterable_, Iterable):
try:
a = next(iter(iterable_))
if isinstance(a, Iterable):
return a
a_ = next(iter(iterable_))
if isinstance(a_, Iterable):
return a_
except StopIteration:
pass

Expand Down
2 changes: 1 addition & 1 deletion warg/iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def pairs(s: Sequence) -> Tuple[Any, Any]:
lst = [4, 7, 11, 2]
pairs(lst) yields (4, 7), (7, 11), (11, 2)
http://stackoverflow.com/questions/1257413/1257446#1257446
https://stackoverflow.com/questions/1257413/1257446#1257446
:param s: An iterable/list
Expand Down
28 changes: 24 additions & 4 deletions warg/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

__all__ = ["recursive_flatten"]

from typing import Sequence
from typing import Sequence, Iterable


def recursive_flatten(seq: Sequence) -> Sequence:
def recursive_flatten_seq(seq: Sequence) -> Sequence:
"""Depth first flatten"""
if not seq: # is empty Sequence
return seq
Expand All @@ -21,6 +21,26 @@ def recursive_flatten(seq: Sequence) -> Sequence:
return (*seq[:1], *recursive_flatten(seq[1:]))


def recursive_flatten(sequence: Iterable) -> Iterable:
"""
Depth first flatten iterable
>>> list(recursive_flatten([1, [2], 3]))
[1, 2, 3]
>>> list(recursive_flatten([1, [2], [3, [4]]]))
[1, 2, 3, 4]
>>> list(recursive_flatten((([[None]], 2), (2,), 2)))
[None, 2, 2, 2]
"""
for element in sequence:
if isinstance(element, Iterable):
yield from recursive_flatten(element)
else:
yield element


if __name__ == "__main__":
print(recursive_flatten((((2,), 2), (2,), 2)))
print(recursive_flatten((([[None]], 2), (2,), 2)))
print(list(recursive_flatten((((2,), 2), (2,), 2))))
print(list(recursive_flatten((([[None]], 2), (2,), 2))))

print(list(recursive_flatten((([[None]], 2), (2,), 2))))
7 changes: 6 additions & 1 deletion warg/packages/editable.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json

# noinspection PyProtectedMember
from importlib.metadata import Distribution, PackageNotFoundError, PathDistribution

__all__ = [
Expand All @@ -9,8 +11,10 @@
]

from pathlib import Path
from typing import Optional


# noinspection PyProtectedMember
def dist_is_editable(dist: Distribution) -> bool:
"""
Return True if given Distribution is an editable installation.
Expand Down Expand Up @@ -70,7 +74,8 @@ def get_package_location(package_name: str) -> Path:
print(p)


def get_dist_package_location(dist: Distribution) -> Path:
# noinspection PyProtectedMember
def get_dist_package_location(dist: Distribution) -> Optional[Path]:
"""
FULL OF ASSUMPTIONS!
Expand Down
8 changes: 8 additions & 0 deletions warg/packages/pip_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
from typing import List, Union

from packaging.requirements import Requirement

# noinspection PyProtectedMember
from pip._internal.network.session import PipSession

# noinspection PyProtectedMember
from pip._internal.req import parse_requirements

# noinspection PyProtectedMember
from pip._internal.req.req_file import ParsedRequirement

# noinspection PyProtectedMember
from pip._internal.utils.packaging import get_requirement


Expand Down
2 changes: 2 additions & 0 deletions warg/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

if sys.version_info[:2] >= (3, 10):
# pylint: disable=no-name-in-module
# noinspection PyProtectedMember
from importlib.metadata import entry_points, EntryPoint
else:
# noinspection PyProtectedMember
from importlib_metadata import entry_points, EntryPoint

from typing import Tuple, Generator, Any
Expand Down

0 comments on commit 7ce6f42

Please sign in to comment.