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
7588import os
7689import random
7790import 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
121135def _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
306322def 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
349365def 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
408424def 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
468484def 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
520536def 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
0 commit comments