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

Add some utility decorators for Model/States methods #247

Merged

Conversation

arcondello
Copy link
Member

These are used in #241, but are not really serialization-specific. So to make that PR easier to review IMO it makes sense to break them out.

The existing testing covers it pretty well, hence no new tests.

@arcondello arcondello added the enhancement New feature or request label Mar 3, 2025
@arcondello arcondello requested a review from randomir March 3, 2025 17:29
@@ -137,6 +138,7 @@ cdef class States:
self._states.swap(states)
return move(states)

@_file_object_arg("rb") # translate str/bytes file inputs into file objects
Copy link
Member Author

Choose a reason for hiding this comment

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

These are redundant for now, but will be used in #241

@arcondello arcondello force-pushed the feature/model-utility-decorators branch from 12bbd60 to a8630a1 Compare March 3, 2025 17:32
@arcondello arcondello added the bug Something isn't working label Mar 3, 2025
Copy link
Member

@randomir randomir left a comment

Choose a reason for hiding this comment

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

LGTM

@functools.wraps(method)
def _method(cls_or_self, file, *args, **kwargs):
if isinstance(file, str):
with open(file, mode) as fobj:
Copy link
Member

Choose a reason for hiding this comment

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

open accepts not only str for file, but also bytes and a os.PathLike(i.e. pathlib.Path instance). Neither of these two are a str instance.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call, os.PathLike especially should be covered. Will fix and add tests.

def decorator(method):
@functools.wraps(method)
def _method(cls_or_self, file, *args, **kwargs):
if isinstance(file, str):
Copy link
Member

Choose a reason for hiding this comment

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

I would consider inverting the test here to test for required file properties (like readable, writeable, binary), instead just str instance, as that would be a bit more flexible (to accept not only bytes, Paths, but also generators, etc).

Also, you could do _file_object_arg(read: bool = False, write: bool: False, binary: bool = True) for explicitness.

But I also appreciate the appeal of simplicity of the implemented approach.

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, especially if we're also doing os.PathLike etc inverting makes sense.

Re the explicit input arguments, because this is private I am inclined to go with just mode. If we eventually make it public then we can consider more explicit arguments.

__all__ = []


def _file_object_arg(mode):
Copy link
Member

Choose a reason for hiding this comment

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

Add type annotations here and below?

@arcondello arcondello force-pushed the feature/model-utility-decorators branch from 4a48583 to f800e22 Compare March 3, 2025 19:05
@arcondello arcondello merged commit b977393 into dwavesystems:main Mar 3, 2025
35 checks passed
@arcondello arcondello deleted the feature/model-utility-decorators branch March 3, 2025 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants