rstr 3.2.2__tar.gz → 4.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rstr-4.0.0/.circleci/config.yml +18 -0
- {rstr-3.2.2 → rstr-4.0.0}/AUTHORS +1 -0
- {rstr-3.2.2 → rstr-4.0.0}/PKG-INFO +10 -26
- {rstr-3.2.2 → rstr-4.0.0}/README.rst +0 -17
- {rstr-3.2.2 → rstr-4.0.0}/RELEASE_NOTES +9 -2
- {rstr-3.2.2 → rstr-4.0.0}/pyproject.toml +26 -7
- rstr-4.0.0/rstr/__init__.py +30 -0
- rstr-3.2.2/rstr/rstr_base.py → rstr-4.0.0/rstr/rstr.py +29 -33
- rstr-4.0.0/rstr/xeger.py +121 -0
- {rstr-3.2.2 → rstr-4.0.0}/rstr.egg-info/PKG-INFO +10 -26
- {rstr-3.2.2 → rstr-4.0.0}/rstr.egg-info/SOURCES.txt +3 -1
- rstr-4.0.0/rstr.egg-info/scm_file_list.json +21 -0
- rstr-4.0.0/rstr.egg-info/scm_version.json +8 -0
- {rstr-3.2.2 → rstr-4.0.0}/tests/test_package_level_access.py +1 -2
- {rstr-3.2.2 → rstr-4.0.0}/tests/test_rstr.py +14 -6
- {rstr-3.2.2 → rstr-4.0.0}/tests/test_xeger.py +26 -4
- rstr-4.0.0/tox.ini +20 -0
- rstr-3.2.2/.circleci/config.yml +0 -16
- rstr-3.2.2/rstr/__init__.py +0 -29
- rstr-3.2.2/rstr/xeger.py +0 -106
- rstr-3.2.2/tox.ini +0 -12
- {rstr-3.2.2 → rstr-4.0.0}/.gitignore +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/LICENSE.txt +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/MANIFEST.in +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/rstr/py.typed +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/rstr.egg-info/dependency_links.txt +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/rstr.egg-info/top_level.txt +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/setup.cfg +0 -0
- {rstr-3.2.2 → rstr-4.0.0}/tests/__init__.py +0 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
version: 2.1
|
|
2
|
+
|
|
3
|
+
jobs:
|
|
4
|
+
test:
|
|
5
|
+
docker:
|
|
6
|
+
- image: cimg/python:3.13
|
|
7
|
+
steps:
|
|
8
|
+
- checkout
|
|
9
|
+
- run:
|
|
10
|
+
name: Run tox
|
|
11
|
+
# cimg/python bundles uv; tox-uv provides the other
|
|
12
|
+
# interpreters, including the Python 3.16 pre-release
|
|
13
|
+
command: uvx --with tox-uv tox
|
|
14
|
+
|
|
15
|
+
workflows:
|
|
16
|
+
test:
|
|
17
|
+
jobs:
|
|
18
|
+
- test
|
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: rstr
|
|
3
|
-
Version:
|
|
3
|
+
Version: 4.0.0
|
|
4
4
|
Summary: Generate random strings in Python
|
|
5
5
|
Author-email: Leapfrog Direct Response LLC <oss@leapfrogdevelopment.com>
|
|
6
6
|
Maintainer-email: Brendan McCollam <rstr@mccoll.am>
|
|
7
|
+
License-Expression: BSD-3-Clause
|
|
7
8
|
Project-URL: Homepage, https://github.com/leapfrogonline/rstr
|
|
8
9
|
Keywords: random string,reverse regex,reverse regular expression,testing,fuzz testing
|
|
9
10
|
Classifier: Development Status :: 5 - Production/Stable
|
|
10
11
|
Classifier: Intended Audience :: Developers
|
|
11
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
12
12
|
Classifier: Operating System :: OS Independent
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
17
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.16
|
|
18
19
|
Classifier: Topic :: Software Development :: Testing
|
|
19
|
-
Requires-Python: >=3.
|
|
20
|
+
Requires-Python: >=3.11
|
|
20
21
|
Description-Content-Type: text/x-rst
|
|
21
22
|
License-File: LICENSE.txt
|
|
22
|
-
|
|
23
|
+
Dynamic: license-file
|
|
23
24
|
|
|
24
25
|
===============================
|
|
25
26
|
rstr = Random Strings in Python
|
|
@@ -34,23 +35,6 @@ applications.
|
|
|
34
35
|
|
|
35
36
|
It has no dependencies outside the standard library.
|
|
36
37
|
|
|
37
|
-
A Word of Caution
|
|
38
|
-
-----------------
|
|
39
|
-
|
|
40
|
-
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.
|
|
41
|
-
|
|
42
|
-
**If you wish to use rstr for password-generation or other cryptographic
|
|
43
|
-
applications, you must create an instance that uses** SystemRandom_.
|
|
44
|
-
|
|
45
|
-
For example:
|
|
46
|
-
|
|
47
|
-
::
|
|
48
|
-
|
|
49
|
-
>> from rstr import Rstr
|
|
50
|
-
>> from random import SystemRandom
|
|
51
|
-
>> rs = Rstr(SystemRandom())
|
|
52
|
-
|
|
53
|
-
|
|
54
38
|
Use
|
|
55
39
|
---
|
|
56
40
|
|
|
@@ -11,23 +11,6 @@ applications.
|
|
|
11
11
|
|
|
12
12
|
It has no dependencies outside the standard library.
|
|
13
13
|
|
|
14
|
-
A Word of Caution
|
|
15
|
-
-----------------
|
|
16
|
-
|
|
17
|
-
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.
|
|
18
|
-
|
|
19
|
-
**If you wish to use rstr for password-generation or other cryptographic
|
|
20
|
-
applications, you must create an instance that uses** SystemRandom_.
|
|
21
|
-
|
|
22
|
-
For example:
|
|
23
|
-
|
|
24
|
-
::
|
|
25
|
-
|
|
26
|
-
>> from rstr import Rstr
|
|
27
|
-
>> from random import SystemRandom
|
|
28
|
-
>> rs = Rstr(SystemRandom())
|
|
29
|
-
|
|
30
|
-
|
|
31
14
|
Use
|
|
32
15
|
---
|
|
33
16
|
|
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 4.0.0
|
|
6
|
+
|
|
7
|
+
- Refactor: Xeger and Rstr are distinct, Xeger no longer inherits from XegerBase
|
|
8
|
+
- Default to use SystemRandom() CSPRNG
|
|
9
|
+
- Expose star_plus_limit as a property on Xeger (#37)
|
|
10
|
+
- Fix error when regex specified a repeat larger than the star_plus limit (#38)
|
|
11
|
+
- Behavior change: explicit repeats (e.g. `a{1000}`) are not restricted by star_plus_limit
|
|
12
|
+
- Drops support for Pythons 3.7, 3.8, 3.9, 3.10 adds support for 3.12, 3.13, 3.14, 3.15, 3.16.
|
|
13
|
+
|
|
5
14
|
## 3.2.2
|
|
6
15
|
|
|
7
16
|
- Removes type stubs for `random` to fix type-checking failures
|
|
@@ -28,5 +37,3 @@
|
|
|
28
37
|
- Drops support for Python 2.7 and 3.5
|
|
29
38
|
- Fixes a bug where `include` could make the generated string too long.
|
|
30
39
|
- Add new `rstr.unambiguous()` method that omits homographs.
|
|
31
|
-
|
|
32
|
-
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[build-system]
|
|
2
|
-
requires = ["setuptools>=
|
|
2
|
+
requires = ["setuptools>=77", "wheel", "setuptools_scm[toml]>=3.4.3"]
|
|
3
3
|
build-backend = "setuptools.build_meta"
|
|
4
4
|
|
|
5
5
|
[project]
|
|
@@ -8,16 +8,18 @@ authors = [{name = "Leapfrog Direct Response LLC", email = "oss@leapfrogdevelopm
|
|
|
8
8
|
maintainers = [{name = "Brendan McCollam", email = "rstr@mccoll.am"}]
|
|
9
9
|
description = "Generate random strings in Python"
|
|
10
10
|
readme = "README.rst"
|
|
11
|
+
license = "BSD-3-Clause"
|
|
12
|
+
license-files = ["LICENSE.txt"]
|
|
11
13
|
classifiers = [
|
|
12
14
|
"Development Status :: 5 - Production/Stable",
|
|
13
15
|
"Intended Audience :: Developers",
|
|
14
|
-
"License :: OSI Approved :: BSD License",
|
|
15
16
|
"Operating System :: OS Independent",
|
|
16
|
-
"Programming Language :: Python :: 3.7",
|
|
17
|
-
"Programming Language :: Python :: 3.8",
|
|
18
|
-
"Programming Language :: Python :: 3.9",
|
|
19
|
-
"Programming Language :: Python :: 3.10",
|
|
20
17
|
"Programming Language :: Python :: 3.11",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Programming Language :: Python :: 3.15",
|
|
22
|
+
"Programming Language :: Python :: 3.16",
|
|
21
23
|
"Topic :: Software Development :: Testing",
|
|
22
24
|
]
|
|
23
25
|
keywords = [
|
|
@@ -28,9 +30,26 @@ keywords = [
|
|
|
28
30
|
"fuzz testing",
|
|
29
31
|
]
|
|
30
32
|
urls = {Homepage = "https://github.com/leapfrogonline/rstr"}
|
|
31
|
-
requires-python = ">=3.
|
|
33
|
+
requires-python = ">=3.11"
|
|
32
34
|
dynamic = ["version"]
|
|
33
35
|
|
|
36
|
+
[tool.ruff]
|
|
37
|
+
extend-exclude = ["build"]
|
|
38
|
+
line-length = 100
|
|
39
|
+
|
|
40
|
+
[tool.ruff.format]
|
|
41
|
+
quote-style = "single"
|
|
42
|
+
|
|
43
|
+
[tool.ruff.lint]
|
|
44
|
+
select = [
|
|
45
|
+
"E", # pycodestyle
|
|
46
|
+
"F", # Pyflakes
|
|
47
|
+
"UP", # pyupgrade
|
|
48
|
+
"B", # flake8-bugbear
|
|
49
|
+
"SIM",# flake8-simplify
|
|
50
|
+
"I", # isort
|
|
51
|
+
]
|
|
52
|
+
|
|
34
53
|
[tool.setuptools]
|
|
35
54
|
package-dir = {}
|
|
36
55
|
include-package-data = true
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from rstr.rstr import Rstr
|
|
2
|
+
from rstr.rstr import SameCharacterError as SameCharacterError
|
|
3
|
+
from rstr.xeger import Xeger
|
|
4
|
+
|
|
5
|
+
_default_xeger = Xeger()
|
|
6
|
+
_default_rstr = Rstr()
|
|
7
|
+
|
|
8
|
+
rstr = _default_rstr.rstr
|
|
9
|
+
xeger = _default_xeger.xeger
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
# This allows convenience methods from rstr to be accessed at the package
|
|
13
|
+
# level, without requiring the user to instantiate an Rstr() object.
|
|
14
|
+
printable = _default_rstr.printable
|
|
15
|
+
letters = _default_rstr.letters
|
|
16
|
+
uppercase = _default_rstr.uppercase
|
|
17
|
+
lowercase = _default_rstr.lowercase
|
|
18
|
+
digits = _default_rstr.digits
|
|
19
|
+
punctuation = _default_rstr.punctuation
|
|
20
|
+
nondigits = _default_rstr.nondigits
|
|
21
|
+
nonletters = _default_rstr.nonletters
|
|
22
|
+
whitespace = _default_rstr.whitespace
|
|
23
|
+
nonwhitespace = _default_rstr.nonwhitespace
|
|
24
|
+
normal = _default_rstr.normal
|
|
25
|
+
word = _default_rstr.word
|
|
26
|
+
nonword = _default_rstr.nonword
|
|
27
|
+
unambiguous = _default_rstr.unambiguous
|
|
28
|
+
postalsafe = _default_rstr.postalsafe
|
|
29
|
+
urlsafe = _default_rstr.urlsafe
|
|
30
|
+
domainsafe = _default_rstr.domainsafe
|
|
@@ -29,32 +29,31 @@
|
|
|
29
29
|
|
|
30
30
|
import itertools
|
|
31
31
|
import string
|
|
32
|
-
from functools import partial
|
|
33
32
|
import typing
|
|
34
|
-
from
|
|
35
|
-
|
|
33
|
+
from collections.abc import Iterable, Sequence
|
|
34
|
+
from functools import partial
|
|
35
|
+
from random import Random, SystemRandom
|
|
36
|
+
from typing import TypeVar
|
|
36
37
|
|
|
37
38
|
_T = TypeVar('_T')
|
|
38
39
|
|
|
39
40
|
|
|
40
41
|
if typing.TYPE_CHECKING:
|
|
41
|
-
from random import Random
|
|
42
42
|
from typing import Protocol
|
|
43
43
|
|
|
44
|
-
|
|
45
44
|
class _PartialRstrFunc(Protocol):
|
|
46
|
-
|
|
47
45
|
def __call__(
|
|
48
46
|
self,
|
|
49
|
-
start_range:
|
|
50
|
-
end_range:
|
|
47
|
+
start_range: int | None = ...,
|
|
48
|
+
end_range: int | None = ...,
|
|
51
49
|
include: str = ...,
|
|
52
50
|
exclude: str = ...,
|
|
53
|
-
) -> str:
|
|
54
|
-
|
|
51
|
+
) -> str: ...
|
|
52
|
+
|
|
55
53
|
|
|
54
|
+
SYSTEM_RANDOM = SystemRandom()
|
|
56
55
|
|
|
57
|
-
|
|
56
|
+
DEFAULT_ALPHABETS: dict[str, str] = {
|
|
58
57
|
'printable': string.printable,
|
|
59
58
|
'letters': string.ascii_letters,
|
|
60
59
|
'uppercase': string.ascii_uppercase,
|
|
@@ -77,8 +76,8 @@ ALPHABETS: Mapping[str, str] = {
|
|
|
77
76
|
}
|
|
78
77
|
|
|
79
78
|
|
|
80
|
-
class
|
|
81
|
-
|
|
79
|
+
class Rstr:
|
|
80
|
+
"""Create random strings from a variety of alphabets.
|
|
82
81
|
|
|
83
82
|
The alphabets for printable(), uppercase(), lowercase(), digits(), and
|
|
84
83
|
punctuation() are equivalent to the constants by those same names in the
|
|
@@ -107,20 +106,19 @@ class RstrBase():
|
|
|
107
106
|
domainsafe() uses an alphabet of characters allowed in hostnames, and
|
|
108
107
|
consequently, in internet domains: letters, digits, and the hyphen.
|
|
109
108
|
|
|
110
|
-
|
|
109
|
+
"""
|
|
111
110
|
|
|
112
|
-
def __init__(self,
|
|
113
|
-
|
|
114
|
-
self.
|
|
115
|
-
self._alphabets = dict(ALPHABETS)
|
|
111
|
+
def __init__(self, random: Random = SYSTEM_RANDOM, **custom_alphabets: str) -> None:
|
|
112
|
+
self._random = random
|
|
113
|
+
self._alphabets = DEFAULT_ALPHABETS.copy()
|
|
116
114
|
for alpha_name, alphabet in custom_alphabets.items():
|
|
117
115
|
self.add_alphabet(alpha_name, alphabet)
|
|
118
116
|
|
|
119
117
|
def add_alphabet(self, alpha_name: str, characters: str) -> None:
|
|
120
|
-
|
|
118
|
+
"""Add an additional alphabet to an Rstr instance and make it available
|
|
121
119
|
via method calls.
|
|
122
120
|
|
|
123
|
-
|
|
121
|
+
"""
|
|
124
122
|
self._alphabets[alpha_name] = characters
|
|
125
123
|
|
|
126
124
|
def __getattr__(self, attr: str) -> '_PartialRstrFunc':
|
|
@@ -129,19 +127,19 @@ class RstrBase():
|
|
|
129
127
|
message = f'Rstr instance has no attribute: {attr}'
|
|
130
128
|
raise AttributeError(message)
|
|
131
129
|
|
|
132
|
-
def sample_wr(self, population: Sequence[str], k: int) ->
|
|
133
|
-
|
|
130
|
+
def sample_wr(self, population: Sequence[str], k: int) -> list[str]:
|
|
131
|
+
"""Samples k random elements (with replacement) from a population"""
|
|
134
132
|
return [self._random.choice(population) for i in itertools.repeat(None, k)]
|
|
135
133
|
|
|
136
134
|
def rstr(
|
|
137
135
|
self,
|
|
138
136
|
alphabet: Iterable[str],
|
|
139
|
-
start_range:
|
|
140
|
-
end_range:
|
|
137
|
+
start_range: int | None = None,
|
|
138
|
+
end_range: int | None = None,
|
|
141
139
|
include: Sequence[str] = '',
|
|
142
140
|
exclude: Sequence[str] = '',
|
|
143
141
|
) -> str:
|
|
144
|
-
|
|
142
|
+
"""Generate a random string containing elements from 'alphabet'
|
|
145
143
|
|
|
146
144
|
By default, rstr() will return a string between 1 and 10 characters.
|
|
147
145
|
You can specify a second argument to get an exact length of string.
|
|
@@ -155,16 +153,14 @@ class RstrBase():
|
|
|
155
153
|
If you want to *prevent* certain characters from appearing, pass them
|
|
156
154
|
as 'exclude'.
|
|
157
155
|
|
|
158
|
-
|
|
159
|
-
same_characters
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
characters=", ".join(same_characters)
|
|
164
|
-
)
|
|
156
|
+
"""
|
|
157
|
+
if same_characters := frozenset(include).intersection(exclude):
|
|
158
|
+
plural = 's' if len(same_characters) > 1 else ''
|
|
159
|
+
chars = ', '.join(same_characters)
|
|
160
|
+
message = f'include and exclude parameters contain same character{plural} ({chars})'
|
|
165
161
|
raise SameCharacterError(message)
|
|
166
162
|
|
|
167
|
-
popul =
|
|
163
|
+
popul = tuple(frozenset(alphabet).difference(exclude))
|
|
168
164
|
|
|
169
165
|
if end_range is None:
|
|
170
166
|
if start_range is None:
|
rstr-4.0.0/rstr/xeger.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import string
|
|
2
|
+
import typing
|
|
3
|
+
from collections.abc import Callable, Mapping, Sequence
|
|
4
|
+
from random import Random, SystemRandom
|
|
5
|
+
from re import Pattern
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
try:
|
|
9
|
+
import re._parser as sre_parse # type: ignore[import-not-found]
|
|
10
|
+
from re._constants import MAXREPEAT # type: ignore[import-not-found]
|
|
11
|
+
except ImportError: # Python < 3.11
|
|
12
|
+
import sre_parse
|
|
13
|
+
from sre_constants import MAXREPEAT
|
|
14
|
+
|
|
15
|
+
SYSTEM_RANDOM = SystemRandom()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class Xeger:
|
|
19
|
+
"""Inspired by the Java library Xeger: http://code.google.com/p/xeger/
|
|
20
|
+
Allows users to generate a semi-random string from a regular expression."""
|
|
21
|
+
|
|
22
|
+
def __init__(self, random: Random = SYSTEM_RANDOM, *, star_plus_limit: int = 100) -> None:
|
|
23
|
+
self._random = random
|
|
24
|
+
self.star_plus_limit = star_plus_limit
|
|
25
|
+
self._cache: dict[str, str] = {}
|
|
26
|
+
_wordchars = string.ascii_letters + string.digits + '_'
|
|
27
|
+
self._categories: dict[str, str] = {
|
|
28
|
+
'category_digit': string.digits,
|
|
29
|
+
'category_not_digit': string.ascii_letters + string.punctuation,
|
|
30
|
+
'category_space': string.whitespace,
|
|
31
|
+
'category_not_space': string.printable.strip(),
|
|
32
|
+
'category_word': _wordchars,
|
|
33
|
+
'category_not_word': ''.join(set(string.printable).difference(_wordchars)),
|
|
34
|
+
}
|
|
35
|
+
_any_but_newline = ''.join(string.printable.split('\n'))
|
|
36
|
+
self._cases: Mapping[str, Callable[..., Any]] = {
|
|
37
|
+
'literal': lambda x: chr(x),
|
|
38
|
+
'not_literal': lambda x: self._random.choice(string.printable.replace(chr(x), '')),
|
|
39
|
+
'at': lambda x: '',
|
|
40
|
+
'in': lambda x: self._handle_in(x),
|
|
41
|
+
'any': lambda x: self._random.choice(_any_but_newline),
|
|
42
|
+
'range': lambda x: [chr(i) for i in range(x[0], x[1] + 1)],
|
|
43
|
+
'category': lambda x: self._categories[x],
|
|
44
|
+
'branch': lambda x: ''.join(self._handle_state(i) for i in self._random.choice(x[1])),
|
|
45
|
+
'subpattern': lambda x: self._handle_group(x),
|
|
46
|
+
'assert': lambda x: ''.join(self._handle_state(i) for i in x[1]),
|
|
47
|
+
'assert_not': lambda x: '',
|
|
48
|
+
'groupref': lambda x: self._cache[x],
|
|
49
|
+
'min_repeat': lambda x: self._handle_repeat(*x),
|
|
50
|
+
'max_repeat': lambda x: self._handle_repeat(*x),
|
|
51
|
+
'negate': lambda x: [False],
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
def xeger(self, string_or_regex: str | Pattern[str]) -> str:
|
|
55
|
+
"""Generate a random string from a regular expression
|
|
56
|
+
|
|
57
|
+
By default, * and + metacharacters will generate a maximum of 100
|
|
58
|
+
repetitions of the character or group of characters that they modify
|
|
59
|
+
for each occurance in the regular expression. You can provide a second
|
|
60
|
+
argument to change this limit (note that the maximum amount of repeats
|
|
61
|
+
in Python is 65535).
|
|
62
|
+
|
|
63
|
+
"""
|
|
64
|
+
try:
|
|
65
|
+
pattern = typing.cast(Pattern[str], string_or_regex).pattern
|
|
66
|
+
except AttributeError:
|
|
67
|
+
pattern = typing.cast(str, string_or_regex)
|
|
68
|
+
|
|
69
|
+
parsed = sre_parse.parse(pattern)
|
|
70
|
+
result = self._build_string(parsed)
|
|
71
|
+
self._cache.clear()
|
|
72
|
+
return result
|
|
73
|
+
|
|
74
|
+
def _build_string(self, parsed: Any) -> str:
|
|
75
|
+
newstr = []
|
|
76
|
+
for state in parsed:
|
|
77
|
+
newstr.append(self._handle_state(state))
|
|
78
|
+
return ''.join(newstr)
|
|
79
|
+
|
|
80
|
+
def _handle_state(self, state: Any) -> Any:
|
|
81
|
+
opcode, value = state
|
|
82
|
+
opcode = opcode.name.lower()
|
|
83
|
+
# Python 3.16 parses a single-member class as a bare CATEGORY/RANGE
|
|
84
|
+
# atom, so route it through _handle_in to get a single character
|
|
85
|
+
if opcode in ('category', 'range'):
|
|
86
|
+
return self._handle_in([state])
|
|
87
|
+
return self._cases[opcode](value)
|
|
88
|
+
|
|
89
|
+
def _handle_group(self, value: Sequence[Any]) -> str:
|
|
90
|
+
result = ''.join(self._handle_state(i) for i in value[-1])
|
|
91
|
+
if value[0]:
|
|
92
|
+
self._cache[value[0]] = result
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
def _handle_in(self, value: Any) -> Any:
|
|
96
|
+
# Expand set members here so a bare CATEGORY/RANGE atom still yields
|
|
97
|
+
# one character in _handle_state
|
|
98
|
+
candidates: list[Any] = []
|
|
99
|
+
for state in value:
|
|
100
|
+
opcode = state[0].name.lower()
|
|
101
|
+
if opcode == 'category':
|
|
102
|
+
candidates.extend(self._categories[state[1].name.lower()])
|
|
103
|
+
elif opcode == 'range':
|
|
104
|
+
candidates.extend(chr(i) for i in range(state[1][0], state[1][1] + 1))
|
|
105
|
+
elif opcode == 'negate':
|
|
106
|
+
candidates.append(False)
|
|
107
|
+
else:
|
|
108
|
+
candidates.extend(self._handle_state(state))
|
|
109
|
+
if candidates and candidates[0] is False:
|
|
110
|
+
candidates = list(set(string.printable).difference(candidates[1:]))
|
|
111
|
+
return self._random.choice(candidates)
|
|
112
|
+
|
|
113
|
+
def _handle_repeat(self, start_range: int, end_range: int, value: str) -> str:
|
|
114
|
+
result = []
|
|
115
|
+
if end_range is MAXREPEAT:
|
|
116
|
+
end_range = self.star_plus_limit
|
|
117
|
+
|
|
118
|
+
times = self._random.randint(start_range, end_range)
|
|
119
|
+
for i in range(times):
|
|
120
|
+
result.append(''.join(self._handle_state(i) for i in value))
|
|
121
|
+
return ''.join(result)
|
|
@@ -1,25 +1,26 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: rstr
|
|
3
|
-
Version:
|
|
3
|
+
Version: 4.0.0
|
|
4
4
|
Summary: Generate random strings in Python
|
|
5
5
|
Author-email: Leapfrog Direct Response LLC <oss@leapfrogdevelopment.com>
|
|
6
6
|
Maintainer-email: Brendan McCollam <rstr@mccoll.am>
|
|
7
|
+
License-Expression: BSD-3-Clause
|
|
7
8
|
Project-URL: Homepage, https://github.com/leapfrogonline/rstr
|
|
8
9
|
Keywords: random string,reverse regex,reverse regular expression,testing,fuzz testing
|
|
9
10
|
Classifier: Development Status :: 5 - Production/Stable
|
|
10
11
|
Classifier: Intended Audience :: Developers
|
|
11
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
12
12
|
Classifier: Operating System :: OS Independent
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
17
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.16
|
|
18
19
|
Classifier: Topic :: Software Development :: Testing
|
|
19
|
-
Requires-Python: >=3.
|
|
20
|
+
Requires-Python: >=3.11
|
|
20
21
|
Description-Content-Type: text/x-rst
|
|
21
22
|
License-File: LICENSE.txt
|
|
22
|
-
|
|
23
|
+
Dynamic: license-file
|
|
23
24
|
|
|
24
25
|
===============================
|
|
25
26
|
rstr = Random Strings in Python
|
|
@@ -34,23 +35,6 @@ applications.
|
|
|
34
35
|
|
|
35
36
|
It has no dependencies outside the standard library.
|
|
36
37
|
|
|
37
|
-
A Word of Caution
|
|
38
|
-
-----------------
|
|
39
|
-
|
|
40
|
-
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.
|
|
41
|
-
|
|
42
|
-
**If you wish to use rstr for password-generation or other cryptographic
|
|
43
|
-
applications, you must create an instance that uses** SystemRandom_.
|
|
44
|
-
|
|
45
|
-
For example:
|
|
46
|
-
|
|
47
|
-
::
|
|
48
|
-
|
|
49
|
-
>> from rstr import Rstr
|
|
50
|
-
>> from random import SystemRandom
|
|
51
|
-
>> rs = Rstr(SystemRandom())
|
|
52
|
-
|
|
53
|
-
|
|
54
38
|
Use
|
|
55
39
|
---
|
|
56
40
|
|
|
@@ -9,11 +9,13 @@ tox.ini
|
|
|
9
9
|
.circleci/config.yml
|
|
10
10
|
rstr/__init__.py
|
|
11
11
|
rstr/py.typed
|
|
12
|
-
rstr/
|
|
12
|
+
rstr/rstr.py
|
|
13
13
|
rstr/xeger.py
|
|
14
14
|
rstr.egg-info/PKG-INFO
|
|
15
15
|
rstr.egg-info/SOURCES.txt
|
|
16
16
|
rstr.egg-info/dependency_links.txt
|
|
17
|
+
rstr.egg-info/scm_file_list.json
|
|
18
|
+
rstr.egg-info/scm_version.json
|
|
17
19
|
rstr.egg-info/top_level.txt
|
|
18
20
|
tests/__init__.py
|
|
19
21
|
tests/test_package_level_access.py
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"files": [
|
|
3
|
+
".circleci/config.yml",
|
|
4
|
+
".gitignore",
|
|
5
|
+
"AUTHORS",
|
|
6
|
+
"LICENSE.txt",
|
|
7
|
+
"MANIFEST.in",
|
|
8
|
+
"README.rst",
|
|
9
|
+
"RELEASE_NOTES",
|
|
10
|
+
"pyproject.toml",
|
|
11
|
+
"rstr/__init__.py",
|
|
12
|
+
"rstr/py.typed",
|
|
13
|
+
"rstr/rstr.py",
|
|
14
|
+
"rstr/xeger.py",
|
|
15
|
+
"tests/__init__.py",
|
|
16
|
+
"tests/test_package_level_access.py",
|
|
17
|
+
"tests/test_rstr.py",
|
|
18
|
+
"tests/test_xeger.py",
|
|
19
|
+
"tox.ini"
|
|
20
|
+
]
|
|
21
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
+
import random
|
|
1
2
|
import re
|
|
2
3
|
import unittest
|
|
3
|
-
import random
|
|
4
4
|
|
|
5
5
|
from rstr import Rstr, SameCharacterError
|
|
6
6
|
|
|
@@ -33,9 +33,9 @@ class TestRstr(unittest.TestCase):
|
|
|
33
33
|
assert_matches('^[ABC]*@[ABC]*$', self.rs.rstr('ABC', include='@'))
|
|
34
34
|
|
|
35
35
|
def test_include_specific_length(self) -> None:
|
|
36
|
-
|
|
36
|
+
"""
|
|
37
37
|
Verify including characters doesn't make the string longer than intended.
|
|
38
|
-
|
|
38
|
+
"""
|
|
39
39
|
assert_matches('^[ABC@]{5}$', self.rs.rstr('ABC', 5, include='@'))
|
|
40
40
|
|
|
41
41
|
def test_exclude(self) -> None:
|
|
@@ -49,11 +49,19 @@ class TestRstr(unittest.TestCase):
|
|
|
49
49
|
for _ in range(0, 100):
|
|
50
50
|
assert 'C' not in self.rs.rstr('ABC', exclude=['C'])
|
|
51
51
|
|
|
52
|
-
def test_raise_exception_if_include_and_exclude_parameters_contain_same_character(
|
|
53
|
-
|
|
52
|
+
def test_raise_exception_if_include_and_exclude_parameters_contain_same_character(
|
|
53
|
+
self,
|
|
54
|
+
) -> None:
|
|
55
|
+
with self.assertRaisesRegex(
|
|
56
|
+
SameCharacterError,
|
|
57
|
+
r'include and exclude parameters contain same character \(B\)',
|
|
58
|
+
):
|
|
54
59
|
self.rs.rstr('A', include='B', exclude='B')
|
|
55
60
|
self.rs.rstr('A', include=['B'], exclude=['B'])
|
|
56
|
-
with self.assertRaisesRegex(
|
|
61
|
+
with self.assertRaisesRegex(
|
|
62
|
+
SameCharacterError,
|
|
63
|
+
r'include and exclude parameters contain same characters \(., .\)',
|
|
64
|
+
):
|
|
57
65
|
self.rs.rstr('A', include='BC', exclude='BC')
|
|
58
66
|
|
|
59
67
|
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
import re
|
|
2
2
|
import unittest
|
|
3
3
|
|
|
4
|
-
from rstr import
|
|
4
|
+
from rstr import Xeger
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
class TestXeger(unittest.TestCase):
|
|
8
8
|
def setUp(self) -> None:
|
|
9
|
-
self.rs =
|
|
9
|
+
self.rs = Xeger()
|
|
10
10
|
|
|
11
11
|
def test_literals(self) -> None:
|
|
12
12
|
pattern = r'foo'
|
|
13
13
|
assert re.match(pattern, self.rs.xeger(pattern))
|
|
14
14
|
|
|
15
15
|
def test_dot(self) -> None:
|
|
16
|
-
|
|
16
|
+
"""
|
|
17
17
|
Verify that the dot character doesn't produce newlines.
|
|
18
18
|
See: https://bitbucket.org/leapfrogdevelopment/rstr/issue/1/
|
|
19
|
-
|
|
19
|
+
"""
|
|
20
20
|
pattern = r'.+'
|
|
21
21
|
for _ in range(100):
|
|
22
22
|
assert re.match(pattern, self.rs.xeger(pattern))
|
|
@@ -96,3 +96,25 @@ class TestXeger(unittest.TestCase):
|
|
|
96
96
|
def test_zero_or_more_non_greedy(self) -> None:
|
|
97
97
|
pattern = r'a*?'
|
|
98
98
|
assert re.match(pattern, self.rs.xeger(pattern))
|
|
99
|
+
|
|
100
|
+
def test_exact_repeat_exceeds_star_plus_limit(self) -> None:
|
|
101
|
+
pattern = r'\d{105}'
|
|
102
|
+
assert re.match(pattern, self.rs.xeger(pattern))
|
|
103
|
+
|
|
104
|
+
def test_range_repeat_exceeds_star_plus_limit(self) -> None:
|
|
105
|
+
pattern = r'\d{102,105}'
|
|
106
|
+
assert re.match(pattern, self.rs.xeger(pattern))
|
|
107
|
+
|
|
108
|
+
def test_star_repeat_respects_limit(self) -> None:
|
|
109
|
+
pattern = r'a*'
|
|
110
|
+
for _ in range(100):
|
|
111
|
+
result = self.rs.xeger(pattern)
|
|
112
|
+
assert len(result) <= 100
|
|
113
|
+
assert re.match(pattern, result)
|
|
114
|
+
|
|
115
|
+
def test_plus_repeat_respects_limit(self) -> None:
|
|
116
|
+
pattern = r'b+'
|
|
117
|
+
for _ in range(100):
|
|
118
|
+
result = self.rs.xeger(pattern)
|
|
119
|
+
assert len(result) <= 100
|
|
120
|
+
assert re.match(pattern, result)
|
rstr-4.0.0/tox.ini
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
[tox]
|
|
2
|
+
envlist = pypy311,pypy312,py311,py312,py313,py314,py315,py316,linting,typing
|
|
3
|
+
skipsdist = true
|
|
4
|
+
|
|
5
|
+
[testenv]
|
|
6
|
+
commands=python -m unittest {posargs}
|
|
7
|
+
|
|
8
|
+
[testenv:linting]
|
|
9
|
+
deps =
|
|
10
|
+
ruff>=0.9.6
|
|
11
|
+
commands =
|
|
12
|
+
ruff format --check
|
|
13
|
+
ruff check
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
[testenv:typing]
|
|
17
|
+
deps =
|
|
18
|
+
mypy>=1.6.0,<2
|
|
19
|
+
commands =
|
|
20
|
+
mypy --strict {posargs: rstr}
|
rstr-3.2.2/.circleci/config.yml
DELETED
rstr-3.2.2/rstr/__init__.py
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
from rstr.xeger import Xeger
|
|
2
|
-
from rstr.rstr_base import SameCharacterError
|
|
3
|
-
|
|
4
|
-
Rstr = Xeger
|
|
5
|
-
_default_instance = Rstr()
|
|
6
|
-
|
|
7
|
-
rstr = _default_instance.rstr
|
|
8
|
-
xeger = _default_instance.xeger
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# This allows convenience methods from rstr to be accessed at the package
|
|
12
|
-
# level, without requiring the user to instantiate an Rstr() object.
|
|
13
|
-
printable = _default_instance.printable
|
|
14
|
-
letters = _default_instance.letters
|
|
15
|
-
uppercase = _default_instance.uppercase
|
|
16
|
-
lowercase = _default_instance.lowercase
|
|
17
|
-
digits = _default_instance.digits
|
|
18
|
-
punctuation = _default_instance.punctuation
|
|
19
|
-
nondigits = _default_instance.nondigits
|
|
20
|
-
nonletters = _default_instance.nonletters
|
|
21
|
-
whitespace = _default_instance.whitespace
|
|
22
|
-
nonwhitespace = _default_instance.nonwhitespace
|
|
23
|
-
normal = _default_instance.normal
|
|
24
|
-
word = _default_instance.word
|
|
25
|
-
nonword = _default_instance.nonword
|
|
26
|
-
unambiguous = _default_instance.unambiguous
|
|
27
|
-
postalsafe = _default_instance.postalsafe
|
|
28
|
-
urlsafe = _default_instance.urlsafe
|
|
29
|
-
domainsafe = _default_instance.domainsafe
|
rstr-3.2.2/rstr/xeger.py
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import random
|
|
2
|
-
import string
|
|
3
|
-
from itertools import chain
|
|
4
|
-
import typing
|
|
5
|
-
from typing import Any, Callable, Dict, Mapping, Pattern, Sequence, Union
|
|
6
|
-
|
|
7
|
-
from rstr.rstr_base import RstrBase
|
|
8
|
-
|
|
9
|
-
if typing.TYPE_CHECKING:
|
|
10
|
-
from random import Random
|
|
11
|
-
|
|
12
|
-
try:
|
|
13
|
-
import re._parser as sre_parse # type: ignore[import-not-found]
|
|
14
|
-
except ImportError: # Python < 3.11
|
|
15
|
-
import sre_parse
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
# The * and + characters in a regular expression
|
|
19
|
-
# match up to any number of repeats in theory,
|
|
20
|
-
# (and actually 65535 repeats in python) but you
|
|
21
|
-
# probably don't want that many repeats in your
|
|
22
|
-
# generated strings. This sets an upper-bound on
|
|
23
|
-
# repeats generated from + and * characters.
|
|
24
|
-
STAR_PLUS_LIMIT = 100
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class Xeger(RstrBase):
|
|
28
|
-
'''Inspired by the Java library Xeger: http://code.google.com/p/xeger/
|
|
29
|
-
This class adds functionality to Rstr allowing users to generate a
|
|
30
|
-
semi-random string from a regular expression.'''
|
|
31
|
-
|
|
32
|
-
def __init__(
|
|
33
|
-
self, _random: 'Random' = typing.cast('Random', random), **custom_alphabets: str,
|
|
34
|
-
) -> None:
|
|
35
|
-
super().__init__(_random, **custom_alphabets)
|
|
36
|
-
self._cache: Dict[str, str] = {}
|
|
37
|
-
self._categories: Mapping[str, Callable[[], str]] = {
|
|
38
|
-
'category_digit': lambda: self._alphabets['digits'],
|
|
39
|
-
'category_not_digit': lambda: self._alphabets['nondigits'],
|
|
40
|
-
'category_space': lambda: self._alphabets['whitespace'],
|
|
41
|
-
'category_not_space': lambda: self._alphabets['nonwhitespace'],
|
|
42
|
-
'category_word': lambda: self._alphabets['word'],
|
|
43
|
-
'category_not_word': lambda: self._alphabets['nonword'],
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
self._cases: Mapping[str, Callable[..., Any]] = {
|
|
47
|
-
'literal': lambda x: chr(x),
|
|
48
|
-
'not_literal': lambda x: self._random.choice(string.printable.replace(chr(x), '')),
|
|
49
|
-
'at': lambda x: '',
|
|
50
|
-
'in': lambda x: self._handle_in(x),
|
|
51
|
-
'any': lambda x: self.printable(1, exclude='\n'),
|
|
52
|
-
'range': lambda x: [chr(i) for i in range(x[0], x[1] + 1)],
|
|
53
|
-
'category': lambda x: self._categories[x](),
|
|
54
|
-
'branch': lambda x: ''.join(self._handle_state(i) for i in self._random.choice(x[1])),
|
|
55
|
-
'subpattern': lambda x: self._handle_group(x),
|
|
56
|
-
'assert': lambda x: ''.join(self._handle_state(i) for i in x[1]),
|
|
57
|
-
'assert_not': lambda x: '',
|
|
58
|
-
'groupref': lambda x: self._cache[x],
|
|
59
|
-
'min_repeat': lambda x: self._handle_repeat(*x),
|
|
60
|
-
'max_repeat': lambda x: self._handle_repeat(*x),
|
|
61
|
-
'negate': lambda x: [False],
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
def xeger(self, string_or_regex: Union[str, Pattern[str]]) -> str:
|
|
65
|
-
try:
|
|
66
|
-
pattern = typing.cast(Pattern[str], string_or_regex).pattern
|
|
67
|
-
except AttributeError:
|
|
68
|
-
pattern = typing.cast(str, string_or_regex)
|
|
69
|
-
|
|
70
|
-
parsed = sre_parse.parse(pattern)
|
|
71
|
-
result = self._build_string(parsed)
|
|
72
|
-
self._cache.clear()
|
|
73
|
-
return result
|
|
74
|
-
|
|
75
|
-
def _build_string(self, parsed: Any) -> str:
|
|
76
|
-
newstr = []
|
|
77
|
-
for state in parsed:
|
|
78
|
-
newstr.append(self._handle_state(state))
|
|
79
|
-
return ''.join(newstr)
|
|
80
|
-
|
|
81
|
-
def _handle_state(self, state: Any) -> Any:
|
|
82
|
-
opcode, value = state
|
|
83
|
-
opcode = opcode.name.lower()
|
|
84
|
-
if opcode == 'category':
|
|
85
|
-
value = value.name.lower()
|
|
86
|
-
return self._cases[opcode](value)
|
|
87
|
-
|
|
88
|
-
def _handle_group(self, value: Sequence[Any]) -> str:
|
|
89
|
-
result = ''.join(self._handle_state(i) for i in value[-1])
|
|
90
|
-
if value[0]:
|
|
91
|
-
self._cache[value[0]] = result
|
|
92
|
-
return result
|
|
93
|
-
|
|
94
|
-
def _handle_in(self, value: Any) -> Any:
|
|
95
|
-
candidates = list(chain(*(self._handle_state(i) for i in value)))
|
|
96
|
-
if candidates[0] is False:
|
|
97
|
-
candidates = list(set(string.printable).difference(candidates[1:]))
|
|
98
|
-
return self._random.choice(candidates)
|
|
99
|
-
|
|
100
|
-
def _handle_repeat(self, start_range: int, end_range: int, value: str) -> str:
|
|
101
|
-
result = []
|
|
102
|
-
end_range = min((end_range, STAR_PLUS_LIMIT))
|
|
103
|
-
times = self._random.randint(start_range, end_range)
|
|
104
|
-
for i in range(times):
|
|
105
|
-
result.append(''.join(self._handle_state(i) for i in value))
|
|
106
|
-
return ''.join(result)
|
rstr-3.2.2/tox.ini
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|