Skip to main content

Python package Documentation Status

PyPI - Python Version PyPi Version

Generate test data, unique ids, passwords, vouchers or other randomized textual data very quickly using a template language. The template language is superficially similar to regular expressions but instead of defining how to match or capture strings, it defines how to generate randomized strings. A very simple invocation to produce a random string with word characters of 30 characters length:

from strgen import StringGenerator as SG
SG(r"[\w]{30}").render()
'wQjLVRIj1sjjslORpqLJyDObaCnDR2'

Full documentation

The current package requires Python 3.7 or higher. Use version 0.3.4 or earlier if you want to use Python 2.7 or an earlier Python 3 version.

NB: with version 0.4.2, the preferred method for generating a unique list is StringGenerator.render_set() instead of render_list(). Generate 50000 unique secure tokens in a few seconds:

secure_tokens = SG(r"[\p\w]{32}").render_set(50000)

render_set() does not support a progress callback.

Performance and secure generation

By default StringGenerator uses random.SystemRandom (cryptographically secure), which reads from the operating system entropy pool on every draw. That is the right default for passwords, keys and tokens, but the per-draw syscall makes very large batches slow.

For large batches you have two faster options:

  • If you do not need cryptographic randomness (e.g. test data), pass a seed or a plain random.Random to use the much faster Mersenne Twister:

    SG(r"[\d]{10}", seed=1).render_set(1_000_000)
  • If you do need cryptographic randomness, use BufferedSecureRandom. It draws the same os.urandom entropy as SystemRandom but reads it in bulk to avoid a syscall per draw, making secure bulk generation many times faster. It is reachable without an extra import:

    SG(r"[\w\p]{32}", randomizer=SG.BufferedSecureRandom()).render_set(50000)

There is a rich feature set to randomize strings in situ or include external data.

The purpose of this module is to save the Python developer from having to write verbose code around the same pattern every time to generate passwords, keys, tokens, test data, etc. of this sort:

my_secret_key = ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(30))

that is:

  1. Hard to read even at this simplistic level.

  2. Hard to safely change quickly. Even modest additions to the requirements need unreasonably verbose solutions.

  3. Doesn’t use safe encryption standards.

  4. Doesn’t provide the implied minimal guarantees of character occurance.

  5. Hard to track back to requirements (“must be between x and y in length and have characters from sets Q, R and S”).

The template uses short forms similar to those of regular expressions. An example template for generating a strong password:

[\w\p]{20}

will generate something like the following:

P{:45Ec5$3)2!I68x`{6

Guarantee at least two “special” characters in a string:

[\w\p]{10}&[\p]{2}

You can also generate useful test data, like fake emails with plenty of variation:

[\c]{10}.[\c]{5:10}@[\c]{3:12}.(com|net|org)

Requirements

From version 0.4.0, support for Python 2 is dropped. If you still need support for Python 2, use version 0.3.4.

There are no dependencies beyond the Python Standard Library.

Installation

Install as standard for Python packages from PyPi:

pip install StringGenerator

License

Released under the BSD license.

Acknowledgements

Thanks to Robert LeBlanc who caught some important errors in escaping special characters. Thanks to Andreas Motl for the progress counter.

Original Author: paul.wolf@yewleaf.com

Release files for StringGenerator 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for StringGenerator 0.5.1
File Size Uploaded
stringgenerator-0.5.1.tar.gz 19.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for StringGenerator 0.5.1
File Interpreter ABI Platform
stringgenerator-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 39.5 kB

Release files / stringgenerator-0.5.1.tar.gz

Download URL stringgenerator-0.5.1.tar.gz
Size 19.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aaa294ca564dd484054c7e6069216d2ce3dc97bb4c1a6079c2fa987745d228d9
BLAKE2b-256 checksum
How to use checksums
1a757439e1335e8f3126ccea827524732a25ca0d880bbf3bb1d5601b508c82f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / stringgenerator-0.5.1-py3-none-any.whl

Download URL stringgenerator-0.5.1-py3-none-any.whl
Size 19.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
058a13b7f17c8d917bda216876888bb809932de980027f736c9fd808a39c783a
BLAKE2b-256 checksum
How to use checksums
df30ba27f0a55721c69df0d078df7e2d5f3a07d6efbb2c5bf7eb222923a1e081
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.5.1 This release

2 release files

0.5.0

2 release files

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.4

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page