Skip to content
Snippets Groups Projects
Commit 82dd22ae authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added random_word

parent 28c502bb
No related branches found
No related tags found
No related merge requests found
# v2.17.21 # v2.17.21
* added random_word
...@@ -11,3 +11,8 @@ random_binary ...@@ -11,3 +11,8 @@ random_binary
------------- -------------
.. autofunction:: satella.random.random_binary .. autofunction:: satella.random.random_binary
random_word
-----------
.. autofunction:: satella.random.random_word
__version__ = '2.17.21a1' __version__ = '2.17.21a2'
import os import os
import random import random
import typing as tp import typing as tp
import string
__all__ = ['shuffle_together', 'random_binary'] __all__ = ['shuffle_together', 'random_binary', 'random_word']
from satella.coding.typing import T
def random_word(length: int, choice: T = string.ascii_lowercase,
join_fun: tp.Callable[[T, ...], T] = lambda *args: ''.join(args)):
"""
Build and return a random word of provided length.
The word will be built by calling join_fun with length of arguments picked
at random from choice.
:param length: length of the word
:param choice: a range of characters to use. By default is string.ascii_lowercase
:param join_fun: an argument to be called with randomly picked characters as *args.
Defaults to ''.join(args), so your T must be a string. If you're passing a
different type, remember to alter this function because the default one expects strings!
:return: a random word
"""
return join_fun(*(random.choice(choice) for _ in range(length)))
def random_binary(length: int) -> bytes: def random_binary(length: int) -> bytes:
......
import unittest import unittest
from satella.random import shuffle_together, random_binary from satella.random import shuffle_together, random_binary, random_word
import string
class TestRandom(unittest.TestCase): class TestRandom(unittest.TestCase):
def test_random_word(self):
rand_word = random_word(5)
self.assertEqual(len(rand_word), 5)
self.assertTrue(all(y in string.ascii_lowercase for y in rand_word))
def test_random_binary(self): def test_random_binary(self):
self.assertFalse(random_binary(0)) self.assertFalse(random_binary(0))
self.assertEqual(len(random_binary(10)), 10) self.assertEqual(len(random_binary(10)), 10)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment