对python脚本readDataToDic脚本进行模块化拆分多个类(V1.2)

来源:互联网 发布:ubuntu软件中心加载慢 编辑:程序博客网 时间:2024/04/29 04:41

参考博文:http://blog.csdn.net/henni_719/article/details/75732138


CreatePropertyList.py

增加的方法如下:
__DicList(methodName,propsDic):输入json数据,该json数据是一个字典类型;输入方法名:min,max;返回以字典类型为数据项的列表

getMinDicList(propsDic):获取最小字典列表
getMaxDicList(propsDic):获取最大字典列表 

#coding=utf8from PropertyList import  ExceptPropertyList import os,loggingfrom printData import printDicData,printStanderDic'''Author:ewangData:2017/07/22该模块的主要功能函数:__createDataDic(properyName,propetyList):创建一个数据字典表 get_ServiceId_DataDic():创建一个数据字典表,以serviceId为key,相同的数据项列表为value get_srcPage_DataDic():创建一个数据字典表,以srcPage为key,相同的数据项列表为value get_srcPageId_DataDic():创建一个数据字典表,以srcPageId为key,相同的数据项列表为value get_srcModule_DataDic():创建一个数据字典表,以srcModule为key,相同的数据项列表为value get_item_DataDic():创建一个数据字典表,以item为key,相同的数据项列表为value get_itemId_DataDic():创建一个数据字典表,以itemId为key,相同的数据项列表为value get_srcPosition_DataDic():创建一个数据字典表,以srcPosition为key,相同的数据项列表为value get_memberType_DataDic():创建一个数据字典表,以memberType为key,相同的数据项列表为value get_eventGroup_DataDic():创建一个数据字典表,以eventGroup为key,相同的数据项列表为value get_albumType_DataDic():创建一个数据字典表,以albumType为key,相同的数据项列表为value get_srcSubModule_DataDic():创建一个数据字典表,以srcSubModule为key,相同的数据项列表为value get_type_DataDic():创建一个数据字典表,以type为key,相同的数据项列表为value get_function_DataDic():创建一个数据字典表,以function为key,相同的数据项列表为value get_shareType_DataDic():创建一个数据字典表,以shareType为key,相同的数据项列表为value    get_srcTitle_DataDic():创建以srcTitle为键的数据字典       get_focusId_DataDic():创建以focusId为键的数据字典      getProDicListLen(propsDic): 输入propsDic数据,该propsDic数据是一个字典类型 ,返回一个字典类型 __DicList(methodName,propsDic):输入json数据,该json数据是一个字典类型输入方法名:min,max返回以字典类型为数据项的列表getMinDicList(propsDic):获取最小字典列表getMaxDicList(propsDic):获取最大字典列表 '''PATH=lambda p:os.path.abspath(os.path.join(    os.path.dirname(__file__), p))logging.basicConfig(level=logging.DEBUG,                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',                datefmt='%a, %d %b %Y %H:%M:%S',                filename=PATH('../Log/readDate.log'),                filemode='w')class ExceptPropertyDic(ExceptPropertyList):        #创建数据字典    def __createDataDic(self,properyName,propetyList):        try:            #定义个数据字典变量            dataDic={}                #调用函数readDataToList创建一个dataList表              dataList=self.readDataToList()               #判断列表中是否有元素,如果有执行if语句            if len(propetyList)>0 and len(dataList)>0:                #对propetyList进行循环,以property作为key                for Property in propetyList:                    #创建一个list用来存放Property相同的数据项                    samePropertyJosnList=[]                    #对数据列表执行循环                    for item in dataList:                        #获取字典中键为Property值,放入变量中                         if properyName in item.keys():                                             itemProperyName=item[properyName]                            #如果值不为空,执行if语句                            if itemProperyName:                                #判断Property与数据项中itemProperyName的值是否相等                                #如果相等执行if语句块                                if Property==itemProperyName:                                     #把数据项加入samePropertyJosnList列表中                                    samePropertyJosnList.append(item)                                                                                          else:                                logging.debug("Property is null")                    #给字典赋值,以Property作为key,                    #Property相同的数据项列表作为值                    dataDic[Property]=samePropertyJosnList                                 else:                logging.debug("Property List  or dataList is null")            #返回字典类型的数据            return dataDic        except Exception,e:            logging.error("Create  %s Data Dictionary Error:%s"  %(properyName,e))            #创建以serviceId为键的数据字典    def get_serviceId_DataDic(self):        try:            servicIdList=self.getAllServiceId()            return self.__createDataDic("serviceId", servicIdList)        except Exception,e:            logging.debug("create serviceId data dictionary:"+e)         #创建以srcPage为键的数据字典                    def get_srcPage_DataDic(self):        try:            srcPageList=self.getAllsrcPage()            return self.__createDataDic("srcPage", srcPageList)        except Exception,e:            print "create srcPage data dictionary:",e                #创建以srcPageId为键的数据字典            def get_srcPageId_DataDic(self):        try:            srcPageIdList=self.getAllsrcPageId()            return self.__createDataDic("srcPageId", srcPageIdList)        except Exception,e:            logging.debug("create srcPageId data dictionary:"+e)             #创建以srcModule为键的数据字典            def get_srcModule_DataDic(self):        try:            srcModuleList=self.getAllsrcModule()            return self.__createDataDic("srcModule", srcModuleList)        except Exception,e:            logging.debug("create srcModule data dictionary:"+e)                     #创建以item为键的数据字典    def get_item_DataDic(self):        try:            itemList=self.getAllitem()            return self.__createDataDic("item", itemList)        except Exception,e:            logging.debug("create item data dictionary:"+e)           #创建以itemId为键的数据字典        def get_itemId_DataDic(self):        try:            itemIdList=self.getAllitemId()            return self.__createDataDic("itemId", itemIdList)        except Exception,e:            logging.debug("create itemId data dictionary:"+e)             #创建以srcPosition为键的数据字典       def get_srcPosition_DataDic(self):        try:            srcPositionList=self.getAllsrcPosition()            if len(srcPositionList)>0:                return self.__createDataDic("srcPosition", srcPositionList)        except Exception,e:            logging.debug("create srcPosition data dictionary:"+e)                #创建以memberType为键的数据字典          def get_memberType_DataDic(self):        try:            memberTypeList=self.getAllmemberType()            return self.__createDataDic("memberType", memberTypeList)        except Exception,e:            logging.debug("create memberType data dictionary:"+e)           #创建以eventGroup为键的数据字典           def get_eventGroup_DataDic(self):        try:            eventGroupList=self.getAlleventGroup()            return self.__createDataDic("eventGroup", eventGroupList)        except Exception,e:            logging.debug("create eventGroup data dictionary:"+e)          #创建以srcTitle为键的数据字典           def get_srcTitle_DataDic(self):        try:            srcTitleList=self.getAllsrcTitle()            return self.__createDataDic("srcTitle", srcTitleList)        except Exception,e:            logging.debug("create srcTitle data dictionary:"+e)                   #创建以albumType为键的数据字典          def get_albumType_DataDic(self):        try:            albumTypeList=self.getAllalbumType()            return self.__createDataDic("albumType", albumTypeList)        except Exception,e:            logging.debug("create albumType data dictionary:"+e)          #创建以srcSubModule为键的数据字典           def get_srcSubModule_DataDic(self):        try:            srcSubModuleList=self.getAllsrcSubModule()            return self.__createDataDic("srcSubModule", srcSubModuleList)        except Exception,e:            logging.debug("create srcSubModule data dictionary:"+e)            #创建以type为键的数据字典           def get_type_DataDic(self):        try:            typeList=self.getAlltype()            return self.__createDataDic("type", typeList)        except Exception,e:            logging.debug("create type data dictionary:"+e)               #创建以function为键的数据字典           def get_function_DataDic(self):        try:            functionList=self.getAllfunction()            return self.__createDataDic("function", functionList)        except Exception,e:            logging.debug("create function data dictionary:"+e)               #创建以shareType为键的数据字典          def get_shareType_DataDic(self):        try:            shareTypeList=self.getAllshareType()            return self.__createDataDic("shareType", shareTypeList)        except Exception,e:            logging.debug("create shareType data dictionary:"+e)                  #创建以focusId为键的数据字典          def get_focusId_DataDic(self):        try:            focusIdList=self.getAllfocusId()            return self.__createDataDic("focusId", focusIdList)        except Exception,e:            logging.debug("create focusId data dictionary:"+e)                                                def getDicDataValue(self,PropertyValue,dataDic):        try:            '''            例如:item="album"                    dataDic=get_item_DataDic()            使用方法是:getDicDataValue("album",dataDic)            功能:获取字典中对应键获取的值列表            '''            PropertyValue=PropertyValue.decode("utf8")            if PropertyValue in dataDic.keys():                return dataDic[PropertyValue]        except Exception,e:            logging.error("Get %s  Dic Data Error:%s"(PropertyValue,e))          #输入json数据,该json数据是一个字典类型    #返回一个字典类型,以属性和值的字符串为key,以列表长度与列表作为value         def getProDicListLen(self,propsDic):        try:            #用来存放属性值,与列表长度            propertyLenDic={}            if len(propsDic)>0:                for PropertyName,PropertyValue in propsDic.items():                    #如果给定的字典的值是字符串类型                    #则执行if语句                    if isinstance(PropertyValue, str):                          #通过属性来创建函数字符串                                         creatDataDicMethod='self.get_%s_DataDic()' %PropertyName                          #判定属性值是否包含在所创建的方法字符串中                                       if creatDataDicMethod.find(PropertyName):                            #通过调用eval方法,把方法字符串转换成表达式                            #返回由方法创建的属性值数据字典                            DicName=eval(creatDataDicMethod)                            #判断是否为空                            if len(DicName)>0 and PropertyValue:                                #调用方法getDicDataValue,返回键值列表                                valueList=self.getDicDataValue(PropertyValue, DicName)                                 #判断列表是否为空                                if valueList:                                     #创建键字符串                                    keyValue="%s:%s" %(PropertyName,PropertyValue)                                     #把计算得来的键和键值长度放入字典,值列表                                                                                               propertyLenDic[keyValue]= (len(valueList),valueList)                                                                     else:                                    logging.error("The  length of  ValueList  little 0")                                                                   else:                                logging.error("The  length of  DicName  little 0  and PropertyValue is Null")                                    else:                logging.error("The  length of  propsDic  little 0")                            return propertyLenDic        except Exception,e:            logging.error("Get Property Dic  List Length Error:%s"%e)                    #输入json数据,该json数据是一个字典类型    #输入方法名:min,max    #返回以字典类型为数据项的列表    def __DicList(self,methodName,propsDic):        try:              #调用getProDicListLen方法             propertyLenDic=self. getProDicListLen(propsDic)             try:                #判断字典是否为空                if len(propertyLenDic)>0:                    try:                        #获取数值列表                        numberList=[itemVal[0] for itemVal in propertyLenDic.values() if itemVal ]                        #创建调用方法字符串                        callMethod="%s(%r)" %(methodName,numberList)                        #把字符串转换成表达式                        Count=eval(callMethod)                    except Exception,e:                        logging.error("Get callMethod error!")                      #循环哈希表值                    #获取长度与列表元组                    try:                        for itemValue in propertyLenDic.values():                            #判断值的长度是否2                            if len(itemValue)==2:                                #通过长度与最小值比对                                if itemValue[0]==Count:                                    #判断字典列表是否为空                                    if len(itemValue[-1])>0:                                        #把最小列表保存变量MinDicList中                                        DicList=itemValue[-1]                                        #阻断循环                                        break                         except Exception,e:                        logging.error("Create dictionary List errror!")                            except Exception,e:                logging.error("The data dictionary is null!")                            #返回字典列表            return DicList        except Exception,e:            logging.debug("Get %s   Dic List Error:%s"  %(methodName,e))           #获取小字典列表    def getMinDicList(self,propsDic):        return self.__DicList("min",propsDic)    #获取最达字典列表    def getMaxDicList(self,propsDic):        return self.__DicList("max",propsDic)        '''                            #输入json数据,该json数据是一个字典类型    #返回以字典类型为数据项的最小列表    def getMinDicList(self,propsDic):        try:              #调用getProDicListLen方法             propertyLenDic=self. getProDicListLen(propsDic)             #判断字典是否为空            if len(propertyLenDic)>0:                #获取最小数值                minCount=min([itemVal[0] for itemVal in propertyLenDic.values() if itemVal ])                #循环哈希表值                #获取长度与列表元组                for itemValue in propertyLenDic.values():                    #判断值的长度是否2                    if len(itemValue)==2:                        #通过长度与最小值比对                        if itemValue[0]==minCount:                            #判断字典列表是否为空                            if len(itemValue[-1])>0:                                #把最小列表保存变量MinDicList中                                MinDicList=itemValue[-1]                                #阻断循环                                break                                        #返回最小字典列表            return MinDicList        except Exception,e:            logging.debug("Get min   Dic List Error:"+e)      '''        def test():    propsDic={    'itemId' : 96975,    'item' : 'track' ,    'serviceId' : 'pageview' ,    'srcSubModule' : '声音条' ,    'srcPosition' : 'XX ',    'srcPage' : '发现_推荐' ,    'srcPageId' : 'XX' ,    'srcModule' : '焦点图',    'srcTitle' : '焦点图标题' ,    'focusId' : '焦点图ID' ,        }       dataDic=ExceptPropertyDic()    #itemDic=dataDic.get_itemId_DataDic()    #valueList=dataDic.getDicDataValue('XX' , itemDic)    prolenDic=dataDic.getProDicListLen(propsDic)    for key,value in prolenDic.items():        print key.decode("utf8"),":",value            MinDicList=dataDic.getMinDicList(propsDic)    printStanderDic(MinDicList[0])    #printDicData(itemDic)    MinDicList=dataDic.getMaxDicList(propsDic)    for dic in MinDicList:          printStanderDic(dic)if __name__=="__main__":    test()

printStanderDic折个函数的代码如下:

#打印字典信息,#输入参数是字典类型的对象      def printStanderDic(propertydic):    try:                    #每一数据项是哈希表            print "\t\t{"            #输出哈希表中的数据            for key,val in propertydic.items():                 print "\t\t\t",key,":",val,","            print "\t\t}"    except Exception,e:        logging.error("OutPut Property Dictionary  Error:%s" %e)

运行结果如下:




原创粉丝点击