return flattened
-def partition_dict(dict_, keys):
- """Return two dicts, one with `keys` the other with everything else."""
- intersection = {}
- difference = {}
- for key, value in dict_.iteritems():
- if key in keys:
- intersection[key] = value
- else:
- difference[key] = value
- return intersection, difference
-
-
def map_dict_keys(dict_, key_map):
"""Return a dict in which the dictionaries keys are mapped to new keys."""
mapped = {}
return mapped
-def subset_dict(dict_, keys):
- """Return a dict that only contains a subset of keys."""
- subset = partition_dict(dict_, keys)[0]
- return subset
-
-
def check_isinstance(obj, cls):
"""Checks that obj is of type cls, and lets PyLint infer types."""
if isinstance(obj, cls):
val == '1' or val == '0')
-def is_valid_ipv4(address):
- """valid the address strictly as per format xxx.xxx.xxx.xxx.
- where xxx is a value between 0 and 255.
- """
- parts = address.split(".")
- if len(parts) != 4:
- return False
- for item in parts:
- try:
- if not 0 <= int(item) <= 255:
- return False
- except ValueError:
- return False
- return True
-
-
def monkey_patch():
"""If the CONF.monkey_patch set as True,
this function patches a decorator
decorator("%s.%s" % (module, key), func))
-def convert_to_list_dict(lst, label):
- """Convert a value or list into a list of dicts"""
- if not lst:
- return None
- if not isinstance(lst, list):
- lst = [lst]
- return [{label: x} for x in lst]
-
-
def timefunc(func):
"""Decorator that logs how long a particular function took to execute"""
@functools.wraps(func)
LOG.exception(message)
-def make_dev_path(dev, partition=None, base='/dev'):
- """Return a path to a particular device.
-
- >>> make_dev_path('xvdc')
- /dev/xvdc
-
- >>> make_dev_path('xvdc', 1)
- /dev/xvdc1
- """
- path = os.path.join(base, dev)
- if partition:
- path += str(partition)
- return path
-
-
def total_seconds(td):
"""Local total_seconds implementation for compatibility with python 2.6"""
if hasattr(td, 'total_seconds'):