cement.utils.misc

Misc utilities.

cement.utils.misc.init_defaults(*sections)[source]

Returns a standard dictionary object to use for application defaults. If sections are given, it will create a nested dict for each section name. This is sometimes more useful, or cleaner than creating an entire dict set (often used in testing).

Parameters:sections – Section keys to create nested dictionaries for.
Returns:Dictionary of nested dictionaries (sections)
Return type:dict

Example

from cement import App, init_defaults

defaults = init_defaults('myapp', 'section2', 'section3')
defaults['myapp']['debug'] = False
defaults['section2']['foo'] = 'bar
defaults['section3']['foo2'] = 'bar2'

app = App('myapp', config_defaults=defaults)
cement.utils.misc.is_true(item)[source]

Given a value, determine if it is one of [True, 'true', 'yes', 'on', '1', 1,] (note: strings are converted to lowercase before comparison).

Parameters:item – The item to convert to a boolean.
Returns:
True if item equates to a true-ish value, False
otherwise
Return type:bool
cement.utils.misc.minimal_logger(namespace, debug=False)[source]

Setup just enough for cement to be able to do debug logging. This is the logger used by the Cement framework, which is setup and accessed before the application is functional (and more importantly before the applications log handler is usable).

Parameters:namespace (str) – The logging namespace. This is generally __name__ or anything you want.
Keyword Arguments:
 debug (bool) – Toggle debug output.
Returns:A Logger object
Return type:object

Example

from cement.utils.misc import minimal_logger
LOG = minimal_logger('cement')
LOG.debug('This is a debug message')
cement.utils.misc.rando(salt=None)[source]

Generate a random MD5 hash for whatever purpose. Useful for testing or any other time that something random is required.

Parameters:salt (str) – Optional salt, if None then random.random() is used.
Returns:Random MD5 hash
Return type:str

Example

from cement.utils.misc import rando

rando('dAhn49amvnah3m')
cement.utils.misc.random() → x in the interval [0, 1).
cement.utils.misc.wrap(text, width=77, indent='', long_words=False, hyphens=False)[source]

Wrap text for cleaner output (this is a simple wrapper around textwrap.TextWrapper in the standard library).

Parameters:

text (str) – The text to wrap

Keyword Arguments:
 
  • width (int) – The max width of a line before breaking
  • indent (str) – String to prefix subsequent lines after breaking
  • long_words (bool) – Whether or not to break on long words
  • hyphens (bool) – Whether or not to break on hyphens
Returns:

The wrapped string

Return type:

str