rstr


Namerstr JSON
Version 3.2.1 PyPI version JSON
download
home_page
SummaryGenerate random strings in Python
upload_time2023-04-18 19:48:06
maintainer
docs_urlNone
author
requires_python>=3.7
license
keywords random string reverse regex reverse regular expression testing fuzz testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===============================
rstr = Random Strings in Python
===============================

.. image:: https://circleci.com/gh/leapfrogonline/rstr.svg?style=svg
    :target: https://circleci.com/gh/leapfrogonline/rstr

rstr is a helper module for easily generating random strings of various types.
It could be useful for fuzz testing, generating dummy data, or other
applications.

It has no dependencies outside the standard library.

A Word of Caution
-----------------

By default, rstr uses the Python ``random`` module to generate pseudorandom text. This module is based on the Mersenne Twister and is *not* cryptographically secure.

**If you wish to use rstr for password-generation or other cryptographic
applications, you must create an instance that uses** SystemRandom_.

For example:

::

    >> from rstr import Rstr
    >> from random import SystemRandom
    >> rs = Rstr(SystemRandom())


Use
---

The basic method of rstr is ``rstr()``. At a minimum, it requires one argument,
an alphabet of characters from which to create a string.

::

    >>> import rstr
    >>> rstr.rstr('ABC')
    'AACAACCB'

By default, it will return a string between 1 and 10 characters in length. You
may specify an exact length by including it as a second argument:

::

    >>> rstr.rstr('ABC', 4)
    'ACBC'

You can also generate a range of lengths by adding two arguments. In the following
case, rstr will return a string with a randomly selected length between 5 and 10
characters.

::

    >>> rstr.rstr('ABC', 5, 10)
    'CBCCCABAA'

It's also possible to include particular characters in your string. This is useful
when testing a validator to make sure that certain characters are rejected.
Characters listed in the 'include' argument will *always* be present somewhere
in the resulting string.

::

    >>> rstr.rstr('ABC', include='&')
    'CA&A'

Conversely, you can exclude particular characters from the generated string. This is
helpful when starting with a pre-defined population of characters.

::

    >>> import string
    >>> rstr.rstr(string.digits, exclude='5')
    '8661442'

Note that any of the arguments that accept strings can also
accept lists or tuples of strings:

::

    >>> rstr.rstr(['A', 'B', 'C'], include = ['@'], exclude=('C',))
    'BAAABBA@BAA'

Other methods
-------------

The other methods provided by rstr, besides ``rstr()`` and ``xeger()``, are convenience
methods that can be called without arguments, and provide a pre-defined alphabet.
They accept the same arguments as ``rstr()`` for purposes of
specifying lengths and including or excluding particular characters.

letters()
    The characters provided by string.letters in the standard library.

uppercase()
    The characters provided by string.uppercase in the standard library.

lowercase()
    The characters provided by string.lowercase in the standard library.

printable()
    The characters provided by string.printable in the standard library.

punctuation()
    The characters provided by string.punctuation in the standard library.

nonwhitespace()
    The characters provided by string.printable in the standard library, except
    for those representing whitespace: tab, space, etc.

digits()
    The characters provided by string.digits in the standard library.

nondigits()
    The characters provided by the concatenation of string.letters and
    string.punctuation in the standard library.

nonletters()
    The characters provided by the concatenation of string.digits and
    string.punctuation in the standard library.

normal()
    Characters commonly accepted in text input, equivalent to string.digits +
    string.letters + ' ' (the space character).

unambiguous()
    The characters provided by the concatenation of string.digits and
    string.letters except characters which are similar: 1, l and I, etc.

postalsafe()
    Characters that are safe for use in postal addresses in the United States:
    upper- and lower-case letters, digits, spaces, and the punctuation marks period,
    hash (#), hyphen, and forward-slash.

urlsafe()
    Characters safe (unreserved) for use in URLs: letters, digits, hyphen, period, underscore,
    and tilde.

domainsafe()
    Characters that are allowed for use in hostnames, and consequently, in internet domains: letters,
    digits, and the hyphen.

Xeger
-----

Inspired by the Java library of the same name, the ``xeger()`` method allows users to
create a random string from a regular expression.

For example to generate a postal code that fits the Canadian format:

    >>> import rstr
    >>> rstr.xeger(r'[A-Z]\d[A-Z] \d[A-Z]\d')
    u'R6M 1W5'

xeger works fine with most simple regular expressions, but it doesn't support all
Python regular expression features.

Custom Alphabets
----------------

If you have custom alphabets of characters that you would like to use with a method
shortcut, you can specify them by keyword when instantiating an Rstr object:

    >>> from rstr import Rstr
    >>> rs = Rstr(vowels='AEIOU')
    >>> rs.vowels()
    'AEEUU'

You can also add an alphabet to an existing instance with the add_alphabet() method:

    >>> rs.add_alphabet('odds', '13579')
    >>> rs.odds()
    '339599519'

Examples
--------

You can combine rstr with Python's built-in string formatting to produce strings
that fit a variety of templates.

An email address:

::

    '{0}@{1}.{2}'.format(rstr.nonwhitespace(exclude='@'),
                         rstr.domainsafe(),
                         rstr.letters(3))

A URL:

::

    'http://{0}.{1}/{2}/?{3}'.format(rstr.domainsafe(),
                                    rstr.letters(3),
                                    rstr.urlsafe(),
                                    rstr.urlsafe())

A postal address:

::

    """{0} {1}
    {2} {3}
    {4}, {5} {6}
    """.format(rstr.letters(4, 8).title(),
               rstr.letters(4, 8).title(),
               rstr.digits(3, 5),
               rstr.letters(4, 10).title(),
               rstr.letters(4, 15).title(),
               rstr.uppercase(2),
               rstr.digits(5),
               )

.. _SystemRandom: https://docs.python.org/3/library/random.html#random.SystemRandom

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "rstr",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "Brendan McCollam <rstr@mccoll.am>",
    "keywords": "random string,reverse regex,reverse regular expression,testing,fuzz testing",
    "author": "",
    "author_email": "Leapfrog Direct Response LLC <oss@leapfrogdevelopment.com>",
    "download_url": "https://files.pythonhosted.org/packages/81/d6/956a9525caf0581da01a6cafa38e239b092a95af3146c715fd5061534546/rstr-3.2.1.tar.gz",
    "platform": null,
    "description": "===============================\nrstr = Random Strings in Python\n===============================\n\n.. image:: https://circleci.com/gh/leapfrogonline/rstr.svg?style=svg\n    :target: https://circleci.com/gh/leapfrogonline/rstr\n\nrstr is a helper module for easily generating random strings of various types.\nIt could be useful for fuzz testing, generating dummy data, or other\napplications.\n\nIt has no dependencies outside the standard library.\n\nA Word of Caution\n-----------------\n\nBy default, rstr uses the Python ``random`` module to generate pseudorandom text. This module is based on the Mersenne Twister and is *not* cryptographically secure.\n\n**If you wish to use rstr for password-generation or other cryptographic\napplications, you must create an instance that uses** SystemRandom_.\n\nFor example:\n\n::\n\n    >> from rstr import Rstr\n    >> from random import SystemRandom\n    >> rs = Rstr(SystemRandom())\n\n\nUse\n---\n\nThe basic method of rstr is ``rstr()``. At a minimum, it requires one argument,\nan alphabet of characters from which to create a string.\n\n::\n\n    >>> import rstr\n    >>> rstr.rstr('ABC')\n    'AACAACCB'\n\nBy default, it will return a string between 1 and 10 characters in length. You\nmay specify an exact length by including it as a second argument:\n\n::\n\n    >>> rstr.rstr('ABC', 4)\n    'ACBC'\n\nYou can also generate a range of lengths by adding two arguments. In the following\ncase, rstr will return a string with a randomly selected length between 5 and 10\ncharacters.\n\n::\n\n    >>> rstr.rstr('ABC', 5, 10)\n    'CBCCCABAA'\n\nIt's also possible to include particular characters in your string. This is useful\nwhen testing a validator to make sure that certain characters are rejected.\nCharacters listed in the 'include' argument will *always* be present somewhere\nin the resulting string.\n\n::\n\n    >>> rstr.rstr('ABC', include='&')\n    'CA&A'\n\nConversely, you can exclude particular characters from the generated string. This is\nhelpful when starting with a pre-defined population of characters.\n\n::\n\n    >>> import string\n    >>> rstr.rstr(string.digits, exclude='5')\n    '8661442'\n\nNote that any of the arguments that accept strings can also\naccept lists or tuples of strings:\n\n::\n\n    >>> rstr.rstr(['A', 'B', 'C'], include = ['@'], exclude=('C',))\n    'BAAABBA@BAA'\n\nOther methods\n-------------\n\nThe other methods provided by rstr, besides ``rstr()`` and ``xeger()``, are convenience\nmethods that can be called without arguments, and provide a pre-defined alphabet.\nThey accept the same arguments as ``rstr()`` for purposes of\nspecifying lengths and including or excluding particular characters.\n\nletters()\n    The characters provided by string.letters in the standard library.\n\nuppercase()\n    The characters provided by string.uppercase in the standard library.\n\nlowercase()\n    The characters provided by string.lowercase in the standard library.\n\nprintable()\n    The characters provided by string.printable in the standard library.\n\npunctuation()\n    The characters provided by string.punctuation in the standard library.\n\nnonwhitespace()\n    The characters provided by string.printable in the standard library, except\n    for those representing whitespace: tab, space, etc.\n\ndigits()\n    The characters provided by string.digits in the standard library.\n\nnondigits()\n    The characters provided by the concatenation of string.letters and\n    string.punctuation in the standard library.\n\nnonletters()\n    The characters provided by the concatenation of string.digits and\n    string.punctuation in the standard library.\n\nnormal()\n    Characters commonly accepted in text input, equivalent to string.digits +\n    string.letters + ' ' (the space character).\n\nunambiguous()\n    The characters provided by the concatenation of string.digits and\n    string.letters except characters which are similar: 1, l and I, etc.\n\npostalsafe()\n    Characters that are safe for use in postal addresses in the United States:\n    upper- and lower-case letters, digits, spaces, and the punctuation marks period,\n    hash (#), hyphen, and forward-slash.\n\nurlsafe()\n    Characters safe (unreserved) for use in URLs: letters, digits, hyphen, period, underscore,\n    and tilde.\n\ndomainsafe()\n    Characters that are allowed for use in hostnames, and consequently, in internet domains: letters,\n    digits, and the hyphen.\n\nXeger\n-----\n\nInspired by the Java library of the same name, the ``xeger()`` method allows users to\ncreate a random string from a regular expression.\n\nFor example to generate a postal code that fits the Canadian format:\n\n    >>> import rstr\n    >>> rstr.xeger(r'[A-Z]\\d[A-Z] \\d[A-Z]\\d')\n    u'R6M 1W5'\n\nxeger works fine with most simple regular expressions, but it doesn't support all\nPython regular expression features.\n\nCustom Alphabets\n----------------\n\nIf you have custom alphabets of characters that you would like to use with a method\nshortcut, you can specify them by keyword when instantiating an Rstr object:\n\n    >>> from rstr import Rstr\n    >>> rs = Rstr(vowels='AEIOU')\n    >>> rs.vowels()\n    'AEEUU'\n\nYou can also add an alphabet to an existing instance with the add_alphabet() method:\n\n    >>> rs.add_alphabet('odds', '13579')\n    >>> rs.odds()\n    '339599519'\n\nExamples\n--------\n\nYou can combine rstr with Python's built-in string formatting to produce strings\nthat fit a variety of templates.\n\nAn email address:\n\n::\n\n    '{0}@{1}.{2}'.format(rstr.nonwhitespace(exclude='@'),\n                         rstr.domainsafe(),\n                         rstr.letters(3))\n\nA URL:\n\n::\n\n    'http://{0}.{1}/{2}/?{3}'.format(rstr.domainsafe(),\n                                    rstr.letters(3),\n                                    rstr.urlsafe(),\n                                    rstr.urlsafe())\n\nA postal address:\n\n::\n\n    \"\"\"{0} {1}\n    {2} {3}\n    {4}, {5} {6}\n    \"\"\".format(rstr.letters(4, 8).title(),\n               rstr.letters(4, 8).title(),\n               rstr.digits(3, 5),\n               rstr.letters(4, 10).title(),\n               rstr.letters(4, 15).title(),\n               rstr.uppercase(2),\n               rstr.digits(5),\n               )\n\n.. _SystemRandom: https://docs.python.org/3/library/random.html#random.SystemRandom\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Generate random strings in Python",
    "version": "3.2.1",
    "split_keywords": [
        "random string",
        "reverse regex",
        "reverse regular expression",
        "testing",
        "fuzz testing"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7afe7c1c3d25d007c611902864992c8f4d8f6e734778dab0ab4fa8a59a48175c",
                "md5": "1783226db1b3e28535d6f73bd0460992",
                "sha256": "c0d59fa8a64683acc43646603e7570546a93d842be49b6e4cb72d0abc96cbacf"
            },
            "downloads": -1,
            "filename": "rstr-3.2.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1783226db1b3e28535d6f73bd0460992",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 12527,
            "upload_time": "2023-04-18T19:48:04",
            "upload_time_iso_8601": "2023-04-18T19:48:04.815773Z",
            "url": "https://files.pythonhosted.org/packages/7a/fe/7c1c3d25d007c611902864992c8f4d8f6e734778dab0ab4fa8a59a48175c/rstr-3.2.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "81d6956a9525caf0581da01a6cafa38e239b092a95af3146c715fd5061534546",
                "md5": "3e7a8d2ccd88ac229939abd2e59f0e9b",
                "sha256": "c51924c626540eb626ed3eb8e5e6c1d2f2c73959d039c8ff61ee562baeb4e0ff"
            },
            "downloads": -1,
            "filename": "rstr-3.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "3e7a8d2ccd88ac229939abd2e59f0e9b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 13587,
            "upload_time": "2023-04-18T19:48:06",
            "upload_time_iso_8601": "2023-04-18T19:48:06.904161Z",
            "url": "https://files.pythonhosted.org/packages/81/d6/956a9525caf0581da01a6cafa38e239b092a95af3146c715fd5061534546/rstr-3.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-18 19:48:06",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "rstr"
}
        
Elapsed time: 0.13938s