3 回答

TA贡献1818条经验 获得超8个赞
好的,您需要的是classobj新模块。这将允许您动态构造异常类(classobj将字符串作为类名称的参数)。
import new
myexc=new.classobj("ExcName",(Exception,),{})
i=myexc("This is the exc msg!")
raise i
这给你:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.ExcName: This is the exc msg!
请记住,您始终可以通过以下方式获取类名称:
self.__class__.__name__
因此,在进行了一些字符串修饰和连接之后,您应该能够获取适当的异常类名称,并使用该名称构造一个类对象,然后引发该异常。
PS-您也可以引发字符串,但已弃用。
raise(self.__class__.__name__+"Exception")

TA贡献2036条经验 获得超8个赞
为什么不使用SQLite?Python提供了良好的支持,您可以编写SQL查询来获取数据。这是sqlite3的Python文档
如果您不想使用SQLite,则可以执行一系列命令。
episodes = []
episodes.append({'season':1, 'episode': 2, 'name':'Something'})
episodes.append({'season':1, 'episode': 2, 'name':'Something', 'actors':['Billy Bob', 'Sean Penn']})
这样,您就可以将元数据添加到任何记录并非常轻松地搜索它
season_1 = [e for e in episodes if e['season'] == 1]
billy_bob = [e for e in episodes if 'actors' in e and 'Billy Bob' in e['actors']]
for episode in billy_bob:
print "Billy bob was in Season %s Episode %s" % (episode['season'], episode['episode'])

TA贡献1799条经验 获得超6个赞
Bartosz /澄清:“这很好,但是没有简单的方法可以检查x [3] [24]是否存在”
x['some show'][3][24]
将返回第3季的“某些节目”第24集。如果没有第3季,则我希望伪指令提高tvdb_seasonnotfound,如果“某些演出”不存在,则提高tvdb_shownotfound
当前系统有一系列的类,每个类都有__getitem__
-Show检查if self.seasons.has_key(requested_season_number)
,Season类检查if self.episodes.has_key(requested_episode_number)
等。
它可以工作,但是似乎有很多重复的代码(每个类基本上是相同的,但是会引发不同的错误)
添加回答
举报