msticpy.common.utility.types module

Utility classes and functions.

class msticpy.common.utility.types.ImportPlaceholder(name: str, required_pkgs: List[str])

Bases: object

Placeholder class for optional imports.

Initialize class with imported item name and reqd. packages.

class msticpy.common.utility.types.ParseableEnum

Bases: object

Mix-in class for parseable Enum sub-classes.

parse(value: str)

Return enumeration matching (case-insensitive) string value.

class msticpy.common.utility.types.SingletonArgsClass(wrapped_cls)

Bases: SingletonClass

SingletonArgs decorator class.

Notes

Using this decorator on a class enforces the following behavior:

  • First instantiation of class will work as normal

  • Subsequent attempts with the same set/values of kwargs will just return the original class

  • Instantiation of the class with a different set of kwargs will instantiate a new class.

  • The class method current() will always return the last instance of the class.

Instantiate the class wrapper.

current()

Return the current instance of the wrapped class.

class msticpy.common.utility.types.SingletonClass(wrapped_cls)

Bases: object

Singleton decorator class.

Notes

Using this decorator on a class enforces the following behavior:

  • First instantiation of class will work as normal

  • Subsequent attempts with the same set/values of kwargs will just return the original class

  • The class method current() will always return the last instance of the class.

Instantiate the class wrapper.

current()

Return the current instance of the wrapped class.

msticpy.common.utility.types.arg_to_list(arg: str | List[str], delims=',; ') List[str]

Convert an optional list/str/str with delims into a list.

Parameters:
  • arg (Union[str, List[str]]) – A string, delimited string or list

  • delims (str, optional) – The default delimiters to use, by default “,; “

Returns:

List of string components

Return type:

List[str]

Raises:

TypeError – If arg is not a string or list

msticpy.common.utility.types.check_kwarg(arg_name: str, legal_args: List[str])

Check argument names against a list.

Parameters:
  • arg_name (str) – Argument to check

  • legal_args (List[str]) – List of possible arguments.

Raises:

NameError – If the argument is not legal. If the arg_name is a close match to one or more, legal_args these are returned in the exception.

msticpy.common.utility.types.check_kwargs(supplied_args: Dict[str, Any], legal_args: List[str])

Check all kwargs names against a list.

Parameters:
  • supplied_args (Dict[str, Any]) – Arguments to check

  • legal_args (List[str]) – List of possible arguments.

Raises:

NameError – If any of the arguments are not legal. If the an arg is a close match to one or more legal_args, these are returned in the exception.

msticpy.common.utility.types.checked_kwargs(legal_args: Iterable[str])

Decorate function to check kwargs names against legal arg names.

Parameters:

legal_args (Iterable[str]) – Iterable of possible arguments.

Raises:

NameError – If any of the arguments are not legal. If the an arg is a close match to one or more legal_args, these are returned in the exception.

Notes

The checking is done against the union of legal_args and any named arguments of the wrapped function.

msticpy.common.utility.types.collapse_dicts(*dicts: Dict[Any, Any]) Dict[Any, Any]

Merge multiple dictionaries - later dicts have higher precedence.

msticpy.common.utility.types.enum_parse(enum_cls: Type[EnumType], value: str) EnumType | None

Try to parse a string value to an Enum member.

Parameters:
  • enum_cls (EnumType) – The Enum type to check against

  • value (str) – The enum value to parse

Returns:

The enumeration value matching value or None

Return type:

Optional[EnumType]

Raises:

TypeError – If something other than an Enum subclass is passed.

msticpy.common.utility.types.export(obj: Callable)

Decorate function or class to export to __all__.

msticpy.common.utility.types.singleton(cls)

Class decorator for singleton classes.