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

[chore]Make get_field_attr safe and improve control flow in ts_core.py #319

Conversation

dardevelin
Copy link

Refactor get_field_attr in library.py and improve control flow in ts_core.py

Changes:

1. Refactor get_field_attr in library.py:

  • Ensure safety using list slicing to avoid IndexError.
  • Use any to determine if there is a value before using it.
  • Use .get() for dictionary access to handle default cases and prevent KeyError.

2. Improve control flow in ts_core.py:

  • Remove nesting by exiting early in match_conditions.
  • Eliminate the need for try-catch blocks for better readability and control flow.

Additional Fix:

Note:

  • Kept existing_fields case unchanged due to add_field_to_entry's procedural nature.

Refactor get_field_attr in library.py:
- Ensure safety using list slicing to avoid IndexError.
- Use .get() for dictionary access to handle default cases and prevent KeyError.

Improve control flow in ts_core.py:
- Remove nesting by exiting early in match_conditions.
- Eliminate the need for try-catch blocks for better readability and control flow.

Note: Kept existing_fields case unchanged due to add_field_to_entry's procedural nature.

this commit introduces changes to library.py get_field_attr
cleans whitespace from file
Fix get_field_attr in library.py to ensure proper handling of empty keys list:
- Previously, slicing avoided IndexError but returned a list instead.
- Ensure the value is read only if present, defaulting to -1 otherwise.

Ideal scenario: Define this function to return an int, as expected by the software elsewhere.
keep modified version only.
This change ensures that the attribute value is correctly
fetched using the _ensure_field dictionary, maintaining expected
functionality.
@seakrueger
Copy link
Collaborator

FYI, in the future you don't need to close and open a new PR when you want to add new commits. You can just (sometimes, force) push to the same remote branch

@dardevelin
Copy link
Author

@seakrueger I know, but I didn't know if id find what was the introduced issue on time, so to prevent people wasting time taking a look I closed it and created a new one when ready. cheers

@CyanVoxel CyanVoxel added Type: Refactor Code that needs to be restructured or cleaned up TagStudio: Library Relating to the TagStudio library system Status: Review Needed A review of this is needed labels Jul 17, 2024
Copy link
Collaborator

@eivl eivl left a comment

Choose a reason for hiding this comment

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

Nice rewrite, Approved once my comments are resolved.

]
match attribute.lower():
case "id":
return val[0] if not any(val := list(entry_field.keys())[:1]) else -1
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is the -1 used for? this looked at first glance as a rewrite to structural pattern matching, are there anything else?
the dense nature of the code makes it hard to read compared to list(entry_field.keys())[0]

Copy link
Collaborator

Choose a reason for hiding this comment

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

image

Copy link
Collaborator

@eivl eivl left a comment

Choose a reason for hiding this comment

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

missclick.

@CyanVoxel CyanVoxel removed the Status: Review Needed A review of this is needed label Sep 6, 2024
@dardevelin dardevelin requested a review from eivl September 7, 2024 20:24
@yedpodtrzitko
Copy link
Collaborator

yedpodtrzitko commented Sep 9, 2024

I'm afraid this PR is now obsolete when #332 has been merged. If you disagree, feel free to reopen it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TagStudio: Library Relating to the TagStudio library system Type: Refactor Code that needs to be restructured or cleaned up
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants