Generation Methods¶
render()¶
Produce a single random string matching the pattern.
render_list()¶
Produce a list of count random strings. May contain duplicates.
render_set()¶
Produce a set of count unique strings. Raises StringGenMaxIterationsReachedError if the iteration limit is reached before collecting enough unique values.
The optional max_iteration parameter controls the maximum number of generation attempts (default: 100,000):
If the pattern cannot produce enough unique strings, a ValueError is raised immediately:
gen = StringGen(r'[ab]')
gen.render_set(5) # ValueError: Cannot generate 5 unique strings: pattern can only produce 4
Iteration¶
StringGen instances are iterable. Iterating yields an infinite stream of random matching strings:
gen = StringGen(r'\d{4}')
for value in gen:
print(value) # e.g. '8374'
break # without break, iterates forever
Works with itertools:
from itertools import islice
gen = StringGen(r'\d{4}')
values = list(islice(gen, 10)) # 10 random strings
stream()¶
Return a lazy iterator that yields count random strings one at a time. Memory-efficient alternative to render_list for large batches:
count()¶
Return the number of unique strings the pattern can produce. Returns math.inf for patterns with unbounded quantifiers (*, +, {n,}). The result is cached after the first call.
gen = StringGen(r'[01]{3}')
gen.count() # 8
gen = StringGen(r'\d')
gen.count() # 10
gen = StringGen(r'\w+')
gen.count() # math.inf
enumerate()¶
Yield all unique strings the pattern can produce. Useful for exhaustive testing over finite patterns.
gen = StringGen(r'[ab]{2}')
list(gen.enumerate()) # ['aa', 'ab', 'ba', 'bb']
gen = StringGen(r'(yes|no)')
list(gen.enumerate()) # ['yes', 'no']
For patterns with unbounded quantifiers, limit caps the maximum repetition count. When limit is None, the parser's max_repeat value is used: