__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from .. import BaseProvider, ElementsType, date_time
  2. localized = True
  3. class Provider(BaseProvider):
  4. city_suffixes: ElementsType = ["Ville"]
  5. street_suffixes: ElementsType = ["Street"]
  6. city_formats: ElementsType = ("{{first_name}} {{city_suffix}}",)
  7. street_name_formats: ElementsType = ("{{last_name}} {{street_suffix}}",)
  8. street_address_formats: ElementsType = ("{{building_number}} {{street_name}}",)
  9. address_formats: ElementsType = ("{{street_address}} {{postcode}} {{city}}",)
  10. building_number_formats: ElementsType = ("##",)
  11. postcode_formats: ElementsType = ("#####",)
  12. countries: ElementsType = [tz["name"] for tz in date_time.Provider.countries]
  13. ALPHA_2 = "alpha-2"
  14. ALPHA_3 = "alpha-3"
  15. alpha_2_country_codes: ElementsType = [tz["alpha-2-code"] for tz in date_time.Provider.countries]
  16. alpha_3_country_codes: ElementsType = [tz["alpha-3-code"] for tz in date_time.Provider.countries]
  17. def city_suffix(self) -> str:
  18. """
  19. :example 'town'
  20. """
  21. return self.random_element(self.city_suffixes)
  22. def street_suffix(self) -> str:
  23. """
  24. :example 'Avenue'
  25. """
  26. return self.random_element(self.street_suffixes)
  27. def building_number(self) -> str:
  28. """
  29. :example '791'
  30. """
  31. return self.numerify(self.random_element(self.building_number_formats))
  32. def city(self) -> str:
  33. """
  34. :example 'Sashabury'
  35. """
  36. pattern: str = self.random_element(self.city_formats)
  37. return self.generator.parse(pattern)
  38. def street_name(self) -> str:
  39. """
  40. :example 'Crist Parks'
  41. """
  42. pattern: str = self.random_element(self.street_name_formats)
  43. return self.generator.parse(pattern)
  44. def street_address(self) -> str:
  45. """
  46. :example '791 Crist Parks'
  47. """
  48. pattern: str = self.random_element(self.street_address_formats)
  49. return self.generator.parse(pattern)
  50. def postcode(self) -> str:
  51. """
  52. :example 86039-9874
  53. """
  54. return self.bothify(self.random_element(self.postcode_formats)).upper()
  55. def address(self) -> str:
  56. """
  57. :example '791 Crist Parks, Sashabury, IL 86039-9874'
  58. """
  59. pattern: str = self.random_element(self.address_formats)
  60. return self.generator.parse(pattern)
  61. def country(self) -> str:
  62. return self.random_element(self.countries)
  63. def country_code(self, representation: str = ALPHA_2) -> str:
  64. if representation == self.ALPHA_2:
  65. return self.random_element(self.alpha_2_country_codes)
  66. elif representation == self.ALPHA_3:
  67. return self.random_element(self.alpha_3_country_codes)
  68. else:
  69. raise ValueError("`representation` must be one of `alpha-2` or `alpha-3`.")
  70. def current_country_code(self) -> str:
  71. try:
  72. return self.__lang__.split("_")[1] # type: ignore
  73. except IndexError:
  74. raise AttributeError("Country code cannot be determined from locale")
  75. def current_country(self) -> str:
  76. current_country_code = self.current_country_code()
  77. current_country = [
  78. tz["name"] for tz in date_time.Provider.countries if tz["alpha-2-code"] == current_country_code
  79. ]
  80. if len(current_country) == 1:
  81. return current_country[0] # type: ignore
  82. elif len(current_country) > 1:
  83. raise ValueError(f"Ambiguous country for country code {current_country_code}: {current_country}")
  84. else:
  85. raise ValueError(f"No appropriate country for country code {current_country_code}")