RuntimeError:超出了最大递归深度(在我的Python代码中没有显式的递归调用)

时间:2013-09-09 21:56:11

标签: python recursion beautifulsoup

我正在尝试从已有的架子中读取键值对,以创建具有更新字段的新类对象,并将该类对象写入新架子。 我的类对象:SongDetails

这是失败的程序:

def updateShelfWithTabBody(shelfFileName, newShelfFileName):
     """this function updates songDetails with
     html body i.e. just the part that contains lyrics and
     chords in the tab """

 #read all songDetails
 shelf = shelve.open(shelfFileName)
 listOfKeys = shelf.keys()
 #create new songDetails object
 temporaryShelfObject = SongDetails.SongDetails()

 #iterate over list of keys
 for key in listOfKeys:
     #print "name:"+shelf[key].name
     #fill details from temporaryShelfObject
     temporaryShelfObject.name = shelf[key].name
     temporaryShelfObject.tabHtmlPageContent = shelf[key].tabHtmlPageContent
     #add new detail information
     htmlPageContent = shelf[key].tabHtmlPageContent
     temporaryShelfObject.htmlBodyContent = extractDataFromDocument.fetchTabBody(htmlPageContent)
     #write SongDetails back to shelf
     writeSongDetails.writeSongDetails(temporaryShelfObject, newShelfFileName)

上述代码中使用的函数的定义:

def fetchTabBody(page_contents):
    soup = BeautifulSoup(page_contents)
    HtmlBody = ""
    try:
            #The lyrics and chords of song are contained in div with id = "cont"
            #Note: This assumtption is specific to ultimate-guitar.com
            HtmlBody = soup.html.body.find("div",{"id":"cont"})
    except:
            print "Error: ",sys.exc_info()[0]
    return HtmlBody

def writeSongDetails(songDetails, shelfFileName):
    shelf = shelve.open(shelfFileName)
    songDetails.name = str(songDetails.name).strip(' ')
    shelf[songDetails.name] = songDetails
    shelf.close()

SongDetails课程:

class SongDetails:
    name = ""
    tabHtmlPageContent = ""
    genre = ""
    year = ""
    artist = ""
    chordsAndLyrics = ""
    htmlBodyContent = ""
    scale = ""
    chordsUsed = []

这是我得到的错误:

Traceback (most recent call last):
File "/l/nx/user/ndhande/Independent_Study_Project_Git/Crawler/updateSongDetailsShelfWithNewAttributes.py", line 69, in <module>
updateShelfWithTabBody(shelfFileName, newShelfFileName)
File "/l/nx/user/ndhande/Independent_Study_Project_Git/Crawler/updateSongDetailsShelfWithNewAttributes.py", line 38, in updateShelfWithTabBody
writeSongDetails.writeSongDetails(temporaryShelfObject, newShelfFileName)
File "/home/nx/user/ndhande/Independent_Study_Project_Git/Crawler/writeSongDetails.py", line 7, in writeSongDetails
shelf[songDetails.name] = songDetails
File "/usr/lib64/python2.6/shelve.py", line 132, in __setitem__
p.dump(value)
File "/usr/lib64/python2.6/copy_reg.py", line 71, in _reduce_ex
state = base(self)
File "/u/ndhande/.local/lib/python2.6/site-packages/BeautifulSoup.py", line 476, in __unicode__
return str(self).decode(DEFAULT_OUTPUT_ENCODING)
**RuntimeError: maximum recursion depth exceeded**

即使我的代码中没有明确的递归调用,我也找不到任何原因导致出现此错误。我在其他stackoverflow帖子中看到过这个错误,但在他们的情况下他们确实有递归调用。

1 个答案:

答案 0 :(得分:0)

str(self)拨打__str__或致电__unicode__来电str(self)