Simple Package Tool 学习

Simple Package Tool 学习

 

1.getattr内置函数

getattr(object, name[, default]) 

python Packages.py install -p "$host_ip" -s "$build_series" -v "$build_version" -n "$ne_type" -i "$ini_path" -d "$dir_name" -b "$sw_name" -m "$active_omu" -g "$digi_ip"

Packages.py

 1 ACTION_LIST = ('config', 'install', 'patch', 'burn', 'sync')
 2 
 3 class Packages(object):
 4 
 5   def __init__(self):
 6 
 7     pass;
 8 
 9   def __getattr__(self, arg):
10 
11     if arg in ACTION_LIST:
12 
13           return getattr(self, 'handle_%s_cmd' % arg)
14 
15        raise AttributeError
16 
17  
18 
19 #sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始
20 
21 def main(argv=None):
22 
23   args = argv[1:]
24 
25   action = args[0]
26 
27   action_args = args[1:]
28 
29   actor = getattr(Packages(), action)(action_args) #getattr()为获取某个类里的某个属性函数等,后面再接括号即为运行该函数
32 
33  
34 
35 if __name__ == "__main__":
36 
37   sys.exit(main())

 2.optparse模块

from optparse import OptionParser  

parser = OptionParser()  

parser.add_option("-f","--file",dest="filename",help="write report to FILE",metavar="FILE")  

parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True,help="don't print status messages to stdout")  

(options, args) = parser.parse_args() 

3. 

    SW-PACKAGE     STATUS   DIRECTORY           ENVIRONMENT          DEF  ACT
                            PACKAGE-ID (REP-ID) DELIVERY
                                                CD-ID

    CRND8155       BU       CRND81550           CB 15.5-0             -    Y 
                            CB 15.5-0           CNR51840 2.7-15   
                                                          

    CR2CZ154       NW       CR2CZ15444          CB 15.4-44            Y    Y 
                            CB 15.4-0           CNR51840 2.7-15   
                                                          

    CRE8H154       UT       CRE8H1540           CB 15.4-0             -    Y 
                            CB 15.4-0           CNR51840 2.7-15   
                                                          

    CRPEY154       UT       CRPEY15443          CB 15.4-43            -    Y 
                            CB 15.4-0           CNR51840 2.7-15   
                                                          

如何将每一行数据和对应的title成键值对输出?

上述数据的特点:每一列的起始位置都是对齐的,因此可以利用这一点,每一列的距离 = 下一列的起始位置 - 该列的起始位置,这样就能将每一列数据获取出来和title对应.

例:我们要获取CRND8155这个值(将该行先定义为line4,第一行定义为line1):line4[line1.index('SW-PACKAGE'):line1.index('STATUS')].strip()

原文地址:https://www.cnblogs.com/jp927/p/4535410.html