Classes and functions independent from a specific task. Can also be used externally.
OptionalModule
(module_name
:str
, name
:str
=None
, package
:str
=None
)
optional_import
(caller
:str
, module
:str
, name
:str
=None
, package
:str
=None
)
Optional import a module.
If module do not exist, return None but not submodules in a package.
module
: the module to import
return: The imported module if exist, None else.
import_modules
(package
)
Dynamically import all modules but not submodules in a package.
package
: the package to import all modules
list_subclasses
(package
, base_class
)
Dynamically import all modules in a package and scan for all subclasses of a base class.
package
: the package to import
base_class
: the base class to scan for subclasses
return: a dictionary of possible subclasses with class name as key and class type information as value
all_subclasses
(base_class
)
Scan recursively for all subclasses of a base class.
base_class
: the base class to scan for subclasses
return: a set of subclasses type information
parse_known_help
(argv
)
Parse the command line arguments for help parameter and decide,
if the help parameter should be assigned to current argument parser.
argv
: the command line arguments to parse
root_parser
: True if the arguments are assigned to the root parser, else False
return: a tuple containing [known, rest], where known are the arguments assigned to current parser
parse_known_args_with_help
(parser
, argv
)
Parse the command line arguments and validate, if help parameter should be assigned to current argument parser.
If the help parameter is not assigned to current parser, then keep it in the remaining argv.
parser
: the argument parser to handle
return: a tuple containing [args, argv], where args are the consumed arguments and argv are the remaining arguments
assign_arg_prefix
(arg_name
, prefix
=None
)
Assign a parameter prefix to a given argument name. (e.g --prefix_)
arg_name
: the argument name to prefix
prefix
: the prefix
return: the prefixed argument name
input_feedback
(msg
, choice
, choices
)
User input request wrapper.
msg
: the message to display
choice
: if previous choice exist
choices
: the possible choices
:return: the choice input