Why does "testmod" fails this test while it shouldn't?.

A forum for general discussion of the Python programming language.

Why does "testmod" fails this test while it shouldn't?.

Postby ismaelvc » Sat Apr 05, 2014 8:26 am

Why is testmod failing on my code?

Code: Select all
#!/usr/bin/env python3


__author__ = 'Ismael Venegas Castelló' + '\t' + 'ismael.vc1337@gmail.com'
__date__ = '04 / Abril / 2014'


from fractions import Fraction  as F
from textwrap  import dedent as dd


def calc(expresión=0, precisión=1000):
    '''
    Calculadora de expresiones con números complejos.

    expresión: expresión númerica válida.
    precisión: precisión de la simplificación.

    Uso:
    >>> from calc_comp import calc
    >>> from numpy import conjugate as c
    >>> z1 =  3-2j
    >>> z2 = -5-6j
    >>> z3 = -4+2j
    >>> exp = ((z1 - c(z2)) / z2) * ((2*z1) / z3)
    >>> calc(exp)
    (-0.49836065573770494-2.281967213114754j) =
         -152             -696
    ------------- + ------------- i
         305             305
    '''
    z = expresión
    frac_real = F.from_float(z.real).limit_denominator(precisión)
    frac_imag = F.from_float(z.imag).limit_denominator(precisión)
    print(dd('''\
                 {0} =
                      {1}             {2}
                 ------------- + ------------- i
                      {3}             {4}       '''.format(expresión,
                                                    frac_real.numerator,
                                                    frac_imag.numerator,
                                                    frac_real.denominator,
                                                    frac_imag.denominator)))


if __name__ == '__main__':
    import doctest
    doctest.testmod()


Code: Select all
ismaelvc@toybox ~/Code/TESCI/Algebra (git)-[master] % python calc_comp.py
**********************************************************************
File "calc_comp.py", line 26, in __main__.calc
Failed example:
    calc(exp)
Expected:
    (-0.49836065573770494-2.281967213114754j) =
         -152             -696
    ------------- + ------------- i
         305             305
Got:
    (-0.49836065573770494-2.281967213114754j) =
         -152             -696
    ------------- + ------------- i
         305             305
**********************************************************************
1 items had failures:
   1 of   7 in __main__.calc
***Test Failed*** 1 failures.
Last edited by Mekire on Sat Apr 05, 2014 1:07 pm, edited 3 times in total.
Reason: Included code instead of a github link.
ismaelvc
 
Posts: 7
Joined: Tue Apr 01, 2014 3:35 pm

Re: Why does "testmod" fails this test while it shouldn't?.

Postby Mekire » Sat Apr 05, 2014 1:04 pm

The whitespace at the end of the last line is making the strings not equal.

This is what you have:
Code: Select all
    print(dd('''\
                 {0} =
                      {1}             {2}
                 ------------- + ------------- i
                      {3}             {4}       '''.format(expresión,
                                                    frac_real.numerator,
                                                    frac_imag.numerator,
                                                    frac_real.denominator,
                                                    frac_imag.denominator)))

This is what you need:
Code: Select all
    print(dd('''\
                {0} =
                     {1}             {2}
                ------------- + ------------- i
                     {3}             {4}'''.format(expresión,
                                            frac_real.numerator,
                                            frac_imag.numerator,
                                            frac_real.denominator,
                                            frac_imag.denominator)))

I've got to say though that using doctest with floating points seems like an inherently bad idea. Use the unittest module.

Also I personally think it is a bad idea to use non-standard characters in identifier names. You might want to consider using expression and precision, as opposed to expresión and precisión.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Why does "testmod" fails this test while it shouldn't?.

Postby ismaelvc » Sat Apr 05, 2014 2:21 pm

Oh! I see, thank you very much Mekire!

This code snippet intention is to teach my classmates, some Python, I added some space there, in order to make it a little more readable for them (but I have Sublime Text delete all whitespace at the end of a line :shock: ), I also wanted to show them how Python 3 accepts unicode characters in strings and identifiers by default, pherhaps accents are not such a big issue, but there are some classic examples in spanish, where using only ASCII reads akward or even cacophonous to some people :lol: , example:

Code: Select all
año    # year
niño   # boy
niña   # girl

ano    # anus
nino
nina

# I hate this "C" style workaround:
anio 
ninio
ninia


Could you please elavorate into why you think it is not a good idea?

I have only used doctest.testmod thus far, but I will check out unittest, I have some other code that may also require it because it uses a lot of floating point math, which I'm also trying to change to use decimal.Decimal instead.
ismaelvc
 
Posts: 7
Joined: Tue Apr 01, 2014 3:35 pm

Re: Why does "testmod" fails this test while it shouldn't?.

Postby Mekire » Sat Apr 05, 2014 2:32 pm

It is just an unnecessary source of incompatibilities between Python 2 and 3.
Not a huge issue but I like to keep code as compatible as possible unless there is a real reason to break compatibility.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests

cron