Skip to content

Commit

Permalink
feat(npc): replace flaky faker with mimesis (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
kierun authored Feb 7, 2024
1 parent e54cda7 commit 2e3e548
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
27 changes: 12 additions & 15 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pynpc/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, param: str = "") -> None:
self.param = param
super()

def filter(self, _: logging.LogRecord) -> bool:
def filter(self, _: logging.LogRecord) -> bool: # pyright: ignore [reportIncompatibleMethodOverride]
# We have no choice in the method's name.
# We do not care about record thus mark it as _.
#
Expand Down
14 changes: 7 additions & 7 deletions pynpc/npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import orjson
import structlog
from faker import Faker
from mimesis import Gender, Locale, Person

from pynpc.name_corruptor import NameCorruptor, parse_patterns
from pynpc.skills import get_skill_value
Expand Down Expand Up @@ -75,8 +75,7 @@ def __init__(self, what: str = "fantasy") -> None:
# so an extra dir can override core behaviour
self._resources[jobj["resource"]] = ResourceObject(source=jobj)

# Faker, for names.
self._fake = Faker(["en_GB"])
# Name corruption.
data = Path(Path(__file__).resolve().parent.parent, "pynpc", "data", "name-corruption-pattern.json")
patterns = parse_patterns(orjson.loads(data.read_text()))
self._corruptor = NameCorruptor(patterns)
Expand Down Expand Up @@ -108,9 +107,10 @@ def reading(self) -> Reading:

def generate(self) -> None:
"""Generate an NPC."""
self.name_fem = self._get_name(self._fake.name_female()) # Female
self.name_mal = self._get_name(self._fake.name_male()) # Male
self.name_non = self._get_name(self._fake.name_nonbinary()) # Non-binary
person = Person(Locale.EN)
self.name_fem = person.full_name(gender=Gender.FEMALE)
self.name_mal = person.full_name(gender=Gender.MALE)
self.name_non = person.full_name()
_arc = self._resources["archetypes"].get_value()
self.nature = Trait(_arc["name"], _arc["description"])
self.demeanour = self.nature
Expand Down Expand Up @@ -164,7 +164,7 @@ def to_markdown(self) -> str:
| **Abilities** | **Name** | **Rank** |
| --- | --- | --- |
| **Primary** | | {self.skill_primary.name} | {self.skill_primary.rank} |
| **Primary** | {self.skill_primary.name} | {self.skill_primary.rank} |
| **Secondary** | {self.skill_secondary.name} | {self.skill_secondary.rank} |
| **Hobby** | {self.skill_hobby.name} | {self.skill_hobby.rank} |
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ structlog = ">=23.3,<25.0"
requests = "^2.31.0"
types-requests = "^2.31.0.20240125"
orjson = "^3.9.12"
faker = "^22.6.0"
mimesis = "^14.0.0"

[tool.poetry.group.dev.dependencies]
black = ">=23.12.1,<25.0.0"
Expand Down
10 changes: 4 additions & 6 deletions tests/test_npc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@

@pytest.fixture(autouse=True, scope="package")
def random() -> Any:
with patch("pynpc.npc.Faker") as mock_faker:
mock_fake = Mock()
mock_fake.name_female.return_value = "Ferro Maljinn"
mock_fake.name_male.return_value = "Logen Ninefingers"
mock_fake.name_nonbinary.return_value = "R2D2"
mock_faker.return_value = mock_fake
with patch("pynpc.npc.Person") as mock_fake:
mock_person = Mock()
mock_person.full_name.side_effect = ["Ferro Maljinn", "Logen Ninefingers", "R2D2"]
mock_fake.return_value = mock_person
return NPC()


Expand Down

0 comments on commit 2e3e548

Please sign in to comment.