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

List wildcard #114

Closed
Closed
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
57 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
417f0a0
wildcard
milhauzindahauz Sep 22, 2022
bda6ceb
wildcard
milhauzindahauz Sep 23, 2022
87f918a
wildcard
milhauzindahauz Oct 18, 2022
d2f1446
wildcard
milhauzindahauz Oct 18, 2022
cc5b7fa
wildcard
milhauzindahauz Oct 18, 2022
f910abb
wildcard
milhauzindahauz Oct 18, 2022
273e674
wildcard
milhauzindahauz Oct 18, 2022
1fb37fd
wildcard
milhauzindahauz Oct 18, 2022
747f27d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
5cd2d6a
wildcard
milhauzindahauz Oct 18, 2022
06029f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
6e70434
wildcard
milhauzindahauz Oct 18, 2022
d09b79e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
30e9f07
wildcard
milhauzindahauz Oct 18, 2022
4bb0b04
wildcard
milhauzindahauz Oct 18, 2022
6f610ec
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 18, 2022
bd0d7fd
wildcard
milhauzindahauz Oct 18, 2022
17edc65
wildcard
milhauzindahauz Oct 18, 2022
1db0e6c
wildcard
milhauzindahauz Oct 18, 2022
53331f2
wildcard
milhauzindahauz Oct 18, 2022
a94bf27
wildcard
milhauzindahauz Oct 19, 2022
6f8487a
wildcard
milhauzindahauz Oct 19, 2022
f54e3d0
wildcard
milhauzindahauz Oct 19, 2022
9d5560b
wildcard
milhauzindahauz Oct 20, 2022
7ac667c
.gitignore - add folder for JB IDEs
milhauzindahauz Oct 27, 2022
6788e78
Merge branch 'master' into list-wildcard
milhauzindahauz Oct 30, 2022
71d23c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 30, 2022
8842f01
wildcard
milhauzindahauz Nov 7, 2022
a43563d
wildcard
milhauzindahauz Nov 7, 2022
fa32b00
wildcard
milhauzindahauz Nov 7, 2022
b928196
wildcard
milhauzindahauz Nov 7, 2022
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
1 change: 0 additions & 1 deletion benedict/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-

from benedict.dicts import benedict
from benedict.metadata import (
__author__,
Expand Down
6 changes: 3 additions & 3 deletions benedict/dicts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,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 @@ -212,7 +212,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
7 changes: 7 additions & 0 deletions benedict/dicts/keylist/keylist_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def __getitem__(self, key):

def _getitem_by_keys(self, keys):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_list_of_dicts(parent) and any(type_util.is_wildcard(_key) for _key in keys):
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
return [item.get(key) for item in parent]
if type_util.is_dict_or_list_or_tuple(parent):
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
return parent[key]
raise KeyError(f"Invalid keys: '{keys}'")
Expand Down Expand Up @@ -78,6 +80,11 @@ 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_of_dicts(parent)
and any(type_util.is_wildcard(_key) for _key in keys)
):
return [_item.pop(key) if key in _item else None for _item in parent]
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
elif type_util.is_list(parent):
return parent.pop(key)
elif type_util.is_tuple(parent):
Expand Down
36 changes: 29 additions & 7 deletions benedict/dicts/keylist/keylist_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ 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)
and type_util.is_list_of_dicts(item)
and any(index in _item.keys() for _item in item)
):
return index, [_item for _item in 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 @@ -45,6 +54,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 @@ -59,7 +72,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)
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -7,6 +7,7 @@


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

milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved

def check_keys(d, separator):
Expand Down Expand Up @@ -45,12 +46,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
8 changes: 8 additions & 0 deletions benedict/utils/type_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,11 @@ 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)
23 changes: 23 additions & 0 deletions tests/dicts/keypath/test_keypath_dict_list_wildcard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import unittest

from benedict.dicts import KeypathDict


class keypath_dict_list_wildcard_test_case(unittest.TestCase):
def setUp(self):
self.blueprint = KeypathDict(
{
"a": [
{"x": 1, "y": 1},
{"x": 2, "y": 2},
],
"x": [
{"a": 10, "b": 10},
{"a": 11, "b": 11},
],
}
)

def test_correct_wildcard(self):
correct_wildcard_path_example = "a[*].x"
self.assertEqual(self.blueprint[correct_wildcard_path_example], [1, 2])
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion tests/dicts/keypath/test_keypath_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def test_split_key_indexes_with_valid_indexes(self):
def test_split_key_indexes_with_invalid_indexes(self):
f = keypath_util._split_key_indexes
self.assertEqual(f("item[]"), ["item[]"])
self.assertEqual(f("item[*]"), ["item[*]"])
self.assertEqual(f("item[0:2]"), ["item[0:2]"])
self.assertEqual(f("item[:1]"), ["item[:1]"])
self.assertEqual(f("item[::1]"), ["item[::1]"])
Expand Down
61 changes: 61 additions & 0 deletions tests/dicts/test_benedict_wildcard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import unittest

from benedict import benedict


class keypath_dict_list_wildcard_test_case(unittest.TestCase):
def test_rename_wildcard(self):
d = {
"a": [
{"x": 1, "y": 1},
{"x": 2, "y": 2},
],
}
d = benedict(d)
b = benedict(d.clone())
b.rename("a[0].x", "a[0].m")
b.rename("a[1].x", "a[1].m")

result = {
"a": [
{
"m": 1,
"y": 1,
},
{
"m": 2,
"y": 2,
},
]
}

self.assertEqual(b, result)
b = benedict(d.clone())
b.rename("a[*].x", "a[*].m")
self.assertEqual(b, result)

def test_swap_wildcard(self):
d = {
"a": [
{"x": 1, "y": 1},
{"x": 2, "y": 2},
],
"x": [
{"a": 10, "b": 10},
{"a": 11, "b": 11},
],
}
b = benedict(d)
b = benedict(b.clone())
b.swap("a[*].x", "x[*].a")
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
result = {
"a": [
{"x": 10, "y": 1},
{"x": 11, "y": 2},
],
"x": [
{"a": 1, "b": 10},
{"a": 2, "b": 11},
],
}
self.assertEqual(b, result)