yahp.utils
Interactive
- yahp.utils.interactive.query_with_default(name: str, default_response: Optional[str] = None) str [source]
Query for a string response on the CLI.
- Parameters
question (str) – Question to ask.
default_response (Optional[str], optional) – Default response. Set to None to require a response.
- Returns
The response.
- Return type
str
- yahp.utils.interactive.query_with_options(name: str, options: List[str], default_response: Optional[str], multiple_ok: Literal[False]) str [source]
- yahp.utils.interactive.query_with_options(name: str, options: List[str], default_response: Optional[str], multiple_ok: Literal[True]) List[str]
Interactively queries the user to select from a list of options.
- Parameters
name (str) – Prompt for the user.
options (List[str]) – List of options.
default_response (Optional[str]) – Default response. Set to None to require a response.
multiple_ok (bool) – Whether the user can specify multiple options via a comma-seperated response.
- Returns
The response, or if
multiple_ok
is True, a list of responses.
Iter Helpers
- class yahp.utils.iter_helpers.ListOfSingleItemDict(data: List)[source]
Simple list wrapper for a list of single-item dicts
This enables string-based gets and sets. If there are duplicate keys in the list, the first one is retrieved/modified.
- yahp.utils.iter_helpers.ensure_tuple(x: Union[T, Tuple[T, ...], List[T], Dict[Any, T]]) Tuple[T, ...] [source]
Converts
x
to atuple
- Parameters
x (Any) – If
x
is a tuple, it is returned as-is. Ifx
is a list, it is converted to a tuple and returned. Ifx
is a dict, its values are converted to a tuple and returned. Otherwise,x
: is wrapped as a one-element tuple and returned.- Returns
x
, as a tuple.- Return type
Tuple[Any, …]
- yahp.utils.iter_helpers.extract_only_item_from_dict(val: Dict[K, V]) Tuple[K, V] [source]
Extracts the only item from a dict and returns it .
- Parameters
val (Dict[K, V]) – A dictionary which should contain only one entry
- Raises
ValueError – Raised if the dictionary does not contain 1 item
- Returns
The key, value pair of the only item
- Return type
Tuple[K, V]
- yahp.utils.iter_helpers.is_list_of_single_item_dicts(obj: JSON) bool [source]
Whether the provided object is a list of single-item dictionaries
- Parameters
obj (List[Dict]) –
- Returns
True if
obj
is a list of single-item dictionaries
- yahp.utils.iter_helpers.list_to_deduplicated_dict(list_of_dict: List[JSON], allow_str: bool = False, separator: str = '+') Dict[str, JSON] [source]
Converts a list of single-item dictionaries to a dictionary, deduplicating keys along the way
- Parameters
list_of_dict (List[Dict[str, Any]]) – A list of single-item dictionaries
allow_str (bool, optional) – If True, list can contain strings, which will be added as keys with None values. Defaults to False.
separator (str, optional) – The separator to use for deduplication. Default ‘+’.
- Returns
Deduplicated dictionary
- Return type
Dict[str, Dict]
Type Helpers
- class yahp.utils.type_helpers.HparamsType(item: Type[Any])[source]
Wrapper to parse type annotations and determine type of field.
HparamsType parses typing annotations and provides convenience methods to determine the field types.
- Parameters
item (type) – Type annotation to parse.
- types
The allowed types for this annotation, as a list. If the annotation is
List[X]
orOptional[X]
, thenX
is stored in this attributed. If the annotation is aUnion[X, Y]
, then this attribute is[X, Y]
. None is never stored here; instead, seeis_optional
.- Type
List[Type]
- is_optional
Whether the annotation allows None.
- Type
bool
- is_list
Whether the annotation is a list.
- Type
bool
- convert(val: Any, field_name: str, *, wrap_singletons: bool = True) Any [source]
Attempt to convert an item into a type allowed by the annotation.
- Parameters
val (Any) – Item to convert.
field_name (str) – Name for field being converted.
wrap_singletons (bool, optional) – If True (the default) and the field is a list, singletons will be wrapped into a list. Otherwise, raise a
TypeError
.
- Raises
ValueError – Raised if
val
is None, but the annotation does not permit None.TypeError – Raised if
val
cannot be converted into a type specified by the annotation.
- Returns
The converted item.
- property is_boolean: bool
Whether the annotation allows for a
bool
, or a list ofbool
.
- property is_enum: bool
Whether the annotation allows for a subclass of
Enum
, or a list ofEnum
.
- property is_json_dict: bool
Whether it is a JSON Dictionary.
- property is_primitive: bool
Whether the annotation allows for a
bool
,int
,str
, orfloat
, a list of such types, or is alwaysNone
.
- property is_recursive: bool
Whether the datatype is recursive (i.e is not JSON, a primitive, or an enum)
- yahp.utils.type_helpers.get_default_value(f: Field[Any]) Any [source]
Returns an instance of a default value for a field.
- Parameters
f (Field) – The field.
- yahp.utils.type_helpers.is_field_required(f: Field[Any]) bool [source]
Returns whether a field is required (i.e. does not have a default value).
- Parameters
f (Field) – The field.