Skip to content

Commit

Permalink
Version 13.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lk-geimfari committed Jan 24, 2024
1 parent f7c5e5b commit e87d20a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 60 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 13.1.0
--------------

- Removed the `age()` and `work_experience()` methods from the `Person` provider. Use ``person.random.randint()`` instead.
- Fixed type hints for `Generic`. (See `#1470 <https://github.com/lk-geimfari/mimesis/issues/1471>`_).
- Added the `birthdate()` method to the `Person` provider. (See `#1470 <https://github.com/lk-geimfari/mimesis/issues/1470>`_).

Version 13.0.0
--------------

Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@
# built documents.
#
# The short X.Y version.
version = "13.0"
version = "13.1"
# The full version, including alpha/beta/rc tags.
release = "13.0.0"
release = "13.1.0"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
2 changes: 1 addition & 1 deletion mimesis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@
"__license__",
]

__version__ = "13.0.0"
__version__ = "13.1.0"
__title__ = "mimesis"
__description__ = "Mimesis: Fake Data Generator."
__url__ = "https://github.com/lk-geimfari/mimesis"
Expand Down
63 changes: 41 additions & 22 deletions mimesis/providers/person.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import typing as t
import uuid
from datetime import date, datetime
from string import ascii_letters, digits, punctuation

from mimesis.data import (
Expand All @@ -16,6 +17,7 @@
)
from mimesis.enums import Gender, TitleType
from mimesis.providers.base import BaseDataProvider
from mimesis.types import Date

__all__ = ["Person"]

Expand All @@ -30,39 +32,56 @@ def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
:param seed: Seed.
"""
super().__init__(*args, **kwargs)
self._store = {
"age": 0,
}

class Meta:
name = "person"
datafile = f"{name}.json"

def age(self, minimum: int = 16, maximum: int = 66) -> int:
"""Generates a random age of a person.
def _validate_birth_year_params(self, min_year: int, max_year: int) -> None:
if min_year > max_year:
raise ValueError("min_year must be less than or equal to max_year")

:param maximum: Maximum value of age.
:param minimum: Minimum value of age.
:return: Random integer.
if min_year < 1900:
raise ValueError("min_year must be greater than or equal to 1900")

:Example:
23.
"""
age = self.random.randint(minimum, maximum)
self._store["age"] = age
return age
if max_year > datetime.now().year:
raise ValueError(
"The max_year must be less than or equal to the current year"
)

def _is_leap_year(self, year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)

def work_experience(self, working_start_age: int = 22) -> int:
"""Generates a random work experience.
def birthdate(self, min_year: int = 1980, max_year: int = 2023) -> Date:
"""Generates a random birthdate as a :py:class:`datetime.date` object.
:param working_start_age: Age then person start to work.
:return: Depend on previous generated age.
:param min_year: Maximum birth year.
:param max_year: Minimum birth year.
:return: Random date object.
"""
age = self._store["age"]
if age == 0:
age = self.age()
self._validate_birth_year_params(min_year, max_year)
year = self.random.randint(min_year, max_year)
feb_days = 29 if self._is_leap_year(year) else 28

month_days_map = {
1: 31,
2: feb_days,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}

return max(age - working_start_age, 0)
month = self.random.randint(1, 12)
max_day = month_days_map[month]
day = self.random.randint(1, max_day)
return date(year=year, month=month, day=day)

def name(self, gender: Gender | None = None) -> str:
"""Generates a random name.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mimesis"
version = "13.0.0"
version = "13.1.0"
description = "Mimesis: Fake Data Generator."
authors = ["Isaak Uchakaev <hey@isaak.dev>"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_providers/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def test_generic_payment(self, g1, g2):
assert g1.payment.paypal() == g2.payment.paypal()

def test_generic_person(self, g1, g2):
assert g1.person.age() == g2.person.age()
assert g1.person.birthdate() == g2.person.birthdate()
assert g1.person.name() == g2.person.name()

def test_generic_science(self, g1, g2):
Expand Down
60 changes: 27 additions & 33 deletions tests/test_providers/test_person.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from datetime import date, datetime

import pytest

Expand All @@ -19,36 +20,31 @@ def test_str(self, person):
assert re.match(patterns.DATA_PROVIDER_STR_REGEX, str(person))

@pytest.mark.parametrize(
"minimum, maximum",
"min_year, max_year",
[
(16, 18),
(18, 21),
(22, 28),
(1900, 1950),
(1951, 2001),
(2001, 2023),
],
)
def test_age(self, _person, minimum, maximum):
result = _person.age(minimum, maximum)
assert (result >= minimum) and (result <= maximum)
def test_birthdate(self, _person, min_year, max_year):
birthdate = _person.birthdate(min_year, max_year)
assert min_year <= birthdate.year <= max_year
assert isinstance(birthdate, date)

def test_age_store(self, _person):
result = _person._store["age"]
assert result == 0

def test_age_update(self, _person):
result = _person.age() - _person._store["age"]
assert result == 0

def test_work_experience(self, _person):
result = _person.work_experience(working_start_age=0) - _person._store["age"]
assert result == 0

def test_work_experience_store(self, _person):
result = _person.work_experience() - _person.work_experience()
assert result == 0
@pytest.mark.parametrize(
"min_year, max_year",
[
(1899, 1950),
(datetime.now().year + 1, datetime.now().year + 3),
],
)
def test_birthdate_with_invalid_params(self, _person, min_year, max_year):
with pytest.raises(ValueError):
_person.birthdate(min_year, max_year)

def test_work_experience_extreme(self, _person):
result = _person.work_experience(working_start_age=100000)
assert result == 0
def test_is_leap_year(self, _person):
assert _person._is_leap_year(2024)

def test_password(self, _person):
result = _person.password(length=15)
Expand Down Expand Up @@ -344,14 +340,6 @@ def p1(self, seed):
def p2(self, seed):
return Person(seed=seed)

def test_age(self, p1, p2):
assert p1.age() == p2.age()
assert p1.age(12, 42) == p2.age(12, 42)

def test_work_experience(self, p1, p2):
assert p1.work_experience() == p2.work_experience()
assert p1.work_experience(19) == p2.work_experience(19)

def test_password(self, p1, p2):
assert p1.password() == p2.password()
assert p1.password(length=12, hashed=True) == p2.password(
Expand Down Expand Up @@ -413,6 +401,12 @@ def test_full_name(self, p1, p2):
def test_gender_code(self, p1, p2):
assert p1.gender_code() == p2.gender_code()

def test_birthdate(self, p1, p2):
assert p1.birthdate() == p2.birthdate()
assert p1.birthdate(min_year=1900, max_year=2023) == p2.birthdate(
min_year=1900, max_year=2023
)

def test_gender_symbol(self, p1, p2):
assert p1.gender_symbol() == p2.gender_symbol()

Expand Down

0 comments on commit e87d20a

Please sign in to comment.