Miscellaneous
Introduction to the Miscellaneous Utilities
Cement includes a Miscellaneous Utilities Module with helpers for common tasks that don't fit anywhere on their own.
API References:
Initialize Default Dictionaries
The included misc.init_defaults() is used to initialize a defaults dictionary for configuration settings and meta options.
from cement.utils.misc import init_defaults
# create a dict with nested dicts
CONFIG = init_defaults('myapp',
'log.logging',
'output.json')
# setup the nested dicts
CONFIG['myapp']['foo'] = 'bar'
CONFIG['log.logging']['level'] = 'INFO'
CONFIG['output.json']['overridable'] = TrueTesting True Values
When reading configuration files and other unknown data sources, we often need to convert strings to boolean. For example, a setting of true read from a configparser text based config file will be a str type, but we want it as an bool.
The misc.is_true() helper converts common true values to boolean.
Minimal Logging
Cement provides a misc.minimal_logger() helper for use outside of the App object, most notably for extensions (though they can use the application log also). This should only be used by the framework, or when the application log is not available (before application is setup maybe).
Random Strings
The misc.rando() helper is available when a random string is necessary, generally used for testing purposes.
Limit Text Line Length
When working with command lines, keeping output lines to less than 78 characters is a good best-practice. Cement provides the misc.wrap() helper to accomplish this by adding a line break \n
Last updated