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 all 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,6 @@ dmypy.json

# Pyre type checker
.pyre/

# JetBrains IDEs
.idea/
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 @@ -195,9 +195,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 @@ -213,7 +213,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
39 changes: 33 additions & 6 deletions benedict/dicts/keylist/keylist_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from benedict.dicts.base import BaseDict
from benedict.dicts.keylist import keylist_util
from benedict.dicts.keylist.keylist_util import generator_to_list, pop_in_generator
from benedict.utils import type_util


Expand All @@ -28,7 +29,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_dict_or_list(parent) and type_util.is_wildcard(key):
parent.clear()
return
elif type_util.is_dict_or_list(parent):
del parent[key]
return
elif type_util.is_tuple(parent):
Expand All @@ -42,8 +46,12 @@ def __getitem__(self, key):
return super(KeylistDict, self).__getitem__(key)

def _getitem_by_keys(self, keys):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_dict_or_list_or_tuple(parent):
parent, key, val = keylist_util.get_item(self, keys)
if type_util.is_generator(val):
return generator_to_list(val)
elif type_util.is_list(parent) and type_util.is_wildcard(key):
return parent
elif type_util.is_dict_or_list_or_tuple(parent):
return parent[key]
raise KeyError(f"Invalid keys: '{keys}'")

Expand All @@ -62,8 +70,22 @@ def get(self, key, default=None):
return super(KeylistDict, self).get(key, default)

def _get_by_keys(self, keys, default=None):
parent, key, _ = keylist_util.get_item(self, keys)
if type_util.is_dict(parent):
parent, key, value = keylist_util.get_item(self, keys)
if type_util.is_generator(value) and type_util.is_generator(parent):
data = generator_to_list(value)
if type_util.is_integer(key):
return data[key]
return data
if type_util.is_list_of_list(parent) and type_util.is_wildcard(key):
return value
elif 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 value
elif type_util.is_dict(parent):
return parent.get(key, default)
elif type_util.is_list_or_tuple(parent):
return parent[key]
Expand All @@ -75,9 +97,14 @@ def pop(self, key, *args):
return super(KeylistDict, self).pop(key, *args)

def _pop_by_keys(self, keys, *args):
parent, key, _ = keylist_util.get_item(self, keys)
parent, key, val = keylist_util.get_item(self, keys)
if type_util.is_dict(parent):
return parent.pop(key, *args)
elif type_util.is_generator(val) and type_util.is_generator(parent):
return pop_in_generator(key, parent)
elif type_util.is_wildcard(key):
data = [parent.pop(0) for _ in range(len(parent))]
return data
elif type_util.is_list(parent):
return parent.pop(key)
elif type_util.is_tuple(parent):
Expand Down
94 changes: 86 additions & 8 deletions benedict/dicts/keylist/keylist_util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from itertools import chain

from benedict.utils import type_util

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


def _get_item_key_and_value(item, key):
if type_util.is_list_or_tuple(item):
index = _get_index(key)
if index is not None:
return (index, item[index])
def low_level_generator(item, index):
for _item in item:
if not type_util.is_dict_or_list_or_tuple(_item):
yield _item
elif type_util.is_list(_item):
for __item in _item:
if index in __item:
yield __item.get(index)
elif __item:
yield __item
elif type_util.is_dict(_item) and index in _item:
yield _item[index]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If _item is tuple, it will not enter any of the conditions above.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the purpose of this method, could you explain it please?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is allowing to travers through complex list and return references to objects on the lowest level of them.



def generator_to_list(generator):
new_list = []
for item in generator:
if type_util.is_list_or_tuple(item):
new_list.extend(item)
else:
new_list.append(item)
return new_list


def _get_item_key_and_value_for_parent_wildcard(item, index):
if type_util.is_list_of_dicts(item) and any(
index in _item.keys() for _item in item
):
return index, low_level_generator(item, index)
elif type_util.is_list_of_list(item):
if type_util.is_integer(index):
data = [_item[index] for _item in item if index < len(_item)]
return index, data
elif type_util.is_wildcard(index):
data = list(chain.from_iterable(item))
return index, data
else:
data = [
_item.get(index)
for _item in chain.from_iterable(item)
if index in _item.keys()
]
return index, data
elif type_util.is_wildcard(index):
return index, item
return index, None


def _get_item_key_and_value(item, index, parent=None):
if type_util.is_generator(item):
return index, low_level_generator(item, index)
elif type_util.is_list_or_tuple(item):
milhauzindahauz marked this conversation as resolved.
Show resolved Hide resolved
if type_util.is_wildcard(parent):
index, item = _get_item_key_and_value_for_parent_wildcard(item, index)
if item:
return index, item
elif type_util.is_wildcard(index):
return index, item
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 +103,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,12 +121,17 @@ 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):
parent = items[-1][1]
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):
items.append((None, None, None))
break

return items


Expand All @@ -80,3 +147,14 @@ def set_item(d, keys, value):
continue
_set_item_value(item, key, value)
break


def pop_in_generator(key, parent):
cleaned_list = []
for item in parent:
if type_util.is_list_or_tuple(item):
if type_util.is_wildcard(key):
cleaned_list.extend(item.pop(0) for _ in range(len(item)))
elif type_util.is_dict(item):
cleaned_list.append(item)
return cleaned_list
14 changes: 10 additions & 4 deletions benedict/dicts/keypath/keypath_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from benedict.utils import type_util

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 @@ -44,12 +45,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
33 changes: 33 additions & 0 deletions benedict/utils/type_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import re
import types
from datetime import datetime
from decimal import Decimal

Expand Down Expand Up @@ -90,3 +91,35 @@ 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)


def is_generator(val):
return isinstance(val, types.GeneratorType)


def is_generator_empty(val):
"""
this function should be used only to check if generator contains any data
don't use it if you want to use any potential data contained in generator

val: object

return: bool
"""
return any(list(val))
Loading