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

Fix set validator #52

Merged
merged 9 commits into from Jan 2, 2018
Merged

Fix set validator #52

merged 9 commits into from Jan 2, 2018

Conversation

syxolk
Copy link
Contributor

@syxolk syxolk commented Dec 22, 2017

Fix the issue with SetValidator in #50: The valid_set is no longer stringified as a whole instead only the first 100 elements are shown.

Some minor design decisions applied here:

  • Don't create any unnecessary lists. Use iterators instead.
  • Don't stringify what is not shown.

@@ -85,6 +85,7 @@ def test_set_validator_works(field_set, field):
([], 'bar'),
(['foo'], 'bar'),
(['foo', 'bar'], 'baz'),
([str(x) for x in range(200)], "notanumber"),
Copy link
Owner

Choose a reason for hiding this comment

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

Would you be able to add some actual tests for stringify_set instead of this?

Copy link
Contributor Author

@syxolk syxolk Dec 30, 2017

Choose a reason for hiding this comment

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

I just found out that stringify_set may not be deterministic since the output is dependent on the order of elements that are generated by the set iterator.

For Python 2.7.12:

$ python2
>>> {'A', 'B', 'C', 'D', 'E', 'F'}
set(['A', 'C', 'B', 'E', 'D', 'F'])

For Python 3.5.2: It can be different every time the python interpreter is started!

$ python3
>>> {'A', 'B', 'C', 'D', 'E', 'F'}
{'D', 'B', 'E', 'F', 'A', 'C'}
>>> exit()

$ python3
>>> {'A', 'B', 'C', 'D', 'E', 'F'}
{'C', 'F', 'E', 'A', 'B', 'D'}
>>> exit()

This makes it quite difficult for proper testing. Should we sort the elements of the set first? But then its quite inperformant if the set is large (see #50)

def stringify_set(a_set, max_len):
''' Stringify `max_len` elements of `a_set` and count the remainings '''
# Don't convert `a_set` to a list for performance reasons
text = "[{}]".format(", ".join(
Copy link
Owner

Choose a reason for hiding this comment

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

Small tweak, could you change this to "[{}]" to "{{{}}}" so that this prints the set in braces instead of brackets and is consistent with the way it was before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, that makes sense.

@syxolk
Copy link
Contributor Author

syxolk commented Jan 2, 2018

I fixed the issues you mentioned in the review.

Copy link
Owner

@di di left a comment

Choose a reason for hiding this comment

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

Small changes but this otherwise looks great!

@@ -208,3 +210,26 @@ def validate(self, field, row={}):
@property
def bad(self):
pass


def stringify_set(a_set, max_len, max_sort_size=8192):
Copy link
Owner

Choose a reason for hiding this comment

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

Could we rename this to _stringify_set to indicate that it is not part of the public API?

raise ValueError("max_len must be non-negative: {}".format(max_len))
if max_sort_size < 0:
raise ValueError(
"max_sort_size must be non-negative: {}".format(max_sort_size))
Copy link
Owner

@di di Jan 2, 2018

Choose a reason for hiding this comment

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

I appreciate these checks but since this is just a utility function for us and doesn't take user input, I think they're unnecessary. We can just drop d355def entirely.

@di di merged commit 8c75eda into di:master Jan 2, 2018
@di
Copy link
Owner

di commented Jan 2, 2018

Thanks @syxolk!

@syxolk syxolk deleted the fix-set-validator branch January 2, 2018 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants