Reduced extractData.generateIndex() code size

This commit is contained in:
Manish 2020-05-01 15:04:25 +10:00
parent eaaaae65b0
commit c10fedcd44

View File

@ -153,51 +153,37 @@ class extractData:
appends keys rather than replacing where multiple map to same appends keys rather than replacing where multiple map to same
""" """
# FIXME: Simplify logic: replace str, tuple, list etc. with 'string' & 'array' # FIXME: Simplify logic: replace str, tuple, list etc. with 'string' & 'array'
def generateIndex( def generateIndex(self)
self,
infoToIndexMap={
"name": ("name", "string"),
"summary": ("summary", "string"),
"description": ("description", "string"),
"tag": ("tags", "array"),
"tags": ("tags", "array"),
"categories": ("tags", "array"),
"category": ("tags", "array")
}
):
unexpectedInputError = (
"main.py generateIndex() : expect only str, list or tuple as "
"kwargs -> value[1] but found "
)
i2IMap = infoToIndexMap for activity in json.loads(self.infoJson):
indexDict = {
"name": "",
"description" : "",
"tags": ()
}
for obj in json.loads(self.infoJson): name = activity.get["name"]
indexDict = {} if name != None:
# Initialize keys with empty value indexDict["name"] == name
for k, v in i2IMap.items():
if v[1] == "string":
indexDict[v[0]] = ""
elif v[1] == "array":
indexDict[v[0]] = ()
else:
print(unexpectedInputError, v[1])
sys.exit(1)
for k, v in obj.items(): summary = activity.get["summary"]
if k in i2IMap: if summary != None:
# Add/Append to existing entries/keys indexDict["summary"] = summary
if i2IMap[k][1] == "string": description = activity.get["description"]
indexDict[i2IMap[k][0]] = ( if description != None:
indexDict[i2IMap[k][0]]+' '+v).strip(' ') indexDict["summary"] = (
elif i2IMap[k][1] == "array": indexDict["summary"] + ' ' + description)
if v.find(';') >= 0:
indexDict[i2IMap[k][0]] += tuple(v.split(';')) tags = []
else: tagsKeys = ["tag", "tags", "category", "categories"]
indexDict[i2IMap[k][0]] += tuple(v.split()) for key in tagsKeys:
else: tagsString = activity.get[tag]
print(unexpectedInputError, i2IMap[k][1]) if tagsString != None:
sys.exit(1) for tag in tagString:
tag = tag.casefold().capitalize()
if tag not in tags:
tags.append[tag]
infoDict["tags"] = tuple(tags)
self.indexDictList.append(indexDict) self.indexDictList.append(indexDict)
self.indexJs = ( self.indexJs = (