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_name
exist 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
key
value ofkw_assign()
orfield
value ofkw_assign()
.- Returns
True
ifattrib_name
exist 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
kwargs
passed into to constructor.all_rules (bool, optional) – Determines if all rules or any rules are to be matched. If
True
then all rules included inrules
must be valid to be considered a success. IfFalse
then any rule included inrules
that is valid is considered a success. DefaultFalse
.field (str, optional) –
the name of the field to assign a value. if
field
is omitted then field name is built usingkey
. If included thenkwargs_helper.field_prefix
will be ignored. Defaults to Empty string.See also: Kw_assign field,
KwArg.kwargs_helper
require (bool, optional) –
Determins if
key
is required to be in kwargs passed into to constructor. ifdefault
is passed in thenrequire
is 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
kwargs
passed into to constructor. Ifdefault
is passed in thenrequire
is 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
str
thentypes=[str]
. In this example if value is not typestr
thenTypeError
is 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
types
is included thentypes
takes priority over this arg. All rules must validate asTrue
before 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
types
is included thentypes
takes priority over this arg. Any rule that validates asTrue
results in assignment taking place. Defaults toNone
.See also: Kw_assign Rule Checking
- Raises
RuleError – If
kwargs_helper.rule_error
isTrue
and Validation ofrules_all
orrules_any
fails.TypeError – If validation of
types
fails.ReservedAttributeError – if
key
is a reserved keywordReservedAttributeError – if
field
is a reserved keyword
- Returns
True
if attribute assignment is successful; Otherwise,False
- Return type
bool
- property kw_unused_keys: Set[str]
Gets any unused keys passed into constructor via
**kwargs
This 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