Best Python code snippet using hypothesis
server.py
Source:server.py
1#===============================================================================2#3# Flatmap server4#5# Copyright (c) 2019 David Brooks6#7# Licensed under the Apache License, Version 2.0 (the "License");8# you may not use this file except in compliance with the License.9# You may obtain a copy of the License at10#11# http://www.apache.org/licenses/LICENSE-2.012#13# Unless required by applicable law or agreed to in writing, software14# distributed under the License is distributed on an "AS IS" BASIS,15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16# See the License for the specific language governing permissions and17# limitations under the License.18#19#===============================================================================20import gzip21import io22import json23import logging24import os.path25import pathlib26import sqlite327import sys28import time29#===============================================================================30import flask31from flask import Blueprint, Flask, Response, request32from flask_cors import CORS33try:34 from werkzeug.wsgi import FileWrapper35except ImportError:36 FileWrapper = None37#===============================================================================38from .knowledgestore import KnowledgeStore39from . import __version__40#===============================================================================41# Global settings42from .settings import settings43settings['ROOT_PATH'] = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]44def normalise_path(path):45#========================46 return os.path.normpath(os.path.join(settings['ROOT_PATH'], path))47FLATMAP_ROOT = os.environ.get('FLATMAP_ROOT', './flatmaps')48settings['FLATMAP_ROOT'] = normalise_path(FLATMAP_ROOT)49settings['ONTOLOGY_ROOT'] = normalise_path('./ontology')50settings['BEARER_TOKENS'] = os.environ.get('BEARER_TOKENS', '').split()51#===============================================================================52# Needed to read JPEG 2000 files with OpenCV2 under Linux53os.environ['OPENCV_IO_ENABLE_JASPER'] = '1'54#===============================================================================55# Don't import unnecessary packages when building documentation as otherwise56# a ``readthedocs`` build aborts with ``excessive memory consumption``57if 'sphinx' not in sys.modules:58 from landez.sources import MBTilesReader, ExtractionError, InvalidFormatError59 from PIL import Image60 # We also don't instantiate a Manager as doing so will prevent Sphinx from61 # exiting (and hang a ``readthedocs`` build)62 from .maker import Manager63 map_maker = Manager()64#===============================================================================65#===============================================================================66flatmap_blueprint = Blueprint('flatmap', __name__,67 root_path=settings['ROOT_PATH'],68 url_prefix='/')69#===============================================================================70knowledge_blueprint = Blueprint('knowledge', __name__, url_prefix='/knowledge')71#===============================================================================72maker_blueprint = Blueprint('maker', __name__, url_prefix='/make')73@maker_blueprint.before_request74def maker_auth_check():75 if not settings['BEARER_TOKENS']:76 return None # no security defined; permit all access.77 auth = request.headers.get('Authorization', '')78 if auth.startswith('Bearer '):79 if auth.split()[1] in settings['BEARER_TOKENS']:80 return None81 return flask.make_response('{"error": "unauthorized"}', 403)82#===============================================================================83viewer_blueprint = Blueprint('viewer', __name__,84 root_path=normalise_path('./viewer/dist'),85 url_prefix='/viewer')86#===============================================================================87#===============================================================================88app = None89#===============================================================================90def wsgi_app(viewer=False):91 global app92 settings['MAP_VIEWER'] = viewer93 app = Flask(__name__)94 CORS(flatmap_blueprint)95 app.register_blueprint(flatmap_blueprint)96 CORS(knowledge_blueprint)97 app.register_blueprint(knowledge_blueprint)98 app.register_blueprint(maker_blueprint)99 app.register_blueprint(viewer_blueprint)100 if __name__ != '__main__':101 gunicorn_logger = logging.getLogger('gunicorn.error')102 app.logger.handlers = gunicorn_logger.handlers103 app.logger.setLevel(gunicorn_logger.level)104 app.logger.info(f'Started flatmap server version {__version__}')105 if not settings['BEARER_TOKENS']:106 # Only warn once...107 app.logger.warning('No bearer tokens defined')108 # Open our knowledge base109 knowledge_store = KnowledgeStore(settings['FLATMAP_ROOT'], create=True)110 if knowledge_store.error is not None:111 app.logger.error('{}: {}'.format(knowledge_store.error, knowledge_store.db_name))112 return app113#===============================================================================114#===============================================================================115def send_bytes(bytes_io, mimetype):116 if FileWrapper is not None:117 return Response(FileWrapper(bytes_io), mimetype=mimetype, direct_passthrough=True)118 else:119 return flask.send_file(bytes_io, mimetype=mimetype)120#===============================================================================121def send_json(filename):122#=======================123 try:124 return flask.send_file(filename)125 except FileNotFoundError:126 return flask.jsonify({})127#===============================================================================128def error_abort(msg):129#====================130 app.logger.error(msg)131 flask.abort(501, msg)132#===============================================================================133def blank_tile():134 tile = Image.new('RGBA', (1, 1), color=(255, 255, 255, 0))135 file = io.BytesIO()136 tile.save(file, 'png')137 file.seek(0)138 return file139#===============================================================================140def normalise_identifier(id):141#============================142 return ':'.join([(s[:-1].lstrip('0') + s[-1])143 for s in id.split(':')])144#===============================================================================145def read_metadata(tile_reader, name):146 try:147 row = tile_reader._query("SELECT value FROM metadata WHERE name='{}'".format(name)).fetchone()148 except (InvalidFormatError, sqlite3.OperationalError):149 flask.abort(404, 'Cannot read tile database')150 return {} if row is None else json.loads(row[0])151def get_metadata(map_id, name):152 mbtiles = os.path.join(settings['FLATMAP_ROOT'], map_id, 'index.mbtiles')153 return read_metadata(MBTilesReader(mbtiles), name)154#===============================================================================155#===============================================================================156@flatmap_blueprint.route('/')157def maps():158 """159 Get a list of available flatmaps.160 :>jsonarr string id: the flatmap's unique identifier on the server161 :>jsonarr string source: the map's source URL162 :>jsonarr string created: when the map was generated163 :>jsonarr string describes: the map's description164 """165 flatmap_list = []166 root_path = pathlib.Path(settings['FLATMAP_ROOT'])167 if root_path.is_dir():168 for flatmap_dir in root_path.iterdir():169 index = os.path.join(settings['FLATMAP_ROOT'], flatmap_dir, 'index.json')170 mbtiles = os.path.join(settings['FLATMAP_ROOT'], flatmap_dir, 'index.mbtiles')171 if os.path.isdir(flatmap_dir) and os.path.exists(index) and os.path.exists(mbtiles):172 with open(index) as fp:173 index = json.loads(fp.read())174 version = index.get('version', 1.0)175 reader = MBTilesReader(mbtiles)176 if version >= 1.3:177 metadata = read_metadata(reader, 'metadata')178 if flatmap_dir.name != metadata['id']:179 app.logger.error('Flatmap id mismatch: {}'.format(flatmap_dir))180 continue181 flatmap = {182 'id': metadata['id'],183 'source': metadata['source'],184 'version': version185 }186 if 'created' in metadata:187 flatmap['created'] = metadata['created']188 if 'taxon' in metadata:189 flatmap['taxon'] = normalise_identifier(metadata['taxon'])190 flatmap['describes'] = metadata['describes'] if 'describes' in metadata else flatmap['taxon']191 elif 'describes' in metadata:192 flatmap['taxon'] = normalise_identifier(metadata['describes'])193 flatmap['describes'] = flatmap['taxon']194 if 'name' in metadata:195 flatmap['name'] = metadata['name']196 else:197 try:198 source_row = reader._query("SELECT value FROM metadata WHERE name='source'").fetchone()199 except (InvalidFormatError, sqlite3.OperationalError):200 flask.abort(404, 'Cannot read tile database: {}'.format(mbtiles))201 if source_row is None:202 continue203 flatmap = {204 'id': flatmap_dir.name,205 'source': source_row[0]206 }207 created = reader._query("SELECT value FROM metadata WHERE name='created'").fetchone()208 if created is not None:209 flatmap['created'] = created[0]210 describes = reader._query("SELECT value FROM metadata WHERE name='describes'").fetchone()211 if describes is not None and describes[0]:212 flatmap['describes'] = normalise_identifier(describes[0])213 flatmap_list.append(flatmap)214 return flask.jsonify(flatmap_list)215#===============================================================================216@flatmap_blueprint.route('flatmap/<string:map_id>/')217def map(map_id):218 """219 Return a representation of a flatmap.220 :param map_id: The flatmap identifier221 :type map_id: string222 :reqheader Accept: Determines the response content223 If an SVG representation of the map exists and the :mailheader:`Accept` header224 doesn't specify a JSON response then the SVG is returned, otherwise the225 flatmap's ``index.json`` is returned.226 """227 if 'json' not in flask.request.accept_mimetypes.best:228 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, '{}.svg'.format(map_id))229 if os.path.exists(filename):230 return flask.send_file(filename, mimetype='image/svg+xml')231 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'index.json')232 return send_json(filename)233#===============================================================================234@flatmap_blueprint.route('flatmap/<string:map_id>/tilejson')235def tilejson(map_id):236 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'tilejson.json')237 return send_json(filename)238#===============================================================================239@flatmap_blueprint.route('flatmap/<string:map_id>/style')240def style(map_id):241 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'style.json')242 return send_json(filename)243#===============================================================================244@flatmap_blueprint.route('flatmap/<string:map_id>/styled')245def styled(map_id):246 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'styled.json')247 return send_json(filename)248#===============================================================================249@flatmap_blueprint.route('flatmap/<string:map_id>/markers')250def markers(map_id):251 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'markers.json')252 return send_json(filename)253#===============================================================================254'''255@flatmap_blueprint.route('flatmap/<string:map_id>/metadata')256def map_metadata(map_id):257 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'metadata.ttl')258 if os.path.exists(filename):259 return flask.send_file(filename, mimetype='text/turtle')260 else:261 flask.abort(404, 'Missing RDF metadata')262'''263#===============================================================================264@flatmap_blueprint.route('flatmap/<string:map_id>/annotations')265def map_annotations(map_id):266 return flask.jsonify(get_metadata(map_id, 'annotations'))267#===============================================================================268@flatmap_blueprint.route('flatmap/<string:map_id>/layers')269def map_layers(map_id):270 return flask.jsonify(get_metadata(map_id, 'layers'))271#===============================================================================272@flatmap_blueprint.route('flatmap/<string:map_id>/metadata')273def map_metadata(map_id):274 return flask.jsonify(get_metadata(map_id, 'metadata'))275#===============================================================================276@flatmap_blueprint.route('flatmap/<string:map_id>/pathways')277def map_pathways(map_id):278 return flask.jsonify(get_metadata(map_id, 'pathways'))279#===============================================================================280@flatmap_blueprint.route('flatmap/<string:map_id>/images/<string:image>')281def map_background(map_id, image):282 filename = os.path.join(settings['FLATMAP_ROOT'], map_id, 'images', image)283 if os.path.exists(filename):284 return flask.send_file(filename)285 else:286 flask.abort(404, 'Missing image: {}'.format(filename))287#===============================================================================288@flatmap_blueprint.route('flatmap/<string:map_id>/mvtiles/<int:z>/<int:x>/<int:y>')289def vector_tiles(map_id, z, y, x):290 try:291 mbtiles = os.path.join(settings['FLATMAP_ROOT'], map_id, 'index.mbtiles')292 tile_reader = MBTilesReader(mbtiles)293 tile_bytes = tile_reader.tile(z, x, y)294 if read_metadata(tile_reader, 'compressed'):295 tile_bytes = gzip.decompress(tile_bytes)296 return send_bytes(io.BytesIO(tile_bytes), 'application/octet-stream')297 except ExtractionError:298 pass299 except (InvalidFormatError, sqlite3.OperationalError):300 flask.abort(404, 'Cannot read tile database')301 return flask.make_response('', 204)302#===============================================================================303@flatmap_blueprint.route('flatmap/<string:map_id>/tiles/<string:layer>/<int:z>/<int:x>/<int:y>')304def image_tiles(map_id, layer, z, y, x):305 try:306 mbtiles = os.path.join(settings['FLATMAP_ROOT'], map_id, '{}.mbtiles'.format(layer))307 reader = MBTilesReader(mbtiles)308 image_bytes = io.BytesIO(reader.tile(z, x, y))309 return send_bytes(image_bytes, 'image/png')310 except ExtractionError:311 pass312 except (InvalidFormatError, sqlite3.OperationalError):313 flask.abort(404, 'Cannot read tile database')314 return send_bytes(blank_tile(), 'image/png')315#===============================================================================316@flatmap_blueprint.route('ontology/<string:ontology>')317def send_ontology(ontology):318 filename = os.path.join(settings['ONTOLOGY_ROOT'], ontology)319 if os.path.exists(filename):320 return flask.send_file(filename, mimetype='application/rdf+xml'321 if os.path.splitext(filename)[1] in ['.owl', '.xml']322 else None)323 else:324 flask.abort(404, 'Missing file: {}'.format(filename))325#===============================================================================326#===============================================================================327@knowledge_blueprint.route('query/', methods=['POST'])328def knowledge_query():329 """330 Query the flatmap server's knowledge base.331 :<json string sql: SQL code to execute332 :<jsonarr string params: any parameters for the query333 :>json array(string) keys: column names of result values334 :>json array(array(string)) values: result data rows335 :>json string error: any error message336 """337 params = flask.request.get_json()338 if params is None or 'sql' not in params:339 return flask.jsonify({'error': 'No SQL specified in request'})340 else:341 knowledge_store = KnowledgeStore(settings['FLATMAP_ROOT'])342 result = knowledge_store.query(params.get('sql'), params.get('params', []))343 knowledge_store.close()344 if 'error' in result:345 app.logger.warning('SQL: {}'.format(result['error']))346 return flask.jsonify(result)347#===============================================================================348#===============================================================================349@maker_blueprint.route('/map', methods=['POST'])350def make_map():351 """352 Generate a flatmap.353 :<json string source: the map's manifest354 :>json int process: the id of the map generation process355 :>json string map: the unique identifier for the map356 :>json string source: the map's manifest357 :>json string status: the status of the map generation process358 """359 params = flask.request.get_json()360 if params is None or 'source' not in params:361 error_abort('No source specified in data')362 map_source = params.get('source')363 maker_process = map_maker.make(map_source)364 s = {365 'process': maker_process.process_id,366 'map': maker_process.map_id,367 'source': map_source,368 'status': 'started'369 }370 return flask.jsonify(s)371@maker_blueprint.route('/log/<int:process_id>')372def make_log(process_id):373 """374 Return the log file of a map generation process.375 :param process_id: The id of a maker process376 :type process_id: int377 """378 filename = map_maker.logfile(process_id)379 if os.path.exists(filename):380 return flask.send_file(filename)381 else:382 flask.abort(404, 'Missing log file')383@maker_blueprint.route('/status/<int:process_id>')384def make_status(process_id):385 """386 Get the status of a map generation process.387 :param process_id: The id of a maker process388 :type process_id: int389 :>json int maker: the id of the map generation process390 :>json string status: the status of the map generation process391 """392 return flask.jsonify({393 'process': process_id,394 'status': map_maker.status(process_id)395 })396#===============================================================================397#===============================================================================398@viewer_blueprint.route('/')399@viewer_blueprint.route('/<path:filename>')400def viewer(filename='index.html'):401 """402 The flatmap viewer application.403 .. :quickref: viewer; Get the flatmap viewer application404 :param filename: The viewer file to get, defaults to ``index.html``405 :type filename: path406 """407 filename = os.path.join(viewer_blueprint.root_path, filename)408 if settings['MAP_VIEWER'] and os.path.exists(filename):409 return flask.send_file(filename)410 else:411 flask.abort(404)412#===============================================================================413#===============================================================================414def server():415 return wsgi_app(False)416def viewer():417 return wsgi_app(True)418#===============================================================================...
strategies.py
Source:strategies.py
...21 to_multipolygon_edges,22 to_pairs,23 to_polygon_edges,24 to_triplets)25points = coordinates_strategies.flatmap(planar.points)26segments = coordinates_strategies.flatmap(planar.segments)27segments_with_points = (coordinates_strategies28 .flatmap(cleave_in_tuples(planar.segments,29 planar.points)))30segments_strategies = coordinates_strategies.map(planar.segments)31segments_pairs = segments_strategies.flatmap(to_pairs)32multisegments = coordinates_strategies.flatmap(planar.multisegments)33multisegments_with_points = (coordinates_strategies34 .flatmap(cleave_in_tuples(planar.multisegments,35 planar.points)))36multisegments_with_segments = (coordinates_strategies37 .flatmap(cleave_in_tuples(planar.multisegments,38 planar.segments)))39to_size_three_or_more_multisegments = partial(planar.multisegments,40 min_size=3)41size_three_or_more_multisegments_with_segments = (42 (coordinates_strategies43 .flatmap(cleave_in_tuples(to_size_three_or_more_multisegments,44 planar.segments))))45def chop_segment(segment: Segment, parts_count: int) -> Sequence[Segment]:46 assert parts_count > 147 step_x, step_y = (Fraction(segment.end.x - segment.start.x) / parts_count,48 Fraction(segment.end.y - segment.start.y) / parts_count)49 start_x, start_y = segment.start.x, segment.start.y50 end_x, end_y = start_x + step_x, start_y + step_y51 result = []52 for part_index in range(parts_count):53 result.append(Segment(Point(start_x, start_y), Point(end_x, end_y)))54 start_x, start_y = end_x, end_y55 end_x, end_y = start_x + step_x, start_y + step_y56 return result57def segment_to_multisegments_with_segments(segment: Segment,58 *,59 min_size: int = 2,60 max_size: int = 10061 ) -> Strategy[Tuple[Multisegment,62 Segment]]:63 just_segment = strategies.just(segment)64 partition_sizes = strategies.integers(min_size, max_size)65 scales = strategies.integers(1, 100)66 partitions = strategies.builds(chop_segment, just_segment, partition_sizes)67 return strategies.tuples(68 (strategies.builds(scale_head,69 strategies.builds(scale_tail, partitions,70 scales),71 scales)72 .flatmap(strategies.permutations)73 .map(Multisegment)),74 just_segment)75def scale_head(segment_partition: List[Segment],76 scale: Scalar) -> List[Segment]:77 return segment_partition[:-1] + [left_scale_segment(segment_partition[-1],78 scale)]79def scale_tail(segment_partition: List[Segment],80 scale: Scalar) -> List[Segment]:81 return segment_partition[:-1] + [right_scale_segment(segment_partition[-1],82 scale)]83multisegments_with_segments |= (84 (rational_coordinates_strategies.flatmap(planar.segments)85 .flatmap(segment_to_multisegments_with_segments)))86size_three_or_more_multisegments_with_segments |= (87 (rational_coordinates_strategies.flatmap(planar.segments)88 .flatmap(partial(segment_to_multisegments_with_segments,89 min_size=3))))90multisegments_strategies = coordinates_strategies.map(planar.multisegments)91multisegments_pairs = (coordinates_strategies.map(planar.multisegments)92 .flatmap(to_pairs))93size_three_or_more_multisegment_with_multisegment = (94 (coordinates_strategies95 .flatmap(cleave_in_tuples(to_size_three_or_more_multisegments,96 planar.multisegments))))97contours = coordinates_strategies.flatmap(planar.contours)98contours_with_points = (coordinates_strategies99 .flatmap(cleave_in_tuples(planar.contours,100 planar.points)))101contours_with_segments = (coordinates_strategies102 .flatmap(cleave_in_tuples(planar.contours,103 planar.segments)))104contours_with_multisegments = (105 coordinates_strategies.flatmap(cleave_in_tuples(planar.contours,106 planar.multisegments)))107contours_with_size_three_or_more_multisegments = (108 (coordinates_strategies109 .flatmap(cleave_in_tuples(planar.contours,110 to_size_three_or_more_multisegments))))111contours_strategies = coordinates_strategies.map(planar.contours)112contours_pairs = contours_strategies.flatmap(to_pairs)113contours_triplets = contours_strategies.flatmap(to_triplets)114to_size_three_or_more_multiregions = partial(planar.multicontours,115 min_size=3)116multiregions = coordinates_strategies.flatmap(planar.multicontours)117multiregions_with_points = (coordinates_strategies118 .flatmap(cleave_in_tuples(planar.multicontours,119 planar.points)))120size_three_or_more_multiregions_with_points = (121 (coordinates_strategies122 .flatmap(cleave_in_tuples(to_size_three_or_more_multiregions,123 planar.points))))124multiregions_with_segments = (coordinates_strategies125 .flatmap(cleave_in_tuples(planar.multicontours,126 planar.segments)))127size_three_or_more_multiregions_with_segments = (128 (coordinates_strategies129 .flatmap(cleave_in_tuples(to_size_three_or_more_multiregions,130 planar.segments))))131multiregions_with_multisegments = (132 coordinates_strategies.flatmap(cleave_in_tuples(planar.multicontours,133 planar.multisegments)))134multiregions_with_size_three_or_more_multisegments = (135 (coordinates_strategies136 .flatmap(cleave_in_tuples(planar.multicontours,137 to_size_three_or_more_multisegments))))138multiregions_with_contours = (coordinates_strategies139 .flatmap(cleave_in_tuples(planar.multicontours,140 planar.contours)))141size_three_or_more_multiregions_with_contours = (142 (coordinates_strategies143 .flatmap(cleave_in_tuples(to_size_three_or_more_multiregions,144 planar.contours))))145multiregions_pairs = (coordinates_strategies.map(planar.multicontours)146 .flatmap(to_pairs))147size_three_or_more_multiregion_with_multiregion = (148 (coordinates_strategies149 .flatmap(cleave_in_tuples(to_size_three_or_more_multiregions,150 planar.multicontours))))151polygons = coordinates_strategies.flatmap(planar.polygons)152polygons_with_points = (coordinates_strategies153 .flatmap(cleave_in_tuples(planar.polygons,154 planar.points)))155polygons_with_segments = (coordinates_strategies156 .flatmap(cleave_in_tuples(planar.polygons,157 planar.segments)))158def polygon_to_polygons_with_multisegments(polygon: Polygon,159 *,160 min_size: int = 2161 ) -> Strategy[Tuple[Polygon,162 Multisegment]]:163 return strategies.tuples(strategies.just(polygon),164 sub_lists(list(to_polygon_edges(polygon)),165 min_size=min_size)166 .map(Multisegment))167polygons_with_multisegments = (168 polygons.flatmap(polygon_to_polygons_with_multisegments)169 | (coordinates_strategies170 .flatmap(cleave_in_tuples(planar.polygons, planar.multisegments))))171polygons_with_with_size_three_or_more_multisegments = (172 polygons.flatmap(partial(polygon_to_polygons_with_multisegments,173 min_size=3))174 | (coordinates_strategies175 .flatmap(cleave_in_tuples(planar.polygons,176 partial(planar.multisegments,177 min_size=3)))))178polygons_with_contours = (coordinates_strategies179 .flatmap(cleave_in_tuples(planar.polygons,180 planar.contours)))181polygons_with_multiregions = (coordinates_strategies182 .flatmap(cleave_in_tuples(planar.polygons,183 planar.multicontours)))184polygons_with_size_three_or_more_multiregions = (185 (coordinates_strategies186 .flatmap(cleave_in_tuples(planar.polygons,187 to_size_three_or_more_multiregions))))188polygons_strategies = coordinates_strategies.map(planar.polygons)189polygons_pairs = polygons_strategies.flatmap(to_pairs)190polygons_triplets = polygons_strategies.flatmap(to_triplets)191to_size_three_or_more_multipolygons = partial(planar.multipolygons,192 min_size=3)193multipolygons = coordinates_strategies.flatmap(planar.multipolygons)194multipolygons_with_points = (coordinates_strategies195 .flatmap(cleave_in_tuples(planar.multipolygons,196 planar.points)))197size_three_or_more_multipolygons_with_points = (198 (coordinates_strategies199 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,200 planar.points))))201multipolygons_with_segments = (coordinates_strategies202 .flatmap(cleave_in_tuples(planar.multipolygons,203 planar.segments)))204size_three_or_more_multipolygons_with_segments = (205 (coordinates_strategies206 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,207 planar.segments))))208def multipolygon_to_multipolygons_with_multisegments(209 multipolygon: Multipolygon,210 *,211 min_size: int = 2) -> Strategy[Tuple[Multipolygon, Multisegment]]:212 edges = list(to_multipolygon_edges(multipolygon))213 return strategies.tuples(strategies.just(multipolygon),214 sub_lists(edges,215 min_size=min_size)216 .map(Multisegment))217multipolygons_with_multisegments = (218 multipolygons.flatmap(multipolygon_to_multipolygons_with_multisegments)219 | (coordinates_strategies220 .flatmap(cleave_in_tuples(planar.multipolygons,221 planar.multisegments))))222multipolygons_with_size_three_or_more_multisegments = (223 (multipolygons224 .flatmap(partial(multipolygon_to_multipolygons_with_multisegments,225 min_size=3)))226 | (coordinates_strategies227 .flatmap(cleave_in_tuples(planar.multipolygons,228 to_size_three_or_more_multisegments))))229multipolygons_with_contours = (coordinates_strategies230 .flatmap(cleave_in_tuples(planar.multipolygons,231 planar.contours)))232size_three_or_more_multipolygons_with_contours = (233 (coordinates_strategies234 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,235 planar.contours))))236multipolygons_with_multiregions = (237 coordinates_strategies.flatmap(cleave_in_tuples(planar.multipolygons,238 planar.multicontours)))239size_three_or_more_multipolygons_with_multiregions = (240 (coordinates_strategies241 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,242 planar.multicontours))))243multipolygons_with_polygons = (coordinates_strategies244 .flatmap(cleave_in_tuples(planar.multipolygons,245 planar.polygons)))246size_three_or_more_multipolygons_with_polygons = (247 (coordinates_strategies248 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,249 planar.polygons))))250multipolygons_pairs = (coordinates_strategies.map(planar.multipolygons)251 .flatmap(to_pairs))252size_three_or_more_multipolygons_with_multipolygons = (253 (coordinates_strategies254 .flatmap(cleave_in_tuples(to_size_three_or_more_multipolygons,...
flatmap_test.py
Source:flatmap_test.py
1# coding=utf-82#3# Licensed to the Apache Software Foundation (ASF) under one or more4# contributor license agreements. See the NOTICE file distributed with5# this work for additional information regarding copyright ownership.6# The ASF licenses this file to You under the Apache License, Version 2.07# (the "License"); you may not use this file except in compliance with8# the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing, software13# distributed under the License is distributed on an "AS IS" BASIS,14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15# See the License for the specific language governing permissions and16# limitations under the License.17#18# pytype: skip-file19import unittest20import mock21from apache_beam.examples.snippets.util import assert_matches_stdout22from apache_beam.testing.test_pipeline import TestPipeline23from . import flatmap24def check_plants(actual):25 expected = '''[START plants]26ðStrawberry27ð¥Carrot28ðEggplant29ð
Tomato30ð¥Potato31[END plants]'''.splitlines()[1:-1]32 assert_matches_stdout(actual, expected)33def check_valid_plants(actual):34 expected = '''[START valid_plants]35{'icon': 'ð', 'name': 'Strawberry', 'duration': 'perennial'}36{'icon': 'ð¥', 'name': 'Carrot', 'duration': 'biennial'}37{'icon': 'ð', 'name': 'Eggplant', 'duration': 'perennial'}38{'icon': 'ð
', 'name': 'Tomato', 'duration': 'annual'}39[END valid_plants]'''.splitlines()[1:-1]40 assert_matches_stdout(actual, expected)41@mock.patch('apache_beam.Pipeline', TestPipeline)42@mock.patch(43 'apache_beam.examples.snippets.transforms.elementwise.flatmap.print', str)44class FlatMapTest(unittest.TestCase):45 def test_flatmap_simple(self):46 flatmap.flatmap_simple(check_plants)47 def test_flatmap_function(self):48 flatmap.flatmap_function(check_plants)49 def test_flatmap_lambda(self):50 flatmap.flatmap_lambda(check_plants)51 def test_flatmap_generator(self):52 flatmap.flatmap_generator(check_plants)53 def test_flatmap_multiple_arguments(self):54 flatmap.flatmap_multiple_arguments(check_plants)55 def test_flatmap_tuple(self):56 flatmap.flatmap_tuple(check_plants)57 def test_flatmap_side_inputs_singleton(self):58 flatmap.flatmap_side_inputs_singleton(check_plants)59 def test_flatmap_side_inputs_iter(self):60 flatmap.flatmap_side_inputs_iter(check_valid_plants)61 def test_flatmap_side_inputs_dict(self):62 flatmap.flatmap_side_inputs_dict(check_valid_plants)63if __name__ == '__main__':...
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!!