Python typing.Generator() Examples
The following are 30
code examples of typing.Generator().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
typing
, or try the search function
.
Example #1
Source File: serialization.py From schemathesis with MIT License | 10 votes |
def _serialize_openapi3(definitions: DefinitionList) -> Generator[Optional[Callable], None, None]: """Different collection styles for Open API 3.0.""" for definition in definitions: name = definition["name"] if "content" in definition: # https://swagger.io/docs/specification/describing-parameters/#schema-vs-content options = iter(definition["content"].keys()) media_type = next(options, None) if media_type == "application/json": yield to_json(name) else: # Simple serialization style = definition.get("style") explode = definition.get("explode") type_ = definition.get("schema", {}).get("type") if definition["in"] == "path": yield from _serialize_path_openapi3(name, type_, style, explode) elif definition["in"] == "query": yield from _serialize_query_openapi3(name, type_, style, explode) elif definition["in"] == "header": yield from _serialize_header_openapi3(name, type_, explode) elif definition["in"] == "cookie": yield from _serialize_cookie_openapi3(name, type_, explode)
Example #2
Source File: serialization.py From schemathesis with MIT License | 8 votes |
def _serialize_query_openapi3( name: str, type_: str, style: Optional[str], explode: Optional[bool] ) -> Generator[Optional[Callable], None, None]: if type_ == "object": if style == "deepObject": yield deep_object(name) if style is None or style == "form": if explode is False: yield comma_delimited_object(name) if explode is True: yield extracted_object(name) elif type_ == "array" and explode is False: if style == "pipeDelimited": yield delimited(name, delimiter="|") if style == "spaceDelimited": yield delimited(name, delimiter=" ") if style is None or style == "form": # "form" is the default style yield delimited(name, delimiter=",")
Example #3
Source File: core.py From schemathesis with MIT License | 8 votes |
def execute(self) -> Generator[events.ExecutionEvent, None, None]: """Common logic for all runners.""" results = TestResultSet() initialized = events.Initialized.from_schema(schema=self.schema) yield initialized for event in self._execute(results): if ( self.exit_first and isinstance(event, events.AfterExecution) and event.status in (Status.error, Status.failure) ): break yield event yield events.Finished.from_results(results=results, running_time=time.monotonic() - initialized.start_time)
Example #4
Source File: parser.py From schemathesis with MIT License | 7 votes |
def _parse(expr: str) -> Generator[nodes.Node, None, None]: tokens = lexer.tokenize(expr) brackets_stack: List[str] = [] for token in tokens: if token.is_string: yield nodes.String(token.value) elif token.is_variable: yield from _parse_variable(tokens, token, expr) elif token.is_left_bracket: if brackets_stack: raise RuntimeExpressionError("Nested embedded expressions are not allowed") brackets_stack.append("{") elif token.is_right_bracket: if not brackets_stack: raise RuntimeExpressionError("Unmatched bracket") brackets_stack.pop() if brackets_stack: raise RuntimeExpressionError("Unmatched bracket")
Example #5
Source File: paging.py From tekore with MIT License | 7 votes |
def all_items( self, page: Paging ) -> Generator[Model, None, None]: """ Retrieve all items from all pages of a paging. Request and yield new (next) items until the end of the paging. The items in the paging that was given as an argument are yielded first. Parameters ---------- page paging object Returns ------- Generator all items within a paging """ if self.is_async: return self._async_all_items(page) else: return self._sync_all_items(page)
Example #6
Source File: paging.py From tekore with MIT License | 7 votes |
def all_pages(self, page: Paging) -> Generator[Paging, None, None]: """ Retrieve all pages of a paging. Request and yield new (next) pages until the end of the paging. The paging that was given as an argument is yielded as the first result. Parameters ---------- page paging object Returns ------- Generator all pages within a paging """ if self.is_async: return self._async_all_pages(page) else: return self._sync_all_pages(page)
Example #7
Source File: formatter.py From Twitch-Chat-Downloader with MIT License | 7 votes |
def use(self, format_name: str) -> Tuple[Generator[Tuple[str, Comment], None, None], str]: """ Use format :param format_name: :return: tuple(Line, comment), output """ if format_name not in Settings().config['formats']: print('Invalid format name') exit(1) if format_name == 'srt': return SRT(self.video).use() elif format_name == 'ssa': return SSA(self.video).use() else: return Custom(self.video, format_name).use()
Example #8
Source File: srt.py From Twitch-Chat-Downloader with MIT License | 7 votes |
def subtitles(self, comments: Comments) -> Generator[Tuple[str, Comment], None, None]: """ Subtitle generator :param comments: Comments to turn into subtitles :return: Generator with subtitles and subtitle data """ for index, comment in enumerate(comments): # Stat and stop timestamps. Add a millisecond for timedelta to include millisecond digits start = datetime.timedelta(seconds=comment.content_offset_seconds) stop: datetime.timedelta = start + datetime.timedelta(milliseconds=self.format_dictionary['duration']) # Format message message: str = Pipe(self.format_dictionary['comments']).comment(comment.data) # Subtitle variables # Subtract the last three milliseconds form timestamp (required by SRT) subtitle: dict = { 'index': index + 1, 'start': SRT.format_timestamp(start), 'stop': SRT.format_timestamp(stop), 'message': message } yield '{index}\n{start} --> {stop}\n{message}\n'.format_map(SafeDict(subtitle)), comment
Example #9
Source File: operation.py From vergeml with MIT License | 6 votes |
def process(self, sample: Sample, ops=List['BaseOperation']) -> Generator[Sample, None, None]: """Complete a processing step in the pipeline and run the next one. :param sample: The sample to be processed :param ops: The next operations to run :return: A generator yielding samples The process function is expected to first transform the sample and then to call the next BaseOperation, yielding the resulting sample as a return value. """ raise NotImplementedError
Example #10
Source File: client.py From gql with MIT License | 6 votes |
def subscribe( self, document: DocumentNode, *args, **kwargs ) -> Generator[Dict, None, None]: """Execute a GraphQL subscription with a python generator. We need an async transport for this functionality. """ async_generator = self.subscribe_async(document, *args, **kwargs) loop = asyncio.get_event_loop() assert not loop.is_running(), ( "Cannot run client.subscribe if an asyncio loop is running." " Use subscribe_async instead." ) try: while True: result = loop.run_until_complete(async_generator.__anext__()) yield result except StopAsyncIteration: pass
Example #11
Source File: core.py From schemathesis with MIT License | 6 votes |
def _run_tests( # pylint: disable=too-many-arguments self, maker: Callable, template: Callable, settings: hypothesis.settings, seed: Optional[int], recursion_level: int = 0, **kwargs: Any, ) -> Generator[events.ExecutionEvent, None, None]: """Run tests and recursively run additional tests.""" if recursion_level > self.stateful_recursion_limit: return for endpoint, test in maker(template, settings, seed): feedback = Feedback(self.stateful, endpoint) for event in run_test(endpoint, test, feedback=feedback, recursion_level=recursion_level, **kwargs): yield event if isinstance(event, events.Interrupted): return # Additional tests, generated via the `feedback` instance yield from self._run_tests( feedback.get_stateful_tests, template, settings, seed, recursion_level=recursion_level + 1, **kwargs )
Example #12
Source File: inflection_distribution.py From turkish-morphology with Apache License 2.0 | 5 votes |
def _analyze(token: str) -> Generator[analysis_pb2.Analysis, None, None]: human_readables = analyze.surface_form(token, use_proper_feature=False) yield from map(decompose.human_readable_analysis, human_readables)
Example #13
Source File: generate.py From turkish-morphology with Apache License 2.0 | 5 votes |
def _symbol_indices(analysis: _Analysis, symbol_table: _SymbolTable) -> Generator[int, None, None]: """Generates the label indices for the symbols that construct the analysis.""" human_readable = pretty_print.analysis(analysis) symbols = _SYMBOLS_REGEX.findall(human_readable) yield from map(symbol_table.find, symbols)
Example #14
Source File: core.py From schemathesis with MIT License | 5 votes |
def _execute(self, results: TestResultSet) -> Generator[events.ExecutionEvent, None, None]: raise NotImplementedError
Example #15
Source File: derivational_morphemes.py From turkish-morphology with Apache License 2.0 | 5 votes |
def _analyze(token: str) -> Generator[analysis_pb2.Analysis, None, None]: human_readables = analyze.surface_form(token, use_proper_feature=False) yield from map(decompose.human_readable_analysis, human_readables)
Example #16
Source File: serialization.py From schemathesis with MIT License | 5 votes |
def _serialize_cookie_openapi3( name: str, type_: str, explode: Optional[bool] ) -> Generator[Optional[Callable], None, None]: # Cookie parameters always use the "form" style if explode and type_ in ("array", "object"): # `explode=true` doesn't make sense # I.e. we can't create multiple values for the same cookie # We use the same behavior as in the examples - https://swagger.io/docs/specification/serialization/ # The item is removed yield nothing(name) if explode is False: if type_ == "array": yield delimited(name, delimiter=",") if type_ == "object": yield comma_delimited_object(name)
Example #17
Source File: serialization.py From schemathesis with MIT License | 5 votes |
def _serialize_header_openapi3( name: str, type_: str, explode: Optional[bool] ) -> Generator[Optional[Callable], None, None]: # Header parameters always use the "simple" style, that is, comma-separated values if type_ == "array": yield delimited(name, delimiter=",") if type_ == "object": if explode is False: yield comma_delimited_object(name) if explode is True: yield delimited_object(name)
Example #18
Source File: test_console_aplication.py From clikit with MIT License | 5 votes |
def config(): # type: () -> Generator[ApplicationConfig] config = ApplicationConfig() config.set_catch_exceptions(False) config.set_terminate_after_run(False) config.set_io_factory( lambda app, args, input_stream, output_stream, error_stream: IO( Input(input_stream), Output(output_stream), Output(error_stream) ) ) yield config
Example #19
Source File: parser.py From turkish-morphology with Apache License 2.0 | 5 votes |
def _normalize(rule_definitions: Iterable[_RuleDefinition] ) -> Generator[_RuleDefinition, None, None]: """Normalizes the tokens of morphotactics rewrite rule definition. This function converts the 'from_state' and 'to_state' values to uppercase, and all bracketed 'output' and 'input' labels to lowercase (e.g. the rewrite rule 'state-1 StAtE-2 +MetaMorpheme[Cat=Val] <EPS>' is normalized to 'STATE-1 STATE-2 +MetaMorpheme[Cat=Val] <eps>'). Normalization is done in-place. Args: rule_definitions: morphotactics rule definitions whose tokens will be normalized. Yields: Morphotactics rewrite rule definitions whose tokens are normalized. """ def _bracketed(token: str) -> bool: return token.startswith("<") and token.endswith(">") def _get_normalized(rule_definition: _RuleDefinition) -> _RuleDefinition: from_state, to_state, input_, output = rule_definition rule_definition[0] = from_state.upper() rule_definition[1] = to_state.upper() rule_definition[2] = input_.lower() if _bracketed(input_) else input_ rule_definition[3] = output.lower() if _bracketed(output) else output return rule_definition yield from (_get_normalized(rd) for rd in rule_definitions)
Example #20
Source File: serialization.py From schemathesis with MIT License | 5 votes |
def make_serializer( func: Callable[[DefinitionList], Generator[Optional[Callable], None, None]] ) -> Callable[[DefinitionList], Optional[Callable]]: """A maker function to avoid code duplication.""" def _wrapper(definitions: DefinitionList) -> Optional[Callable]: conversions = list(func(definitions)) if conversions: return compose(*[conv for conv in conversions if conv is not None]) return None return _wrapper
Example #21
Source File: cassettes.py From schemathesis with MIT License | 5 votes |
def replay( cassette: Dict[str, Any], id_: Optional[str] = None, status: Optional[str] = None, uri: Optional[str] = None, method: Optional[str] = None, ) -> Generator[Replayed, None, None]: """Replay saved interactions.""" session = requests.Session() for interaction in filter_cassette(cassette["http_interactions"], id_, status, uri, method): request = get_prepared_request(interaction["request"]) response = session.send(request) # type: ignore yield Replayed(interaction, response)
Example #22
Source File: callbacks.py From schemathesis with MIT License | 5 votes |
def reraise_format_error(raw_value: str) -> Generator[None, None, None]: try: yield except ValueError: raise click.BadParameter(f"Should be in KEY:VALUE format. Got: {raw_value}")
Example #23
Source File: __init__.py From schemathesis with MIT License | 5 votes |
def execute( # pylint: disable=too-many-arguments prepared_runner: Generator[events.ExecutionEvent, None, None], workers_num: int, show_errors_tracebacks: bool, store_network_log: Optional[click.utils.LazyFile], junit_xml: Optional[click.utils.LazyFile], verbosity: int, ) -> None: """Execute a prepared runner by drawing events from it and passing to a proper handler.""" handlers: List[EventHandler] = [] if junit_xml is not None: handlers.append(JunitXMLHandler(junit_xml)) if store_network_log is not None: # This handler should be first to have logs writing completed when the output handler will display statistic handlers.append(cassettes.CassetteWriter(store_network_log)) handlers.append(get_output_handler(workers_num)) execution_context = ExecutionContext( workers_num=workers_num, show_errors_tracebacks=show_errors_tracebacks, cassette_file_name=store_network_log.name if store_network_log is not None else None, junit_xml_file=junit_xml.name if junit_xml is not None else None, verbosity=verbosity, ) GLOBAL_HOOK_DISPATCHER.dispatch("after_init_cli_run_handlers", HookContext(), handlers, execution_context) try: for event in prepared_runner: for handler in handlers: handler.handle_event(execution_context, event) except click.exceptions.Exit: raise except Exception as exc: for handler in handlers: handler.shutdown() if isinstance(exc, click.Abort): # To avoid showing "Aborted!" message, which is the default behavior in Click sys.exit(1) raise
Example #24
Source File: pytest_plugin.py From schemathesis with MIT License | 5 votes |
def _gen_items(self, endpoint: Endpoint) -> Generator[Function, None, None]: """Generate all items for the given endpoint. Could produce more than one test item if parametrization is applied via ``pytest.mark.parametrize`` or ``pytest_generate_tests``. This implementation is based on the original one in pytest, but with slight adjustments to produce tests out of hypothesis ones. """ name = self._get_test_name(endpoint) funcobj = self._make_test(endpoint) cls = self._get_class_parent() definition = create(FunctionDefinition, name=self.name, parent=self.parent, callobj=funcobj) fixturemanager = self.session._fixturemanager fixtureinfo = fixturemanager.getfixtureinfo(definition, funcobj, cls) metafunc = self._parametrize(cls, definition, fixtureinfo) if not metafunc._calls: yield create(SchemathesisFunction, name=name, parent=self.parent, callobj=funcobj, fixtureinfo=fixtureinfo) else: fixtures.add_funcarg_pseudo_fixture_def(self.parent, metafunc, fixturemanager) fixtureinfo.prune_dependency_tree() for callspec in metafunc._calls: subname = "{}[{}]".format(name, callspec.id) yield create( SchemathesisFunction, name=subname, parent=self.parent, callspec=callspec, callobj=funcobj, fixtureinfo=fixtureinfo, keywords={callspec.id: True}, originalname=name, )
Example #25
Source File: models.py From schemathesis with MIT License | 5 votes |
def cookie_handler(client: werkzeug.Client, cookies: Optional[Cookies]) -> Generator[None, None, None]: """Set cookies required for a call.""" if not cookies: yield else: for key, value in cookies.items(): client.set_cookie("localhost", key, value) yield for key in cookies: client.delete_cookie("localhost", key)
Example #26
Source File: utils.py From schemathesis with MIT License | 5 votes |
def capture_hypothesis_output() -> Generator[List[str], None, None]: """Capture all output of Hypothesis into a list of strings. It allows us to have more granular control over Schemathesis output. Usage:: @given(i=st.integers()) def test(i): assert 0 with capture_hypothesis_output() as output: test() # hypothesis test # output == ["Falsifying example: test(i=0)"] """ output = [] def get_output(value: str) -> None: # Drop messages that could be confusing in the Schemathesis context if value.startswith(IGNORED_PATTERNS): return output.append(value) # the following context manager is untyped with with_reporter(get_output): # type: ignore yield output
Example #27
Source File: schemas.py From schemathesis with MIT License | 5 votes |
def get_all_tests( self, func: Callable, settings: Optional[hypothesis.settings] = None, seed: Optional[int] = None ) -> Generator[Tuple[Endpoint, Union[Callable, InvalidSchema]], None, None]: """Generate all endpoints and Hypothesis tests for them.""" test: Union[Callable, InvalidSchema] for endpoint in self.get_all_endpoints(): test = make_test_or_exception(endpoint, func, settings, seed) yield endpoint, test
Example #28
Source File: schemas.py From schemathesis with MIT License | 5 votes |
def get_all_endpoints(self) -> Generator[Endpoint, None, None]: raise NotImplementedError
Example #29
Source File: binary_tree.py From algo with Apache License 2.0 | 5 votes |
def post_order(root: Optional[TreeNode[T]]) -> Generator[T, None, None]: if root: yield from post_order(root.left) yield from post_order(root.right) yield root.val
Example #30
Source File: serialization.py From schemathesis with MIT License | 5 votes |
def _serialize_swagger2(definitions: DefinitionList) -> Generator[Optional[Callable], None, None]: """Different collection formats for Open API 2.0.""" for definition in definitions: name = definition["name"] collection_format = definition.get("collectionFormat", "csv") type_ = definition.get("type") if definition["in"] != "body" and type_ in ("array", "object"): if collection_format == "csv": yield delimited(name, delimiter=",") if collection_format == "ssv": yield delimited(name, delimiter=" ") if collection_format == "tsv": yield delimited(name, delimiter="\t") if collection_format == "pipes": yield delimited(name, delimiter="|")