KwArg Class
- class kwhelp.KwArg(**kwargs)[source]
Class for assigning kwargs to autogen fields with type checking and testing
- __init__(**kwargs)[source]
Constructor
- Keyword Arguments
kwargs (dict) – dictionary of args
Example
from kwhelp import KwArg def my_method(**kwargs) -> str: kw = KwArg(**kwargs) # assign args kw.kw_assign(key='first', require=True, types=[int]) kw.kw_assign(key='second', require=True, types=[int]) kw.kw_assign(key='msg', types=[str], default='Result:') kw.kw_assign(key='end', types=[str]) _result = kw.first + kw.second if kw.is_attribute_exist('end'): return_msg = f'{kw.msg} {_result}{kw.end}' else: return_msg = f'{kw.msg} {_result}' return return_msg
- is_attribute_exist(attrib_name: str) bool[source]
Gets if
attrib_nameexist the current instance.Use this method when:
When assigning a key that is not required it may not exist in the current instance.
When assing key that are required then the field will exist in the current instance.
- Parameters
attrib_name (str) – This is usually the
keyvalue ofkw_assign()orfieldvalue ofkw_assign().- Returns
Trueifattrib_nameexist in current instance; Otherwise,False.- Return type
bool
- is_key_existing(key: str) bool[source]
Gets if the key exist in current kwargs. Basically shortcut for key in kwargs
- kw_assign(key: str, field: Optional[str] = None, require: bool = False, default: Optional[object] = <kwhelp.helper.NoThing object>, types: Optional[List[type]] = None, rules_all: Optional[List[Callable[[kwhelp.rules.IRule], bool]]] = None, rules_any: Optional[List[Callable[[kwhelp.rules.IRule], bool]]] = None) bool[source]
Assigns attribute value to current instance passed in to constructor. Attributes automatically.
- Parameters
key (str) – the key of the key, value pair that is required or optional in
kwargspassed into to constructor.all_rules (bool, optional) – Determines if all rules or any rules are to be matched. If
Truethen all rules included inrulesmust be valid to be considered a success. IfFalsethen any rule included inrulesthat is valid is considered a success. DefaultFalse.field (str, optional) –
the name of the field to assign a value. if
fieldis omitted then field name is built usingkey. If included thenkwargs_helper.field_prefixwill be ignored. Defaults to Empty string.See also: Kw_assign field,
KwArg.kwargs_helperrequire (bool, optional) –
Determins if
keyis required to be in kwargs passed into to constructor. ifdefaultis passed in thenrequireis ignored. Defaults toFalse.See also: Kw_assign Require Arg
default (object, optional) –
default value to assign to key attribute if no value is found in
kwargspassed into to constructor. Ifdefaultis passed in thenrequireis ignored. Defaults toNO_THING.See also: Kw_assign Default Value
types (List[type], optional) –
a type list of one or more types that the value of the key value pair must match. For example if a value is required to be only
strthentypes=[str]. In this example if value is not typestrthenTypeErroris raised If value is required to be str or int thentypes=[str, int]. Defaults toNone.See also: Kw_assign Type Checking
rules_all (List[Callable[[IRule], bool]], optional) –
List of rules that must be passed before assignment can take place. If
typesis included thentypestakes priority over this arg. All rules must validate asTruebefore assignment takes place. Defaults toNone.See also: Kw_assign Rule Checking
rules_any (List[Callable[[IRule], bool]], optional) –
List of rules that must be passed before assignment can take place. If
typesis included thentypestakes priority over this arg. Any rule that validates asTrueresults in assignment taking place. Defaults toNone.See also: Kw_assign Rule Checking
- Raises
RuleError – If
kwargs_helper.rule_errorisTrueand Validation ofrules_allorrules_anyfails.TypeError – If validation of
typesfails.ReservedAttributeError – if
keyis a reserved keywordReservedAttributeError – if
fieldis a reserved keyword
- Returns
Trueif attribute assignment is successful; Otherwise,False- Return type
bool
- property kw_unused_keys: Set[str]
Gets any unused keys passed into constructor via
**kwargsThis would be a set of keys that were never used passed into the constructor.
- property kwargs_helper: kwhelp.KwargsHelper
Get instance of KwargsHelper used to add fields current instance