Skip to content

Commit adfbfa6

Browse files
committed
New distribution [1.1.0]
* `word`, `sentence`, `paragraph` will return *indefinite* iterators * added `set_pool` to customise word pool * updated test cases correspondingly
1 parent 0a9f5a0 commit adfbfa6

File tree

4 files changed

+114
-45
lines changed

4 files changed

+114
-45
lines changed

README.md

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* [Get random paragraphs](#get-random-paragraphs)
2222
* [`paragraph` -- renerate a list of random paragraphs](#paragraph)
2323
* [`get_paragraph`-- return random paragraphs](#get_paragraph)
24+
* [Customise word pool](#customise-word-pool)
25+
* [`set_pool` -- customise random word pool](#set_pool)
2426
* [Internal APIs](#internal-apis)
2527
* [`_TEXT` -- original *lorem ipsum* text pool](#_text)
2628
* [`_gen_word` -- generate random word](#_gen_word)
@@ -86,19 +88,19 @@ The `lorem` module provides two different ways for getting random words.
8688
Generate a list of random words.
8789

8890
```python
89-
>>> list(word(count=3))
91+
>>> list(itertools.islice(word(count=3), 3))
9092
['labore', 'tempor', 'commodo']
91-
>>> list(word(count=3, func='capitalize'))
93+
>>> list(itertools.islice(word(count=3, func='capitalize'), 3))
9294
['Ea', 'Lorem', 'Do']
93-
>>> list(word(count=3, func=lambda s: s.upper()))
95+
>>> list(itertools.islice(word(count=3, func=lambda s: s.upper()), 3))
9496
['UT', 'AMET', 'EXCEPTEUR']
9597
```
9698

9799
- Args:
98100

99101
* `count` -- `int`
100102

101-
Number of random words.
103+
Number of non-repeated random words.
102104

103105
*default*: `1`
104106

@@ -123,7 +125,7 @@ The `lorem` module provides two different ways for getting random words.
123125

124126
- Returns:
125127

126-
* `Iterator[str]` -- random words generator
128+
* `Iterator[str]` -- indefinite random words generator
127129

128130
<a name="get_word"></a>
129131

@@ -194,15 +196,15 @@ The `lorem` module provides two different ways for getting random sentences.
194196
Generate a list of random sentences.
195197

196198
```python
197-
>>> list(sentence())
199+
>>> list(itertools.islice(sentence(), 1))
198200
['Aute irure et commodo sunt do duis dolor.']
199201
```
200202

201203
- Args:
202204

203205
* `count` -- `int`
204206

205-
Number of random sentences.
207+
Number of non-repeated random sentences.
206208

207209
*default*: `1`
208210

@@ -222,7 +224,7 @@ The `lorem` module provides two different ways for getting random sentences.
222224

223225
- Returns:
224226

225-
* `Iterator[str]` -- random sentence generator
227+
* `Iterator[str]` -- indefinite random sentence generator
226228

227229
<a name="get_sentence"></a>
228230

@@ -286,7 +288,7 @@ The `lorem` module provides two different ways for getting random paragraphs.
286288
Generate a list of random paragraphs.
287289

288290
```python
289-
>>> list(paragraph())
291+
>>> list(itertools.islice(paragraph(), 1))
290292
['Aute sint et cupidatat aliquip. Non exercitation est aliquip voluptate '
291293
'fugiat, reprehenderit ad occaecat laboris velit consequat. Magna enim '
292294
'deserunt aute laborum fugiat exercitation. Aliqua ex sunt fugiat in '
@@ -300,7 +302,7 @@ The `lorem` module provides two different ways for getting random paragraphs.
300302

301303
* `count` -- `int`
302304

303-
Number of random paragraphs.
305+
Number of non-repeated random paragraphs.
304306

305307
*default*: `1`
306308

@@ -328,7 +330,7 @@ The `lorem` module provides two different ways for getting random paragraphs.
328330

329331
- Returns:
330332

331-
* `Iterator[str]` -- random paragraph generator
333+
* `Iterator[str]` -- indefinite random paragraph generator
332334

333335
<a name="get_paragraph"></a>
334336

@@ -393,6 +395,25 @@ The `lorem` module provides two different ways for getting random paragraphs.
393395

394396
* `str` -- random paragraphs
395397

398+
### Customise word pool
399+
400+
If wanted, the `lorem` module also provides an interface to customise the word
401+
pool as you wish.
402+
403+
<a name="set_pool"></a>
404+
405+
1. ```python
406+
def set_pool(pool): ...
407+
```
408+
409+
Customise random word pool.
410+
411+
- Args:
412+
413+
* `pool` -- `Iterable[str]`
414+
415+
List of words to be used as random word pool.
416+
396417
### Internal APIs
397418

398419
Following are internal APIs for the `lorem` module.

lorem.py

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,26 @@
7171
get_paragraph(count=1, sep=os.linesep, comma=(0, 2), word_range=(4, 8), sentence_range=(5, 10)) -> Union[str]
7272
```
7373
74+
Customise Word Pool
75+
-------------------
76+
77+
If wanted, the `lorem` module also provides an interface to customise the word
78+
pool as you wish.
79+
80+
1. `set_pool` -- customise random word pool
81+
82+
```python
83+
set_pool(pool)
84+
```
85+
7486
"""
87+
import itertools
7588
import os
7689
import random
7790
import typing
7891

7992
__all__ = [
93+
'set_pool',
8094
'word', 'sentence', 'paragraph',
8195
'get_word', 'get_sentence', 'get_paragraph',
8296
]
@@ -119,7 +133,7 @@ def _gen_pool(dupe: int = 1) -> typing.Iterator[str]:
119133

120134

121135
def _gen_word(pool: typing.Iterator[str], # pylint: disable=dangerous-default-value
122-
func: typing.Optional[typing.Union[str, typing.Callable[[str], str]]],
136+
func: typing.Optional[typing.Union[str, typing.Callable[[str], str]]] = None,
123137
args: typing.Tuple[str] = (), kwargs: typing.Dict[str, str] = {}) -> str:
124138
"""Generate random word.
125139
@@ -134,6 +148,8 @@ def _gen_word(pool: typing.Iterator[str], # pylint: disable=dangerous-default-v
134148
Filter function. It can be an attribute name of `str`, or a customised
135149
function that takes the original `str` and returns the modified `str`.
136150
151+
*default*: `None`
152+
137153
* `args` -- `Tuple[str]`
138154
139155
Additional positional arguments for `func`.
@@ -153,7 +169,7 @@ def _gen_word(pool: typing.Iterator[str], # pylint: disable=dangerous-default-v
153169
"""
154170
text = next(pool)
155171
if func is not None:
156-
if isinstance(func, str) and hasattr(text, func):
172+
if isinstance(func, str):
157173
text = getattr(text, func)(*args, **kwargs)
158174
else:
159175
text = func(text, *args, **kwargs)
@@ -192,14 +208,14 @@ def _gen_sentence(pool: typing.Iterator[str],
192208
"""
193209
text = _gen_word(pool=pool, func='capitalize')
194210
for _ in range(random.randint(*word_range) - 1):
195-
text += ' ' + _gen_word(pool=pool, func=None)
211+
text += ' ' + _gen_word(pool=pool)
196212

197213
for _ in range(random.randint(*comma)):
198214
include_comma = random.choice([True, False])
199215
if include_comma:
200216
text += ','
201217
for _ in range(random.randint(*word_range)):
202-
text += ' ' + _gen_word(pool=pool, func=None)
218+
text += ' ' + _gen_word(pool=pool)
203219
continue
204220
break
205221
return text + '.'
@@ -256,19 +272,19 @@ def word(count: int = 1, # pylint: disable=dangerous-default-value
256272
"""Generate a list of random words.
257273
258274
```python
259-
>>> list(word(count=3))
275+
>>> list(itertools.cycle(word(count=3), 3))
260276
['labore', 'tempor', 'commodo']
261-
>>> list(word(count=3, func='capitalize'))
277+
>>> list(itertools.cycle(word(count=3, func='capitalize'), 3))
262278
['Ea', 'Lorem', 'Do']
263-
>>> list(word(count=3, func=lambda s: s.upper()))
279+
>>> list(itertools.cycle(word(count=3, func=lambda s: s.upper()), 3))
264280
['UT', 'AMET', 'EXCEPTEUR']
265281
```
266282
267283
- Args:
268284
269285
* `count` -- `int`
270286
271-
Number of random words.
287+
Number of non-repeated random words.
272288
273289
*default*: `1`
274290
@@ -293,14 +309,14 @@ def word(count: int = 1, # pylint: disable=dangerous-default-value
293309
294310
- Returns:
295311
296-
* `Iterator[str]` -- random words generator
312+
* `Iterator[str]` -- indefinite random words generator
297313
298314
"""
299315
pool = _gen_pool(count)
300-
yield from (_gen_word(pool=pool,
301-
func=func,
302-
args=args,
303-
kwargs=kwargs) for _ in range(count))
316+
yield from itertools.cycle(_gen_word(pool=pool,
317+
func=func,
318+
args=args,
319+
kwargs=kwargs) for _ in range(count))
304320

305321

306322
def sentence(count: int = 1,
@@ -309,15 +325,15 @@ def sentence(count: int = 1,
309325
"""Generate a list of random sentences.
310326
311327
```python
312-
>>> list(sentence())
328+
>>> list(itertools.islice(sentence(), 1))
313329
['Aute irure et commodo sunt do duis dolor.']
314330
```
315331
316332
- Args:
317333
318334
* `count` -- `int`
319335
320-
Number of random sentences.
336+
Number of non-repeated random sentences.
321337
322338
*default*: `1`
323339
@@ -337,13 +353,13 @@ def sentence(count: int = 1,
337353
338354
- Returns:
339355
340-
* `Iterator[str]` -- random sentence generator
356+
* `Iterator[str]` -- indefinite random sentence generator
341357
342358
"""
343359
pool = _gen_pool(count * random.randint(*word_range))
344-
yield from (_gen_sentence(pool=pool,
345-
comma=comma,
346-
word_range=word_range) for _ in range(count))
360+
yield from itertools.cycle(_gen_sentence(pool=pool,
361+
comma=comma,
362+
word_range=word_range) for _ in range(count))
347363

348364

349365
def paragraph(count: int = 1,
@@ -353,7 +369,7 @@ def paragraph(count: int = 1,
353369
"""Generate a list of random paragraphs.
354370
355371
```python
356-
>>> list(paragraph())
372+
>>> list(itertools.islice(paragraph(), 1))
357373
['Aute sint et cupidatat aliquip. Non exercitation est aliquip voluptate '
358374
'fugiat, reprehenderit ad occaecat laboris velit consequat. Magna enim '
359375
'deserunt aute laborum fugiat exercitation. Aliqua ex sunt fugiat in '
@@ -367,7 +383,7 @@ def paragraph(count: int = 1,
367383
368384
* `count` -- `int`
369385
370-
Number of random paragraphs.
386+
Number of non-repeated random paragraphs.
371387
372388
*default*: `1`
373389
@@ -399,10 +415,10 @@ def paragraph(count: int = 1,
399415
400416
"""
401417
pool = _gen_pool(count * random.randint(*word_range) * random.randint(*sentence_range))
402-
yield from (_gen_paragraph(pool=pool,
403-
comma=comma,
404-
word_range=word_range,
405-
sentence_range=sentence_range) for _ in range(count))
418+
yield from itertools.cycle(_gen_paragraph(pool=pool,
419+
comma=comma,
420+
word_range=word_range,
421+
sentence_range=sentence_range) for _ in range(count))
406422

407423

408424
def get_word(count: int = 1, # pylint: disable=dangerous-default-value
@@ -462,7 +478,7 @@ def get_word(count: int = 1, # pylint: disable=dangerous-default-value
462478
"""
463479
if isinstance(count, tuple):
464480
count = random.randint(*count)
465-
return sep.join(word(count, func, args, kwargs))
481+
return sep.join(itertools.islice(word(count, func, args, kwargs), count))
466482

467483

468484
def get_sentence(count: int = 1,
@@ -514,7 +530,7 @@ def get_sentence(count: int = 1,
514530
"""
515531
if isinstance(count, tuple):
516532
count = random.randint(*count)
517-
return sep.join(sentence(count, comma, word_range))
533+
return sep.join(itertools.islice(sentence(count, comma, word_range), count))
518534

519535

520536
def get_paragraph(count: int = 1,
@@ -582,4 +598,18 @@ def get_paragraph(count: int = 1,
582598
"""
583599
if isinstance(count, tuple):
584600
count = random.randint(*count)
585-
return sep.join(paragraph(count, comma, word_range, sentence_range))
601+
return sep.join(itertools.islice(paragraph(count, comma, word_range, sentence_range), count))
602+
603+
604+
def set_pool(pool: typing.Iterable[str]):
605+
"""Customise random word pool.
606+
607+
- Args:
608+
609+
* `pool` -- `Iterable[str]`
610+
611+
List of words to be used as random word pool.
612+
613+
"""
614+
global _TEXT
615+
_TEXT = pool

setup.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,22 @@
7070
```python
7171
get_paragraph(count=1, comma=(0, 2), word_range=(4, 8), sentence_range=(5, 10), sep=os.linesep) -> Union[str]
7272
```
73+
74+
Customise Word Pool
75+
-------------------
76+
77+
If wanted, the `lorem` module also provides an interface to customise the word
78+
pool as you wish.
79+
80+
1. `set_pool` -- customise random word pool
81+
82+
```python
83+
set_pool(pool)
84+
```
85+
7386
"""
7487
# version string
75-
__version__ = '1.0.0'
88+
__version__ = '1.1.0'
7689

7790
# setup attributes
7891
attrs = dict(

0 commit comments

Comments
 (0)