From 19b9e52de408beb4d1558ee5184b47b13e58e573 Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Tue, 18 Apr 2023 11:37:22 +0100 Subject: [PATCH] Add CountryCodeSource.to_string() helper --- python/phonenumbers/phonenumber.py | 16 +++++++++++++++- python/phonenumbers/phonenumber.pyi | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/phonenumbers/phonenumber.py b/python/phonenumbers/phonenumber.py index 26c084bd3..ef0c1238c 100644 --- a/python/phonenumbers/phonenumber.py +++ b/python/phonenumbers/phonenumber.py @@ -17,7 +17,7 @@ # See the License for the specific language governing permissions and # limitations under the License. from .util import UnicodeMixin, ImmutableMixin, mutating_method -from .util import to_long, unicod, rpr, force_unicode +from .util import to_long, unicod, rpr, force_unicode, u class CountryCodeSource(object): @@ -48,6 +48,20 @@ class CountryCodeSource(object): # supplied as France. FROM_DEFAULT_COUNTRY = 20 + @classmethod + def to_string(cls, val): + """Return a string representation of a CountryCodeSource value""" + if val == CountryCodeSource.UNSPECIFIED: + return u("UNSPECIFIED") + elif val == CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN: + return u("FROM_NUMBER_WITH_PLUS_SIGN") + elif val == CountryCodeSource.FROM_NUMBER_WITH_IDD: + return u("FROM_NUMBER_WITH_IDD") + elif val == CountryCodeSource.FROM_DEFAULT_COUNTRY: + return u("FROM_DEFAULT_COUNTRY") + else: + return u("INVALID (%d)" % val) + class PhoneNumber(UnicodeMixin): """Class representing international telephone numbers. diff --git a/python/phonenumbers/phonenumber.pyi b/python/phonenumbers/phonenumber.pyi index 668ec6396..76daea8c1 100644 --- a/python/phonenumbers/phonenumber.pyi +++ b/python/phonenumbers/phonenumber.pyi @@ -9,6 +9,8 @@ class CountryCodeSource: FROM_NUMBER_WITH_IDD: int FROM_NUMBER_WITHOUT_PLUS_SIGN: int FROM_DEFAULT_COUNTRY: int + @classmethod + def to_string(cls, val: int) -> str: ... class PhoneNumber(UnicodeMixin): country_code: int | None