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={ for activity in json.loads(self.infoJson):
"name": ("name", "string"), indexDict = {
"summary": ("summary", "string"), "name": "",
"description": ("description", "string"), "description" : "",
"tag": ("tags", "array"), "tags": ()
"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 name = activity.get["name"]
if name != None:
indexDict["name"] == name
for obj in json.loads(self.infoJson): summary = activity.get["summary"]
indexDict = {} if summary != None:
# Initialize keys with empty value indexDict["summary"] = summary
for k, v in i2IMap.items(): description = activity.get["description"]
if v[1] == "string": if description != None:
indexDict[v[0]] = "" indexDict["summary"] = (
elif v[1] == "array": indexDict["summary"] + ' ' + description)
indexDict[v[0]] = ()
else:
print(unexpectedInputError, v[1])
sys.exit(1)
for k, v in obj.items(): tags = []
if k in i2IMap: tagsKeys = ["tag", "tags", "category", "categories"]
# Add/Append to existing entries/keys for key in tagsKeys:
if i2IMap[k][1] == "string": tagsString = activity.get[tag]
indexDict[i2IMap[k][0]] = ( if tagsString != None:
indexDict[i2IMap[k][0]]+' '+v).strip(' ') for tag in tagString:
elif i2IMap[k][1] == "array": tag = tag.casefold().capitalize()
if v.find(';') >= 0: if tag not in tags:
indexDict[i2IMap[k][0]] += tuple(v.split(';')) tags.append[tag]
else: infoDict["tags"] = tuple(tags)
indexDict[i2IMap[k][0]] += tuple(v.split())
else:
print(unexpectedInputError, i2IMap[k][1])
sys.exit(1)
self.indexDictList.append(indexDict) self.indexDictList.append(indexDict)
self.indexJs = ( self.indexJs = (