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

Pass setval calls through SubstructureField to the contained Structure #31

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions suitcase/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import six
from suitcase.exceptions import SuitcaseChecksumException, SuitcaseProgrammingError, \
SuitcaseParseError, SuitcaseException, SuitcasePackStructException
import suitcase
Copy link
Contributor

Choose a reason for hiding this comment

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

Stylistically, I think I would prefer:

from suitcase.structure import Structure

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that it would look nicer, but it introduces a circular dependency between fields.py and structure.py.

Copy link
Contributor

Choose a reason for hiding this comment

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

That could be resolved by adding from suitcase.structure import Structure to the setval method.

from six import BytesIO, StringIO


Expand Down Expand Up @@ -831,6 +832,12 @@ def unpack(self, data, **kwargs):
self._value = self.substructure()
return self._value.unpack(data, **kwargs)

def setval(self, value):
if isinstance(value, suitcase.structure.Structure):
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps I am thinking slowly today. Is there some instance where this would not be the case? I think this change makes sense, but I'm wondering if it might make more sense to do something like this instead:

def setval(self, value):
    if not isinstance(value, self.substructure):
        raise TypeError("SubstructureField expected %r instance, got %r" % (self.substructure, type(value)))
    else:
        self._value = value

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, I was thinking slowly. I see what you are trying to do here.

I've been thinking for awhile about having a way to instantiate new Structures without having to assign to each field individually. I think that could help with this problem as well. This would look something like this:

# Arguments for creating a new structure with fields initialized
Structure(*positional_field_values, **field_values_by_key)

# Assign to members of existing structure in memory
structure.set(*positional_field_values, **field_values_by_key)

The .set variant could be used in this context. I don't think overloading the normal assignment operator makes sense as many structures have multiple fields.

Does this sound like an acceptable approach?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So what I was thinking was:

  1. Assigning a non-Structure in place of an arbitrary Structure should raise an exception, rather than overwriting it. Admittedly, there are other ways to do that.
  2. A user is less likely to want to assigned directly to a larger, multi-field structure.
  3. Smaller structures (like strings or RGB colors, etc.) could be more convenient to work with if the Structure subclass handled converting from a Python-native data structure to one that will be packed.

Anyway, maybe it's a little too magical, but I definitely think an exception should be thrown for overwriting a Structure with a non-Structure.

BaseField.setval(self, value)
else:
self._value.setval(value)


class FieldArray(BaseField):
"""Field which contains a list of some other field.
Expand Down