pygexml.strategies
1# mypy: disallow_untyped_defs=False 2# mypy: disallow_untyped_calls=False 3 4import string 5 6import hypothesis.strategies as st 7 8from pygexml.geometry import Point, Box, Polygon 9from pygexml.page import Coords, Page, TextLine, TextRegion 10 11st_points = st.builds(Point, x=st.integers(min_value=0), y=st.integers(min_value=0)) 12 13 14@st.composite 15def st_box_points(draw): 16 tl = draw(st_points) 17 br_x = draw(st.integers(min_value=tl.x + 1)) 18 br_y = draw(st.integers(min_value=tl.y + 1)) 19 br = Point(x=br_x, y=br_y) 20 return tl, br 21 22 23st_boxes = st.builds( 24 lambda pp: Box(top_left=pp[0], bottom_right=pp[1]), st_box_points() 25) 26 27st_polygons = st.builds(Polygon, st.lists(st_points, min_size=1)) 28st_polygons2 = st.builds(Polygon, st.lists(st_points, min_size=2)) 29 30st_coords = st.builds(Coords, polygon=st_polygons2) 31 32st_coords_strings = st.builds(str, st_coords) 33 34 35def st_xml_text(**kwargs): 36 xml_chars = ( 37 st.characters(min_codepoint=0x20, max_codepoint=0xD7FF) 38 | st.characters(min_codepoint=0xE000, max_codepoint=0xFFFD) 39 | st.characters(min_codepoint=0x10000, max_codepoint=0x10FFFF) 40 | st.sampled_from(["\t", "\n"]) 41 ) 42 return st.text(alphabet=xml_chars, **kwargs) 43 44 45def st_simple_text(**kwargs): 46 simple = string.ascii_letters + string.digits + " _-" 47 return st.text(alphabet=simple, max_size=20, **kwargs) 48 49 50st_text_lines = st.builds( 51 TextLine, id=st_simple_text(), coords=st_coords, text=st_xml_text() 52) 53 54st_text_regions = st.builds( 55 TextRegion, 56 id=st_simple_text(), 57 coords=st_coords, 58 textlines=st.builds( 59 lambda lines: {l.id: l for l in lines}, st.lists(st_text_lines) 60 ), 61) 62 63 64@st.composite 65def st_pages(draw): 66 image_filename = draw(st_simple_text()) 67 regions = {tr.id: tr for tr in draw(st.lists(st_text_regions))} 68 page = Page(image_filename=image_filename, regions=regions) 69 return page
st_points =
builds(Point, x=integers(min_value=0), y=integers(min_value=0))
def
st_box_points() -> hypothesis.strategies.SearchStrategy:
The type of the None singleton.
st_boxes =
builds(lambda pp: Box(top_left=pp[0], bottom_right=pp[1]), st_box_points())
st_polygons =
builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=1))
st_polygons2 =
builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2))
st_coords =
builds(Coords, polygon=builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2)))
st_coords_strings =
builds(str, builds(Coords, polygon=builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2))))
def
st_xml_text(**kwargs):
36def st_xml_text(**kwargs): 37 xml_chars = ( 38 st.characters(min_codepoint=0x20, max_codepoint=0xD7FF) 39 | st.characters(min_codepoint=0xE000, max_codepoint=0xFFFD) 40 | st.characters(min_codepoint=0x10000, max_codepoint=0x10FFFF) 41 | st.sampled_from(["\t", "\n"]) 42 ) 43 return st.text(alphabet=xml_chars, **kwargs)
def
st_simple_text(**kwargs):
st_text_lines =
builds(TextLine, coords=builds(Coords, polygon=builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2))), id=text(alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-', max_size=20), text=text(alphabet=one_of(characters(min_codepoint=32, max_codepoint=55295), characters(min_codepoint=57344, max_codepoint=65533), characters(min_codepoint=65536, max_codepoint=1114111), sampled_from(['\t', '\n']))))
st_text_regions =
builds(TextRegion, coords=builds(Coords, polygon=builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2))), id=text(alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-', max_size=20), textlines=builds(lambda lines: {l.id: l for l in lines}, lists(builds(TextLine, coords=builds(Coords, polygon=builds(Polygon, lists(builds(Point, x=integers(min_value=0), y=integers(min_value=0)), min_size=2))), id=text(alphabet='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-', max_size=20), text=text(alphabet=one_of(characters(min_codepoint=32, max_codepoint=55295), characters(min_codepoint=57344, max_codepoint=65533), characters(min_codepoint=65536, max_codepoint=1114111), sampled_from(['\t', '\n'])))))))
def
st_pages() -> hypothesis.strategies.SearchStrategy:
The type of the None singleton.