python - 标准化 Python 中的数据结构

标签 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]                                                         
        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&timestamp=http://www.gravatar.com/avatar/59e9efab5fcf64b3d391641f5?&d=http%3A%2F%2Fwebservice.com%2Floqootv%2FLTVlogo.png&size=2048 

最佳答案

为了排除冗余代码,我会编写一个装饰器:

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]

定义每个函数并用装饰器包装它后,记录特定项目的结果并处理可能的索引异常:

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

现在您的结果字典应该为您提供一个映射,并且这应该与您当前的 API 向后兼容。

results = datamodule.get_results(ok)

现在,你的行数从 70 行减少到了 55 行(其中大约 1/4 是空行)(如果有空行分隔它们,那就是 83 行?),而且新函数的可读性要强得多,所以这也带来了一点维护 yield 。

现在,如果您从每个文件中导入此内容,则必须维护一致的 API,如果您经常这样做,则需要真正考虑更改它的成本和 yield 。

关于python - 标准化 Python 中的数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21371843/

相关文章:

python - 了解 tf.scatter_nd_update : How to update column values?

python - 朴素贝叶斯分类问题 - ValueError : domain math error

language-agnostic - 为什么高级语言中缺少这么多数据结构?

Java:返回实际对象与返回引用

python - Django REST : Serializer lookup by UUID

python - PIL - 需要抖动,但限制调色板会导致问题

JavaScript 变量作用域

arrays - 插入多个元素后恢复堆属性

C++ 构建错误 - '{' 标记之前的预期类名|

C++:让多个类成为函数的 friend ?