Doctest的输出与shell的输出不同

时间:2012-04-03 23:16:00

标签: python linked-list doctest

在doctest和shell中使用相同的代码会产生不同的输出 我有一个名为a()的函数,它经历了一些测试 这些相同的测试用于doctest(test()) 使用()我得到OBJECT-BLANKLINE-OBJECT,而test()给出了一个错误,只显示了第一个OBJECT。 这是doctest模块中的一个缺陷吗?

这是整个文件,顶部有一个()和test():

'''
>>> u1 = User("luis@fc.up.pt", "simples")
>>> u2 = User("ana@fc.up.pt", "complicada")
>>> u2.askFriend(u1)
>>> u1.recvRequest(u2)
>>> u1.confirmFriend(u2)
>>> p1 = Post(u1, "O ultimo post", "http://www.wikipedia.org")
>>> p2 = Post(u2, "A ultima resposta", "http://www.google.com")
>>> c = Comments()
>>> c.add(p1)
>>> c.add(p2)
>>> f1 = c.search(user="ana@fc.up.pt")
>>> print(f1)
A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
>>> f2 = c.search(likes=1)
>>> print(f2)
<BLANKLINE>
>>> f3 = c.search(text='post')
>>> print(f3)
O ultimo post
http://www.wikipedia.org
0 Gosto, 0 Nao gosto
<BLANKLINE>
A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
'''

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

def a():
    u1 = User("luis@fc.up.pt", "simples")
    u2 = User("ana@fc.up.pt", "complicada")
    u2.askFriend(u1)
    u1.recvRequest(u2)
    u1.confirmFriend(u2)
    p1 = Post(u1, "O ultimo post", "http://www.wikipedia.org")
    p2 = Post(u2, "A ultima resposta", "http://www.google.com")
    c = Comments()
    c.add(p1)
    c.add(p2)
    f3 = c.search(text="post")
    print(f3)

import string

class User:

    def __init__(self,email,passwd):
        self.email = email
        self.passwd = passwd
        self.name = None
        self.year = None
        self.active = True
        self.recv = [] 
        self.conf = []
        self.setPassword(self.passwd)

    def __str__(self):
        if self.name == None and self.active == True:
            return str(self.email) + ':' + 'ativa'

        elif self.name != None and self.active == True:
            return str(self.name) + ':' + str(self.email) + ':' + 'ativa'

        elif self.name != None and self.active == False:
            return str(self.name) + ':' + str(self.email) + ':' + 'inativa'

        else:
            return str(self.email) + ':' + 'inativa'

    def getEmail(self):
        return self.email

    def setPassword(self,passwd):
        abc = string.ascii_lowercase
        cifra = abc[3:] + abc[:3]
        dec = list(passwd)
        passwdCif = ""
        for i in dec:
            cif = cifra[abc.find(i)]
            passwdCif += cif
        self.passwd = passwdCif

    def getPassword(self):
        return self.passwd

    def setName(self,name):
        self.name = name

    def getName(self):
        return self.name

    def setBirth(self,year,month,day):
        self.year = year
        self.month = month
        self.day = day

    def getBirth(self):
        if self.year == None:
            return None
        else:
            return '(' + str(self.year) + ', ' + str(self.month) + ', ' + str(self.day) + ')'

    def isActive(self):
        if self.active == True:
            return True
        else:
            return False

    def setActive(self):
        self.active = True

    def setInactive(self):
        self.active = False

    def askFriend(self,u):
        self.conf.append(u)

    def recvRequest(self,u):
        self.recv.append(u)

    def confirmFriend(self,u):
        if len(self.recv) == 0:
            return None
        else:
            for i in self.recv:
                if i == u:
                    self.recv.remove(i)
                    self.conf.append(i)
                else:
                    return None


    def isFriend(self,u):
        if u in self.conf:
            return True
        elif self == u:
            return True
        else:
            return False

    def showPending(self):
        if len(self.recv) == 0:
            return None
        else:
            for i in self.recv:
                print i

    def showFriends(self):
        if len(self.conf) == 0:
            return None
        else:
            for i in self.conf:
                print i

class Post():
    def __init__(self,u,text='',link=None):
        self.u = u
        self.text = text
        self.link = link
        self.seg = None
        self.ant = None
        self.likeList = []
        self.dislikeList = []

    def __str__(self):
        if self.link == None:
            return str(self.text) + '\n' + str(len(self.likeList)) + ' Gosto, ' + str(len(self.dislikeList)) + ' Nao gosto'
        else:
            return str(self.text) + '\n' + str(self.link) + '\n' + str(len(self.likeList)) + ' Gosto, ' + str(len(self.dislikeList)) + ' Nao gosto'

    def updateText(self,text):
        self.text = text

    def updateLink(self,link):
        self.link = link

    def like(self,u):
        if (u in self.u.conf) or (u == self.u):
            if u in self.dislikeList:
                self.dislikeList.remove(u)
                self.likeList.append(u)
            elif u in self.likeList:
                return None
            else:
                self.likeList.append(u)
        else:
            return None

    def dislike(self,u):
        if (u in self.u.conf) or (u == self.u):
            if u in self.likeList:
                self.likeList.remove(u)
                self.dislikeList.append(u)
            elif u in self.dislikeList:
                return None
            else:
                self.dislikeList.append(u)
        else:
            return None

class Comments():
    def __init__(self, u=None, text='', link=None):
        self.u = u
        self.text = text
        self.link = link
        self.topo = None
        self.fim = None

    def __str__(self):
        actual = self.topo
        s = ''
        if actual == None:
            return ''
        while actual != None:
            if actual.seg == None:
                s += str(actual)
                actual = actual.seg
            elif actual.seg != None:
                s += str(actual) + '\n' + '\n'
                actual = actual.seg
        return s

    def add(self,comment):
        if self.topo == None:
            comment.ant = None
            comment.seg = None
            self.topo = comment
            self.fim = comment
        else:
            comment.ant = None
            comment.seg = self.topo
            self.topo.ant = comment
            self.topo = comment

    def remove(self,comment):
        actual = self.topo
        if (self.topo == self.fim) and (self.topo == comment):
            self.topo = None
            self.fim = None
            actual = None
        while actual!=None:
            if actual == comment:
                if actual.ant == None:
                    self.topo = actual.seg
                    actual.seg.ant = None
                elif actual.seg == None:
                    self.fim = actual.ant
                    actual.ant.seg = None
                else:
                    actual.seg.ant = actual.ant
                    actual.ant.seg = actual.seg
                break
            else:
                actual = actual.seg

    def countLike(self):
        count = 0
        actual = self.topo
        while actual != None:
            if len(actual.likeList) >= 1:
                count += 1
                actual = actual.seg
            else:
                actual = actual.seg
        return count

    def showRecentComments(self,n):
        count = 1
        actual = self.topo
        sC = ''
        if actual == None:
            return None
        while actual != None:
            if count < n:
                if actual.seg == None:
                    sC += str(actual)
                    count += 1
                    actual = actual.seg
                else:
                    sC += str(actual) + '\n' + '\n'
                    count += 1
                    actual = actual.seg
            elif count == n:
                sC += str(actual)
                count += 1
                actual = actual.seg
            elif count > n:
                break 
        print sC

    def search(self, user=None, likes=None, dislikes=None, text=None):
        result = []
        actual = self.topo
        cR = Comments()
        if actual == None:
            return None

        while actual != None:
            if user != None:
                if actual.u.email != user:
                    actual = actual.seg
                elif actual.u.email == user:
                    result.append(actual)
                    actual = actual.seg
            elif user == None:
                break
        actual = self.topo

        while actual != None:
            if likes != None:
                if likes > len(actual.likeList):
                    actual = actual.seg
                elif likes <= len(actual.likeList):
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif likes == None:
                break
        actual = self.topo

        while actual != None:
            if dislikes != None:
                if dislikes > len(actual.dislikeList):
                    actual = actual.seg
                elif dislikes <= len(actual.dislikeList):
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif dislikes == None:
                break
        actual = self.topo

        while actual != None:
            if text != None:
                if text not in actual.text:
                    actual = actual.seg
                elif text in actual.text:
                    if actual in result:
                        actual = actual.seg
                    else:
                        result.append(actual)
                        actual = actual.seg
            elif text == None:
                break
        if len(result) != 0:
            for i in result:
                cR.add(i)
        return cR

这是我使用a()和test()获得的输出:

>>> a()
O ultimo post
http://www.wikipedia.org
0 Gosto, 0 Nao gosto

A ultima resposta
http://www.google.com
0 Gosto, 0 Nao gosto
>>> test()
**********************************************************************
File "__main__", line 22, in __main__
Failed example:
    print(f3)
Expected:
    O ultimo post
    http://www.wikipedia.org
    0 Gosto, 0 Nao gosto
    <BLANKLINE>
    A ultima resposta
    http://www.google.com
    0 Gosto, 0 Nao gosto
Got:
    A ultima resposta
    http://www.google.com
    0 Gosto, 0 Nao gosto
**********************************************************************
1 items had failures:
   1 of  16 in __main__
***Test Failed*** 1 failures.
>>> 

我还要为我之前提出的问题道歉 希望这个会帮助我和其他人。

1 个答案:

答案 0 :(得分:2)

你的doctest和你的函数之间有一个明显的区别:虽然你的函数是这样做的:

# previous code
c.add(p2)
f3 = c.search(text="post")
print(f3)

你的doctest做到了这一点:

# previous code
c.add(p2)
f1 = c.search(user="ana@fc.up.pt")
print(f1)
f2 = c.search(likes=1)
print(f2)
f3 = c.search(text='post')
print(f3)

我不知道究竟发生了什么,但可能你应该写你的功能来做同样的事情。如果结果仍然不同,则doctest会出现问题,否则会出现问题。

修改

现在我开始明白你为什么遇到麻烦了,这是麻烦的问题。它从搜索功能顶部的这一行开始:

actual = self.topo

我认为您不了解Python分配属性的方式。稍后在您的代码中更改实际值,然后再将self.topo指定为实际值:

actual.seg = "somevalue" # sorry, i can't remember what you did"
actual = self.topo

第二行完全没有意义,因为实际已经是self.topo!它不是self.topo的价值,而是self.topo。

而不是写

actual.seg = "somevalue"
你可以写

self.topo.seg = "somevalue"

两条线完全相同。所以,当你以为你正在改变一些独立的财产actual时,你真的一直在改变自我。这意味着actual在函数的每个开始处都不相等,但始终具有上次运行c.search时的值。

我刚刚看到你在其他一些函数中也这样做了,这意味着你每次调用这样的函数时都会改变Comments实例c的状态。当然,这会导致每次测试的输出不同。

为了使这更容易理解,这里有一个列表的例子:

>>> a_list = ['one','two','three']
>>> b_list = a_list
>>> b_list.pop()
'three'
>>> a_list
['one', 'two']

如你所见:Allthoug我确实从b_list弹出了元素,它也从a_list中消失了。这是因为语句b_list = a_list实际上意味着b_list现在与a_list相同。

希望这有帮助。