Skip to content

Commit 2a01552

Browse files
authored
Merge pull request #1007 from compas-dev/where-api
Add support for kwargs to where filters
2 parents 41f680d + aa81c05 commit 2a01552

File tree

4 files changed

+86
-42
lines changed

4 files changed

+86
-42
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
* Changed `planarity` to optional requirement on all platforms.
4040
* Changed `numba` to optional requirement on all platforms.
4141
* Changed raw github content path for `compas.get`.
42+
* Changed `compas.datastructures.Graph.nodes_where` to accept conditions as kwargs.
43+
* Changed `compas.datastructures.Graph.edges_where` to accept conditions as kwargs.
44+
* Changed `compas.datastructures.Halfedge.vertices_where` to accept conditions as kwargs.
45+
* Changed `compas.datastructures.Halfedge.edges_where` to accept conditions as kwargs.
46+
* Changed `compas.datastructures.Halfedge.faces_where` to accept conditions as kwargs.
47+
* Changed `compas.datastructures.Halfface.vertices_where` to accept conditions as kwargs.
48+
* Changed `compas.datastructures.Halfface.edges_where` to accept conditions as kwargs.
49+
* Changed `compas.datastructures.Halfface.faces_where` to accept conditions as kwargs.
50+
* Changed `compas.datastructures.Halfface.cells_where` to accept conditions as kwargs.
4251
* Fixed `compas_blender.artists.VolMeshArtist.draw` and `compas_blender.artists.VolMeshArtist.draw_cells`.
4352
* Fixed `compas_ghpython.artists.VolMeshArtist.draw` and `compas_ghpython.artists.VolMeshArtist.draw_cells`.
4453
* Fixed `compas_rhino.artists.VolMeshArtist.draw` and `compas_rhino.artists.VolMeshArtist.draw_cells`.

src/compas/datastructures/graph/graph.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,12 @@ def nodes(self, data=False):
622622
else:
623623
yield key, self.node_attributes(key)
624624

625-
def nodes_where(self, conditions, data=False):
625+
def nodes_where(self, conditions=None, data=False, **kwargs):
626626
"""Get nodes for which a certain condition or set of conditions is true.
627627
628628
Parameters
629629
----------
630-
conditions : dict
630+
conditions : dict, optional
631631
A set of conditions in the form of key-value pairs.
632632
The keys should be attribute names. The values can be attribute
633633
values or ranges of attribute values in the form of min/max pairs.
@@ -641,6 +641,9 @@ def nodes_where(self, conditions, data=False):
641641
If `data` is True, the next node and its attributes.
642642
643643
"""
644+
conditions = conditions or {}
645+
conditions.update(kwargs)
646+
644647
for key, attr in self.nodes(True):
645648
is_match = True
646649

@@ -740,17 +743,19 @@ def edges(self, data=False):
740743
else:
741744
yield u, v
742745

743-
def edges_where(self, conditions, data=False):
746+
def edges_where(self, conditions=None, data=False, **kwargs):
744747
"""Get edges for which a certain condition or set of conditions is true.
745748
746749
Parameters
747750
----------
748-
conditions : dict
751+
conditions : dict, optional
749752
A set of conditions in the form of key-value pairs.
750753
The keys should be attribute names. The values can be attribute
751754
values or ranges of attribute values in the form of min/max pairs.
752755
data : bool, optional
753756
If True, yield the edge attributes in addition to the edge identifiers.
757+
**kwargs : dict[str, Any], optional
758+
Additional conditions provided as named function arguments.
754759
755760
Yields
756761
------
@@ -759,6 +764,9 @@ def edges_where(self, conditions, data=False):
759764
If `data` is True, the next edge identifier and its attributes as a ((u, v), attr) tuple.
760765
761766
"""
767+
conditions = conditions or {}
768+
conditions.update(kwargs)
769+
762770
for key in self.edges():
763771
is_match = True
764772

src/compas/datastructures/halfedge/halfedge.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,17 +620,19 @@ def edges(self, data=False):
620620
else:
621621
yield key, self.edge_attributes(key)
622622

623-
def vertices_where(self, conditions, data=False):
623+
def vertices_where(self, conditions=None, data=False, **kwargs):
624624
"""Get vertices for which a certain condition or set of conditions is true.
625625
626626
Parameters
627627
----------
628-
conditions : dict
628+
conditions : dict, optional
629629
A set of conditions in the form of key-value pairs.
630630
The keys should be attribute names. The values can be attribute
631631
values or ranges of attribute values in the form of min/max pairs.
632632
data : bool, optional
633633
If True, yield the vertex attributes in addition to the vertex identifiers.
634+
**kwargs : dict[str, Any], optional
635+
Additional conditions provided as named function arguments.
634636
635637
Yields
636638
------
@@ -639,6 +641,9 @@ def vertices_where(self, conditions, data=False):
639641
If `data` is True, the next vertex and its attributes.
640642
641643
"""
644+
conditions = conditions or {}
645+
conditions.update(kwargs)
646+
642647
for key, attr in self.vertices(True):
643648
is_match = True
644649

@@ -721,17 +726,19 @@ def vertices_where_predicate(self, predicate, data=False):
721726
else:
722727
yield key
723728

724-
def edges_where(self, conditions, data=False):
729+
def edges_where(self, conditions=None, data=False, **kwargs):
725730
"""Get edges for which a certain condition or set of conditions is true.
726731
727732
Parameters
728733
----------
729-
conditions : dict
734+
conditions : dict, optional
730735
A set of conditions in the form of key-value pairs.
731736
The keys should be attribute names. The values can be attribute
732737
values or ranges of attribute values in the form of min/max pairs.
733738
data : bool, optional
734739
If True, yield the edge attributes in addition to the edge identifiers.
740+
**kwargs : dict[str, Any], optional
741+
Additional conditions provided as named function arguments.
735742
736743
Yields
737744
------
@@ -740,6 +747,9 @@ def edges_where(self, conditions, data=False):
740747
If `data` is True, the next edge as a (u, v, data) tuple.
741748
742749
"""
750+
conditions = conditions or {}
751+
conditions.update(kwargs)
752+
743753
for key in self.edges():
744754
is_match = True
745755

@@ -803,17 +813,19 @@ def edges_where_predicate(self, predicate, data=False):
803813
else:
804814
yield key
805815

806-
def faces_where(self, conditions, data=False):
816+
def faces_where(self, conditions=None, data=False, **kwargs):
807817
"""Get faces for which a certain condition or set of conditions is true.
808818
809819
Parameters
810820
----------
811-
conditions : dict
821+
conditions : dict, optional
812822
A set of conditions in the form of key-value pairs.
813823
The keys should be attribute names. The values can be attribute
814824
values or ranges of attribute values in the form of min/max pairs.
815825
data : bool, optional
816826
If True, yield the face attributes in addition to face identifiers.
827+
**kwargs : dict[str, Any], optional
828+
Additional conditions provided as named function arguments.
817829
818830
Yields
819831
------
@@ -822,6 +834,9 @@ def faces_where(self, conditions, data=False):
822834
If `data` is True, the next face and its attributes.
823835
824836
"""
837+
conditions = conditions or {}
838+
conditions.update(kwargs)
839+
825840
for fkey in self.faces():
826841
is_match = True
827842

src/compas/datastructures/halfface/halfface.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -715,18 +715,19 @@ def cells(self, data=False):
715715
else:
716716
yield cell, self.cell_attributes(cell)
717717

718-
def vertices_where(self, conditions, data=False):
718+
def vertices_where(self, conditions=None, data=False, **kwargs):
719719
"""Get vertices for which a certain condition or set of conditions is true.
720720
721721
Parameters
722722
----------
723-
conditions : dict
723+
conditions : dict, optional
724724
A set of conditions in the form of key-value pairs.
725725
The keys should be attribute names. The values can be attribute
726726
values or ranges of attribute values in the form of min/max pairs.
727727
data : bool, optional
728-
Yield the vertices and their data attributes.
729-
Default is ``False``.
728+
If True, yield the vertex attributes in addition to the identifiers.
729+
**kwargs : dict[str, Any], optional
730+
Additional conditions provided as named function arguments.
730731
731732
Yields
732733
------
@@ -735,6 +736,9 @@ def vertices_where(self, conditions, data=False):
735736
If `data` is True, the next vertex and its attributes.
736737
737738
"""
739+
conditions = conditions or {}
740+
conditions.update(kwargs)
741+
738742
for key, attr in self.vertices(True):
739743
is_match = True
740744

@@ -793,11 +797,10 @@ def vertices_where_predicate(self, predicate, data=False):
793797
Parameters
794798
----------
795799
predicate : callable
796-
The condition you want to evaluate. The callable takes 2 parameters:
797-
``key``, ``attr`` and should return ``True`` or ``False``.
800+
The condition you want to evaluate.
801+
The callable takes 2 parameters: the vertex identifier and the vertex attributes, and should return True or False.
798802
data : bool, optional
799-
Yield the vertices and their data attributes.
800-
Default is ``False``.
803+
If True, yield the vertex attributes in addition to the identifiers.
801804
802805
Yields
803806
------
@@ -813,18 +816,19 @@ def vertices_where_predicate(self, predicate, data=False):
813816
else:
814817
yield key
815818

816-
def edges_where(self, conditions, data=False):
819+
def edges_where(self, conditions=None, data=False, **kwargs):
817820
"""Get edges for which a certain condition or set of conditions is true.
818821
819822
Parameters
820823
----------
821-
conditions : dict
824+
conditions : dict, optional
822825
A set of conditions in the form of key-value pairs.
823826
The keys should be attribute names. The values can be attribute
824827
values or ranges of attribute values in the form of min/max pairs.
825828
data : bool, optional
826-
Yield the edges and their data attributes.
827-
Default is ``False``.
829+
If True, yield the edge attributes in addition to the identifiers.
830+
**kwargs : dict[str, Any], optional
831+
Additional conditions provided as named function arguments.
828832
829833
Yields
830834
------
@@ -833,6 +837,9 @@ def edges_where(self, conditions, data=False):
833837
If `data` is True, the next edge as a (u, v, data) tuple.
834838
835839
"""
840+
conditions = conditions or {}
841+
conditions.update(kwargs)
842+
836843
for key in self.edges():
837844
is_match = True
838845

@@ -875,11 +882,10 @@ def edges_where_predicate(self, predicate, data=False):
875882
Parameters
876883
----------
877884
predicate : callable
878-
The condition you want to evaluate. The callable takes 3 parameters:
879-
``u``, ``v``, ``attr`` and should return ``True`` or ``False``.
885+
The condition you want to evaluate.
886+
The callable takes 2 parameters: the edge identifier and the edge attributes, and should return True or False.
880887
data : bool, optional
881-
Yield the vertices and their data attributes.
882-
Default is ``False``.
888+
If True, yield the edge attributes in addition to the identifiers.
883889
884890
Yields
885891
------
@@ -895,18 +901,19 @@ def edges_where_predicate(self, predicate, data=False):
895901
else:
896902
yield key
897903

898-
def faces_where(self, conditions, data=False):
904+
def faces_where(self, conditions=None, data=False, **kwargs):
899905
"""Get faces for which a certain condition or set of conditions is true.
900906
901907
Parameters
902908
----------
903-
conditions : dict
909+
conditions : dict, optional
904910
A set of conditions in the form of key-value pairs.
905911
The keys should be attribute names. The values can be attribute
906912
values or ranges of attribute values in the form of min/max pairs.
907913
data : bool, optional
908-
Yield the faces and their data attributes.
909-
Default is ``False``.
914+
If True, yield the face attributes in addition to the identifiers.
915+
**kwargs : dict[str, Any], optional
916+
Additional conditions provided as named function arguments.
910917
911918
Yields
912919
------
@@ -915,6 +922,9 @@ def faces_where(self, conditions, data=False):
915922
If `data` is True, the next face and its attributes.
916923
917924
"""
925+
conditions = conditions or {}
926+
conditions.update(kwargs)
927+
918928
for fkey in self.faces():
919929
is_match = True
920930

@@ -957,11 +967,10 @@ def faces_where_predicate(self, predicate, data=False):
957967
Parameters
958968
----------
959969
predicate : callable
960-
The condition you want to evaluate. The callable takes 2 parameters:
961-
``key``, ``attr`` and should return ``True`` or ``False``.
970+
The condition you want to evaluate.
971+
The callable takes 2 parameters: the face identifier and the the face attributes, and should return True or False.
962972
data : bool, optional
963-
Yield the faces and their data attributes.
964-
Default is ``False``.
973+
If True, yield the face attributes in addition to the identifiers.
965974
966975
Yields
967976
------
@@ -977,18 +986,19 @@ def faces_where_predicate(self, predicate, data=False):
977986
else:
978987
yield fkey
979988

980-
def cells_where(self, conditions, data=False):
989+
def cells_where(self, conditions=None, data=False, **kwargs):
981990
"""Get cells for which a certain condition or set of conditions is true.
982991
983992
Parameters
984993
----------
985-
conditions : dict
994+
conditions : dict, optional
986995
A set of conditions in the form of key-value pairs.
987996
The keys should be attribute names. The values can be attribute
988997
values or ranges of attribute values in the form of min/max pairs.
989998
data : bool, optional
990-
Yield the cells and their data attributes.
991-
Default is ``False``.
999+
If True, yield the cell attributes in addition to the identifiers.
1000+
**kwargs : dict[str, Any], optional
1001+
Additional conditions provided as named function arguments.
9921002
9931003
Yields
9941004
------
@@ -997,6 +1007,9 @@ def cells_where(self, conditions, data=False):
9971007
If `data` is True, the next cell and its attributes.
9981008
9991009
"""
1010+
conditions = conditions or {}
1011+
conditions.update(kwargs)
1012+
10001013
for ckey in self.cells():
10011014
is_match = True
10021015

@@ -1039,11 +1052,10 @@ def cells_where_predicate(self, predicate, data=False):
10391052
Parameters
10401053
----------
10411054
predicate : callable
1042-
The condition you want to evaluate. The callable takes 2 parameters:
1043-
``key``, ``attr`` and should return ``True`` or ``False``.
1055+
The condition you want to evaluate.
1056+
The callable takes 2 parameters: the cell identifier and the cell attributes, and should return True or False.
10441057
data : bool, optional
1045-
Yield the cells and their data attributes.
1046-
Default is ``False``.
1058+
If True, yield the cell attributes in addition to the identifiers.
10471059
10481060
Yields
10491061
------

0 commit comments

Comments
 (0)