Best Python code snippet using avocado_python
test_xml.py
Source:test_xml.py
1# -*- coding: utf-8 -*-2from __future__ import absolute_import, print_function, division3import sys4from collections import OrderedDict5from tempfile import NamedTemporaryFile6import pytest7from petl.test.helpers import ieq8from petl.util import nrows, look9from petl.io.xml import fromxml, toxml10from petl.compat import urlopen11def test_fromxml():12 # initial data13 f = NamedTemporaryFile(delete=False, mode='wt')14 data = """<table>15 <tr>16 <td>foo</td><td>bar</td>17 </tr>18 <tr>19 <td>a</td><td>1</td>20 </tr>21 <tr>22 <td>b</td><td>2</td>23 </tr>24 <tr>25 <td>c</td><td>2</td>26 </tr>27 </table>"""28 f.write(data)29 f.close()30 actual = fromxml(f.name, 'tr', 'td')31 expect = (('foo', 'bar'),32 ('a', '1'),33 ('b', '2'),34 ('c', '2'))35 ieq(expect, actual)36 ieq(expect, actual) # verify can iterate twice37def test_fromxml_2():38 # initial data39 f = NamedTemporaryFile(delete=False, mode='wt')40 data = """<table>41 <tr>42 <td v='foo'/><td v='bar'/>43 </tr>44 <tr>45 <td v='a'/><td v='1'/>46 </tr>47 <tr>48 <td v='b'/><td v='2'/>49 </tr>50 <tr>51 <td v='c'/><td v='2'/>52 </tr>53 </table>"""54 f.write(data)55 f.close()56 actual = fromxml(f.name, 'tr', 'td', 'v')57 expect = (('foo', 'bar'),58 ('a', '1'),59 ('b', '2'),60 ('c', '2'))61 ieq(expect, actual)62 ieq(expect, actual) # verify can iterate twice63def test_fromxml_3():64 # initial data65 f = NamedTemporaryFile(delete=False, mode='wt')66 data = """<table>67 <row>68 <foo>a</foo><baz><bar v='1'/></baz>69 </row>70 <row>71 <foo>b</foo><baz><bar v='2'/></baz>72 </row>73 <row>74 <foo>c</foo><baz><bar v='2'/></baz>75 </row>76 </table>"""77 f.write(data)78 f.close()79 actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': ('baz/bar', 'v')})80 # N.B., requested fields come out in name sorted order81 expect = (('bar', 'foo'),82 ('1', 'a'),83 ('2', 'b'),84 ('2', 'c'))85 ieq(expect, actual)86 ieq(expect, actual) # verify can iterate twice87def test_fromxml_4():88 # initial data89 f = NamedTemporaryFile(delete=False, mode='wt')90 data = """<table>91 <row>92 <foo>a</foo><baz><bar>1</bar><bar>3</bar></baz>93 </row>94 <row>95 <foo>b</foo><baz><bar>2</bar></baz>96 </row>97 <row>98 <foo>c</foo><baz><bar>2</bar></baz>99 </row>100 </table>"""101 f.write(data)102 f.close()103 actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': './/bar'})104 # N.B., requested fields come out in name sorted order105 expect = (('bar', 'foo'),106 (('1', '3'), 'a'),107 ('2', 'b'),108 ('2', 'c'))109 ieq(expect, actual)110 ieq(expect, actual) # verify can iterate twice111def test_fromxml_5():112 # initial data113 f = NamedTemporaryFile(delete=False, mode='wt')114 data = """<table>115 <row>116 <foo>a</foo><baz><bar v='1'/><bar v='3'/></baz>117 </row>118 <row>119 <foo>b</foo><baz><bar v='2'/></baz>120 </row>121 <row>122 <foo>c</foo><baz><bar v='2'/></baz>123 </row>124 </table>"""125 f.write(data)126 f.close()127 actual = fromxml(f.name, 'row', {'foo': 'foo', 'bar': ('baz/bar', 'v')})128 # N.B., requested fields come out in name sorted order129 expect = (('bar', 'foo'),130 (('1', '3'), 'a'),131 ('2', 'b'),132 ('2', 'c'))133 ieq(expect, actual)134 ieq(expect, actual) # verify can iterate twice135def test_fromxml_6():136 data = """<table class='petl'>137 <thead>138 <tr>139 <th>foo</th>140 <th>bar</th>141 </tr>142 </thead>143 <tbody>144 <tr>145 <td>a</td>146 <td style='text-align: right'>2</td>147 </tr>148 <tr>149 <td>b</td>150 <td style='text-align: right'>1</td>151 </tr>152 <tr>153 <td>c</td>154 <td style='text-align: right'>3</td>155 </tr>156 </tbody>157 </table>"""158 f = NamedTemporaryFile(delete=False, mode='wt')159 f.write(data)160 f.close()161 actual = fromxml(f.name, './/tr', ('th', 'td'))162 print(look(actual))163 expect = (('foo', 'bar'),164 ('a', '2'),165 ('b', '1'),166 ('c', '3'))167 ieq(expect, actual)168 ieq(expect, actual) # verify can iterate twice169def test_fromxml_url():170 # check internet connection171 try:172 url = 'http://raw.githubusercontent.com/petl-developers/petl/master/petl/test/resources/test.xml'173 urlopen(url)174 import pkg_resources175 filename = pkg_resources.resource_filename('petl', 'test/resources/test.xml')176 except Exception as e:177 pytest.skip('SKIP test_fromxml_url: %s' % e)178 else:179 actual = fromxml(url, 'pydev_property', {'name': ( '.', 'name'), 'prop': '.'})180 assert nrows(actual) > 0181 expect = fromxml(filename, 'pydev_property', {'name': ( '.', 'name'), 'prop': '.'})182 ieq(expect, actual)183def _write_temp_file(content, out=None):184 with NamedTemporaryFile(delete=False, mode='wt') as f:185 f.write(content)186 res = f.name187 f.close()188 if out is not None:189 outf = sys.stderr if out else sys.stdout190 print('TEST %s:\n%s' % (res, content), file=outf)191 return res192def _dump_file(filename, out=None):193 if out is not None:194 outf = sys.stderr if out else sys.stdout195 print('FILE:\n%s' % open(filename).read(), file=outf)196def _dump_both(expected, actual, out=None):197 if out is not None:198 outf = sys.stderr if out else sys.stdout199 print('EXPECTED:\n', look(expected), file=outf)200 print('ACTUAL:\n', look(actual), file=outf)201def _compare(expected, actual, out=None):202 try:203 _dump_both(expected, actual, out)204 ieq(expected, actual)205 except Exception as ex:206 _dump_both(expected, actual, False)207 raise ex208def _write_test_file(data, pre='', pos=''):209 content = pre + '<table>' + data + pos + '</table>'210 return _write_temp_file(content)211def test_fromxml_entity():212 _DATA1 = """213 <tr><td>foo</td><td>bar</td></tr>214 <tr><td>a</td><td>1</td></tr>215 <tr><td>b</td><td>2</td></tr>216 <tr><td>c</td><td>3</td></tr>217 """218 _DATA2 = '<td>X</td><td>9</td>'219 _DOCTYPE = """<?xml version="1.0" encoding="ISO-8859-1"?>220 <!DOCTYPE foo [ 221 <!ELEMENT table ANY >222 <!ENTITY inserted SYSTEM "file://%s" >]>223 """224 _INSERTED = '<tr>&inserted;</tr>'225 _TABLE1 = (('foo', 'bar'),226 ('a', '1'),227 ('b', '2'),228 ('c', '3'))229 temp_file1 = _write_test_file(_DATA1)230 actual11 = fromxml(temp_file1, 'tr', 'td')231 _compare(_TABLE1, actual11)232 try:233 from lxml import etree234 except:235 return236 data_file_tmp = _write_temp_file(_DATA2)237 doc_type_temp = _DOCTYPE % data_file_tmp238 doc_type_miss = _DOCTYPE % '/tmp/doesnotexist'239 _EXPECT_IT = (('X', '9'),)240 _EXPECT_NO = ((None, None),)241 temp_file2 = _write_test_file(_DATA1, pre=doc_type_temp, pos=_INSERTED)242 temp_file3 = _write_test_file(_DATA1, pre=doc_type_miss, pos=_INSERTED)243 parser_off = etree.XMLParser(resolve_entities=False)244 parser_onn = etree.XMLParser(resolve_entities=True)245 actual12 = fromxml(temp_file1, 'tr', 'td', parser=parser_off)246 _compare(_TABLE1, actual12)247 actual21 = fromxml(temp_file2, 'tr', 'td')248 _compare(_TABLE1 + _EXPECT_NO, actual21)249 actual22 = fromxml(temp_file2, 'tr', 'td', parser=parser_off)250 _compare(_TABLE1 + _EXPECT_NO, actual22)251 actual23 = fromxml(temp_file2, 'tr', 'td', parser=parser_onn)252 _compare(_TABLE1 + _EXPECT_IT, actual23)253 actual31 = fromxml(temp_file3, 'tr', 'td')254 _compare(_TABLE1 + _EXPECT_NO, actual31)255 actual32 = fromxml(temp_file3, 'tr', 'td', parser=parser_off)256 _compare(_TABLE1 + _EXPECT_NO, actual32)257 try:258 actual33 = fromxml(temp_file3, 'tr', 'td', parser=parser_onn)259 for _ in actual33:260 pass261 except etree.XMLSyntaxError:262 # print('XMLSyntaxError', ex, file=sys.stderr)263 pass264 else:265 assert True, 'Error testing XML'266def _check_toxml(table, expected, check=(), dump=None, **kwargs):267 with NamedTemporaryFile(delete=True, suffix='.xml') as f:268 filename = f.name269 toxml(table, filename, **kwargs)270 _dump_file(filename, dump)271 if check:272 try:273 actual = fromxml(filename, *check)274 _compare(expected, actual, dump)275 except Exception as ex:276 _dump_file(filename, False)277 raise ex278_HEAD1 = (('ABCD', 'N123'),)279_BODY1 = (('a', '1'),280 ('b', '2'),281 ('c', '3'))282_TABLE1 = _HEAD1 + _BODY1283def test_toxml00():284 _check_toxml(285 _TABLE1, _TABLE1,286 check=('.//tr', ('th', 'td'))287 )288def test_toxml01():289 _check_toxml(290 _TABLE1, _TABLE1,291 check=('//tr', ('th', 'td')),292 root='table',293 head='thead/tr/th',294 rows='tbody/tr/td'295 )296def test_toxml02():297 _check_toxml(298 _TABLE1, _BODY1,299 check=('.//row', 'col'),300 root='matrix',301 rows='row/col'302 )303def test_toxml03():304 _check_toxml(305 _TABLE1, _BODY1,306 check=('line', 'cell'),307 rows='plan/line/cell'308 )309def test_toxml04():310 _check_toxml(311 _TABLE1, _BODY1,312 check=('.//line', 'cell'),313 rows='dir/file/book/plan/line/cell'314 )315def test_toxml05():316 _check_toxml(317 _TABLE1, _TABLE1,318 check=('.//x', 'y'),319 root='a',320 head='h/k/x/y',321 rows='r/v/x/y'322 )323def test_toxml06():324 _check_toxml(325 _TABLE1, _TABLE1,326 check=('.//row', 'col'),327 root='table',328 head='head/row/col',329 rows='row/col'330 )331def test_toxml07():332 _check_toxml(333 _TABLE1, _TABLE1,334 check=('.//field-list', 'field-name'),335 root='root-tag',336 head='head-tag/field-list/field-name',337 rows='body-row/field-list/field-name'338 )339def test_toxml08():340 _check_toxml(341 _TABLE1, _TABLE1,342 check=('.//field.list', 'field.name'),343 root='root.tag',344 head='head.tag/field.list/field.name',345 rows='body.row/field.list/field.name'346 )347def test_toxml09():348 _check_toxml(349 _TABLE1, _BODY1,350 check=('.//tr/td', '*'),351 style='name',352 root='table',353 rows='tbody/tr/td'354 )355def test_toxml10():356 _check_toxml(357 _TABLE1, _BODY1,358 check=('.//tr/td', '*'),359 style='name',360 root='table',361 head='thead/tr/th',362 rows='tbody/tr/td'363 )364_ATTRIB_COLS = {'ABCD': ('.', 'ABCD'), 'N123': ('.', 'N123')}365def test_toxml11():366 _check_toxml(367 _TABLE1, _TABLE1,368 check=('.//tr/td', _ATTRIB_COLS),369 style='attribute',370 root='table',371 rows='tbody/tr/td'372 )373def test_toxml12():374 _check_toxml(375 _TABLE1, _TABLE1,376 check=('.//tr/td', _ATTRIB_COLS),377 style='attribute',378 root='table',379 head='thead/tr/th',380 rows='tbody/tr/td'381 )382def test_toxml13():383 _check_toxml(384 _TABLE1, _BODY1,385 check=('.//tr', ('td', 'th')),386 style=' <tr><td>{ABCD}</td><td>{N123}</td></tr>\n',387 root='table',388 rows='tbody'389 )390def test_toxml131():391 _check_toxml(392 _TABLE1, _TABLE1,393 check=('.//tr', ('th', 'td')),394 style=' <tr><td>{ABCD}</td><td>{N123}</td></tr>\n',395 root='table',396 head='thead/tr/td',397 rows='tbody'398 )399def test_toxml14():400 table1 = [['foo', 'bar'],401 ['a', 1],402 ['b', 2]]403 _check_toxml(404 table1, table1,405 style='attribute',406 rows='row/col'407 )408 _check_toxml(409 table1, table1,410 style='name',411 rows='row/col'412 )413_ROW_A0 = (('A', '0'),)414_ROW_Z9 = (('Z', '9'),)415_TAB_ABZ = _ROW_A0 + _BODY1 + _ROW_Z9416_TAB_HAZ = _HEAD1 + _ROW_A0 + _BODY1 + _ROW_Z9417_TAG_A0 = ' <row><col>A</col><col>0</col></row>'418_TAG_Z9 = ' <row><col>Z</col><col>9</col></row>'419_TAG_TOP = '<table>\n'420_TAG_END = '\n</table>'421def test_toxml15():422 _check_toxml(423 _TABLE1, _TAB_ABZ,424 check=('row', 'col'),425 root='table',426 rows='row/col',427 prologue=_TAG_A0, 428 epilogue=_TAG_Z9429 )430def test_toxml16():431 _check_toxml(432 _TABLE1, _TAB_HAZ,433 check=('.//row', 'col'),434 root='table',435 head='thead/row/col',436 rows='tbody/row/col',437 prologue=_TAG_A0, 438 epilogue=_TAG_Z9439 )440def test_toxml17():441 _check_toxml(442 _TABLE1, _TAB_ABZ,443 check=('row', 'col'),444 rows='row/col',445 prologue=_TAG_TOP + _TAG_A0, 446 epilogue=_TAG_Z9 + _TAG_END447 )448def test_toxml18():449 _TAB_AHZ = _ROW_A0 + _HEAD1 + _BODY1 + _ROW_Z9450 _check_toxml(451 _TABLE1, _TAB_AHZ,452 check=('.//row', 'col'),453 head='thead/row/col',454 rows='row/col',455 prologue=_TAG_TOP + _TAG_A0, 456 epilogue=_TAG_Z9 + _TAG_END457 )458def test_toxml19():459 _check_toxml(460 _TABLE1, _BODY1,461 check=('.//line', 'cell'),462 rows='tbody/line/cell',463 prologue=_TAG_TOP + _TAG_A0, 464 epilogue=_TAG_Z9 + _TAG_END465 )466def test_toxml20():467 _check_toxml(468 _TABLE1, _TABLE1,469 check=('.//line', 'cell'),470 root='book',471 head='thead/line/cell',472 rows='tbody/line/cell',473 prologue=_TAG_TOP + _TAG_A0,474 epilogue=_TAG_Z9 + _TAG_END475 )476def test_toxml21():477 _check_toxml(478 _TABLE1, _TAB_HAZ,479 check=('//row', 'col'),480 root='book',481 head='thead/row/col',482 rows='tbody/row/col',483 prologue=_TAG_TOP + _TAG_A0,484 epilogue=_TAG_Z9 + _TAG_END485 )486def test_toxml22():487 _check_toxml(488 _TABLE1, _TAB_HAZ,489 check=('.//tr/td', _ATTRIB_COLS),490 style='attribute',491 root='table',492 rows='tbody/tr/td',493 # dump=True,494 prologue='<td ABCD="A" N123="0" />', 495 epilogue='<td ABCD="Z" N123="9" />'...
test.py
Source:test.py
1from os import name2from nbt.nbt import *3from nbt import nbt4import time5current_time = int(time.time() * 1000)6nbtfile = NBTFile()7nbtfile.tags.append(TAG_Int(name="MinecraftDataVersion", value=2230))8nbtfile.tags.append(TAG_Int(name="Version", value=5))9metadata = TAG_Compound()10metadata.name = 'Metadata'11metadata.tags.append(TAG_Long(name='TimeCreated', value=current_time))12metadata.tags.append(TAG_Long(name='TimeModified', value=current_time))13enclosing_size = TAG_Compound()14enclosing_size.name = 'EnclosingSize'15enclosing_size.tags.extend([TAG_Int(name='x', value=2), TAG_Int(name='y', value=2), TAG_Int(name='z', value=2)])16metadata.tags.append(enclosing_size)17metadata.tags.append(TAG_String(name='Description', value=''))18metadata.tags.append(TAG_Int(name='RegionCount', value=1))19metadata.tags.append(TAG_Int(name='TotalBlocks', value=4))20metadata.tags.append(TAG_String(name='Author', value='Spirited_Away_'))21metadata.tags.append(TAG_Int(name='TotalVolume', value=8))22metadata.tags.append(TAG_String(name='Name', value='RedstoneMusic'))23nbtfile.tags.append(metadata)24regions = TAG_Compound()25regions.name = 'Regions'26region_first = TAG_Compound()27region_first.name = 'RedstoneMusic'28region_first.tags.append(TAG_List(name='PendingBlockTicks', type=nbt._TAG_End))29block_states = TAG_Long_Array(name='BlockStates')30block_states.value = [0b0100010110]31region_first.tags.append(block_states)32position = TAG_Compound()33position.name = 'Position'34position.tags.extend([TAG_Int(name='x', value=0), TAG_Int(name='y', value=0), TAG_Int(name='z', value=0)])35region_first.tags.append(position)36blockState_palette = TAG_List(name='BlockStatePalette', type=TAG_Compound)37air_block = TAG_Compound()38air_block.tags.append(TAG_String(name='Name', value='minecraft:air'))39iron_block = TAG_Compound()40iron_block.tags.append(TAG_String(name='Name', value='minecraft:iron_block'))41gold_block = TAG_Compound()42gold_block.tags.append(TAG_String(name='Name', value='minecraft:gold_block'))43blockState_palette.extend([air_block, iron_block, gold_block])44region_first.tags.append(blockState_palette)45region_size = TAG_Compound()46region_size.name = 'Size'47region_size.tags.extend([TAG_Int(name='x', value=2), TAG_Int(name='y', value=2), TAG_Int(name='z', value=2)])48region_first.tags.append(region_size)49region_first.tags.append(TAG_List(name='PendingFluidTicks', type=nbt._TAG_End))50region_first.tags.append(TAG_List(name='TileEntities', type=nbt._TAG_End))51region_first.tags.append(TAG_List(name='Entities', type=nbt._TAG_End))52regions.tags.append(region_first)53nbtfile.tags.append(regions)54print(nbtfile.pretty_tree())...
Region.py
Source:Region.py
1from container.TagBuilder import TagBuilder2from nbt import nbt3from container.NBTTagBuilders import BlockStateBuilder4from container.BlockStateArray import BlockStateArray5class Region:6 def __init__(self, sx, sy, sz, pending_block_tick=False,7 blockstatebit=3, px=0, py=0, pz=0, name=None) -> None:8 self._sx = sx9 self._sy = sy10 self._sz = sz11 self._px = px12 self._py = py13 self._pz = pz14 assert not name is None15 self.name = name16 17 self._blockstate = BlockStateArray(blockstatebit, (sx * sy * sz * blockstatebit + 63) // 64)18 self._blockstatepalette = nbt.TAG_List(name='BlockStatePalette', type=nbt.TAG_Compound)19 self._tileeneities = nbt.TAG_List(name='TileEntities', type=nbt.TAG_Compound)20 if pending_block_tick:21 self._pendingblockticks = nbt.TAG_List(name='PendingBlockTicks', type=nbt.TAG_Compound)22 else:23 self._pendingblockticks = nbt.TAG_List(name='PendingBlockTicks', type=nbt._TAG_End)24 25 self._pendingfluidticks = nbt.TAG_List(name='PendingFluidTicks', type=nbt._TAG_End)26 self._entities = nbt.TAG_List(name='Entities', type=nbt._TAG_End)27 28 def _get_array_index(self, x, y, z):29 return z * self._sx + y * self._sx * self._sz + x30 def build_nbt_full(self):31 self._build()32 root = nbt.TAG_Compound()33 root.name = self.name34 position = TagBuilder(root_name='Position')35 position.add_common_property(nbt.TAG_Int(self._px, 'x'))36 position.add_common_property(nbt.TAG_Int(self._py, 'y'))37 position.add_common_property(nbt.TAG_Int(self._pz, 'z'))38 size = TagBuilder(root_name='Size')39 size.add_common_property(nbt.TAG_Int(self._sx, 'x'))40 size.add_common_property(nbt.TAG_Int(self._sy, 'y'))41 size.add_common_property(nbt.TAG_Int(self._sz, 'z'))42 root.tags.extend(map(lambda x: x.get_nbt_tag(), [position, size, self._blockstate]))43 root.tags.extend([self._blockstatepalette, self._tileeneities, self._pendingblockticks, self._pendingfluidticks, self._entities])44 return root45 def _build(self):...
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!!