from lxml import etree as E class DenyExternal(E.Resolver): def resolve(self, url, public_id, context): raise ValueError("External resources are disabled") def parse(text): if "\n Marigold12flower\n Fenugreek0herb\n Basil7herb\n" root = parse(source) checks = { "root": root.tag == "exchange", "three records": len(root.findall("packet")) == 3, "total stock": root.xpath("sum(packet/quantity)") == 19, } for name, passed in checks.items(): print(name + ": " + ("pass" if passed else "fail"))