Skip to content

Commit

Permalink
Fix -- fake_email default max_size is too fragile (#59)
Browse files Browse the repository at this point in the history
* Fix -- fake_email default max_size is too fragile

* Fix fake_text

* Update changelog
  • Loading branch information
caioariede authored Dec 18, 2020
1 parent 7a02db6 commit 12dcdfa
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
14 changes: 8 additions & 6 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ Releases
:local:


master
~~~~~~

* Updated bulk_update method to use Django's built-in method if available
* Changed default ``max_size`` for ``fake_email`` to ``40``
* Fixed error in ``fake_text`` when ``max_size`` is too short


0.2
~~~

Expand All @@ -31,9 +39,3 @@ Releases
~~~

* Initial release


master
~~~~~~

* Updated bulk_update method to use Django's built-in method if available
15 changes: 9 additions & 6 deletions anon/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ def _cycle_over_sample_range(start, end, sample_size):
return itertools.cycle(random.sample(xrange(start, end), sample_size))


# Holds the average size of word sample
_avg_word_size = sum(map(len, _WORD_LIST)) / len(_WORD_LIST)
# Holds the maximum size of word sample
_max_word_size = max(len(s) for s in _WORD_LIST)

# Holds a generator that each iteration returns a different word
_word_generator = itertools.cycle(_WORD_LIST)
Expand Down Expand Up @@ -266,12 +266,15 @@ def fake_text(max_size=255, max_diff_allowed=5, separator=" "):
if max_diff_allowed < 1:
raise ValueError("max_diff_allowed must be > 0")

num_words = int(max_size / _avg_word_size)
num_words = max(1, int(max_size / _max_word_size))
words = itertools.islice(_word_generator, num_words)

text = separator.join(words)
while len(text) > max_size:
text = text[: text.rindex(separator)]
try:
while len(text) > max_size:
text = text[: text.rindex(separator)]
except ValueError:
text = text[:max_size]

return text

Expand Down Expand Up @@ -328,7 +331,7 @@ def fake_username(max_size=10, separator=""):
return fake_text(max_size, separator=separator) + random_number


def fake_email(max_size=25, suffix="@example.com"):
def fake_email(max_size=40, suffix="@example.com"):
""" Returns fake email address
:max_size: Maximum number of chars
Expand Down

0 comments on commit 12dcdfa

Please sign in to comment.