API Reference¶
StringGen¶
string_gen.StringGen
¶
StringGen(
pattern: Union[Pattern, AnyStr],
seed: SeedType = None,
*,
max_repeat: Union[int, None] = None,
alphabet: Union[str, None] = None,
)
Random string generator driven by a regular expression pattern.
Compiles the given regex and produces random strings that match it. Supports literals, character classes, quantifiers, groups, alternation, anchors, lookahead assertions, and backreferences.
The max_repeat parameter controls the upper bound for unbounded
quantifiers (*, +, {n,}). It can be set per-instance
via the constructor or globally via :func:configure.
Instances are iterable: iterating yields an infinite stream of random
matching strings. Use :meth:stream for a bounded lazy generator.
Instances can be combined with | to concatenate patterns, compared
with == against other patterns, and tested for emptiness with bool().
render_list
¶
Produce a list of random strings matching the pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of strings to generate. |
required |
render_set
¶
Produce a set of unique random strings matching the pattern.
Keeps generating until count unique strings are collected or max_iteration attempts are exhausted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Required number of unique strings. |
required |
max_iteration
|
int
|
Maximum generation attempts before giving up. |
100000
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_iteration is less than count. |
StringGenMaxIterationsReachedError
|
If the iteration limit is reached before collecting enough unique strings. |
stream
¶
Return a lazy iterator that yields count random matching strings.
Unlike :meth:render_list, strings are generated one at a time and
never collected into a list, making stream memory-efficient for
large batches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Number of strings to yield. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If count is negative. .. code-block:: python |
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.
enumerate
¶
Yield all unique strings the pattern can produce.
For patterns with unbounded quantifiers, limit caps the maximum
repetition count. When limit is None the parser's
max_repeat value is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
limit
|
Union[int, None]
|
Maximum repetitions for unbounded quantifiers. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If limit is less than 1. |
seed
¶
Re-seed the internal random number generator.
Calling with the same seed produces the same sequence of rendered strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
SeedType
|
Seed value accepted by |
None
|
configure¶
string_gen.configure
¶
Configure global defaults for new StringGen instances.
Settings apply to all subsequently created instances unless overridden in the constructor. Omit a parameter to leave it unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_repeat
|
Union[int, None]
|
Maximum repetitions for unbounded quantifiers
( |
None
|
alphabet
|
Union[str, None]
|
Custom alphabet string replacing |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_repeat is less than 1. |
TypeError
|
If alphabet is not a non-empty string. |
reset¶
Exceptions¶
StringGenError¶
string_gen.StringGenError
¶
Bases: Exception
Base exception for all string-gen errors.
StringGenPatternError¶
string_gen.StringGenPatternError
¶
Bases: StringGenError
Exception raised when an invalid regex pattern is provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
Union[Pattern, AnyStr]
|
The pattern that failed to compile. |
required |
StringGenMaxIterationsReachedError¶
string_gen.StringGenMaxIterationsReachedError
¶
Bases: StringGenError
Exception raised when render_set exhausts its iteration budget.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_iterations
|
int
|
The iteration limit that was reached. |
required |