Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added typing information and fixed bug in sentence, paragraph, and text functions #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 63 additions & 7 deletions lorem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,79 @@
The project was initiated by Stefan Fischer.

"""
import sys

if sys.version_info.minor >= 9:
# INFO: See this for more info
# https://www.python.org/dev/peps/pep-0585/
from collections.abc import Sequence
Tuple = tuple
else:
from typing import Tuple, Sequence
from .text import TextLorem as _Lorem

__author__ = 'Stefan Fischer'
__email__ = '[email protected]'
__version__ = '0.1.1'
__version__ = '0.1.2'

__all__ = ['sentence', 'paragraph', 'text']


def sentence(*args, **kwargs):
return _Lorem().sentence(*args, **kwargs)
def sentence(
wsep: str = ' ',
ssep: str = ' ',
psep: str = '\n\n',
srange: Tuple[int, int] = (4, 8),
prange: Tuple[int, int] = (5, 10),
trange: Tuple[int, int] = (3, 6),
words: Sequence[str] = None
) -> str:
return _Lorem(
wsep=wsep,
ssep=ssep,
psesp=psep,
srange=srange,
prange=prange,
trange=trange,
words=words
).sentence()


def paragraph(*args, **kwargs):
return _Lorem().paragraph(*args, **kwargs)
def paragraph(
wsep: str = ' ',
ssep: str = ' ',
psep: str = '\n\n',
srange: Tuple[int, int] = (4, 8),
prange: Tuple[int, int] = (5, 10),
trange: Tuple[int, int] = (3, 6),
words: Sequence[str] = None
):
return _Lorem(
wsep=wsep,
ssep=ssep,
psesp=psep,
srange=srange,
prange=prange,
trange=trange,
words=words
).paragraph()


def text(*args, **kwargs):
return _Lorem().text(*args, **kwargs)
def text(
wsep: str = ' ',
ssep: str = ' ',
psep: str = '\n\n',
srange: Tuple[int, int] = (4, 8),
prange: Tuple[int, int] = (5, 10),
trange: Tuple[int, int] = (3, 6),
words: Sequence[str] = None
):
return _Lorem(
wsep=wsep,
ssep=ssep,
psesp=psep,
srange=srange,
prange=prange,
trange=trange,
words=words
).text()
22 changes: 15 additions & 7 deletions lorem/text.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import random

from . import Sequence, Tuple
from .data import WORDS


class TextLorem():
def __init__(self, wsep=' ', ssep=' ', psep='\n\n',
srange=(4, 8), prange=(5, 10), trange=(3, 6),
words=None):
def __init__(
self,
wsep: str = ' ',
ssep: str = ' ',
psep: str = '\n\n',
srange: Tuple[int, int] = (4, 8),
prange: Tuple[int, int] = (5, 10),
trange: Tuple[int, int] = (3, 6),
words: Sequence[str] = None
):
self._wsep = wsep
self._ssep = ssep
self._psep = psep
Expand All @@ -18,20 +26,20 @@ def __init__(self, wsep=' ', ssep=' ', psep='\n\n',
else:
self._words = WORDS

def sentence(self):
def sentence(self) -> str:
n = random.randint(*self._srange)
s = self._wsep.join(self._word() for _ in range(n))
return s[0].upper() + s[1:] + '.'

def paragraph(self):
def paragraph(self) -> str:
n = random.randint(*self._prange)
p = self._ssep.join(self.sentence() for _ in range(n))
return p

def text(self):
def text(self) -> str:
n = random.randint(*self._trange)
t = self._psep.join(self.paragraph() for _ in range(n))
return t

def _word(self):
def _word(self) -> str:
return random.choice(self._words)