我们从Python开源项目中,提取了以下3个代码示例,用于说明如何使用yaml.representer()。
def change_style(style, representer): def new_representer(dumper, data): scalar = representer(dumper, data) scalar.style = style return scalar return new_representer # represent_str does handle some corner cases, so use that # instead of calling represent_scalar directly
def change_style(style, representer): def new_representer(dumper, data): scalar = representer(dumper, data) scalar.style = style return scalar return new_representer
def represent_data(self, data): """ This is very hackish. There is for sure a better way either by using the add_multi_representer or add_representer, the issue though lies in the fact that Scapy packets are metaclasses that leads to yaml.representer.get_classobj_bases to not be able to properly get the base of class of a Scapy packet. XXX fully debug this problem """ if isinstance(data, Packet): data = createPacketReport(data) return SafeRepresenter.represent_data(self, data)