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.139 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.138 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.137 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.136 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.135 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.134 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.133 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.132 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.131 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.130 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.129 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.128 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.127 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.126 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.125 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.124 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.123 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.122 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.121 tracks/python/exercises/phone-number/example.py
trackler-2.2.1.120 tracks/python/exercises/phone-number/example.py