您的位置:首页 > 编程语言 > Python开发

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

2017-07-22 14:22 453 查看
针对博文:http://blog.csdn.net/henni_719/article/details/75423284

进行相关部分的代码修改,增加一些方法,来更好的定位,抓取的json数据在内存中存放的位置,来提高性能!

主要修改的模块有两个:

CreatePropertyDic.py:

增加的方法如下:

getAllsrcTitle(): 获取所有srcTitle列表


getAllfocusId():获取所有focusId列表

#coding=utf8
from BaseList import BaseDataList
import os,logging
from printData import printPropertyList
'''
Author:ewang
Data:2017/07/22
该模块的主要功能函数:
__propertyValueList(propetryName):创建不同属性值列表

getAllServiceId():获取所有的serviceId列表
getAllsrcPage():获取所有srcPage列表
getAllsrcPageId():获取所有srcPageId列表
getAllsrcModule():获取所有srcModule列表
getAllitem(): 获取所有item列表
getAllitemId():获取所有itemId列表
getAllsrcPosition():获取所有srcPosition列表
getAllmemberType():获取所有memberType列表
getAlleventGroup():获取所有eventGroup列表
getAllalbumType():获取所有albumType列表
getAllsrcSubModule():获取所有srcSubModule列表
getAlltype():获取所有type列表
getAllfunction():获取所有function列表
getAllshareType():获取所有shareType列表
getAllsrcTitle(): 获取所有srcTitle列表
getAllfocusId():获取所有focusId列表
'''

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 ExceptPropertyList(BaseDataList):

def __propertyValueList(self,propertyName):
try:
#调用readDataToList函数创建一个数据list
dataList=self.readDataToList()
#把数据list中的所有propertyName放入表propertyValueList中
#propertyValueList=[item[propertyName] for item in dataList if item[propertyName] ]
propertyValueList=[]
for item in dataList:
#print propertyName
if propertyName in item.keys():
if item[propertyName]:
propertyValueList.append(item[propertyName])
#对propertyValueList中的数据去重,分为两步:
#第一步把列表转换成集合:set(propertyValueList)
#第二步:把集合转换为list:list(set(propertyValueList))
#集合和list的区别:集合中的数据是唯一性,不存在相同部分
propertyValueList=list(set(propertyValueList))
#返回serList
return propertyValueList
except Exception,e:
logging.error("Create Property  %s  Value  List Error: %s"  %(propertyName,e))
#print "Create Property  %s  Value  List Error: %s"  %(propertyName,e)

#获取所有serviceId列表
def getAllServiceId(self,):

return self.__propertyValueList("serviceId")

#获取所有srcPage列表
def getAllsrcPage(self):
return self.__propertyValueList("srcPage")

#获取所有srcPageId列表
def getAllsrcPageId(self):
return self.__propertyValueList("srcPageId")

#获取所有srcModule列表
def getAllsrcModule(self):
return self.__propertyValueList("srcModule")

#获取所有itemId列表
def getAllitem(self):
return self.__propertyValueList("item")

#获取所有itemId列表
def getAllitemId(self):
return self.__propertyValueList("itemId")

#获取所有srcPosition列表
def getAllsrcPosition(self):
return self.__propertyValueList("srcPosition")

#获取所有srcTitle列表
def getAllsrcTitle(self):
return self.__propertyValueList("srcTitle")

#获取所有memberType列表
def getAllmemberType(self):
return self.__propertyValueList("memberType")

#获取所有eventGroup列表
def getAlleventGroup(self):
return self.__propertyValueList("eventGroup")

#获取所有albumType列表
def getAllalbumType(self):
return self.__propertyValueList("albumType")

#获取所有srcSubModule列表
def getAllsrcSubModule(self):
return self.__propertyValueList("srcSubModule")

#获取所有type列表
def getAlltype(self):
return self.__propertyValueList("type")

#获取所有function列表
def getAllfunction(self):
return self.__propertyValueList("function")

#获取所有shareType列表
def getAllshareType(self):
return self.__propertyValueList("shareType")

#获取所有focusId列表
def getAllfocusId(self):
return self.__propertyValueList("focusId")

def test():
baseList=ExceptPropertyList()
itemlist=baseList.getAllsrcTitle()
itemIdList=baseList.getAllitemId()
printPropertyList(itemlist)
print
printPropertyList(itemIdList)
print
focusIdList=baseList.getAllfocusId()
printPropertyList(focusIdList)

if __name__=="__main__":
test()


CreatePropertyList.py

增加的方法如下:
get_srcTitle_DataDic():创建以srcTitle为键的数据字典

get_focusId_DataDic():创建以focusId为键的数据字典


优化了功能:getProDicListLen()
#coding=utf8
from PropertyList import  ExceptPropertyList
import os,logging
from printData import printDicData
'''
Author:ewang
Data: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数据是一个字典类型 ,返回一个字典类型

'''

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数据是一个字典类型
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)

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

print
printDicData(itemDic)

if __name__=="__main__":
test()


以上两个模块使,进行数据定位的核心功能模块,通过getProDicListLen()能获取实际获取的json数据键值对在读取的数据字典中分布的位置以及个数!通过该功能能获取,键值对所在的最小字典列表中!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: