Skip to content

Commit

Permalink
Merge aeab86f into 53cd400
Browse files Browse the repository at this point in the history
  • Loading branch information
syxolk committed Jan 2, 2018
2 parents 53cd400 + aeab86f commit 9f03540
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
21 changes: 20 additions & 1 deletion vladiate/test/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from ..validators import (
CastValidator, EmptyValidator, FloatValidator, Ignore, IntValidator,
NotEmptyValidator, RangeValidator, RegexValidator, SetValidator,
UniqueValidator, Validator
UniqueValidator, Validator, _stringify_set
)
from ..exceptions import BadValidatorException, ValidationException

Expand Down Expand Up @@ -216,3 +216,22 @@ def test_base_class_raises():
def test_all_validators_support_empty_ok(validator_class, args):
validator = validator_class(*args, empty_ok=True)
validator.validate('')


@pytest.mark.parametrize('a_set, max_len, stringified', [
({'A', 'B', 'C'}, 4, "{'A', 'B', 'C'}"),
({'A', 'B', 'C'}, 3, "{'A', 'B', 'C'}"),
({'A', 'B', 'C'}, 2, "{'A', 'B'} (1 more suppressed)"),
({'A', 'B', 'C'}, 0, "{} (3 more suppressed)"),
({}, 5, "{}"),
({}, 0, "{}"),
])
def test_stringify_set(a_set, max_len, stringified):
assert _stringify_set(a_set, max_len) == stringified


def test_stringify_set_invalid_params():
with pytest.raises(ValueError):
_stringify_set({}, -1, 10)
with pytest.raises(ValueError):
_stringify_set({}, 10, -1)
27 changes: 26 additions & 1 deletion vladiate/validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from itertools import islice

from vladiate.exceptions import ValidationException, BadValidatorException

Expand Down Expand Up @@ -70,7 +71,8 @@ def validate(self, field, row={}):
if field not in self.valid_set:
self.invalid_set.add(field)
raise ValidationException(
"'{}' is not in {}".format(field, self.valid_set))
"'{}' is not in {}".format(field,
_stringify_set(self.valid_set, 100)))

@property
def bad(self):
Expand Down Expand Up @@ -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):
''' Stringify `max_len` elements of `a_set` and count the remainings
Small sets (len(a_set) <= max_sort_size) are displayed sorted.
Large sets won't be sorted for performance reasons.
This may result in an arbitrary ordering in the returned string.
'''
if max_len < 0:
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))
# Don't convert `a_set` to a list for performance reasons
text = "{{{}}}".format(", ".join(
"'{}'".format(value) for value in islice(
sorted(a_set) if len(a_set) <= max_sort_size else a_set,
max_len)
))
if len(a_set) > max_len:
text += " ({} more suppressed)".format(len(a_set) - max_len)
return text

0 comments on commit 9f03540

Please sign in to comment.