Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"adding height and weight for person provider" #1982

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions faker/providers/person/__init__.py
Expand Up @@ -328,3 +328,31 @@ def suffix_female(self) -> str:
def language_name(self) -> str:
"""Generate a random i18n language name (e.g. English)."""
return self.random_element(self.language_names)

def height(self, fmt: str = "ft") -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt should probably be of type LiteralString in this case if it only supports a limited set of values. Additionally, I would argue that invalid values should not return imperial units, but just fail. Besides this, I am not sure whether defaulting to ft should really be done or if we have a way to use the current locale for it - speaking of myself, I never actually have to deal with ft.

Copy link
Collaborator

@fcurella fcurella Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the unit should be according to the locale. If a locale only uses one unit (eg: Italy only uses Metric), I wouldn't even have the parameter, and reserve it only for those locales that use both.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will look into way for using current locale

"""
Generate random height for person in feet and inches

Arguments:
----------
fmt = format of the data, by default it will be taken as ft and inch

"""
if fmt == "cm":
return f"{self.random_number(digits=2)} cms"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the trailing s really make sense here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the trailing s really make sense here?

No. There is no plural for it...

else:
return f"{self.random_number(digits=1)}'{self.random_number(digits=2)}\""

def weight(self, fmt: str = "lbs") -> str:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue the same as for the height regarding parameter handling here.

"""
Generate random weight for the person

Arguments:
---------
fmt = format of the data, by default it will be taken as lbs
"""
weight = self.random_number(digits=3)
if fmt == "kg":
return f"{weight} kg"
else:
return f"{weight} lbs"
24 changes: 24 additions & 0 deletions tests/providers/test_person.py
Expand Up @@ -5,6 +5,7 @@
from unittest import mock

from faker import Faker
from faker.providers.person import Provider as PersonProvider
from faker.providers.person.ar_AA import Provider as ArProvider
from faker.providers.person.az_AZ import Provider as AzAzProvider
from faker.providers.person.cs_CZ import Provider as CsCZProvider
Expand Down Expand Up @@ -1331,5 +1332,28 @@ def test_full_name(self):
raise AssertionError("Invalid number of name parts. Expected 2 or 3.")


class TestPersonProvider(unittest.TestCase):
def setUp(self):
self.fake = Faker()
self.provider = PersonProvider
Faker.seed(0)

def test_height(self):
"""
Test height generation for person
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Test height generation for person
Test height generation for person

"""
self.assertTrue(hasattr(self.provider, "height"))
self.assertRegex(self.fake.height(), r"\d'\d+\"")
self.assertRegex(self.fake.height("cm"), r"\d{1,3}\scms")

def test_weight(self):
"""
Test weight generation for person
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Test weight generation for person
Test weight generation for person

"""
self.assertTrue(hasattr(self.provider, "weight"))
self.assertRegex(self.fake.weight(), r"\d{1,3}\slbs")
self.assertRegex(self.fake.weight("kg"), r"\d{1,3}\skg")


if __name__ == "__main__":
unittest.main()