-
Notifications
You must be signed in to change notification settings - Fork 367
filter/map/apply/sort/[]/invoke #1514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 18 commits
6099302
51de9b3
390620d
8cafcb5
02d4827
5a68e35
976a950
97d28fa
a6d1be1
73ad16f
419ecc7
01e44b0
b0ff18b
eea7a2e
e4052b0
916305c
be84adf
6faa20a
c947ba3
0c3b810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -35,7 +35,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Dict, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from typing_extensions import Literal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from inspect import Parameter, Signature | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from inspect import Parameter, Signature, isbuiltin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
from .occ_impl.geom import Vector, Plane, Location | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -4430,6 +4430,81 @@ def _repr_javascript_(self) -> Any: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_selectShapes(self.objects) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)._repr_javascript_() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def __getitem__(self: T, item: Union[int, Sequence[int], slice]) -> T: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if isinstance(item, Iterable): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rv = self.newObject(self.objects[i] for i in item) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elif isinstance(item, slice): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rv = self.newObject(self.objects[item]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rv = self.newObject([self.objects[item]]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def filter(self: T, f: Callable[[CQObject], bool]) -> T: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Filter items using a boolean predicate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param f: Callable to be used for filtering. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: Workplane object with filtered items. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.newObject(filter(f, self.objects)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def map(self: T, f: Callable[[CQObject], CQObject]): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Apply a callable to every item separately. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param f: Callable to be applied to every item separately. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: Workplane object with f applied to all items. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.newObject(map(f, self.objects)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def apply(self: T, f: Callable[[Iterable[CQObject]], Iterable[CQObject]]): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Apply a callable to all items at once. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param f: Callable to be applied. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: Workplane object with f applied to all items. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.newObject(f(self.objects)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def sort(self: T, key: Callable[[CQObject], Any]) -> T: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Sort items using a callable. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param key: Callable to be used for sorting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: Workplane object with items sorted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return self.newObject(sorted(self.objects, key=key)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
def invoke( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
self: T, f: Union[Callable[[T], T], Callable[[T], None], Callable[[], None]] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Invoke a callable mapping Workplane to Workplane, Workplane to None or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
without any parameters. In the two latter cases self is returned. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:param f: Callable to be invoked. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
:return: Workplane object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
adam-urbanczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if isbuiltin(f): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arity = 0 # assume 0 arity for builtins; they cannot be introspected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
arity = f.__code__.co_argcount # NB: this is not understood by mypy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rv = self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if arity == 0: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
f() # type: ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
elif arity == 1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res = f(self) # type: ignore | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if res is not None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rv = res | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
raise ValueError("Provided function {f} accepts too many arguemnts") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
adam-urbanczyk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return rv | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not requesting this to be changed, but I think something like this would be a bit more mundane...?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, but I find mine more clear. Also it has slightly different semantics, f could throw for other reason than wrong number of parameters. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
# alias for backward compatibility | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CQ = Workplane |
Uh oh!
There was an error while loading. Please reload this page.