-
Notifications
You must be signed in to change notification settings - Fork 0
/
path.py
38 lines (32 loc) · 1.44 KB
/
path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from collections.abc import Iterable
from itertools import zip_longest, starmap
from numbers import Number
from points import Vector, Point, PolarPoint
class Path(Vector):
@classmethod
def _judge_candidate_value(cls, a_potential_point):
# accept only instances of Vector as values for a Path
if isinstance(a_potential_point, Vector):
return a_potential_point
raise TypeError(
f'{cls} members must be a Vector instance, "{a_potential_point}" is not'
)
def _operation(self, the_other, a_dyadic_fn):
# return a new instance with members that result from a_dyadic_fn applied to
# this instance zipped with the_other
match the_other:
case Number() | Point() | PolarPoint():
# match scalars or any instances of the two point families
return self.__class__(
*starmap(
a_dyadic_fn,
zip_longest(self, (the_other,), fillvalue=the_other),
)
)
case Iterable():
# match an instance any other iterable,
# including another instance of Path
return self.__class__(*starmap(a_dyadic_fn, zip(self, the_other)))
case _:
# no idea how to apply this value in a dyadic manner with this Vector instance
raise TypeError(f"{the_other} disallowed")