Package: dz-phone-numbers
Algerian phone numbers as a value objectInspired fromthe PHP implementationwith some differences (see below).Algerian phone numbers as a value object implementation in Python. This can be used in your domain models or be integrated with your favorite framework.What is value object?In computer science, a value object is a small object that represents a simple entity whose equality is not based on identity: i.e. two value objects are equal when they have the same value, not necessarily being the same object.Read more onwikipedia.Installation:pip install dz-phone-numbersUsage:fromdz_phone_numberimportDZPhoneNumberdz_phone_number=DZPhoneNumber("0599000000")# or DZPhoneNumber.from_string("0599000000")dz_phone_number.indicative# <CountryCode.LOCAL: '0'>dz_phone_number.operator_or_region# <MobileOperator.OOREDOO: 5>dz_phone_number.suffix# '99000000'dz_phone_number.is_mobile()# Truedz_phone_number.is_landline()# a.k.a Fixe. Falsedz_phone_number.is_ooredoo()# Truedz_phone_number.is_djezzy()# falsedz_phone_number.is_annaba()# falseDZPhoneNumber("038123456").is_annaba()# True# repr:<DZPhoneNumber:CountryCode.LOCAL-MobileOperator:OOREDOO-99000000># str:str(dz_phone_number)# 0599000000EqualityDZPhoneNumber("0599000000")==DZPhoneNumber("+213 599000000")# TrueDZPhoneNumber("(0) 599000000")==DZPhoneNumber("0 599-00-00-00")# TrueDZPhoneNumber("(0) 599000000")==DZPhoneNumber("(0) 699000000"") # FalseCorrectnesstry:DZPhoneNumber("09 12 34 56 78")exceptValueError:# ValueError can catch itpass# Otherwise you can also expect `InvalidDZPhoneNumber` (an alias of ValueError).ImmutabilityThe object can't be modified if you try to modify of its members, aTypeErrorwill be raised:dz_phone_number.number='038123456'# will raise TypeError.Understanding the regex03 main parts of the full number are categorized into three groups: indicative (Country Code), Operator or Region (e.g. Ooredoo or Annaba), and the rest of the dial number.The regex uses Python'scapturing groupfeature built in its regex engine. Where "Country Code", "Operator or Region" and the "Number" are put into numbered groups when matched.The regex also uses a conditional statemet in the form of(?(1)yes|no)where(1)is the capturing group number. The following picture explain how it's working:Differences from the PHP implementation (as of writing this):This raises aValueError(Python built-in) instead of the broadExceptionInvalidDZPhoneNumberis an alias ofValueError.A different version of regex with support for landline (a.k.a fixe) numbers.Enums are used to both limit landline possible values, and make it extensible (e.g. very easy if, say, a new operator got into Algeria).This uses Python regex "capturing groups" feature. Where "Country Code", "Operator or Region" and the "Number" are put in groups when matched.pytest are used instead of any other spec or behavior testing.Immutability is achieved through__slots__and overriding__setattr__and__delattr__. This was a bit more flexibile that@dataclass(frozen=True).BonusesThis is a simple, self-contained problem. One of the reasons I wrote this is to serve as a python package example for the Algerian Python community where:The code isBlack-formatted.Type annotated code using Python type hints. Checked withMyPy.The project structure follows what's common for Python projects. See theHitchhiker guide.A simpleMakefilewithin to show how to build this (with Python wheels support) and how to release a package to pypi. Just issuemaketo see the available commands.Examplesetup.pyTesting with pytest.