Best Python code snippet using avocado_python
serializers.py
Source:serializers.py  
...49def _raise_serialization_error(text):  # pragma: no cover50    raise TypeError(51        "cannot serialize {!r} (type {})".format(text, type(text).__name__)52        )53def _escape_cdata(text):54    # escape character data55    try:56        # it's worth avoiding do-nothing calls for strings that are57        # shorter than 500 character, or so.  assume that's, by far,58        # the most common case in most applications.59        if "&" in text:60            # Only replace & when not part of an entity61            text = RE_AMP.sub('&', text)62        if "<" in text:63            text = text.replace("<", "<")64        if ">" in text:65            text = text.replace(">", ">")66        return text67    except (TypeError, AttributeError):  # pragma: no cover68        _raise_serialization_error(text)69def _escape_attrib(text):70    # escape attribute value71    try:72        if "&" in text:73            # Only replace & when not part of an entity74            text = RE_AMP.sub('&', text)75        if "<" in text:76            text = text.replace("<", "<")77        if ">" in text:78            text = text.replace(">", ">")79        if "\"" in text:80            text = text.replace("\"", """)81        if "\n" in text:82            text = text.replace("\n", "
")83        return text84    except (TypeError, AttributeError):  # pragma: no cover85        _raise_serialization_error(text)86def _escape_attrib_html(text):87    # escape attribute value88    try:89        if "&" in text:90            # Only replace & when not part of an entity91            text = RE_AMP.sub('&', text)92        if "<" in text:93            text = text.replace("<", "<")94        if ">" in text:95            text = text.replace(">", ">")96        if "\"" in text:97            text = text.replace("\"", """)98        return text99    except (TypeError, AttributeError):  # pragma: no cover100        _raise_serialization_error(text)101def _serialize_html(write, elem, format):102    tag = elem.tag103    text = elem.text104    if tag is Comment:105        write("<!--%s-->" % _escape_cdata(text))106    elif tag is ProcessingInstruction:107        write("<?%s?>" % _escape_cdata(text))108    elif tag is None:109        if text:110            write(_escape_cdata(text))111        for e in elem:112            _serialize_html(write, e, format)113    else:114        namespace_uri = None115        if isinstance(tag, QName):116            # QNAME objects store their data as a string: `{uri}tag`117            if tag.text[:1] == "{":118                namespace_uri, tag = tag.text[1:].split("}", 1)119            else:120                raise ValueError('QName objects must define a tag.')121        write("<" + tag)122        items = elem.items()123        if items:124            items = sorted(items)  # lexical order125            for k, v in items:126                if isinstance(k, QName):127                    # Assume a text only QName128                    k = k.text129                if isinstance(v, QName):130                    # Assume a text only QName131                    v = v.text132                else:133                    v = _escape_attrib_html(v)134                if k == v and format == 'html':135                    # handle boolean attributes136                    write(" %s" % v)137                else:138                    write(' {}="{}"'.format(k, v))139        if namespace_uri:140            write(' xmlns="%s"' % (_escape_attrib(namespace_uri)))141        if format == "xhtml" and tag.lower() in HTML_EMPTY:142            write(" />")143        else:144            write(">")145            if text:146                if tag.lower() in ["script", "style"]:147                    write(text)148                else:149                    write(_escape_cdata(text))150            for e in elem:151                _serialize_html(write, e, format)152            if tag.lower() not in HTML_EMPTY:153                write("</" + tag + ">")154    if elem.tail:155        write(_escape_cdata(elem.tail))156def _write_html(root, format="html"):157    assert root is not None158    data = []159    write = data.append160    _serialize_html(write, root, format)161    return "".join(data)162# --------------------------------------------------------------------163# public functions164def to_html_string(element):165    return _write_html(ElementTree(element).getroot(), format="html")166def to_xhtml_string(element):...xml_with_escaping.py
Source:xml_with_escaping.py  
...3# https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references#Predefined_entities_in_XML4import xml.etree.ElementTree as etree5from xml.etree.ElementTree import _escape_cdata, _raise_serialization_error6from mock import patch7def _escape_cdata(text):8    # escape character data9    try:10        # it's worth avoiding do-nothing calls for strings that are11        # shorter than 500 character, or so.  assume that's, by far,12        # the most common case in most applications.13        if "&" in text:14            text = text.replace("&", "&")15        if "<" in text:16            text = text.replace("<", "<")17        if ">" in text:18            text = text.replace(">", ">")19        if "'" in text:20            text = text.replace("'", "'")21        if '"' in text:...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
