Skip to content

Commit

Permalink
Update lorem.py to Python 3 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
alexchantastic committed Mar 18, 2022
1 parent 7d32e83 commit c82bf92
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions lib/lorem/lorem.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import itertools
import os
import random
import typing

__all__ = [
'set_pool',
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -414,4 +433,4 @@ def set_pool(pool):
"""
global _TEXT
_TEXT = pool
_TEXT = pool

0 comments on commit c82bf92

Please sign in to comment.