8000 Refactor `BaseModel._deserialize` by c0rydoras · Pull Request #16 · adfinis/libtimed · GitHub
[go: up one dir, main page]

Skip to content

Refactor BaseModel._deserialize - #16

Open
c0rydoras wants to merge 1 commit into
adfinis:mainfrom
c0rydoras:refactor-deserialize
Open

c0rydoras wants to merge 1 commit into
adfinis:mainfrom
c0rydoras:refactor-deserialize

Conversation

@c0rydoras
Copy link
Copy Markdown
Collaborator

No description provided.

@c0rydoras
c0rydoras requested a review from hairmare August 22, 2023 07:11
@c0rydoras c0rydoras self-assigned this Aug 22, 2023
@hairmare hairmare left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this looks like it reduces the complexity of _deserialize() method quite a bit!

one downside i see is that the new _get_included_item() is still tightly coupled to _deserialize(). this means it isn't easy to implement an isolated unit test for the new method.

Comment thread src/libtimed/models.py
relationships = item.get("relationships")
if not relationships:

if not (relationships := item.get("relationships")):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

image

Comment thread src/libtimed/models.py
for relationship_data in data
if (
included_item := self._get_included_item(
relationship_data["type"], relationship_data["id"], included
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is there a reason why it's not using relationship_data.get("type")

you seem to primarily use get() up until here

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what about this?

Comment thread src/libtimed/models.py
relationships: list[tuple]
filters: list[tuple]

def _get_included_item(self, item_type: str, item_id, included: dict):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

good job, this method doesn't contain a single walrus :trollface:

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

That can be arranged :trollface:

    def _get_included_item(self, item_type: str, item_id, included: dict):
        if not (included_item:= next(
            (
                included_item
                for included_item in included
                if included_item.get("type") == item_type
                and included_item.get("id") == item_id
            ),
            None,
        )):
            return None
        included_model = self.client._type_map[item_type](self.client)
        return included_model._deserialize(included_item, included)

Comment thread src/libtimed/models.py
if not included_item:
return None
included_model = self.client._type_map[item_type](self.client)
return included_model._deserialize(included_item, included)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

_deserialize() used to fully contain the recursion logic, so the stack looked something like so:

  • _deserialize()
    • _deserialize()
      • _deserialize()
        • ...

    with this change the stack will look more like this:

    • _deserialize()
      • _get_included_item()
        • _deserialize()
          • _get_included_item()
            • _deserialize()
              • _get_included_item()
                • ...

    is this a trade off, you're willing to make?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes

@c0rydoras c0rydoras closed this Oct 11, 2023
@c0rydoras c0rydoras reopened this Oct 11, 2023
@github-actions
Copy link
Copy Markdown
Contributor

Alex Recommends Report

Alex recommends the following language changes, but Alex is a regular expression based algorithm, so take them with a grain of salt.

✨ 🚀 ✨ Nothing to Report ✨ 🚀 ✨

@c0rydoras c0rydoras added the refactor refactor request / implementation label Nov 14, 2023
@c0rydoras
c0rydoras force-pushed the refactor-deserialize branch from bd9fe1d to 7586f6c Compare March 27, 2024 12:06
@c0rydoras
c0rydoras force-pushed the refactor-deserialize branch from 7586f6c to dd9f8de Compare March 27, 2024 12:09
@c0rydoras
c0rydoras requested a review from hairmare March 27, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor refactor request / implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

0