From 99cd214a4e853a4000b5a1245417718ccd46396c Mon Sep 17 00:00:00 2001 From: boopathinmc7217 Date: Wed, 24 Jan 2024 21:15:41 +0530 Subject: [PATCH 1/2] "adding height and weight for person provider" --- faker/providers/person/__init__.py | 17 +++++++++++++++++ tests/providers/test_person.py | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/faker/providers/person/__init__.py b/faker/providers/person/__init__.py index 54760558bb..b4f47a354c 100644 --- a/faker/providers/person/__init__.py +++ b/faker/providers/person/__init__.py @@ -328,3 +328,20 @@ 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) -> str: + """ + Generate random height for person in feet and inches + + Example : + 4'5" + """ + return f"{self.random_number(digits=1)}'{self.random_number(digits=2)}\"" + + def weight(self) -> str: + """ + Generate random weight for the person + Example : + 140 lbs + """ + return f"{self.random_number(digits=3)} lbs" diff --git a/tests/providers/test_person.py b/tests/providers/test_person.py index e361029a46..3523542359 100644 --- a/tests/providers/test_person.py +++ b/tests/providers/test_person.py @@ -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,24 @@ 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 + """ + self.assertTrue(hasattr(self.provider, "height")) + + def test_weight(self): + """ + Test weight generation foe person + """ + self.assertTrue(hasattr(self.provider, "weight")) + + if __name__ == "__main__": unittest.main() From 715e31ee7654f71c6993368d993c014b86c2aa92 Mon Sep 17 00:00:00 2001 From: boopathinmc7217 Date: Wed, 24 Jan 2024 21:42:28 +0530 Subject: [PATCH 2/2] "adding height and weight for person provider" --- faker/providers/person/__init__.py | 27 +++++++++++++++++++-------- tests/providers/test_person.py | 6 +++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/faker/providers/person/__init__.py b/faker/providers/person/__init__.py index b4f47a354c..78c7247098 100644 --- a/faker/providers/person/__init__.py +++ b/faker/providers/person/__init__.py @@ -329,19 +329,30 @@ def language_name(self) -> str: """Generate a random i18n language name (e.g. English).""" return self.random_element(self.language_names) - def height(self) -> str: + def height(self, fmt: str = "ft") -> str: """ Generate random height for person in feet and inches - Example : - 4'5" + Arguments: + ---------- + fmt = format of the data, by default it will be taken as ft and inch + """ - return f"{self.random_number(digits=1)}'{self.random_number(digits=2)}\"" + if fmt == "cm": + return f"{self.random_number(digits=2)} cms" + else: + return f"{self.random_number(digits=1)}'{self.random_number(digits=2)}\"" - def weight(self) -> str: + def weight(self, fmt: str = "lbs") -> str: """ Generate random weight for the person - Example : - 140 lbs + + Arguments: + --------- + fmt = format of the data, by default it will be taken as lbs """ - return f"{self.random_number(digits=3)} lbs" + weight = self.random_number(digits=3) + if fmt == "kg": + return f"{weight} kg" + else: + return f"{weight} lbs" diff --git a/tests/providers/test_person.py b/tests/providers/test_person.py index 3523542359..0e63767aa6 100644 --- a/tests/providers/test_person.py +++ b/tests/providers/test_person.py @@ -1343,12 +1343,16 @@ def test_height(self): 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 foe 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__":