model-bakery 1.12.0__tar.gz → 1.13.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.

Potentially problematic release.


This version of model-bakery might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: model-bakery
3
- Version: 1.12.0
3
+ Version: 1.13.0
4
4
  Summary: Smart object creation facility for Django.
5
5
  Project-URL: Homepage, https://github.com/model-bakers/model_bakery
6
6
  Author-email: berin <bernardoxhc@gmail.com>, amureki <amureki@hey.com>
@@ -0,0 +1 @@
1
+ __version__ = "1.13.0"
@@ -59,6 +59,10 @@ def _valid_quantity(quantity: Optional[Union[str, int]]) -> bool:
59
59
  return quantity is not None and (not isinstance(quantity, int) or quantity < 1)
60
60
 
61
61
 
62
+ def seed(seed: Union[int, float, str, bytes, bytearray, None]) -> None:
63
+ Baker.seed(seed)
64
+
65
+
62
66
  @overload
63
67
  def make(
64
68
  _model: Union[str, Type[M]],
@@ -312,13 +316,22 @@ def _custom_baker_class() -> Optional[Type]:
312
316
 
313
317
 
314
318
  class Baker(Generic[M]):
319
+ SENTINEL = object()
320
+
315
321
  attr_mapping: Dict[str, Any] = {}
316
322
  type_mapping: Dict = {}
317
323
 
324
+ _global_seed: Union[object, int, float, str, bytes, bytearray, None] = SENTINEL
325
+
318
326
  # Note: we're using one finder for all Baker instances to avoid
319
327
  # rebuilding the model cache for every make_* or prepare_* call.
320
328
  finder = ModelFinder()
321
329
 
330
+ @classmethod
331
+ def seed(cls, seed: Union[int, float, str, bytes, bytearray, None]) -> None:
332
+ random_gen.baker_random.seed(seed)
333
+ cls._global_seed = seed
334
+
322
335
  @classmethod
323
336
  def create(
324
337
  cls,
@@ -14,7 +14,7 @@ import warnings
14
14
  from datetime import date, datetime, time, timedelta
15
15
  from decimal import Decimal
16
16
  from os.path import abspath, dirname, join
17
- from random import choice, randint, random, uniform
17
+ from random import Random
18
18
  from typing import Any, Callable, List, Optional, Tuple, Union
19
19
  from uuid import UUID
20
20
 
@@ -27,6 +27,8 @@ MAX_LENGTH = 300
27
27
  # Postgres database.
28
28
  MAX_INT = 100000000000
29
29
 
30
+ baker_random = Random()
31
+
30
32
 
31
33
  def get_content_file(content: bytes, name: str) -> ContentFile:
32
34
  return ContentFile(content, name=name)
@@ -57,7 +59,7 @@ def gen_from_list(a_list: Union[List[str], range]) -> Callable:
57
59
  >>> attr_mapping = {'some_field': gen_from_list(['A', 'B', 'C'])}
58
60
 
59
61
  """
60
- return lambda: choice(list(a_list))
62
+ return lambda: baker_random.choice(list(a_list))
61
63
 
62
64
 
63
65
  # -- DEFAULT GENERATORS --
@@ -75,16 +77,16 @@ def gen_from_choices(choices: List) -> Callable:
75
77
 
76
78
 
77
79
  def gen_integer(min_int: int = -MAX_INT, max_int: int = MAX_INT) -> int:
78
- return randint(min_int, max_int)
80
+ return baker_random.randint(min_int, max_int)
79
81
 
80
82
 
81
83
  def gen_float() -> float:
82
- return random() * gen_integer()
84
+ return baker_random.random() * gen_integer()
83
85
 
84
86
 
85
87
  def gen_decimal(max_digits: int, decimal_places: int) -> Decimal:
86
88
  def num_as_str(x: int) -> str:
87
- return "".join([str(randint(0, 9)) for _ in range(x)])
89
+ return "".join([str(baker_random.randint(0, 9)) for _ in range(x)])
88
90
 
89
91
  if decimal_places:
90
92
  return Decimal(
@@ -110,7 +112,9 @@ def gen_time() -> time:
110
112
 
111
113
 
112
114
  def gen_string(max_length: int) -> str:
113
- return str("".join(choice(string.ascii_letters) for _ in range(max_length)))
115
+ return str(
116
+ "".join(baker_random.choice(string.ascii_letters) for _ in range(max_length))
117
+ )
114
118
 
115
119
 
116
120
  def _gen_string_get_max_length(field: Field) -> Tuple[str, int]:
@@ -125,7 +129,7 @@ gen_string.required = [_gen_string_get_max_length] # type: ignore[attr-defined]
125
129
 
126
130
  def gen_slug(max_length: int) -> str:
127
131
  valid_chars = string.ascii_letters + string.digits + "_-"
128
- return str("".join(choice(valid_chars) for _ in range(max_length)))
132
+ return str("".join(baker_random.choice(valid_chars) for _ in range(max_length)))
129
133
 
130
134
 
131
135
  gen_slug.required = ["max_length"] # type: ignore[attr-defined]
@@ -136,11 +140,11 @@ def gen_text() -> str:
136
140
 
137
141
 
138
142
  def gen_boolean() -> bool:
139
- return choice((True, False))
143
+ return baker_random.choice((True, False))
140
144
 
141
145
 
142
146
  def gen_null_boolean():
143
- return choice((True, False, None))
147
+ return baker_random.choice((True, False, None))
144
148
 
145
149
 
146
150
  def gen_url() -> str:
@@ -152,15 +156,15 @@ def gen_email() -> str:
152
156
 
153
157
 
154
158
  def gen_ipv6() -> str:
155
- return ":".join(format(randint(1, 65535), "x") for _ in range(8))
159
+ return ":".join(format(baker_random.randint(1, 65535), "x") for _ in range(8))
156
160
 
157
161
 
158
162
  def gen_ipv4() -> str:
159
- return ".".join(str(randint(1, 255)) for _ in range(4))
163
+ return ".".join(str(baker_random.randint(1, 255)) for _ in range(4))
160
164
 
161
165
 
162
166
  def gen_ipv46() -> str:
163
- ip_gen = choice([gen_ipv4, gen_ipv6])
167
+ ip_gen = baker_random.choice([gen_ipv4, gen_ipv6])
164
168
  return ip_gen()
165
169
 
166
170
 
@@ -197,7 +201,7 @@ gen_ip.required = ["protocol", "default_validators"] # type: ignore[attr-define
197
201
 
198
202
 
199
203
  def gen_byte_string(max_length: int = 16) -> bytes:
200
- generator = (randint(0, 255) for x in range(max_length))
204
+ generator = (baker_random.randint(0, 255) for x in range(max_length))
201
205
  return bytes(generator)
202
206
 
203
207
 
@@ -212,7 +216,7 @@ def gen_content_type():
212
216
  from django.contrib.contenttypes.models import ContentType
213
217
 
214
218
  try:
215
- return ContentType.objects.get_for_model(choice(apps.get_models()))
219
+ return ContentType.objects.get_for_model(baker_random.choice(apps.get_models()))
216
220
  except (AssertionError, RuntimeError):
217
221
  # AssertionError is raised by Django's test framework when db access is not available:
218
222
  # https://github.com/django/django/blob/stable/4.0.x/django/test/testcases.py#L150
@@ -276,7 +280,7 @@ gen_m2m.required = [_fk_model, "_using"] # type: ignore[attr-defined]
276
280
 
277
281
 
278
282
  def gen_coord() -> float:
279
- return uniform(0, 1)
283
+ return baker_random.uniform(0, 1)
280
284
 
281
285
 
282
286
  def gen_coords() -> str:
@@ -1 +0,0 @@
1
- __version__ = "1.12.0"
File without changes
File without changes
File without changes