Simple Doc Test failing without any reason [Help Needed]

afrobeard afrobeard at gmail.com
Wed May 28 07:48:10 EDT 2008


The following following code fails with the failiure:-


File "test.py", line 27, in __main__.sanitize_number
Failed example:
    sanitize_number('0321-4683113')
Expected:
    '03214683113'
Got:
    '03214683113'


Expected and Got looks the same. The value should verify.  What am I
doing wrong here ?

Thanks in advance. I really appreciate your time.

P.S. I tested this Python2.5 installed on both Ubuntu Hardy Heron and
Windows XP


/////////////////// Code /////////////////////////////////

import os;
import string;

def sanitize_number(input_number):
    """
    Sanitize Number Module Please make Test Cases in Here

    >>> sanitize_number('0321-4683113')
    '03214683113'
    >>> sanitize_number('03214683113')
    '03214683113'
    >>> sanitize_number('0321-468-3113')
    '03214683113'
    >>> sanitize_number('0321 4683113')
    '03214683113'
    >>> sanitize_number('0321 468 3113')
    '03214683113'
    >>> sanitize_number('3214683113')
    '03214683113'
    >>> sanitize_number('923214683113')
    '03214683113'
    >>> sanitize_number('00923214683113')
    '03214683113'
    >>> sanitize_number('+923214683113')
    '03214683113'
    >>> sanitize_number('+923214+683113')
    False


    """

    countries = {
        '92' : 'Pakistan'
        }

    """
        Reference http://en.wikipedia.org/wiki/List_of_mobile_codes_in_Pakistan
        Please update original wiki article if more codes are
discovered
    """
    providers = {
        '300' : 'Mobilink',
        '301' : 'Mobilink',
        '302' : 'Mobilink',
        '306' : 'Mobilink',
        '307' : 'Mobilink',
        '308' : 'Mobilink',
        '309' : 'Mobilink',
        '342' : 'Telenor',
        '343' : 'Telenor',
        '344' : 'Telenor',
        '345' : 'Telenor',
        '346' : 'Telenor',
        '321' : 'Warid',
        '322' : 'Warid',
        '323' : 'Warid',
        '331' : 'Ufone',
        '332' : 'Ufone',
        '333' : 'Ufone',
        '334' : 'Ufone',
        '313' : 'Zong',
        '314' : 'Zong',
        '320' : 'Instaphone',
        '335' : 'SCOM'
        }

    #Rule 1 Numbers can only contain spaces, + and

    input_number = string.join(input_number.split(), '') # Split
Spaces and join
    input_number = string.join(input_number.split('-'),'') # Split
Spaces and join

    #print "After Split Join", input_number

    if input_number.startswith('00'):
        input_number = input_number[2:]
    elif input_number.startswith('0'):
        input_number = input_number[1:]
    elif input_number.startswith('+'):
        input_number = input_number[1:]

    #print "Phase1", input_number

    #The number should now have either 10 or 12 digits depending on
whether or not country code is included

    if len(input_number) == 10 or len(input_number) == 12:
        if len(input_number) == 12:

            for code in countries.keys():
                if input_number.startswith(code):
                    input_number = input_number[2:]
                    break;
                else:
                    return False # Country Code not Supported

        for code in providers.keys():
                if input_number.startswith(code):
                    #input_number = input_number[3:]
                    input_number = '0'+input_number
                    #print "Result", input_number
                    return input_number

    return False

#print sanitize_number('+923214+683113') == False


def _test():
    import doctest
    doctest.testmod()

if __name__ == "__main__":
    _test()



More information about the Python-list mailing list