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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
""" | ||
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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the trailing s really make sense here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
""" | ||||||
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() |
There was a problem hiding this comment.
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 typeLiteralString
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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