Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcard #236

Draft
wants to merge 31 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
98f1bf6
add wildcard for rename
milhauzindahauz Sep 12, 2022
d69450a
wildcard
milhauzindahauz Sep 12, 2022
4f8e893
wildcard
milhauzindahauz Sep 13, 2022
7e26ee2
wildcard
milhauzindahauz Sep 13, 2022
6987943
wildcard
milhauzindahauz Sep 13, 2022
c85c1a4
wildcard
milhauzindahauz Sep 13, 2022
baf4f70
wildcard
milhauzindahauz Sep 13, 2022
9eba3fc
wildcard
milhauzindahauz Sep 13, 2022
95e3ee8
wildcard
milhauzindahauz Sep 13, 2022
48070d3
fix typos
milhauzindahauz Sep 14, 2022
1c7da03
wildcard
milhauzindahauz Sep 14, 2022
6dfff10
wildcard
milhauzindahauz Sep 14, 2022
4de87dd
wildcard
milhauzindahauz Sep 14, 2022
df926ca
wildcard
milhauzindahauz Sep 14, 2022
409d742
wildcard
milhauzindahauz Sep 15, 2022
38931d6
wildcard
milhauzindahauz Sep 15, 2022
b34a803
wildcard
milhauzindahauz Sep 15, 2022
ec6caac
wildcard
milhauzindahauz Sep 15, 2022
64e355e
wildcard
milhauzindahauz Sep 15, 2022
87b87b1
wildcard
milhauzindahauz Sep 15, 2022
07bdb1b
wildcard
milhauzindahauz Sep 15, 2022
f7401eb
wildcard
milhauzindahauz Sep 15, 2022
a631b9a
wildcard
milhauzindahauz Sep 15, 2022
750b78f
wildcard
milhauzindahauz Sep 15, 2022
9e136ea
wildcard
milhauzindahauz Sep 22, 2022
1142a4b
wildcard
milhauzindahauz Sep 22, 2022
a522ca5
Merge branch 'pr/114' into wildcard
fabiocaccamo Sep 22, 2022
52ec541
Merge branch 'master' into wildcard
fabiocaccamo Oct 19, 2022
3e5857a
Merge branch 'main' into wildcard
fabiocaccamo Jan 18, 2023
a429259
Merge branch 'main' into wildcard
fabiocaccamo Feb 1, 2023
30f8914
Merge branch 'main' into wildcard
fabiocaccamo Feb 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions benedict/dicts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ def match(self, pattern, indexes=True):
def merge(self, other, *args, **kwargs):
"""
Merge one or more dict objects into current instance (deepupdate).
Sub-dictionaries will be merged toghether.
Sub-dictionaries will be merged together.
If overwrite is False, existing values will not be overwritten.
If concat is True, list values will be concatenated toghether.
If concat is True, list values will be concatenated together.
"""
_merge(self, other, *args, **kwargs)

Expand All @@ -209,7 +209,7 @@ def nest(
):
"""
Nest a list of dicts at the given key and return a new nested list
using the specified keys to establish the correct items hierarchy.
using the specified keys to establish the correct item's hierarchy.
"""
return _nest(self[key], id_key, parent_id_key, children_key)

Expand Down
25 changes: 23 additions & 2 deletions benedict/dicts/keylist/keylist_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def __delitem__(self, key):

def _delitem_by_keys(self, keys):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_dict_or_list(parent):
if type_util.is_wildcard(key):
self[keys[:-1]].clear()
return
elif type_util.is_dict_or_list(parent):
del parent[key]
return
elif type_util.is_tuple(parent):
Expand All @@ -41,6 +44,10 @@ def __getitem__(self, key):

def _getitem_by_keys(self, keys):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_list(parent) and type_util.is_wildcard(key):
return parent
if type_util.is_list_of_dicts(parent) and type_util.any_wildcard_in_list(keys):
return [item.get(key) for item in parent]
if type_util.is_dict_or_list_or_tuple(parent):
return parent[key]
raise KeyError(f"Invalid keys: '{keys}'")
Expand All @@ -61,7 +68,14 @@ def get(self, key, default=None):

def _get_by_keys(self, keys, default=None):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_dict(parent):
if type_util.is_list(parent) and type_util.is_wildcard(key):
return parent
elif type_util.is_wildcard(keys[-2]):
if type_util.is_list_of_dicts(parent):
return [item.get(key) for item in parent]
elif type_util.is_list_of_list(parent):
return _
elif type_util.is_dict(parent):
return parent.get(key, default)
elif type_util.is_list_or_tuple(parent):
return parent[key]
Expand All @@ -76,6 +90,13 @@ def _pop_by_keys(self, keys, *args):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_dict(parent):
return parent.pop(key, *args)
elif type_util.is_list(parent) and type_util.is_wildcard(key):
del self[keys[:-1]]
return parent
elif type_util.is_list_of_dicts(parent) and type_util.any_wildcard_in_list(
keys
):
return [_item.pop(key) if key in _item else None for _item in parent]
elif type_util.is_list(parent):
return parent.pop(key)
elif type_util.is_tuple(parent):
Expand Down
45 changes: 38 additions & 7 deletions benedict/dicts/keylist/keylist_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import chain

from benedict.utils import type_util


Expand All @@ -7,14 +9,30 @@ def _get_index(key):
return None


def _get_item_key_and_value(item, key):
def _get_item_key_and_value(item, index, parent=None):
if type_util.is_list_or_tuple(item):
index = _get_index(key)
if index is not None:
return (index, item[index])
if type_util.is_wildcard(index):
return index, item
elif type_util.is_wildcard(parent):
if type_util.is_list_of_dicts(item) and any(
index in _item.keys() for _item in item
):
return index, [
_item.get(index) for _item in item if index in _item.keys()
]
elif type_util.is_list_of_list(item):
return index, [
_item.get(index)
for _item in chain.from_iterable(item)
if index in _item.keys()
]
else:
index = _get_index(index)
if index is not None:
return index, item[index]
elif type_util.is_dict(item):
return (key, item[key])
raise KeyError(f"Invalid key: '{key}'")
return index, item[index]
raise KeyError(f"Invalid key: '{index}'")


def _get_or_new_item_value(item, key, subkey):
Expand Down Expand Up @@ -43,6 +61,10 @@ def _set_item_value(item, key, value):
# insert index
item += [None] * (index - len(item))
item.insert(index, value)
elif type_util.is_list(item):
for idx, _item in enumerate(value):
if _item is not None:
item[idx].update({key: _item})
else:
item[key] = value

Expand All @@ -57,7 +79,16 @@ def get_items(d, keys):
item = d
for key in keys:
try:
item_key, item_value = _get_item_key_and_value(item, key)
if any(items):
if type_util.is_wildcard(val=key):
parent = items[-1][1]
elif type_util.is_wildcard(items[-1][1]):
parent = items[-1][1]
else:
parent = None
else:
parent = None
item_key, item_value = _get_item_key_and_value(item, key, parent)
items.append((item, item_key, item_value))
item = item_value
except (IndexError, KeyError):
Expand Down
14 changes: 10 additions & 4 deletions benedict/dicts/keypath/keypath_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from benedict.utils import type_util

KEY_INDEX_RE = r"(?:\[[\'\"]*(\-?[\d]+)[\'\"]*\]){1}$"
KEY_WILDCARD_RE = r"(?:\[[\'\"]*(\-?[\*]+)[\'\"]*\]){1}$"


def check_keys(d, separator):
Expand Down Expand Up @@ -42,12 +43,17 @@ def _split_key_indexes(key):
if "[" in key and key.endswith("]"):
keys = []
while True:
matches = re.findall(KEY_INDEX_RE, key)
if matches:
index_matches = re.findall(KEY_INDEX_RE, key)
if index_matches:
key = re.sub(KEY_INDEX_RE, "", key)
index = int(matches[0])
index = int(index_matches[0])
keys.insert(0, index)
continue
index_matches = re.findall(KEY_WILDCARD_RE, key)
if bool(re.search(KEY_WILDCARD_RE, key)):
key = re.sub(KEY_WILDCARD_RE, "", key)
index = index_matches[0]
keys.insert(0, index)
# keys.insert(0, { keylist_util.INDEX_KEY:index })
continue
keys.insert(0, key)
break
Expand Down
16 changes: 16 additions & 0 deletions benedict/utils/type_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ def is_tuple(val):

def is_uuid(val):
return is_string(val) and uuid_re.match(val)


def is_wildcard(val):
return is_string(val) and val == "*"


def is_list_of_dicts(val):
return is_list(val) and all(is_dict(_val) for _val in val)


def any_wildcard_in_list(val):
return is_list(val) and any(is_wildcard(_val) for _val in val)


def is_list_of_list(val):
return is_list(val) and all(is_list(_val) for _val in val)
Loading