From 50eb9f8b9dfae88ee32aac06cd6ce46345d6c1fb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Fri, 5 Jun 2020 19:06:43 +0200
Subject: [PATCH] add random.shuffle_together

---
 CHANGELOG.md         |  1 +
 docs/index.rst       |  1 +
 docs/random.rst      |  4 ++++
 satella/__init__.py  |  2 +-
 satella/random.py    | 26 ++++++++++++++++++++++++++
 tests/test_random.py | 10 ++++++++++
 6 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 docs/random.rst
 create mode 100644 satella/random.py
 create mode 100644 tests/test_random.py

diff --git a/CHANGELOG.md b/CHANGELOG.md
index d99e3acd..88c20d23 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,2 +1,3 @@
 # v2.7.47
 
+added `random.shuffle_together`
diff --git a/docs/index.rst b/docs/index.rst
index 2a7504fc..28eff3ed 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -24,6 +24,7 @@ Visit the project's page at GitHub_!
            posix
            import
            files
+           random
            time
            exceptions
            processes
diff --git a/docs/random.rst b/docs/random.rst
new file mode 100644
index 00000000..5652fb6c
--- /dev/null
+++ b/docs/random.rst
@@ -0,0 +1,4 @@
+shuffle_together
+================
+
+.. autofunction:: satella.random.shuffle_together
diff --git a/satella/__init__.py b/satella/__init__.py
index a0c7fdb6..19935a15 100644
--- a/satella/__init__.py
+++ b/satella/__init__.py
@@ -1 +1 @@
-__version__ = '2.7.47_a1'
+__version__ = '2.7.47'
diff --git a/satella/random.py b/satella/random.py
new file mode 100644
index 00000000..5e1eb649
--- /dev/null
+++ b/satella/random.py
@@ -0,0 +1,26 @@
+import random
+import typing as tp
+
+__all__ = ['shuffle_together']
+
+
+def shuffle_together(*args: tp.Sequence) -> tp.List:
+    """
+    args, being sequences of equal length, will be permuted in such a way
+    that their indices will still correspond to each other.
+
+    So given:
+
+    >>> a = [1, 2, 3]
+    >>> b = ['a', 'b', 'c']
+    >>> c = permute_together(a, b)
+
+    Might equal
+
+    >>> c == ([3, 1, 2], ['c', 'a', 'b'])
+    """
+
+    indices = list(range(len(args[0])))
+
+    random.shuffle(indices)
+    return [[arg[i] for arg, i in zip(arg, indices)] for arg in args]
diff --git a/tests/test_random.py b/tests/test_random.py
new file mode 100644
index 00000000..a282dd81
--- /dev/null
+++ b/tests/test_random.py
@@ -0,0 +1,10 @@
+import unittest
+from satella.random import shuffle_together
+
+
+class TestRandom(unittest.TestCase):
+    def test_random(self):
+        a = [(1, 2, 3), ('a', 'b', 'c')]
+        b = zip(shuffle_together(*a))
+        self.assertEqual(set(b), {(1, 'a'), (2, 'b'), (3, 'c')})
+
-- 
GitLab