Skip to content
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

Added newer DefaultTraversal rules to align with V3 sharp connectors #367

Open
wants to merge 4 commits into
base: v3-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/specklepy/objects/graph_traversal/default_traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from specklepy.objects.base import Base
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the advantage of creating separate variables and methods instead of putting it as properties and @ staticmethods of new class DefaultTraversal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason... generally I haven't bothered with all static classes before in python...
if you'd prefer it I can do just that.

from specklepy.objects.graph_traversal.traversal import GraphTraversal, TraversalRule

DISPLAY_VALUE_PROPERTY_ALIASES = {"displayValue", "@displayValue"}
ELEMENTS_PROPERTY_ALIASES = {"elements", "@elements"}


def has_display_value(x: Base):
return any(hasattr(x, alias) for alias in DISPLAY_VALUE_PROPERTY_ALIASES)


def create_default_traversal_function() -> GraphTraversal:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a test missing

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we are missing a test for this... I'm not sure we really need one since its just a factory function for traversal rules which are tested...
I can add one if you think its important.

"""
Traversal func for traversing the root object of a Speckle Model
"""

convertible_rule = TraversalRule(
[lambda b: b.speckle_type != "Base", has_display_value],
lambda _: ELEMENTS_PROPERTY_ALIASES,
)

default_rule = TraversalRule(
[lambda _: True],
# NOTE: Unlike the C# implementation, this does not ignore Obsolete members
lambda o: o.get_member_names(),
False,
)

return GraphTraversal([convertible_rule, default_rule])
14 changes: 13 additions & 1 deletion src/specklepy/objects/graph_traversal/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@


class ITraversalRule(Protocol):
@property
def should_return(self) -> bool:
pass

def get_members_to_traverse(self, o: Base) -> Set[str]:
"""Get the members to traverse."""
pass
Expand Down Expand Up @@ -50,10 +54,13 @@ def traverse(self, root: Base) -> Iterator[TraversalContext]:

while len(stack) > 0:
head = stack.pop()
yield head

current = head.current
active_rule = self._get_active_rule_or_default_rule(current)

if active_rule.should_return:
yield head

members_to_traverse = active_rule.get_members_to_traverse(current)
for child_prop in members_to_traverse:
try:
Expand Down Expand Up @@ -118,6 +125,11 @@ def _get_active_rule(self, o: Base) -> Optional[ITraversalRule]:
class TraversalRule:
_conditions: Collection[Callable[[Base], bool]]
_members_to_traverse: Callable[[Base], Iterable[str]]
_should_return_to_output: bool = True

@property
def should_return(self) -> bool:
return self._should_return_to_output
JR-Morgan marked this conversation as resolved.
Show resolved Hide resolved

def get_members_to_traverse(self, o: Base) -> Set[str]:
return set(self._members_to_traverse(o))
Expand Down
8 changes: 3 additions & 5 deletions tests/unit/test_traverse_value.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List
from dataclasses import dataclass
from typing import List

from specklepy.objects.base import Base
from specklepy.serialization.base_object_serializer import BaseObjectSerializer
Expand All @@ -12,16 +12,14 @@ class FakeBase(Base):


def test_traverse_value():
base = FakeBase(bar=1)
base.foo = [None]
base = FakeBase(bar=1, foo=["abcd"])
serializer = BaseObjectSerializer()
object_id, object_dict = serializer.traverse_base(base)
assert object_dict == {
"id": object_id,
"speckle_type": "Tests.Unit.TestTraverseValue.FakeBase",
"applicationId": None,
"foo": [None],
"units": None,
"foo": ["abcd"],
"bar": 1,
"totalChildrenCount": 0,
}