我目前正在构建一个Python IRC机器人。我找到了一些从bash.org网站在频道上发布了一个随机的引语。我尝试过修改它以从另一个源代码中提取—修改它正在查看的类—但它最终会出现“调用Python对象时超出最大递归深度”的错误。我正在拔头发。任何建议都会有帮助。你知道吗
from plugins.util import command, get_url
from bs4 import BeautifulSoup
@command("q", "qdb")
def quote(m):
"""Return a quote from sysadmin quotes."""
#- Post a quote from quote.org. If given a quote number, it will try to post it. Otherwise it
#- will post a random quote. If the quote is too long, it will direct the user to the URL.
#- Please note that there is no filtering here, so some of the quotes may be inappropriate.
if len(m.line) > 1 and m.line[1].isnumeric():
quote_specific(m, m.line[1])
else:
quote_rand(m)
def quote_rand(m):
"""Get a random quote from sysadmin quotes"""
resp = get_url(m, "http://quotes.sysadmin.technology/?random1")
soup = BeautifulSoup(resp)
raw = soup.find(class_="quote_quote")
if raw:
meta = soup.find(class_="quote_option-bar")
while True:
if not raw:
quote_rand(m)
return
lines = raw.get_text().splitlines()
if len(lines) <= 5:
break
raw = raw.find_next(class_="quote_quote")
meta = soup.find_next(class_="quote_option-bar")
format_quote(m, lines, meta)
else:
m.bot.private_message(m.location, "Could not find quote.")
def quote_specific(m, number):
"""Get a specific quote from sysadmin quotes."""
resp = get_url(m, "http://quotes.sysadmin.technology/?" + number)
soup = BeautifulSoup(resp)
raw = soup.find(class_="quote_qoute")
if raw:
meta = soup.find(class_="quote_option-bar")
lines = raw.get_text().splitlines()
if len(lines) > 5 and not m.is_pm:
m.bot.private_message(m.location, "This quote is too long to post publicly, "
"but you can view it at http://quotes.sysadmin.technology/?"
"{}.".format(number))
else:
format_quote(m, lines, meta, number)
else:
m.bot.private_message(m.location, "Could not find quote.")
def format_quote(m, raw, meta, number=None):
"""Format the quote with some metadata."""
try:
score = meta.font.string
score_str = "\x0304{}\x03".format(score) if "-" in score else "\x0303{}\x03".format(score)
url = "http://quotes.sysadmin.technology/?" + (number if number else meta.b.string.strip("#"))
meta_str = "--- {} ({}) {} ".format(meta.b.string, score_str, url)
except AttributeError as e:
if number:
m.bot.private_message(m.location, "Could not find quote.")
else:
quote_rand(m)
else:
m.bot.private_message(m.location, meta_str.ljust(83, '-'))
for line in raw:
m.bot.private_message(m.location, line)
m.bot.private_message(m.location, "-" * 80)
Python具有有限的递归深度,因此对于可能达到此限制的问题,您最好提出算法的迭代版本。如果您懒得这样做,并且确信您的递归总是比Python设置的默认限制稍微深一点,那么只需更改这个限制就可以了。怎么会这样?前往What is the maximum recursion depth in Python, and how to increase it?
相关问题 更多 >
编程相关推荐