Skip to content

Commit

Permalink
Fix errors and add list testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
n1nj4t4nuk1 committed Apr 24, 2021
1 parent 88c1e13 commit ac1fbe7
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 20 deletions.
File renamed without changes.
13 changes: 8 additions & 5 deletions deepgetter/deep_get.py → deepfinder/deep_find.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def deep_get(
def deep_find(
obj: any,
path: str,
token: str = '.',
Expand Down Expand Up @@ -41,17 +41,20 @@ def __rec_helper(obj: any, path: [str]):
current_path = path.pop(0)

if isinstance(obj, dict):
return __rec_helper(obj[current_path], path)
return __rec_helper(obj.get(current_path), path)

if isinstance(obj, list):
if current_path == '*':
return [__rec_helper(sub_obj, path) for sub_obj in obj]
return [__rec_helper(sub_obj, path.copy()) for sub_obj in obj]
if current_path == '?':
for sub_obj in obj:
result = __rec_helper(sub_obj, path)
result = __rec_helper(sub_obj, path.copy())
if result is not None:
return result
return None
return __rec_helper(obj[int(path)], path)
current_path_index = int(current_path)
if current_path_index >= len(obj):
return None
return __rec_helper(obj[current_path_index], path)

return None
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
long_description = fh.read()

setup(
name='deepgetter',
name='deepfinder',
packages=find_packages(),
version='0.0.1',
description='',
author='Javier Parada',
author_email="[email protected]",
license='MIT',
url="https://github.com/parada3desu/deepgetter.py",
url="https://github.com/parada3desu/deepfinder.py",
long_description=long_description,
long_description_content_type="text/markdown",
package_dir={"": "deepgetter"},
package_dir={"": "deepfinder"},
python_requires=">=3.6",
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import unittest

from deepgetter.deep_get import deep_get
from deepfinder.deep_find import deep_find


class TestGetByType(unittest.TestCase):
class TestFindByType(unittest.TestCase):
def test_with_basic_number(self):
"""
Test basic number
"""
data: int = 39
result = deep_get(data, '')
result = deep_find(data, '')
self.assertEqual(result, 39)

def test_with_number_in_dict(self):
Expand All @@ -19,15 +19,15 @@ def test_with_number_in_dict(self):
data: dict = {
'value': 39,
}
result = deep_get(data, 'value')
result = deep_find(data, 'value')
self.assertEqual(result, 39)

def test_with_basic_str(self):
"""
Test basic string
"""
data: str = 'test str'
result = deep_get(data, '')
result = deep_find(data, '')
self.assertEqual(result, 'test str')

def test_with_str_in_dict(self):
Expand All @@ -37,7 +37,7 @@ def test_with_str_in_dict(self):
data: dict = {
'value': 'test str',
}
result = deep_get(data, 'value')
result = deep_find(data, 'value')
self.assertEqual(result, 'test str')


Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import unittest

from deepgetter.deep_get import deep_get
from deepfinder.deep_find import deep_find


class TestFindInDeep(unittest.TestCase):
class TestFindInDicts(unittest.TestCase):
def test_dict_with_0_lvl(self):
"""
Test basic dictionary with no profundity level.
"""
data: dict = {
'value': 39,
}
result = deep_get(data, '')
result = deep_find(data, '')
self.assertEqual(result, data)

def test_dict_with_1_lvl(self):
Expand All @@ -21,7 +21,7 @@ def test_dict_with_1_lvl(self):
data: dict = {
'value': 39,
}
result = deep_get(data, 'value')
result = deep_find(data, 'value')
self.assertEqual(result, 39)

def test_dict_with_2_lvl(self):
Expand All @@ -33,7 +33,7 @@ def test_dict_with_2_lvl(self):
'subdata': 39,
},
}
result = deep_get(data, 'value.subdata')
result = deep_find(data, 'value.subdata')
self.assertEqual(result, 39)

def test_dict_with_3_lvl(self):
Expand All @@ -47,7 +47,7 @@ def test_dict_with_3_lvl(self):
},
},
}
result = deep_get(data, 'value.sub-data.sub-sub-data')
result = deep_find(data, 'value.sub-data.sub-sub-data')
self.assertEqual(result, 39)


Expand Down
141 changes: 141 additions & 0 deletions tests/unit/deep_find_in_lists_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import unittest

from deepfinder.deep_find import deep_find


class TestFindInLists(unittest.TestCase):
def test_first_value_of_a_list(self):
"""
Should return first value of a list.
"""
data: [str] = ['a', 'b', 'c']
result = deep_find(data, '0')
self.assertEqual(result, 'a')

def test_first_value_of_embedded_list(self):
"""
Should return first value of a embedded list.
"""
data: dict = {
'values': ['a', 'b', 'c'],
}
result = deep_find(data, 'values.0')
self.assertEqual(result, 'a')

def test_all_values_of_list(self):
"""
Should return all values of a list.
"""
data: dict = {
'values': ['a', 'b', 'c'],
}
result = deep_find(data, 'values.*')
self.assertEqual(result, data['values'])

def test_first_value_in_dict_of_a_list(self):
"""
Should return first subvalue of a list.
"""
data: dict = {
'values': [{
'value': 'a',
}, {
'value': 'b',
}, {
'value': 'c',
}],
}
result = deep_find(data, 'values.0.value')
self.assertEqual(result, 'a')

def test_all_values_of_a_list(self):
"""
Should return all subvalues of a list.
"""
data: dict = {
'values': [{
'value': 'a',
}, {
'value': 'b',
}, {
'value': 'c',
}],
}
result = deep_find(data, 'values.*.value')
self.assertEqual(result, [value['value'] for value in data['values']])

def test_existing_path_values_of_a_list(self):
"""
Should return first existing path in list.
"""
data: dict = {
'values': [{
'nya': 'a',
}, {
'value': 'b',
}, {
'nya': 'c',
}],
}
result = deep_find(data, 'values.?.value')
self.assertEqual(result, 'b')

def test_all_values_of_a_list_inside_list(self):
"""
Should return complete lists inside list.
"""
data: dict = {
'all-values': [{
'values': ['a'],
}, {
'values': ['b', 'c', 'd'],
}, {
'values': ['e'],
}],
}
result = deep_find(data, 'all-values.*.values')
self.assertEqual(result, [['a'], ['b', 'c', 'd'], ['e']])

def test_complete_list_inside_list(self):
"""
Should return complete lists inside direct list.
"""
data: dict = {
'all-values': [['a'], ['b', 'c', 'd'], ['e']],
}
result = deep_find(data, 'all-values.*.*')
self.assertEqual(result, [['a'], ['b', 'c', 'd'], ['e']])

def test_first_value_of_list_inside_list(self):
"""
Should return first value of all lists inside list.
"""
data: dict = {
'all-values': [['a'], ['b', '3', '4'], ['c']],
}
result = deep_find(data, 'all-values.*.0')
self.assertEqual(result, ['a', 'b', 'c'])

def test_existing_path_of_list_inside_list(self):
"""
Should return existing path of lists inside direct list.
"""
data: dict = {
'all-values': [['a'], ['b', 'c', 'd'], ['e']],
}
result = deep_find(data, 'all-values.?.2')
self.assertEqual(result, 'd')

def test_existing_path_inside_existing_path(self):
"""
Should return existing path inside existing path of lists inside list.
"""
data: dict = {
'all-values': [['a'], ['b', {'correct': 'correct'}, 'c'], ['d']],
}
result = deep_find(data, 'all-values.?.?.correct')
self.assertEqual(result, 'correct')


if __name__ == '__main__':
unittest.main()

0 comments on commit ac1fbe7

Please sign in to comment.