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 of kw_assign() or field value of kw_assign().

Returns

True if attrib_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 in rules must be valid to be considered a success. If False then any rule included in rules that is valid is considered a success. Default False.

  • field (str, optional) –

    the name of the field to assign a value. if field is omitted then field name is built using key. If included then kwargs_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. if default is passed in then require is ignored. Defaults to False.

    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. If default is passed in then require is ignored. Defaults to NO_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 then types=[str]. In this example if value is not type str then TypeError is raised If value is required to be str or int then types=[str, int]. Defaults to None.

    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 then types takes priority over this arg. All rules must validate as True before assignment takes place. Defaults to None.

    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 then types takes priority over this arg. Any rule that validates as True results in assignment taking place. Defaults to None.

    See also: Kw_assign Rule Checking

Raises
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