Python:以优雅的方式(代码保存)改进一个函数以避免多个语句

标签 python performance optimization if-statement

我写这个函数是为了读取Las文件并保存一个 shapefile。该函数创建一个包含 8 个字段的 shapefile。我希望在函数中插入一个 parse 元素,以便选择我希望保存的字段 LAS2SHP(inFile,outFile=None,parse=None)。如果没有,则保存所有字段。如果解析是 parse="irn"字段 intensity、return_number 和 number_of_returns 被保存。按照传说

"i": p.intensity,
"r": p.return_number,
"n": p.number_of_returns,
"s": p.scan_direction,
"e": p.flightline_edge,
"c": p.classification,
"a": p.scan_angle, 

我写了一个解决方案 if....ifelse....else 真的很费代码(而且不优雅)。感谢所有保存代码的帮助和建议

提前致谢 詹尼

这里是python中的原始函数

import shapefile
from liblas import file as lasfile

def LAS2SHP(inFile,outFile=None):
    w = shapefile.Writer(shapefile.POINT)
    w.field('Z','C','10')
    w.field('Intensity','C','10')
    w.field('Return','C','10')
    w.field('NumberRet','C','10')
    w.field('ScanDir','C','10')
    w.field('FlightEdge','C','10')
    w.field('Class','C','10')
    w.field('ScanAngle','C','10')
    for p in lasfile.File(inFile,None,'r'):
        w.point(p.x,p.y)
        w.record(float(p.z),float(p.intensity),float(p.return_number),float(p.number_of_returns),float(p.scan_direction),float(p.flightline_edge),float(p.classification),float(p.scan_angle))
    if outFile == None:
        inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
        inFile_name = os.path.splitext(inFile_name_ext)[0]
        w.save("{0}\\{1}.shp".format(inFile_path,inFile_name))
    else:
        w.save(outFile)

最佳答案

也许可以尝试这样的事情:

    pdata = [p.z] + [getattr(p, pattr[key]) for key in parse]
    pdata = map(float, pdata)
    w.record(*pdata)
  • for key in parse 循环遍历 parse 中的字母。例如, 如果 parse = 'irn' 然后键循环遍历值 irn
  • pattr 是一个字典。 pattr[key] 是关联的名称 属性。例如,pattr['i']"intensity"
  • getattr(p, pattr[key])pattr[key]属性的值 在 p 中。例如,getattr(p, "intensity")p.intensity。当您知道作为字符串的属性名称(例如 pattr[key])时,这是获取属性值的方法。 w.record(*pdata) 中的 * 在将参数发送到 w.record 之前解压缩 pdata。例如,w.record(*[1,2,3]) 等同于 w.record(1,2,3)。这是一个方式sends an arbitrary number of arguments to a function .

例如,

import shapefile
from liblas import file as lasfile

pattr = {
    "i": 'intensity',
    "r": 'return_number',
    "n": 'number_of_returns',
    "s": 'scan_direction',
    "e": 'flightline_edge',
    "c": 'classification',
    "a": 'scan_angle',
    }

wattr = {
    "i": 'Intensity',
    "r": 'Return',
    "n": 'NumberRet',
    "s": 'ScanDir',
    "e": 'FlightEdge',
    "c": 'Class',
    "a": 'ScanAngle',
    }

def LAS2SHP(inFile, outFile=None, parse = 'irnseca'):
    w = shapefile.Writer(shapefile.POINT)
    w.field('Z','C','10')
    for key in parse:
        w.field(wattr[key],'C','10')
    for p in lasfile.File(inFile,None,'r'):
        w.point(p.x,p.y)
        pdata = [p.z] + [getattr(p, pattr[key]) for key in parse]
        pdata = map(float, pdata)
        w.record(*pdata)       
    if outFile == None:
        inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
        inFile_name = os.path.splitext(inFile_name_ext)[0]
        w.save("{0}\\{1}.shp".format(inFile_path,inFile_name))
    else:
        w.save(outFile)

关于Python:以优雅的方式(代码保存)改进一个函数以避免多个语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13073777/

相关文章:

python - 同时对三个变量数组进行for循环

algorithm - 使用 Haskell 进行 Prime 测试的性能

c - 证明 : Pythagorean Triple algorithm is faster by Euclid's Formula?

c# - 简化winforms代码跨线程调用

java - 在java中搜索对象列表并增加该对象的变量的最有效方法

带有 cx_freeze TCL_LIBRARY 错误的 Python pygame exe 构建

python - 在 sympy 中,使用规范交换关系简化表达式

python - 无法在google colab中上传本地文件

javascript - HTML 中的 <script> 标记位置是否会影响网页的性能?

javascript - 与普通 JS 相比,React 在移动设备上的表现如何?