用Python标准化数据结构
如果我通过许多不同的文件/脚本使用此结构 当需要更改时,我如何只在一个地方更改,而不必在每个文件中更改它用Python标准化数据结构,python,oop,data-structures,Python,Oop,Data Structures,如果我通过许多不同的文件/脚本使用此结构 当需要更改时,我如何只在一个地方更改,而不必在每个文件中更改它 u = contents incomingUrl = urlparse(u).query ok = parse_qsl(urlparse(u).query, keep_blank_values=True) def eventType(ok): try: return o[0][1]
u = contents
incomingUrl = urlparse(u).query
ok = parse_qsl(urlparse(u).query, keep_blank_values=True)
def eventType(ok):
try:
return o[0][1]
except (IndexError):
pass
def networkName(ok):
try:
return ok[1][1]
except (IndexError):
pass
def networkEmail(ok):
try:
return ok[2][1]
except (IndexError):
pass
def channelName(ok):
try:
return ok[3][1]
except (IndexError):
pass
def sceneType(ok):
try:
return ok[4][1]
except (IndexError):
pass
def sceneUrl(ok):
try:
return ok[5][1]
except (IndexError):
pass
def sceneTag1(ok):
try:
return ok[6][1]
except (IndexError):
pass
def sceneTag3(ok):
try:
return ok[7][1]
except (IndexError):
pass
def scenePrice(ok):
try:
return ok[8][1]
except (IndexError):
pass
def scenePriceDnom(ok):
try:
return ok[9][1]
except (IndexError):
pass
def networkAvatar(ok):
try:
return ok[10][1]
except (IndexError):
pass `
def sceneLat(ok):
try:
return ok[11][1]
except (IndexError):
pass
def sceneLong(ok):
try:
return ok[12][1]
except (IndexError):
pass
def timestamp(ok):
try:
return ok[13][1]
except (IndexError):
pass
我怎样才能客观化我猜的结构,改变它一个地方,并把它带到许多地方。
我一直在使用类和方法……这只是一个类,但我如何将它放入另一个文件并使用它??在这个问题上有点困惑
编辑:
来自源代码的示例数据
http://webservice.com/log?eventType=youtubeScene&networkName=loqootv&networkEmail=sirthomas@gmail.com&sendToChannel=loqootv&sceneType=youtubeScene&
sceneUrl=https://webservice.s3.amazonaws.com/tv_702ef50873f7270323b7285c28aae078837b7ecb.mp4&sceneTag1=youtube_gdata_player&
sceneTag3=&sceneTip=&sceneTipDnom=&networkAvatar=BTC×tamp=http://www.gravatar.com/avatar/59e9efab5fcf64b3d391641f5?&d=http%3A%2F%2Fwebservice.com%2Floqootv%2FLTVlogo.png&size=2048
为了消除冗余代码,我将编写一个decorator:
results = {} # This dict can map your API
def indexcatcher(func):
def inner(*args, **kwargs):
try:
results[func.__name__] = result = func(*args, **kwargs)
return result
except IndexError:
pass
return inner
然后针对每个功能:
@indexcatcher
def eventType(ok):
return ok[0][1]
@indexcatcher
def networkName(ok):
return ok[1][1]
@indexcatcher
def networkEmail(ok):
return ok[2][1]
@indexcatcher
def channelName(ok):
return ok[3][1]
@indexcatcher
def sceneType(ok):
return ok[4][1]
@indexcatcher
def sceneUrl(ok):
return ok[5][1]
@indexcatcher
def sceneTag1(ok):
return ok[6][1]
@indexcatcher
def sceneTag3(ok):
return ok[7][1]
@indexcatcher
def scenePrice(ok):
return ok[8][1]
@indexcatcher
def scenePriceDnom(ok):
return ok[9][1]
@indexcatcher
def networkAvatar(ok):
return ok[10][1]
@indexcatcher
def sceneLat(ok):
return ok[11][1]
@indexcatcher
def sceneLong(ok):
return ok[12][1]
@indexcatcher
def timestamp(ok):
return ok[13][1]
在定义每个函数并使用decorator包装它之后,decorator将记录特定项的结果并处理可能的索引异常:
def get_results(ok):
eventType(ok)
networkName(ok)
networkEmail(ok)
channelName(ok)
sceneType(ok)
sceneUrl(ok)
sceneTag1(ok)
sceneTag3(ok)
scenePrice(ok)
scenePriceDnom(ok)
networkAvatar(ok)
sceneLat(ok)
sceneLong(ok)
timestamp(ok)
return results
现在,您的results dict应该为您提供一个映射,它应该与您当前的API向后兼容
results = datamodule.get_results(ok)
现在从70行减少到55行(其中约1/4是空行)(如果有空行分隔它们怎么办,83行?),新函数的可读性更高,因此这也增加了一点维护收益
现在,如果你从每一个文件中导入这个文件,你必须保持一个一致的API,如果你做了很多,你需要真正考虑改变它的成本和收益。
可能的重复我使用的模块,一直以来,IM JUS坚持我将如何定位你拥有这个模式的特定结构,您要在每个独立的Python脚本中重写它吗?有一个原则是不要重复你自己(干)。如果您将此代码从一个脚本复制到另一个脚本,则违反了该原则。每个脚本都应该将其作为模块导入一次。然而,这里有很多重复性,但我不清楚如何立即将其考虑在内,同时使其独立于使用它的代码。Decorator可能会执行异常处理。因此,您只想知道在文件系统中的何处放置代码,以便将其导入任何python程序?感谢您这么做,您让我阅读并学习了decorators!谢谢你的时间和你的想法,是的,不断地改变api是我经常做的事情,这是一个与生产接近的问题,它必须停止。这肯定会有帮助,我担心您是在通过索引定义数据位置。我更喜欢语义结构,它可以让我通过关键字访问数据。然后你甚至不需要这些函数,你可以用一个函数来访问数据并提供你想要检索的信息。。。通过关键字访问数据。。数据以附加参数的形式或url形式提供。你能告诉我搜索的方向吗?听起来好像你对数据源没有控制权,但你可能会出于自己的目的将其打包成一个一致的API。我会看看是否能给你一个简洁的方法。我编辑了qs并添加了一个来自源代码的数据示例…如果有帮助的话