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

add one_tuple

parent 1ce4eab9
No related branches found
Tags v2.9.5
No related merge requests found
......@@ -2,3 +2,4 @@
* more pydoc for `satella.files`
* add `try_unlink`
* add `one_tuple`
......@@ -7,6 +7,11 @@ split_shuffle_and_join
.. autofunction:: satella.coding.transforms.split_shuffle_and_join
one_tuple
---------
.. autofunction:: satella.coding.transforms.one_tuple
stringify
---------
......
__version__ = '2.9.5_a2'
__version__ = '2.9.5'
......@@ -2,7 +2,8 @@ import collections
import random
import typing as tp
__all__ = ['stringify', 'split_shuffle_and_join']
__all__ = ['stringify', 'split_shuffle_and_join', 'one_tuple']
def _stringify_none(str_none, stringifier):
......@@ -14,6 +15,21 @@ def _stringify_none(str_none, stringifier):
T = tp.TypeVar('T')
def one_tuple(x: tp.Iterable[T]) -> tp.Iterator[tp.Tuple[T]]:
"""
Change a sequence of iterables into a sequence that displays each element as
a part of one-element tuple. Essentially syntactic sugar for:
>>> for y in x:
>>> yield y,
:param x: sequence to tupleify
:return: a iterator of one-element tuples
"""
for y in x:
yield y,
def split_shuffle_and_join(entries: tp.List[T],
whether_to_shuffle: tp.Callable[[T], bool] = lambda x: True,
not_shuffled_to_front: bool = True) -> tp.List[T]:
......
import unittest
from satella.coding.transforms import stringify, split_shuffle_and_join
from satella.coding.transforms import stringify, split_shuffle_and_join, one_tuple
class MyTestCase(unittest.TestCase):
def test_one_tuple(self):
self.assertEqual(list(one_tuple([1, 2, 3])), [(1, ), (2, ), (3, )])
def test_split_shuffle_and_join(self):
x = [1, 2, 3, 4]
x = split_shuffle_and_join(x, lambda y: y % 2 == 0)
......
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