python-benedict/benedict/dicts/__init__.py

150 lines
4.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2019-09-10 14:58:26 +00:00
from benedict.dicts.io import IODict
from benedict.dicts.keypath import KeypathDict
from benedict.dicts.parse import ParseDict
2019-10-03 16:15:44 +00:00
from benedict.utils import dict_util
2019-10-03 16:15:44 +00:00
class benedict(IODict, KeypathDict, ParseDict):
def __init__(self, *args, **kwargs):
2019-12-12 17:24:16 +00:00
"""
Constructs a new instance.
"""
super(benedict, self).__init__(*args, **kwargs)
2019-10-03 16:15:44 +00:00
def clean(self, strings=True, dicts=True, lists=True):
2019-12-12 17:24:16 +00:00
"""
Clean the current dict instance removing all empty values: None, '', {}, [], ().
If strings, dicts or lists flags are False, related empty values will not be deleted.
"""
2019-10-03 16:15:44 +00:00
dict_util.clean(self, strings=strings, dicts=dicts, lists=lists)
def clone(self):
2019-12-12 17:24:16 +00:00
"""
Creates and return a clone of the current dict instance (deep copy).
"""
2019-10-03 16:15:44 +00:00
return dict_util.clone(self)
def copy(self):
2019-12-12 17:24:16 +00:00
"""
Creates and return a copy of the current instance (shallow copy).
"""
return benedict(super(benedict, self).copy())
def deepcopy(self):
2019-12-12 17:24:16 +00:00
"""
Alias of 'clone' method.
"""
2019-10-03 16:15:44 +00:00
return self.clone()
def deepupdate(self, other, *args):
2019-12-12 17:24:16 +00:00
"""
Alias of 'merge' method.
"""
2019-10-03 16:15:44 +00:00
self.merge(other, *args)
def dump(self, data=None):
2019-12-12 17:24:16 +00:00
"""
Return a readable string representation of any dict/list.
This method can be used both as static method or instance method.
"""
2019-10-03 16:15:44 +00:00
return dict_util.dump(data or self)
def filter(self, predicate):
2019-12-12 17:24:16 +00:00
"""
Return a new filtered dict using the given predicate function.
Predicate function receives key, value arguments and should return a bool value.
"""
2019-10-03 16:15:44 +00:00
return dict_util.filter(self, predicate)
2019-07-02 13:15:31 +00:00
2019-09-12 15:04:22 +00:00
def flatten(self, separator='_'):
2019-12-12 17:24:16 +00:00
"""
Return a new flatten dict using the given separator to concat nested dict keys.
"""
2019-10-03 16:15:44 +00:00
return dict_util.flatten(self, separator)
2019-09-12 15:04:22 +00:00
def invert(self, flat=False):
2019-12-12 17:24:16 +00:00
"""
Return a new inverted dict, where values become keys and keys become values.
Since multiple keys could have the same value, each value will be a list of keys.
If flat is True each value will be a single value (use this only if values are unique).
"""
return dict_util.invert(self, flat)
def items_sorted_by_keys(self, reverse=False):
2019-12-12 17:24:16 +00:00
"""
Return items (key/value list) sorted by keys.
If reverse is True, the list will be reversed.
"""
return dict_util.items_sorted_by_keys(self, reverse=reverse)
def items_sorted_by_values(self, reverse=False):
2019-12-12 17:24:16 +00:00
"""
Return items (key/value list) sorted by values.
If reverse is True, the list will be reversed.
"""
return dict_util.items_sorted_by_values(self, reverse=reverse)
def keypaths(self):
2019-12-12 17:24:16 +00:00
"""
Return a list of all keypaths in the dict.
"""
sep = self._keypath_separator or '.'
return dict_util.keypaths(self, separator=sep)
2019-10-03 16:15:44 +00:00
def merge(self, other, *args):
2019-12-12 17:24:16 +00:00
"""
Merge one or more dict objects into current instance (deepupdate).
Sub-dictionaries will be merged toghether.
"""
2019-10-07 10:03:01 +00:00
dict_util.merge(self, other, *args)
2019-10-04 13:54:28 +00:00
def move(self, key_src, key_dest):
2019-12-12 17:24:16 +00:00
"""
Move a dict instance value item from 'key_src' to 'key_dst'.
It can be used to rename a key.
If key_dst exists, its value will be overwritten.
"""
2019-10-07 10:03:01 +00:00
dict_util.move(self, key_src, key_dest)
2019-10-04 13:54:28 +00:00
def remove(self, keys, *args):
2019-12-12 17:24:16 +00:00
"""
Remove multiple keys from the current dict instance.
It is possible to pass a single key or more keys (as list or *args).
"""
2019-10-07 10:03:01 +00:00
dict_util.remove(self, keys, *args)
2019-07-19 08:59:44 +00:00
def standardize(self):
2019-12-12 17:24:16 +00:00
"""
Standardize all dict keys (e.g. 'Location Latitude' -> 'location_latitude').
"""
dict_util.standardize(self)
def subset(self, keys, *args):
2019-12-12 17:24:16 +00:00
"""
Return a new dict subset for the given keys.
It is possible to pass a single key or multiple keys (as list or *args).
"""
2019-10-07 10:03:01 +00:00
return dict_util.subset(self, keys, *args)
2019-10-04 13:53:54 +00:00
def swap(self, key1, key2):
2019-12-12 17:24:16 +00:00
"""
Swap items values at the given keys.
"""
2019-10-07 10:03:01 +00:00
dict_util.swap(self, key1, key2)
2019-10-07 10:03:46 +00:00
def traverse(self, callback):
2019-12-12 17:24:16 +00:00
"""
Traverse the current dict instance (including nested dicts),
and pass each item (dict, key, value) to the callback function.
"""
dict_util.traverse(self, callback)
2019-10-07 10:03:46 +00:00
def unique(self):
2019-12-12 17:24:16 +00:00
"""
Remove duplicated values from the current dict instance.
"""
2019-10-07 10:03:46 +00:00
dict_util.unique(self)