-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add some utility decorators for Model/States methods #247
Conversation
@@ -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 |
There was a problem hiding this comment.
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
12bbd60
to
a8630a1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
dwave/optimization/utilities.py
Outdated
@functools.wraps(method) | ||
def _method(cls_or_self, file, *args, **kwargs): | ||
if isinstance(file, str): | ||
with open(file, mode) as fobj: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
dwave/optimization/utilities.py
Outdated
def decorator(method): | ||
@functools.wraps(method) | ||
def _method(cls_or_self, file, *args, **kwargs): | ||
if isinstance(file, str): |
There was a problem hiding this comment.
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
, Path
s, 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.
There was a problem hiding this comment.
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.
dwave/optimization/utilities.py
Outdated
__all__ = [] | ||
|
||
|
||
def _file_object_arg(mode): |
There was a problem hiding this comment.
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?
Previously they incorrectly claimed that we could accept ByteStrings.
4a48583
to
f800e22
Compare
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.