Sha256: e2cb1a2a8d3093ed523cafaa0b16aa43985b99beca774f52815874f8308e4435

Contents?: true

Size: 895 Bytes

Versions: 104

Compression:

Stored size: 895 Bytes

Contents

import re


class Phone(object):
    def __init__(self, number):
        self.number = self._clean(number)
        self.area_code = self.number[:3]
        self.exchange_code = self.number[3:6]
        self.subscriber_number = self.number[-4:]

    def pretty(self):
        return "({}) {}-{}".format(
            self.area_code,
            self.exchange_code,
            self.subscriber_number,
        )

    def _clean(self, number):
        return self._normalize(
            re.sub(r'[^\d]', '', number),
        )

    def _normalize(self, number):
        if len(number) == 10 or len(number) == 11 and number.startswith('1'):
            valid = number[-10] in "23456789" and number[-7] in "23456789"
        else:
            valid = False

        if valid:
            return number[-10:]
        else:
            raise ValueError("{} is not a valid phone number".format(number))

Version data entries

104 entries across 104 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.179 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.178 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.177 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.176 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.175 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.174 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.173 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.172 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.171 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.170 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.169 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.167 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.166 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.165 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.164 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.163 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.162 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.161 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.160 tracks/python/exercises/phone-number/example.py