|
| 1 | +--- |
| 2 | +title: Country Input Component |
| 3 | +sidebar_position: 1 |
| 4 | +description: This guide describes the implementation of ISO 3166 as a reusable input component, and how to use it for input validation or output transoformations. |
| 5 | +keywords: |
| 6 | +- country |
| 7 | +- countries |
| 8 | +- ISO 3166 |
| 9 | +- input |
| 10 | +- develop |
| 11 | +- odp |
| 12 | +- python |
| 13 | +--- |
| 14 | + |
| 15 | +import HeadTitle from '@site/src/components/General/HeadTitle.tsx'; |
| 16 | + |
| 17 | +<HeadTitle title="Country Input Component - Developer | OpenBB Docs" /> |
| 18 | + |
| 19 | +The `Country` class is a special type of string object. It behaves and evaluates like a string, but is extended with input validators and output formatters. Use this class for functions with a 'country' parameter, or to convert two and three-letter country codes, display labels, and names. All countries are spelled with English letters and exclude UTF characters such as umlauts. |
| 20 | + |
| 21 | +## Import |
| 22 | + |
| 23 | +```python |
| 24 | +from openbb_core.provider.utils.country_utils import Country |
| 25 | +``` |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +The `__repr__` of class instance will be the 2-letter country code. |
| 30 | + |
| 31 | +```python |
| 32 | +>>> Country("United States") |
| 33 | +'US' |
| 34 | +>>> Country("gb").name |
| 35 | +'United Kingdom' |
| 36 | +>>> Country("hk").alpha_3 |
| 37 | +'HKG' |
| 38 | +>>> Country("United States") == 'US' |
| 39 | +True |
| 40 | +>>> c = Country("united_states") |
| 41 | +>>> isinstance(c, str) |
| 42 | +True |
| 43 | +>>> c.alpha_2 |
| 44 | +'US' |
| 45 | +>>> c.alpha_3 |
| 46 | +'USA' |
| 47 | +>>> c.name |
| 48 | +'United States' |
| 49 | +>>> c.groups |
| 50 | +['G7', 'G20', 'NATO', 'OECD'] |
| 51 | +>>> c.is_member_of("G7") |
| 52 | +True |
| 53 | +``` |
| 54 | + |
| 55 | +### Properties |
| 56 | + |
| 57 | +- **alpha_2** : str - ISO 3166-1 alpha-2 code. |
| 58 | +- **alpha_3** : str - ISO 3166-1 alpha-3 code. |
| 59 | +- **name** : str - The country's display name, in English. |
| 60 | +- **numeric**: str - ISO 3166-1 numeric code. |
| 61 | +- **groups**: list[str] - List of membership groups the country belongs to. |
| 62 | + |
| 63 | +### Methods |
| 64 | + |
| 65 | +- **is_member_of(self, group: str) -> bool**: Check if a country is a member of a specific group. |
| 66 | + |
| 67 | +## Updating Definitions |
| 68 | + |
| 69 | +If the definitions have become stale, you can refresh them in the environment with the update script. |
| 70 | + |
| 71 | +```sh |
| 72 | +pip install pycountry |
| 73 | +python -m openbb_core.provider.utils.update_country_data |
| 74 | +``` |
| 75 | + |
| 76 | +## Sources |
| 77 | + |
| 78 | +- ISO 3166-1: https://en.wikipedia.org/wiki/ISO_3166-1 |
| 79 | +- pycountry: https://github.com/pycountry/pycountry |
| 80 | +- G7: https://en.wikipedia.org/wiki/G7 |
| 81 | +- G20: https://en.wikipedia.org/wiki/G20 |
| 82 | +- EU: https://european-union.europa.eu/principles-countries-history/eu-countries_en |
| 83 | +- NATO: https://www.nato.int/en/about-us/organization/nato-member-countries |
| 84 | +- OECD: https://en.wikipedia.org/wiki/OECD |
| 85 | +- OPEC: https://en.wikipedia.org/wiki/OPEC |
| 86 | +- BRICS: https://en.wikipedia.org/wiki/BRICS |
0 commit comments