etc Module (stx.etc)
Utility functions for miscellaneous tasks.
This module provides utility functions that don’t fit into other categories, such as keyboard input handling for interactive programs.
- scitex.etc.count(*, printer=<built-in function print>, sleeper=<built-in function sleep>)[source]
Print an incrementing counter forever, sleeping 1s between values.
- Parameters:
printer (callable, optional) – Output sink, defaults to the builtin
print. Injectable test seam.sleeper (callable, optional) – One-arg sleep callable, defaults to
time.sleep. Injectable so tests can run without real delay and bound the loop.
- scitex.etc.count_grids(params_grid)[source]
Return the total number of combinations in a parameter grid.
- scitex.etc.yield_grids(params_grid, random=False)[source]
Yield every parameter combination from a grid as a dict.
- Parameters:
- Yields:
dict – One combination, mapping each parameter name to a single value.
Example
>>> grid = {"a": [1, 2], "b": ["x", "y"]} >>> list(yield_grids(grid)) [{'a': 1, 'b': 'x'}, {'a': 1, 'b': 'y'}, {'a': 2, 'b': 'x'}, {'a': 2, 'b': 'y'}]
- scitex.etc.search(patterns, strings, only_perfect_match=False, as_bool=False, ensure_one=False)[source]
Search for patterns in strings using regular expressions.
- Parameters:
patterns (str or list of str) – The pattern(s) to search for. A single string or a list of strings.
strings (str or list of str) – The string(s) to search in. A single string or a list of strings.
only_perfect_match (bool, optional) – If True, only exact matches are considered (default False).
as_bool (bool, optional) – If True, return a boolean mask instead of indices (default False).
ensure_one (bool, optional) – If True, assert exactly one match is found (default False).
- Returns:
If
as_boolis False:(indices, matched_strings)whereindicesis a list of int positions of matches.If
as_boolis True:(mask, matched_strings)wheremaskis a boolean sequence (numpy array when numpy is installed, else a list) flagging matched positions.
- Return type:
Example
>>> patterns = ['orange', 'banana'] >>> strings = ['apple', 'orange', 'apple', 'apple_juice', 'banana', 'orange_juice'] >>> search(patterns, strings) ([1, 4, 5], ['orange', 'banana', 'orange_juice'])
>>> search('orange', strings) ([1, 5], ['orange', 'orange_juice'])