Best Python code snippet using slash
test_alto.py
Source:test_alto.py  
...28    'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '29    'xsi:schemaLocation="http://www.loc.gov/standards/alto/ns-v3# '30    'http://www.loc.gov/alto/v3/alto-3-0.xsd">'31)32def _build_xml(xml_str: str) -> ElementTree.Element:33    return ElementTree.fromstring(f"{namespaces}{xml_str}</alto>")[0]34def _string(word: str = 'abc') -> String:35    return String(36        id="string_3", hpos=712, vpos=133, width=55, height=13, confidence=0.92, content=word, alternatives=[]37    )38def _text_line() -> TextLine:39    strings: List[Union[String, SP]] = [_string('abc'), SP(hpos=767, vpos=133, width=9), _string('def')]40    return TextLine(id="line_2", hpos=712, vpos=129, width=235, height=21, strings=strings)41def _text_block() -> TextBlock:42    return TextBlock("block_1", 53, 235, 712, 129, [_text_line(), _text_line()])43def _composed_blocks() -> ComposedBlock:44    return ComposedBlock("composed_block_1", 53, 235, 712, 129, [_text_block(), _text_block()])45def test_extract_unique_child_name_to_child():46    xml_str = '<Page WIDTH="1654" HEIGHT="2339" ID="page_0"></Page>'47    element = ElementTree.fromstring(xml_str)48    assert _extract_unique_child_name_to_child(element) == {}49    xml_str = '<Page WIDTH="1654" HEIGHT="2339" ID="page_0"><Description></Description></Page>'50    element = ElementTree.fromstring(xml_str)51    res = _extract_unique_child_name_to_child(element)52    assert len(res) == 153    assert "Description" in res54    assert res["Description"].tag == "Description"55    assert res["Description"].attrib == {}56    xml_str = '<Page WIDTH="1654" HEIGHT="2339" ID="page_0"><Description a="1"></Description></Page>'57    element = ElementTree.fromstring(xml_str)58    res = _extract_unique_child_name_to_child(element)59    assert len(res) == 160    assert "Description" in res61    assert res["Description"].tag == "Description"62    assert res["Description"].attrib == {"a": "1"}63    with pytest.raises(ValueError):64        xml_str = (65            '<Page WIDTH="1654" HEIGHT="2339" ID="page_0"><Description>'66            '</Description><Description></Description></Page>'67        )68        element = ElementTree.fromstring(xml_str)69        _extract_unique_child_name_to_child(element)70def test_get_tag():71    child_1 = ElementTree.Element("child_1")72    assert _get_tag("parent", {"child_1": child_1}, "child_1") == child_173    with pytest.raises(ValueError):74        _get_tag("parent", {"child_1": child_1}, "child_2")75        _get_tag("parent", {}, "child_1")76def test_get_attr():77    xml_str = '<Page WIDTH="1654" HEIGHT="2339" ID="page_0"></Page>'78    element = ElementTree.fromstring(xml_str)79    assert _get_attr(element, "WIDTH", int) == 165480    assert _get_attr(element, "WIDTH", float) == 1654.081    assert _get_attr(element, "WIDTH", str) == "1654"82    assert _get_attr(element, "ID", str) == "page_0"83    assert _get_attr(element, "HEIGHT", str) == "2339"84    with pytest.raises(ValueError):85        assert _get_attr(element, "FOO", str)86def test_check_type():87    assert _check_type(1, int) == 188    assert _check_type("1", str) == "1"89    with pytest.raises(ValueError):90        _check_type(1, str)91    with pytest.raises(ValueError):92        _check_type("1", int)93def test_assert_name_is():94    _assert_name_is("", "")95    _assert_name_is("1", "1")96    _assert_name_is("abc", "abc")97    with pytest.raises(ValueError):98        _assert_name_is("1", "3")99    with pytest.raises(ValueError):100        _assert_name_is("1", "")101def test_build_description():102    xml_str = """103        <Description>104            <MeasurementUnit>pixel</MeasurementUnit>105            <sourceImageInformation>106                <fileName></fileName>107            </sourceImageInformation>108            <OCRProcessing ID="OCR_0">109                <ocrProcessingStep>110                    <processingSoftware>111                        <softwareName>tesseract 4.1.1</softwareName>112                    </processingSoftware>113                </ocrProcessingStep>114            </OCRProcessing>115        </Description>116    """117    element = _build_xml(xml_str)118    assert Description.from_xml(element) == Description(None)119    xml_str = """120        <Description>121            <MeasurementUnit>pixel</MeasurementUnit>122            <sourceImageInformation>123                <fileName>abc</fileName>124            </sourceImageInformation>125            <OCRProcessing ID="OCR_0">126                <ocrProcessingStep>127                    <processingSoftware>128                        <softwareName>tesseract 4.1.1</softwareName>129                    </processingSoftware>130                </ocrProcessingStep>131            </OCRProcessing>132        </Description>133    """134    element = _build_xml(xml_str)135    assert Description.from_xml(element) == Description("abc")136def test_build_alternative():137    xml_str = """138        <Alternative>test</Alternative>139    """140    element = _build_xml(xml_str)141    assert Alternative.from_xml(element) == Alternative("test")142def test_build_string():143    xml_str = """<String ID="string_3" HPOS="712" VPOS="133" WIDTH="55" HEIGHT="13" WC="0.92" CONTENT="Liberté"/>"""144    element = _build_xml(xml_str)145    assert String.from_xml(element) == String(146        id="string_3",147        hpos=712,148        vpos=133,149        width=55,150        height=13,151        confidence=0.92,152        content="Liberté",153        alternatives=[],154    )155    xml_str = """156        <String ID="string_3" HPOS="712" VPOS="133" WIDTH="55" HEIGHT="13" WC="0.92" CONTENT="Liberté">157            <Alternative>alt</Alternative>158        </String>159    """160    element = _build_xml(xml_str)161    assert String.from_xml(element) == String(162        id="string_3",163        hpos=712,164        vpos=133,165        width=55,166        height=13,167        confidence=0.92,168        content="Liberté",169        alternatives=[Alternative("alt")],170    )171def test_build_sp():172    xml_str = """<SP WIDTH="9" VPOS="133" HPOS="767"/>"""173    element = _build_xml(xml_str)174    assert SP.from_xml(element) == SP(hpos=767, vpos=133, width=9)175def test_load_string_or_sp():176    xml_str = """<SP WIDTH="9" VPOS="133" HPOS="767"/>"""177    element = _build_xml(xml_str)178    assert _load_string_or_sp(element) == SP(hpos=767, vpos=133, width=9)179    xml_str = """<String ID="string_3" HPOS="712" VPOS="133" WIDTH="55" HEIGHT="13" WC="0.92" CONTENT="abc"/>"""180    element = _build_xml(xml_str)181    assert _load_string_or_sp(element) == String(182        id="string_3",183        hpos=712,184        vpos=133,185        width=55,186        height=13,187        confidence=0.92,188        content="abc",189        alternatives=[],190    )191    with pytest.raises(ValueError):192        xml_str = """<Alternative>test</Alternative>"""193        element = _build_xml(xml_str)194        _load_string_or_sp(element)195def test_build_text_line():196    xml_str = """197    <TextLine ID="line_2" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="21">198        <String ID="string_3" HPOS="712" VPOS="133" WIDTH="55" HEIGHT="13" WC="0.92" CONTENT="abc"/>199        <SP WIDTH="9" VPOS="133" HPOS="767"/>200    </TextLine>201    """202    element = _build_xml(xml_str)203    strings = [204        String(205            id="string_3",206            hpos=712,207            vpos=133,208            width=55,209            height=13,210            confidence=0.92,211            content="abc",212            alternatives=[],213        ),214        SP(hpos=767, vpos=133, width=9),215    ]216    assert TextLine.from_xml(element) == TextLine(217        id="line_2", hpos=712, vpos=129, width=235, height=21, strings=strings218    )219    xml_str = """220    <TextLine ID="line_2" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="21">221    </TextLine>222    """223    element = _build_xml(xml_str)224    assert TextLine.from_xml(element) == TextLine(id="line_2", hpos=712, vpos=129, width=235, height=21, strings=[])225def test_text_line_extract_words():226    assert _text_line().extract_words() == ['abc', 'def']227    assert TextLine(id="line_2", hpos=712, vpos=129, width=235, height=21, strings=[]).extract_words() == []228def test_build_text_block():229    element = _build_xml(230        """231        <TextBlock ID="block_1" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="53">232            <TextLine ID="line_2" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="21">233                <String ID="string_3" HPOS="712" VPOS="133" WIDTH="55" HEIGHT="13" WC="0.92" CONTENT="abc"/>234                <SP WIDTH="9" VPOS="133" HPOS="767"/>235            </TextLine>236        </TextBlock>237    """238    )239    strings = [240        String(241            id="string_3",242            hpos=712,243            vpos=133,244            width=55,245            height=13,246            confidence=0.92,247            content="abc",248            alternatives=[],249        ),250        SP(hpos=767, vpos=133, width=9),251    ]252    lines = [TextLine(id="line_2", hpos=712, vpos=129, width=235, height=21, strings=strings)]253    assert TextBlock.from_xml(element) == TextBlock("block_1", 53, 235, 712, 129, lines)254def test_text_block_extract_strings_lines():255    assert _text_block().extract_string_lines() == ['abc def', 'abc def']256def test_text_block_extract_words():257    assert _text_block().extract_words() == ['abc', 'def', 'abc', 'def']258def test_build_composed_block():259    element = _build_xml(260        """261        <ComposedBlock ID="cblock_1" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="53">262            <TextBlock ID="block_1" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="53">263            </TextBlock>264        </ComposedBlock>265        """266    )267    blocks = [TextBlock("block_1", 53, 235, 712, 129, [])]268    assert ComposedBlock.from_xml(element) == ComposedBlock("cblock_1", 53, 235, 712, 129, blocks)269def test_composed_block_extract_words():270    assert _composed_blocks().extract_words() == ['abc', 'def', 'abc', 'def', 'abc', 'def', 'abc', 'def']271def test_build_print_space():272    element = _build_xml(273        """274        <PrintSpace HPOS="0" VPOS="0" WIDTH="1654" HEIGHT="2339">275            <ComposedBlock ID="cblock_1" HPOS="712" VPOS="129" WIDTH="235" HEIGHT="53">276            </ComposedBlock>277        </PrintSpace>278        """279    )280    blocks = [ComposedBlock("cblock_1", 53, 235, 712, 129, [])]281    assert PrintSpace.from_xml(element) == PrintSpace(2339, 1654, 0, 0, None, blocks)282def test_build_page():283    element = _build_xml("""<Page WIDTH="1654" HEIGHT="2339" PHYSICAL_IMG_NR="0" ID="page_0"></Page>""")284    assert Page.from_xml(element) == Page("page_0", 2339, 1654, 0, None, [])285def test_page_extract_blocks():286    assert Page("page_0", 2339, 1654, 0, None, []).extract_blocks() == []287    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [])]).extract_blocks() == []288    block = ComposedBlock("", 1, 1, 1, 1, [])289    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_blocks() == [block]290def test_page_extract_text_blocks():291    assert Page("page_0", 2339, 1654, 0, None, []).extract_text_blocks() == []292    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [])]).extract_text_blocks() == []293    block = ComposedBlock("", 1, 1, 1, 1, [])294    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_text_blocks() == []295    tb = TextBlock("", 1, 1, 1, 1, [])296    block = ComposedBlock("", 1, 1, 1, 1, [tb])297    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_text_blocks() == [tb]298def test_page_extract_strings():299    assert Page("page_0", 2339, 1654, 0, None, []).extract_strings() == []300    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [])]).extract_strings() == []301    block = ComposedBlock("", 1, 1, 1, 1, [])302    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_strings() == []303    tb = TextBlock("", 1, 1, 1, 1, [])304    block = ComposedBlock("", 1, 1, 1, 1, [tb])305    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_strings() == []306    tb = TextBlock("", 1, 1, 1, 1, [TextLine("", 1, 1, 1, 1, [String("", 1, 1, 1, 1, "", 0, [])])])307    block = ComposedBlock("", 1, 1, 1, 1, [tb])308    page = Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])])309    assert page.extract_strings() == [String("", 1, 1, 1, 1, "", 0, [])]310def test_page_extract_lines():311    assert Page("page_0", 2339, 1654, 0, None, []).extract_lines() == []312    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [])]).extract_lines() == []313    block = ComposedBlock("", 1, 1, 1, 1, [])314    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_lines() == []315    tb = TextBlock("", 1, 1, 1, 1, [])316    block = ComposedBlock("", 1, 1, 1, 1, [tb])317    assert Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])]).extract_lines() == []318    line = TextLine("", 1, 1, 1, 1, [String("", 1, 1, 1, 1, "", 0, [])])319    tb = TextBlock("", 1, 1, 1, 1, [line])320    block = ComposedBlock("", 1, 1, 1, 1, [tb])321    page = Page("page_0", 2339, 1654, 0, None, [PrintSpace(1, 1, 1, 1, 1, [block])])322    assert page.extract_lines() == [line]323def test_build_layout():324    element = _build_xml("""<Layout></Layout>""")325    Layout.from_xml(element) == Layout([])326def _get_test_data_file() -> str:327    to_replace = "test_alto.py"328    if not __file__.endswith(to_replace):329        raise ValueError(f"Expecting __file__ to end with {to_replace}, got {__file__}")330    return __file__.replace(to_replace, "data/alto_example.xml")331def test_build_alto(real_life_example: str):332    res = Alto.from_xml(ElementTree.fromstring(real_life_example))333    assert len(res.layout.pages) == 1334def test_alto_from_str():335    res = Alto.parse_file(_get_test_data_file())336    assert len(res.layout.pages) == 1337def test_alto_extract_words():338    res = Alto.parse_file(_get_test_data_file())...tests.py
Source:tests.py  
1# -*- coding: utf-8 -*-2from unittest import TestCase3from dict2xml import dict2XML4class SuccessTest(TestCase):5    def _build_xml(self, lines, indent=True, utf8=False):6        xml_header = (utf8 and '<?xml version="1.0" encoding="utf-8"?>'7                            or '<?xml version="1.0" ?>')8        if indent:9            merged_lines = '\n'.join(lines)10            basestr = '%s\n%s\n'11        else:12            merged_lines = ''.join([line.strip() for line in lines])13            basestr = '%s%s'14        if utf8:15            merged_lines = merged_lines.encode('utf-8')16        return basestr % (xml_header, merged_lines)17    def test_1(self):18        """19        Empty dict.20        """21        dict_ = {'root': {}}22        expected_xml = ['<root/>']23        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))24        self.assertEqual(dict2XML(dict_, indent=False),25                         self._build_xml(expected_xml, indent=False))26    def test_2(self):27        """28        Simple unordered dict (key/simple values).29        """30        dict_ = {'root': {31            'nothing': '',32            'foo': 'oof',33            'bar': 'rab',34            'foobar': 'raboof'35        }}36        expected_xml = [37            '<root>',38            '  <bar>rab</bar>',39            '  <foo>oof</foo>',40            '  <foobar>raboof</foobar>',41            '  <nothing></nothing>',42            '</root>',43        ]44        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))45        self.assertEqual(dict2XML(dict_, indent=False),46                         self._build_xml(expected_xml, indent=False))47    def test_3(self):48        """49        Simple ordered dict (key/simple values).50        """51        dict_ = {'1__root': {52            '2__nothing': '',53            '4__foo': 'oof',54            '1__bar': 'rab',55            '3__foobar': 'raboof'56        }}57        expected_xml = [58            '<root>',59            '  <bar>rab</bar>',60            '  <nothing></nothing>',61            '  <foobar>raboof</foobar>',62            '  <foo>oof</foo>',63            '</root>',64        ]65        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))66        self.assertEqual(dict2XML(dict_, indent=False),67                         self._build_xml(expected_xml, indent=False))68    def test_4(self):69        """70        Simple mixed-ordered dict (key/simple values).71        """72        dict_ = {'root': {73            '2__nothing': '',74            'foo': 'oof',75            'bar': 'rab',76            '3__foobar': 'raboof'77        }}78        expected_xml = [79            '<root>',80            '  <nothing></nothing>',81            '  <foobar>raboof</foobar>',82            '  <bar>rab</bar>',83            '  <foo>oof</foo>',84            '</root>',85        ]86        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))87        self.assertEqual(dict2XML(dict_, indent=False),88                         self._build_xml(expected_xml, indent=False))89    def test_5(self):90        """91        Simple ordered dict (key/simple values) encoded with utf-8.92        """93        dict_ = {u'ròót': {94            '2__nothing': '',95            u'4__fôó': 'oof',96            '1__bar': u'ráb',97            u'3__fõôbár': u'rábôõf'98        }}99        expected_xml = [100            u'<ròót>',101            u'  <bar>ráb</bar>',102            u'  <nothing></nothing>',103            u'  <fõôbár>rábôõf</fõôbár>',104            u'  <fôó>oof</fôó>',105            u'</ròót>',106        ]107        self.assertEqual(dict2XML(dict_, utf8=True), self._build_xml(expected_xml, utf8=True))108        self.assertEqual(dict2XML(dict_, indent=False, utf8=True),109                         self._build_xml(expected_xml, indent=False, utf8=True))110    def test_6(self):111        """112        Ordered dict with nested dicts.113        """114        dict_ = {'root': {115            '1__bar': 'rab',116            '2__nothing': '',117            '3__subdict': {118                '1__anothersubdict': {119                    'hey': 'ya'120                },121                '2__foobar': 'raboof'122            },123            '4__emptysubdict': {},124            '5__foo': 'oof',125        }}126        expected_xml = [127            '<root>',128            '  <bar>rab</bar>',129            '  <nothing></nothing>',130            '  <subdict>',131            '    <anothersubdict>',132            '      <hey>ya</hey>',133            '    </anothersubdict>',134            '    <foobar>raboof</foobar>',135            '  </subdict>',136            '  <emptysubdict/>',137            '  <foo>oof</foo>',138            '</root>',139        ]140        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))141        self.assertEqual(dict2XML(dict_, indent=False),142                         self._build_xml(expected_xml, indent=False))143    def test_7(self):144        """145        Ordered dict with nested dicts and lists.146        """147        dict_ = {'root': {148            '1__list': [149                '1',150                {151                    'foo': ['bar']152                },153                {154                    '1__foo': {155                        '1__a': '1',156                        '2__b': ['2', '3', '4']157                    },158                    '2__bar': [159                        {160                          'foo': 'bar'161                        },162                        ''163                    ],164                },165                '2'166            ],167            '2__nothing': {}168        }}169        expected_xml = [170            '<root>',171            '  <list>1</list>',172            '  <list>',173            '    <foo>bar</foo>',174            '  </list>',175            '  <list>',176            '    <foo>',177            '      <a>1</a>',178            '      <b>2</b>',179            '      <b>3</b>',180            '      <b>4</b>',181            '    </foo>',182            '    <bar>',183            '      <foo>bar</foo>',184            '    </bar>',185            '    <bar></bar>',186            '  </list>',187            '  <list>2</list>',188            '  <nothing/>',189            '</root>',190        ]191        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))192        self.assertEqual(dict2XML(dict_, indent=False),193                         self._build_xml(expected_xml, indent=False))194    def test_8(self):195        """196        Ordered dict with nested dicts and lists and element attributes197        """198        dict_ = {'root': ({'a': 'b'}, {199            '1__list': [200                '1',201                {202                    'foo': ['bar']203                },204                {205                    '1__foo': ({'c': 'd', 'e': 'f'}, {206                        '1__a': '1',207                        '2__b': [208                            ({'A': 'B', 'C': 'D'}, '2'),209                            '3',210                            ({'E': 'F'}, '4')211                        ]212                    }),213                    '2__bar': [214                        {215                          'foo': ({'g': 'h', 'i': 'j'}, 'bar')216                        },217                        ''218                    ],219                },220                '2'221            ],222            '2__nothing': {}223        })}224        expected_xml = [225            '<root a="b">',226            '  <list>1</list>',227            '  <list>',228            '    <foo>bar</foo>',229            '  </list>',230            '  <list>',231            '    <foo c="d" e="f">',232            '      <a>1</a>',233            '      <b A="B" C="D">2</b>',234            '      <b>3</b>',235            '      <b E="F">4</b>',236            '    </foo>',237            '    <bar>',238            '      <foo g="h" i="j">bar</foo>',239            '    </bar>',240            '    <bar></bar>',241            '  </list>',242            '  <list>2</list>',243            '  <nothing/>',244            '</root>',245        ]246        self.assertEqual(dict2XML(dict_), self._build_xml(expected_xml))247        self.assertEqual(dict2XML(dict_, indent=False),...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!!
