Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

exercism phone number python

import re

class PhoneNumber:
    def __init__(self, number):
      self.number = re.sub(r"^[+1]|[.s-()]", "", number)
      self.number = self.validate(self.number)
      self.area_code = self.number[:3]
      self.exchange_code = self.number[3:6]
      self.suscriber_num= self.number[6:]

    def validate(self, number):
      length = len(number)

      if length < 10 or length > 11:
        raise ValueError("Wrong Number Length")

      if length == 11:
        if number[0] != '1':
          raise ValueError("Incorrect country code")
        else:
          number = number[1:]

      if re.findall("[A-z]", number):
        raise ValueError("Incorrect Number: alpha inside")

      if int(number[0]) <= 1 or int(number[3]) <= 1:
        raise ValueError("Incorrect Number")

      return number

    def pretty(self):
      return f"({self.area_code})-{self.exchange_code}-{self.suscriber_num}"


# print(PhoneNumber("(223) 456-7890").number)
# print(PhoneNumber("(223) 456-7890").area_code)
# print(PhoneNumber("(223) 45a-7890").number)
Comment

PREVIOUS NEXT
Code Example
Python :: convert to pdf fresh little library that outputs our notebook in a nice LaTex format without installing/doing anything else. 
Python :: TemplateSyntaxError 
Python :: block url selenium python 
Python :: recieve output from java python 
Python :: python input text in file 
Python :: pandas get cvvlaue from antoiher column fom one coluikmnn value 
Python :: pycaw , Python Audio Control Lib 
Python :: Ipython.display latex in the IDE like spyder 
Python :: sphix dont see .py file 
Python :: numpy rolling 2d 
Python :: what optimizer to simplernn 
Python :: how to map data to a scale python 
Python :: he escape() function is used to convert the <, &, and characters to the corresponding entity references: 
Python :: rounding a number high up 
Python :: change column row box colour tkinter 
Python :: HTTP Error 403: Forbidden django account signup email 
Python :: eeetimetable 
Python :: condtion for equal time in selenium python 
Python :: print 2 letter python 
Python :: kwargs handling multiple arguments and iterating them loop 
Python :: Python:Gann square of 9 
Python :: accuracy sensitivity specificity 
Python :: Make exact copy of an environment 
Python :: Python - Comment vérifier une corde contient un nombre 
Python :: Type conversions in python 
Python :: ex: python 
Python :: pygame.k_kp_enter 
Python :: how to print the freq of each char by using dict in python 
Python :: Source code for making Telegram robot with Python 
Python :: networkx select edge 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =