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

added random binary

parent 5fa2056b
No related branches found
No related tags found
No related merge requests found
# v2.14.34
* added iterate_callable
* added random_binary
......@@ -6,3 +6,8 @@ shuffle_together
----------------
.. autofunction:: satella.random.shuffle_together
random_binary
-------------
.. autofunction:: satella.random.random_binary
import os
import random
import typing as tp
__all__ = ['shuffle_together']
__all__ = ['shuffle_together', 'random_binary']
def random_binary(length: int) -> bytes:
"""
Return a random bytes string of given length.
An attempt will be made to utilize /dev/random, if exists
:param length: length of string to generate
"""
if os.path.exists('/dev/random'):
with open('/dev/random', 'rb') as f_in:
return f_in.read(length)
else:
return bytes([random.randint(0, 255) for _ in range(length)])
def shuffle_together(*args: tp.Sequence) -> tp.List[tp.List]:
......
import unittest
from satella.random import shuffle_together
from satella.random import shuffle_together, random_binary
class TestRandom(unittest.TestCase):
def test_random_binary(self):
self.assertFalse(random_binary(0))
self.assertEqual(len(random_binary(10)), 10)
def test_random(self):
a = [(1, 2, 3), ('a', 'b', 'c')]
b = zip(*shuffle_together(*a))
......
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