Common, basic, and reusable development tools
Project description
wipac-dev-tools
Common, basic, and reusable development tools
Utilities
Logging Tools
wipac_dev_tools.logging_tools.set_level()
def set_level(
level: str,
first_party_loggers: Optional[List[Union[str, logging.Logger]]] = None,
third_party_level: LoggerLevel = "WARNING",
use_coloredlogs: bool = False,
) -> None:
"""Set the level of the root logger, first-party loggers, and third-party loggers.
The root logger and first-party logger(s) are set to the same level
(`level`). The third-party loggers are non-root and non-first-party
loggers that are defined at the time of invocation. If a logger is
created after this function call, then its level defaults to its
parent (that's the root logger for non-child loggers).
Passing `use_coloredlogs=True` will import and use the `coloredlogs`
package. This will set the logger format and use colored text.
"""
wipac_dev_tools.logging_tools.log_argparse_args()
def log_argparse_args(
args: argparse.Namespace,
logger: Optional[Union[str, logging.Logger]] = None,
level: LoggerLevel = "WARNING",
) -> argparse.Namespace:
"""Log the argparse args and their values at the given level.
Return the args (Namespace) unchanged.
Example:
2022-05-13 22:37:21 fv-az136-643 my-logs[61] WARNING in_file: in_msg.pkl
2022-05-13 22:37:21 fv-az136-643 my-logs[61] WARNING out_file: out_msg.pkl
2022-05-13 22:37:21 fv-az136-643 my-logs[61] WARNING log: DEBUG
2022-05-13 22:37:21 fv-az136-643 my-logs[61] WARNING log_third_party: WARNING
"""
Environment Variable Tool(s)
wipac_dev_tools.from_environment()
def from_environment(keys: KeySpec) -> Dict[str, RetVal]:
"""Obtain configuration values from the OS environment.
Parsing Details:
Types are inferred from the default values, and casted as such:
`bool`: *(case-insensitive)*:
- `True` => ("y", "yes", "t", "true", "on", or "1")
- `False` => ("n", "no", "f", "false", "off", or "0")
- `Error` => any other string
`int`: normal cast (`int(str)`)
`float`: normal cast (`float(str)`)
`other`: no change (`str`)
Arguments:
keys - Specify the configuration values to obtain.
This can be a string, specifying a single key, such as:
config_dict = from_environment("LANGUAGE")
This can be a list of strings, specifying multiple keys,
such as:
config_dict = from_environment(["HOME", "LANGUAGE"])
This can be a dictionary that provides some default values,
and will accept overrides from the environment:
default_config = {
"HOST": "localhost",
"PORT": "8080",
"REQUIRED_FROM_ENVIRONMENT": None
}
config_dict = from_environment(default_config)
Note in this case that if 'HOST' or 'PORT' were defined in the
environment, those values would be returned in config_dict. If
the values were not defined in the environment, the default values
from default_config would be returned in config_dict.
Also note, that if 'REQUIRED_FROM_ENVIRONMENT' is not defined,
an OSError will be raised. The sentinel value of None indicates
that the configuration parameter MUST be sourced from the
environment.
Returns:
a dictionary mapping configuration keys to configuration values
Raises:
OSError - If a configuration value is requested and no default
value is provided (via a dict), to indicate that the
component's configuration is incomplete due to missing
data from the OS.
ValueError - If a type-indicated value is not a legal value
"""
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wipac-dev-tools-1.3.0.tar.gz
(13.3 kB
view hashes)
Built Distribution
Close
Hashes for wipac_dev_tools-1.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7eb4227627b79f4d316b98460088e8569edc37e4ddc6d73c7696c6d17fc41b99 |
|
MD5 | 9e7b8b02980731b9ec2ebff8abaf7053 |
|
BLAKE2b-256 | 81f348ec540dbe51696c91c8d04bc888ab3d14267599c974238ff5269aeaec1a |