How can you optimize the following code and write less?
class CleanItem():
def process_item(self, item, spider):
PSCV = str(item['page_source_canonical']).split("'")[1]
if PSCV != "":
if PSCV != item['page_source']:
item['page_source_canonical_is_itself'] = False
else:
item['page_source_canonical_is_itself'] = True
else:
item['page_source_canonical_is_itself'] = True
return item
First it checks if it is empty. If it is empty, it is true. If it is not empty it should be checked and if it is the same then again it is true. Otherwise it is false.
PSCV in ("", item['page_source'])gives you your desired boolean value directly.