From c82bf923aa573497c2cfc4cdcd31baf9895f6e3e Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Fri, 18 Mar 2022 09:08:01 -0700 Subject: [PATCH] Update lorem.py to Python 3 compatibility --- lib/lorem/lorem.py | 67 +++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 24 deletions(-) diff --git a/lib/lorem/lorem.py b/lib/lorem/lorem.py index 21eb479..0ae18c6 100755 --- a/lib/lorem/lorem.py +++ b/lib/lorem/lorem.py @@ -87,6 +87,7 @@ import itertools import os import random +import typing __all__ = [ 'set_pool', @@ -104,7 +105,7 @@ 'sint', 'sit', 'sunt', 'tempor', 'ullamco', 'ut', 'velit', 'veniam', 'voluptate') -def _gen_pool(dupe = 1): +def _gen_pool(dupe: int = 1) -> typing.Iterator[str]: """Generate word pool. Args: @@ -125,8 +126,9 @@ def _gen_pool(dupe = 1): random.shuffle(pool) -def _gen_word(pool, # pylint: disable=dangerous-default-value - func = None, args = (), kwargs = {}): +def _gen_word(pool: typing.Iterator[str], # pylint: disable=dangerous-default-value + func: typing.Optional[typing.Union[str, typing.Callable[[str], str]]] = None, + args: typing.Tuple[str] = (), kwargs: typing.Dict[str, str] = {}) -> str: """Generate random word. Args: @@ -150,7 +152,9 @@ def _gen_word(pool, # pylint: disable=dangerous-default-value return text -def _gen_sentence(pool, comma, word_range): +def _gen_sentence(pool: typing.Iterator[str], + comma: typing.Tuple[int], + word_range: typing.Tuple[int]) -> str: """Generate random sentence. Args: @@ -180,7 +184,10 @@ def _gen_sentence(pool, comma, word_range): return text + '.' -def _gen_paragraph(pool, comma, word_range, sentence_range): +def _gen_paragraph(pool: typing.Iterator[str], + comma: typing.Tuple[int], + word_range: typing.Tuple[int], + sentence_range: typing.Tuple[int]) -> str: """Generate random paragraph. Args: @@ -204,8 +211,9 @@ def _gen_paragraph(pool, comma, word_range, sentence_range): return text -def word(count = 1, # pylint: disable=dangerous-default-value - func = None, args = (), kwargs = {}): +def word(count: int = 1, # pylint: disable=dangerous-default-value + func: typing.Optional[typing.Union[str, typing.Callable[[str], str]]] = None, + args: typing.Tuple[str] = (), kwargs: typing.Dict[str, str] = {}) -> typing.Iterator[str]: """Generate a list of random words. .. code:: python @@ -230,14 +238,15 @@ def word(count = 1, # pylint: disable=dangerous-default-value """ pool = _gen_pool(count) - for word in itertools.cycle(_gen_word(pool=pool, + yield from itertools.cycle(_gen_word(pool=pool, func=func, args=args, - kwargs=kwargs) for _ in range(count)): - yield word + kwargs=kwargs) for _ in range(count)) -def sentence(count = 1, comma = (0, 2), word_range = (4, 8)): +def sentence(count: int = 1, + comma: typing.Tuple[int] = (0, 2), + word_range: typing.Tuple[int] = (4, 8)) -> typing.Iterator[str]: """Generate a list of random sentences. .. code:: python @@ -258,13 +267,15 @@ def sentence(count = 1, comma = (0, 2), word_range = (4, 8)): """ pool = _gen_pool(count * random.randint(*word_range)) - for sentence in itertools.cycle(_gen_sentence(pool=pool, + yield from itertools.cycle(_gen_sentence(pool=pool, comma=comma, - word_range=word_range) for _ in range(count)): - yield sentence + word_range=word_range) for _ in range(count)) -def paragraph(count = 1, comma = (0, 2), word_range = (4, 8), sentence_range = (5, 10)): +def paragraph(count: int = 1, + comma: typing.Tuple[int] = (0, 2), + word_range: typing.Tuple[int] = (4, 8), + sentence_range: typing.Tuple[int] = (5, 10)) -> typing.Iterator[str]: """Generate a list of random paragraphs. .. code:: python @@ -295,15 +306,16 @@ def paragraph(count = 1, comma = (0, 2), word_range = (4, 8), sentence_range = ( """ pool = _gen_pool(count * random.randint(*word_range) * random.randint(*sentence_range)) - for paragraph in itertools.cycle(_gen_paragraph(pool=pool, + yield from itertools.cycle(_gen_paragraph(pool=pool, comma=comma, word_range=word_range, - sentence_range=sentence_range) for _ in range(count)): - yield paragraph + sentence_range=sentence_range) for _ in range(count)) -def get_word(count = 1, # pylint: disable=dangerous-default-value - sep = ' ', func = None, args = (), kwargs = {}): +def get_word(count: typing.Union[int, typing.Tuple[int]] = 1, # pylint: disable=dangerous-default-value + sep: str = ' ', + func: typing.Optional[typing.Union[str, typing.Callable[[str], str]]] = None, + args: typing.Tuple[str] = (), kwargs: typing.Dict[str, str] = {}) -> str: """Return random words. .. code:: python @@ -335,7 +347,10 @@ def get_word(count = 1, # pylint: disable=dangerous-default-value return sep.join(itertools.islice(word(count, func, args, kwargs), count)) -def get_sentence(count = 1, sep = ' ', comma = (0, 2), word_range = (4, 8)): +def get_sentence(count: typing.Union[int, typing.Tuple[int]] = 1, + sep: str = ' ', + comma: typing.Tuple[int] = (0, 2), + word_range: typing.Tuple[int] = (4, 8)) -> str: """Return random sentences. .. code:: python @@ -365,7 +380,11 @@ def get_sentence(count = 1, sep = ' ', comma = (0, 2), word_range = (4, 8)): return sep.join(itertools.islice(sentence(count, comma, word_range), count)) -def get_paragraph(count = 1, sep = os.linesep, comma = (0, 2), word_range = (4, 8), sentence_range = (5, 10)): +def get_paragraph(count: typing.Union[int, typing.Tuple[int]] = 1, + sep: str = os.linesep, + comma: typing.Tuple[int] = (0, 2), + word_range: typing.Tuple[int] = (4, 8), + sentence_range: typing.Tuple[int] = (5, 10)) -> str: """Return random paragraphs. .. code:: python @@ -405,7 +424,7 @@ def get_paragraph(count = 1, sep = os.linesep, comma = (0, 2), word_range = (4, return sep.join(itertools.islice(paragraph(count, comma, word_range, sentence_range), count)) -def set_pool(pool): +def set_pool(pool: typing.Iterable[str]): """Customise random word pool. Args: @@ -414,4 +433,4 @@ def set_pool(pool): """ global _TEXT - _TEXT = pool + _TEXT = pool \ No newline at end of file