By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Simple Doc Test failing without any reason [Help Needed]

P: n/a
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...es_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()
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 28, 1:48 pm, afrobeard <afrobe...@gmail.comwrote:
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
Your code works for me (Python 2.5 WinXP).

Are you mixing tabs and spaces?

G.

Jun 27 '08 #2

P: n/a
I copied the text off here into a new file and it worked!.

I then took a diff between the version that didnt work and the version
that worked and the only difference was a couple of spaces after this
line:-
>>sanitize_number('0321-4683113')<There were some spaces in these angle brackets>
Apparently they caused the test case to fail on this.

Weird behavior :/

Thanks for your time Gerard and thanks to everyone else too.
On May 28, 5:11*pm, Gerard Flanagan <grflana...@gmail.comwrote:
On May 28, 1:48 pm, afrobeard <afrobe...@gmail.comwrote:
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

Your code works for me (Python 2.5 WinXP).

Are you mixing tabs and spaces?

G.
Jun 27 '08 #3

P: n/a
afrobeard <af*******@gmail.comwrites:
>sanitize_number('0321-4683113')<There were some spaces in these angle brackets>

Apparently they caused the test case to fail on this.

Weird behavior :/
Nope, exactly as specified: doctest is reporting differences berween
what output was generated and what output you described.
Thanks for your time Gerard and thanks to everyone else too.
Moral of the story: Configure your editor to highlight trailing space
on a line as an error, and always remove it before saving a program
source code file.

--
\ “Men never do evil so completely and cheerfully as when they |
`\ do it from religious conviction.” —Blaise Pascal |
_o__) (1623-1662), Pensées, #894. |
Ben Finney
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.