How to use the phonenumbers.phonenumberutil.parse function in phonenumbers

To help you get started, we’ve selected a few phonenumbers 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 stefanfoulis / django-phonenumber-field / phonenumber_field / tests.py View on Github external
def test_null_field_returns_none(self):
        model = NullablePhoneNumber()
        self.assertEqual(model.phone_number, None)
        model.phone_number = self.test_number_1
        self.assertEqual(type(model.phone_number), PhoneNumber)
        model.phone_number = phonenumberutil.parse(
            self.test_number_1, keep_raw_input=True
        )
        self.assertEqual(type(model.phone_number), PhoneNumber)
github stefanfoulis / django-phonenumber-field / tests / tests.py View on Github external
def test_null_field_returns_none(self):
        model = models.NullablePhoneNumber()
        self.assertIsNone(model.phone_number)
        model.phone_number = self.test_number_1
        self.assertIsInstance(model.phone_number, PhoneNumber)
        model.phone_number = phonenumberutil.parse(
            self.test_number_1, keep_raw_input=True
        )
        self.assertIsInstance(model.phone_number, PhoneNumber)
github SerenitySoftware / cahoots / tests / parsers / phone.py View on Github external
def test_build_phone_number_dict(self):

        numObj = phonenumberutil.parse("+1-979-549-5150")

        numDict = self.pp.build_phone_number_dict(numObj, "Angleton, TX", "US")

        self.assertEqual(numDict, self.testDict)
github SerenitySoftware / cahoots / cahoots / parsers / phone.py View on Github external
# description we want to modify the data and give it another go
        # with a country code added
        if not check_region and not num_desc:
            prefix = None
            if len(self.digits) == 11 and data_string[0] == "1":
                prefix = "+"
            elif len(self.digits) == 10 \
                    and (data_string[0].isdigit() or
                         data_string[0] in string.punctuation):
                prefix = "+1"

            if prefix:
                try:
                    # Second pass to see if we can get an actual
                    # geocode out of it using a hammer
                    second_pass = phonenumberutil.parse(prefix + data_string)
                    num_desc = geocoder.description_for_valid_number(
                        second_pass, "en"
                    ).strip()
                    if num_desc:
                        num_obj = second_pass
                        # confidence hit because we had to
                        # modify the data to get a result
                        self.confidence -= 5
                except (NumberParseException, Exception):
                    pass

        # Attempting to get a valid region
        num_region = phonenumberutil.region_code_for_number(num_obj)

        # This is the compiled phone number data that
        # we will use for the confidence decision
github SerenitySoftware / cahoots / cahoots / parsers / phone.py View on Github external
# description we want to modify the data and give it another go
        # with a country code added
        if not check_region and not num_desc:
            prefix = None
            if len(self.digits) == 11 and data_string[0] == "1":
                prefix = "+"
            elif len(self.digits) == 10 \
                    and (data_string[0].isdigit() or
                         data_string[0] in string.punctuation):
                prefix = "+1"

            if prefix:
                try:
                    # Second pass to see if we can get an actual
                    # geocode out of it using a hammer
                    second_pass = phonenumberutil.parse(prefix + data_string)
                    num_desc = geocoder.description_for_valid_number(
                        second_pass, "en"
                    ).strip()
                    if num_desc:
                        num_obj = second_pass
                        # confidence hit because we had to
                        # modify the data to get a result
                        self.confidence -= 5
                except (NumberParseException, Exception):
                    pass

        # Attempting to get a valid region
        num_region = phonenumberutil.region_code_for_number(num_obj)

        # This is the compiled phone number data that
        # we will use for the confidence decision
github 475Cumulus / TBone / tbone / data / fields / phone_number.py View on Github external
def __eq__(self, other):
        '''
        Override parent equality because we store only string representation
        of phone number, so we must compare only this string representation
        '''
        if (isinstance(other, PhoneNumber) or
                isinstance(other, phonenumbers.phonenumber.PhoneNumber) or
                isinstance(other, str)):

            fmt = self.FORMATS['E164']
            if isinstance(other, str):
                # convert string to phonenumbers.phonenumber.PhoneNumber instance
                try:
                    other = phonenumbers.phonenumberutil.parse(other)
                except phonenumbers.phonenumberutil.NumberParseException:
                    # Conversion is not possible, thus not equal
                    return False
            other_string = phonenumbers.format_number(other, fmt)
            return self.format_as(fmt) == other_string
        else:
            return False
github SerenitySoftware / cahoots / cahoots / parsers / phone.py View on Github external
def get_phone_number_object(self, data_string):
        """
        Takes the data_string and tries to parse a phone number out of it

        :param data_string: data that might be a phone number
        :type data_string: str
        :return: phone number data if it's a phone number
        :rtype: dict
        """
        try:
            check_region = (data_string[0] == "+")

            # First pass to see if it's a valid number
            num_obj = phonenumberutil.parse(
                data_string,
                _check_region=check_region
            )

            num_desc = geocoder.description_for_valid_number(
                num_obj, "en"
            ).strip()

        except NumberParseException:
            # If we can't parse it out, it's not a valid number
            return False

        # if we weren't able to check the region, and we didn't get a
        # description we want to modify the data and give it another go
        # with a country code added
        if not check_region and not num_desc:
github SerenitySoftware / cahoots / cahoots / parsers / equation.py View on Github external
def calculate_confidence(cls, data):
        """Calculates a confidence rating for this (possible) equation"""
        # We start with 100% confidence, and
        # then lower our confidence if needed.
        confidence = 100

        # lowering confidence if we have a phone number
        try:
            if len(data) <= 30 and len(data) >= 7:
                phonenumberutil.parse(data, _check_region=False)
                for _ in [c for c in data if c in string.punctuation]:
                    confidence -= 10
        except NumberParseException:
            pass

        return confidence