关于性能:Python:以优雅的方式进行改进(节省代码),以避免出现多个语句

Python: improve in elegant way (code saving) a function in order to avoid several Statements

我编写了此函数来读取Las文件并保存shapefile。 该函数创建一个带有8个字段的shapefile。 我希望在函数中插入一个parse元素,以便选择要保存的字段LAS2SHP(inFile,outFile = None,parse = None)。 如果为None,则保存所有字段。 如果解析是
parse =" irn"会保存强度,return_number和number_of_returns个字段。 跟随传说

1
2
3
4
5
6
7
"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,

我写了一个解决方案,如果.... ifelse ....其他确实消耗代码(而不是优雅)。 感谢您提供的所有帮助和建议,以节省代码

提前致谢
詹妮

这是python中的原始功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)


也许尝试这样的事情:

1
2
3
    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)。 这是向函数发送任意数量的参数的方式。

例如,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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)