How to use the pycountry.countries.lookup function in pycountry

To help you get started, we’ve selected a few pycountry examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
if len(row) == 1 and row[0].strip() == '':
                        continue

                    email = val(row, 2, "").lower()
                    orcid = val(row, 15)
                    external_id = val(row, 16)

                    if not email and not orcid and external_id and validators.email(external_id):
                        # if email is missing and external ID is given as a valid email, use it:
                        email = external_id

                    # The uploaded country must be from ISO 3166-1 alpha-2
                    country = val(row, 11)
                    if country:
                        try:
                            country = countries.lookup(country).alpha_2
                        except Exception:
                            raise ModelException(
                                f" (Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                                f"#{row_no+2}: {row}. Header: {header}")

                    if not (email or orcid):
                        raise ModelException(
                            f"Missing user identifier (email address or ORCID iD) in the row "
                            f"#{row_no+2}: {row}. Header: {header}")

                    if orcid:
                        validate_orcid_id(orcid)

                    if not email or not validators.email(email):
                        raise ValueError(
                            f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")
github AnyBlok / AnyBlok / anyblok / column.py View on Github external
def setter_format_value(self, value):
        """Return formatted country value

        :param value:
        :return:
        """
        if value and not isinstance(value, self.sqlalchemy_type.python_type):
            value = pycountry.countries.get(
                **{
                    self.mode: value,
                    'default': pycountry.countries.lookup(value)
                })

        return value
github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
raise ModelException(
                    f"Review Group ID is mandatory, #{row_no+2}: {row}. Header: {header}")

            convening_org_name = val(row, 16)
            convening_org_city = val(row, 17)
            convening_org_country = val(row, 19)

            if not (convening_org_name and convening_org_city and convening_org_country):
                raise ModelException(
                    f"Information about Convening Organisation (Name, City and Country) is mandatory, "
                    f"#{row_no+2}: {row}. Header: {header}")

            # The uploaded country must be from ISO 3166-1 alpha-2
            if convening_org_country:
                try:
                    convening_org_country = countries.lookup(convening_org_country).alpha_2
                except Exception:
                    raise ModelException(
                        f" (Convening Org Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                        f"#{row_no+2}: {row}. Header: {header}")

            orcid, email = val(row, 23), val(row, 22, "").lower()
            if orcid:
                validate_orcid_id(orcid)
            if email and not validators.email(email):
                raise ValueError(
                    f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")

            external_id_type = val(row, 29)
            external_id_value = val(row, 30)
            if bool(external_id_type) != bool(external_id_value):
                raise ModelException(
github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
# skip empty lines:
            if len(row) == 0:
                continue
            if len(row) == 1 and row[0].strip() == '':
                continue

            funding_type = val(row, 4)
            if not funding_type:
                raise ModelException(
                    f"Funding type is mandatory, #{row_no+2}: {row}. Header: {header}")

            # The uploaded country must be from ISO 3166-1 alpha-2
            country = val(row, 14)
            if country:
                try:
                    country = countries.lookup(country).alpha_2
                except Exception:
                    raise ModelException(
                        f" (Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                        f"#{row_no+2}: {row}. Header: {header}")

            orcid, email = val(row, 18), val(row, 21, "").lower()
            if orcid:
                validate_orcid_id(orcid)
            if email and not validators.email(email):
                raise ValueError(
                    f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")

            external_id_type = val(row, 22)
            external_id_value = val(row, 23)
            if bool(external_id_type) != bool(external_id_value):
                raise ModelException(
github DOAJ / doaj / portality / datasets.py View on Github external
def get_country_code(current_country, fail_if_not_found=False):
    """ Get the two-character country code for a given country name """
    try:
        return pycountry.countries.lookup(current_country).alpha_2
    except LookupError:
        return None if fail_if_not_found else current_country
github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
# skip empty lines:
            if len(row) == 0:
                continue
            if len(row) == 1 and row[0].strip() == '':
                continue

            work_type = val(row, 6)
            if not work_type:
                raise ModelException(
                    f"Funding type is mandatory, #{row_no+2}: {row}. Header: {header}")

            # The uploaded country must be from ISO 3166-1 alpha-2
            country = val(row, 14)
            if country:
                try:
                    country = countries.lookup(country).alpha_2
                except Exception:
                    raise ModelException(
                        f" (Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                        f"#{row_no+2}: {row}. Header: {header}")

            orcid, email = val(row, 16), val(row, 19, "").lower()
            if orcid:
                validate_orcid_id(orcid)
            if email and not validators.email(email):
                raise ValueError(
                    f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")

            external_id_type = val(row, 20)
            external_id_value = val(row, 21)
            if bool(external_id_type) != bool(external_id_value):
                raise ModelException(
github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
raise ModelException(
                    f"Review Group ID is mandatory, #{row_no+2}: {row}. Header: {header}")

            convening_org_name = val(row, 16)
            convening_org_city = val(row, 17)
            convening_org_country = val(row, 19)

            if not (convening_org_name and convening_org_city and convening_org_country):
                raise ModelException(
                    f"Information about Convening Organisation (Name, City and Country) is mandatory, "
                    f"#{row_no+2}: {row}. Header: {header}")

            # The uploaded country must be from ISO 3166-1 alpha-2
            if convening_org_country:
                try:
                    convening_org_country = countries.lookup(convening_org_country).alpha_2
                except Exception:
                    raise ModelException(
                        f" (Convening Org Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                        f"#{row_no+2}: {row}. Header: {header}")

            orcid, email = val(row, 23), val(row, 22, "").lower()
            if orcid:
                validate_orcid_id(orcid)
            if email and not validators.email(email):
                raise ValueError(
                    f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")

            external_id_type = val(row, 29)
            external_id_value = val(row, 30)
            if bool(external_id_type) != bool(external_id_value):
                raise ModelException(
github eilbecklab / clinvar-miner / scrape-submitter-info.py View on Github external
root = html5lib.parse(f, transport_encoding=f.info().get_content_charset())
            submitter_el = root.find('.//html:div[@id="maincontent"]//html:div[@class="submitter_main indented"]', ns)
            name_el = submitter_el.find('./html:h2', ns)
            contact_info_el = submitter_el.find('.//html:div[@class="indented"]/html:div[@class="indented"]', ns)

            if name_el != None and name_el.text: #submitter 1 has no name
                submitter_name = re.sub(r'\s+', ' ', name_el.text.strip())

            if contact_info_el != None: #submitter 1 has no contact information
                contact_info = list(contact_info_el.itertext())[1:]
                contact_info = list(filter(lambda info: not re.match('http://|https://|Organization ID:', info), contact_info))
                if len(contact_info) >= 1:
                    country_and_zip = re.match('(.+) - (.+)', contact_info[-1])
                    country_name = country_and_zip.group(1) if country_and_zip else contact_info[-1]
                    try:
                        country_code = countries.lookup(country_name).alpha_3
                    except LookupError: #not a real country
                        pass
    except HTTPError as err:
        if err.code == 404:
            print('\r\033[KNo information for submitter ' + str(submitter_id))
        else:
            raise err

    return [submitter_id, submitter_name, country_code]
github Royal-Society-of-New-Zealand / NZ-ORCID-Hub / orcid_hub / models.py View on Github external
# skip empty lines:
            if len(row) == 0:
                continue
            if len(row) == 1 and row[0].strip() == '':
                continue

            work_type = val(row, 6)
            if not work_type:
                raise ModelException(
                    f"Funding type is mandatory, #{row_no+2}: {row}. Header: {header}")

            # The uploaded country must be from ISO 3166-1 alpha-2
            country = val(row, 14)
            if country:
                try:
                    country = countries.lookup(country).alpha_2
                except Exception:
                    raise ModelException(
                        f" (Country must be 2 character from ISO 3166-1 alpha-2) in the row "
                        f"#{row_no+2}: {row}. Header: {header}")

            orcid, email = val(row, 16), val(row, 19, "").lower()
            if orcid:
                validate_orcid_id(orcid)
            if email and not validators.email(email):
                raise ValueError(
                    f"Invalid email address '{email}'  in the row #{row_no+2}: {row}")

            external_id_type = val(row, 20)
            external_id_value = val(row, 21)
            if bool(external_id_type) != bool(external_id_value):
                raise ModelException(
github DOAJ / doaj / portality / datasets.py View on Github external
def get_country_name(code):
    """ Get the name of a country from its two-character code """
    try:
        return pycountry.countries.lookup(code).name
    except LookupError:
        return code  # return what was passed in if not found