Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/faker/providers/address/__init__.py: 59%

57 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:30 -0600

1from .. import BaseProvider, ElementsType, date_time 

2 

3localized = True 

4 

5 

6class Provider(BaseProvider): 

7 city_suffixes: ElementsType[str] = ["Ville"] 

8 street_suffixes: ElementsType[str] = ["Street"] 

9 city_formats: ElementsType[str] = ("{{first_name}} {{city_suffix}}",) 

10 street_name_formats: ElementsType[str] = ("{{last_name}} {{street_suffix}}",) 

11 street_address_formats: ElementsType[str] = ("{{building_number}} {{street_name}}",) 

12 address_formats: ElementsType[str] = ("{{street_address}} {{postcode}} {{city}}",) 

13 building_number_formats: ElementsType[str] = ("##",) 

14 postcode_formats: ElementsType[str] = ("#####",) 

15 countries: ElementsType[str] = [country.name for country in date_time.Provider.countries] 

16 

17 ALPHA_2 = "alpha-2" 

18 ALPHA_3 = "alpha-3" 

19 

20 alpha_2_country_codes: ElementsType[str] = [country.alpha_2_code for country in date_time.Provider.countries] 

21 alpha_3_country_codes: ElementsType[str] = [country.alpha_3_code for country in date_time.Provider.countries] 

22 

23 def city_suffix(self) -> str: 

24 """ 

25 :example: 'town' 

26 """ 

27 return self.random_element(self.city_suffixes) 

28 

29 def street_suffix(self) -> str: 

30 """ 

31 :example: 'Avenue' 

32 """ 

33 return self.random_element(self.street_suffixes) 

34 

35 def building_number(self) -> str: 

36 """ 

37 :example: '791' 

38 """ 

39 return self.numerify(self.random_element(self.building_number_formats)) 

40 

41 def city(self) -> str: 

42 """ 

43 :example: 'Sashabury' 

44 """ 

45 pattern: str = self.random_element(self.city_formats) 

46 return self.generator.parse(pattern) 

47 

48 def street_name(self) -> str: 

49 """ 

50 :example: 'Crist Parks' 

51 """ 

52 pattern: str = self.random_element(self.street_name_formats) 

53 return self.generator.parse(pattern) 

54 

55 def street_address(self) -> str: 

56 """ 

57 :example: '791 Crist Parks' 

58 """ 

59 pattern: str = self.random_element(self.street_address_formats) 

60 return self.generator.parse(pattern) 

61 

62 def postcode(self) -> str: 

63 """ 

64 :example: 86039-9874 

65 """ 

66 return self.bothify(self.random_element(self.postcode_formats)).upper() 

67 

68 def address(self) -> str: 

69 """ 

70 :example: '791 Crist Parks, Sashabury, IL 86039-9874' 

71 """ 

72 pattern: str = self.random_element(self.address_formats) 

73 return self.generator.parse(pattern) 

74 

75 def country(self) -> str: 

76 return self.random_element(self.countries) 

77 

78 def country_code(self, representation: str = ALPHA_2) -> str: 

79 if representation == self.ALPHA_2: 79 ↛ 81line 79 didn't jump to line 81, because the condition on line 79 was never false

80 return self.random_element(self.alpha_2_country_codes) 

81 elif representation == self.ALPHA_3: 

82 return self.random_element(self.alpha_3_country_codes) 

83 else: 

84 raise ValueError("`representation` must be one of `alpha-2` or `alpha-3`.") 

85 

86 def current_country_code(self) -> str: 

87 try: 

88 return self.__lang__.split("_")[1] # type: ignore 

89 except IndexError: 

90 raise AttributeError("Country code cannot be determined from locale") 

91 

92 def current_country(self) -> str: 

93 current_country_code = self.current_country_code() 

94 current_country = [ 

95 country.name for country in date_time.Provider.countries if country.alpha_2_code == current_country_code 

96 ] 

97 if len(current_country) == 1: 

98 return current_country[0] # type: ignore 

99 elif len(current_country) > 1: 

100 raise ValueError(f"Ambiguous country for country code {current_country_code}: {current_country}") 

101 else: 

102 raise ValueError(f"No appropriate country for country code {current_country_code}")