forked from joke2k/faker
/
__init__.py
201 lines (166 loc) · 6.18 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# coding=utf-8
import re
import string
from faker.generator import random
from faker.utils.distribution import choice_distribution
_re_hash = re.compile(r'#')
_re_perc = re.compile(r'%')
_re_excl = re.compile(r'!')
_re_at = re.compile(r'@')
_re_qm = re.compile(r'\?')
class BaseProvider(object):
__provider__ = 'base'
__lang__ = None
def __init__(self, generator):
self.generator = generator
@classmethod
def random_int(cls, min=0, max=9999):
"""
Returns a random integer between two values.
:param min: lower bound value (inclusive; default=0)
:param max: upper bound value (inclusive; default=9999)
:returns: random integer between min and max
"""
return random.randint(min, max)
@classmethod
def random_digit(cls):
"""
Returns a random digit/number
between 0 and 9.
"""
return random.randint(0, 9)
@classmethod
def random_digit_not_null(cls):
"""
Returns a random non-zero digit/number
between 1 and 9.
"""
return random.randint(1, 9)
@classmethod
def random_digit_or_empty(cls):
"""
Returns a random digit/number
between 0 and 9 or an empty string.
"""
if random.randint(0, 1):
return random.randint(0, 9)
else:
return ''
@classmethod
def random_digit_not_null_or_empty(cls):
"""
Returns a random non-zero digit/number
between 1 and 9 or and empty string.
"""
if random.randint(0, 1):
return random.randint(1, 9)
else:
return ''
@classmethod
def random_number(cls, digits=None):
"""
Returns a random number with 1 digit (default, when digits==None)
or a random number with 0 to given number of digits.
:param digits: maximum number of digits
:returns: random number with 0 to given number of digits
"""
if digits is None:
digits = BaseProvider.random_digit()
return random.randint(0, pow(10, digits) - 1)
@classmethod
def random_letter(cls):
"""Returns a random letter (between a-z and A-Z)."""
return random.choice(getattr(string, 'letters', string.ascii_letters))
@classmethod
def random_element(cls, elements=('a', 'b', 'c')):
"""
Returns a random element from a passed object.
If `elements` is a dictionary, the value will be used as
a weighting element. For example::
random_element({"{{variable_1}}": 0.5, "{{variable_2}}": 0.2, "{{variable_3}}": 0.2, "{{variable_4}}": 0.1})
will have the following distribution:
* `variable_1`: 50% probability
* `variable_2`: 20% probability
* `variable_3`: 20% probability
* `variable_4`: 10% probability
"""
if isinstance(elements, dict):
choices = elements.keys()
probabilities = elements.values()
return choice_distribution(list(choices), list(probabilities))
else:
return random.choice(list(elements))
@classmethod
def random_sample(cls, elements=('a', 'b', 'c'), length=None):
if length is None:
length = random.randint(1, len(elements))
return [cls.random_element(elements) for _ in range(length)]
@classmethod
def random_sample_unique(cls, elements=('a', 'b', 'c'), length=None):
"""
Returns a `set` of random unique elements for the specified length.
"""
if length is None:
length = random.randint(1, len(elements))
if length > len(elements):
raise ValueError("Sample length cannot be longer than the number of elements to pick from.")
sample = set()
while len(sample) < length:
sample.add(cls.random_element(elements))
return sample
@classmethod
def randomize_nb_elements(cls, number=10, le=False, ge=False):
"""
Returns a random value near number.
:param number: value to which the result must be near
:param le: result must be lower or equal to number
:param ge: result must be greater or equal to number
:returns: a random int near number
"""
if le and ge:
return number
_min = 100 if ge else 60
_max = 100 if le else 140
return int(number * random.randint(_min, _max) / 100) + 1
@classmethod
def numerify(cls, text='###'):
"""
Replaces all placeholders in given text with randomized values,
replacing: all hash sign ('#') occurrences with a random digit
(from 0 to 9); all percentage sign ('%') occurrences with a
random non-zero digit (from 1 to 9); all exclamation mark ('!')
occurrences with a random digit (from 0 to 9) or an empty string;
and all at symbol ('@') occurrences with a random non-zero digit
(from 1 to 9) or an empty string.
:param text: string to be parsed
:returns: string with all numerical placeholders filled in
"""
text = _re_hash.sub(
lambda x: str(BaseProvider.random_digit()),
text)
text = _re_perc.sub(
lambda x: str(BaseProvider.random_digit_not_null()),
text)
text = _re_excl.sub(
lambda x: str(BaseProvider.random_digit_or_empty()),
text)
text = _re_at.sub(
lambda x: str(BaseProvider.random_digit_not_null_or_empty()),
text)
return text
@classmethod
def lexify(cls, text='????'):
"""
Replaces all question mark ('?') occurrences with a random letter.
:param text: string to be parsed
:returns: string with all letter placeholders filled in
"""
return _re_qm.sub(lambda x: BaseProvider.random_letter(), text)
@classmethod
def bothify(cls, text='## ??'):
"""
Replaces all placeholders with random numbers and letters.
:param text: string to be parsed
:returns: string with all numerical and letter placeholders filled in
"""
return BaseProvider.lexify(BaseProvider.numerify(text))