Python: improve in elegant way (code saving) a function in order to avoid several statements I wrote this function to read Las file and save a shapefile. The function creates a shapefile with 8 fields. What I wish insert a parse element in the function in order to select the fields I wish to save LAS2SHP(inFile, outFile=None, parse=None). If parse=None all fields are saved. If  parse="irn" the fields intensity, return_number, and number_of_returns are saved, following the legend:

"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, 


I wrote a solution if....ifelse....else really code consuming (and not elegant). Thanks for all helps and suggestions for saving code.

Here is the original function in 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)
A possible dictionary based solution:

import os.path

import shapefile
from liblas import file as lasfile


# map fields representing characters to field name and attribute
FIELDS = {
    'i': ('Intensity', 'intensity'),
    'r': ('Return', 'return_number'),
    'n': ('NumberRet', 'number_of_returns'),
    's': ('ScanDir', 'scan_direction'),
    'e': ('FlightEdge', 'flightline_edge'),
    'c': ('Class', 'classification'),
    'a': ('ScanAngle', 'scan_angle'),
}

# assuming that field order is important define it explicitly, otherwise it
# could simply be DEFAULT_PARSE = FIELDS.keys()
DEFAULT_PARSE = 'irnseca'


def enhanced_LAS2SHP(inFile, outFile=None, parse=DEFAULT_PARSE):
    w = shapefile.Writer(shapefile.POINT)
    # add 'Z' field not present in fields map
    w.field('Z', 'C', '10')
    # initialise list of desired record attributes
    attributes = []
    # add mapped 'shapefile' fields and desired attributes to list
    for f in parse:
        field, attr = FIELDS[f]
        w.field(field_name, 'C', '10')
        attributes.append(attr)
    # create record from attributes in list of desired attributes
    for p in lasfile.File(inFile, None, 'r'):
        w.point(p.x, p.y)
        record_args = [float(p.z)]
        record_args += (float(getattr(p, attr)) for attr in attributes)
        w.record(*record_args)
    # if not output filename was supplied derive one from input filename
    if outFile is None:
        inFile_path, inFile_name_ext = os.path.split(os.path.abspath(inFile))
        inFile_name, _ = os.path.splitext(inFile_name_ext)
        outFile = os.path.join(inFile_path, '{}.shp'.format(inFile_name))
    # save records to 'shapefile'
    w.save(outFile)