Merge pull request #5 from rameshrvr/master
Addition of new feature (get_all_keys)
This commit is contained in:
commit
7260463455
5 changed files with 159 additions and 4 deletions
39
README.rst
39
README.rst
|
|
@ -4,7 +4,9 @@ nested_lookup
|
|||
.. image:: https://img.shields.io/badge/pypi-0.1.5-green.svg
|
||||
:target: https://pypi.python.org/pypi/nested-lookup
|
||||
|
||||
A small Python library which enables key lookups on deeply nested documents.
|
||||
A small Python library which enables:
|
||||
1. key lookups on deeply nested documents.
|
||||
2. fetching all keys from a nested dictionary.
|
||||
|
||||
Documents may be built out of dictionaries (dicts) and/or lists.
|
||||
|
||||
|
|
@ -42,6 +44,11 @@ quick tutorial
|
|||
>>> print(nested_lookup('taco', document))
|
||||
[42, 69]
|
||||
|
||||
>>> from nested_lookup import get_all_keys
|
||||
|
||||
>>> get_all_keys(document)
|
||||
['taco', 'salsa', 'burrito', 'taco']
|
||||
|
||||
longer tutorial
|
||||
===============
|
||||
|
||||
|
|
@ -109,6 +116,36 @@ Additionally, if you also needed the matched key names, you could do this:
|
|||
}
|
||||
|
||||
|
||||
Tutorial to get all keys from a nested dictionary
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
sample_data = {
|
||||
"hardware_details": {
|
||||
"model_name": "MacBook Pro",
|
||||
"processor_details": {
|
||||
"processor_name": "Intel Core i7",
|
||||
"processor_speed": "2.7 GHz",
|
||||
"core_details": {
|
||||
"total_numberof_cores": "4",
|
||||
"l2_cache(per_core)": "256 KB"
|
||||
}
|
||||
},
|
||||
"total_number_of_cores": "4",
|
||||
"memory": "16 GB",
|
||||
},
|
||||
"os_details": {
|
||||
"product_version": "10.13.6",
|
||||
"build_version": "17G65"
|
||||
},
|
||||
"name": "Test",
|
||||
"date": "YYYY-MM-DD HH:MM:SS"
|
||||
}
|
||||
|
||||
result = get_all_keys(sample_data)
|
||||
|
||||
print(result)
|
||||
|
||||
misc
|
||||
========
|
||||
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
from .nested_lookup import nested_lookup
|
||||
from .nested_lookup import nested_lookup, get_all_keys
|
||||
|
|
|
|||
|
|
@ -32,3 +32,29 @@ def _nested_lookup(key, document, wild=False, with_keys=False):
|
|||
for d in v:
|
||||
for result in _nested_lookup(key, d, wild=wild, with_keys=with_keys):
|
||||
yield result
|
||||
|
||||
|
||||
def get_all_keys(dictionary):
|
||||
"""
|
||||
Method to get all keys from a nested dictionary as a List
|
||||
Args:
|
||||
dictionary: Nested dictionary
|
||||
Returns:
|
||||
List of keys in the dictionary
|
||||
"""
|
||||
result_list = []
|
||||
|
||||
def recrusion(dictionary):
|
||||
for key, value in iteritems(dictionary):
|
||||
if isinstance(value, dict):
|
||||
result_list.append(key)
|
||||
recrusion(dictionary=value)
|
||||
elif isinstance(value, list):
|
||||
result_list.append(key)
|
||||
for list_items in value:
|
||||
recrusion(dictionary=list_items)
|
||||
else:
|
||||
result_list.append(key)
|
||||
|
||||
recrusion(dictionary=dictionary)
|
||||
return result_list
|
||||
|
|
|
|||
2
setup.py
2
setup.py
|
|
@ -13,7 +13,7 @@ with open('requirements.txt', 'r') as f:
|
|||
|
||||
setup(
|
||||
name = 'nested-lookup',
|
||||
version = '0.1.5',
|
||||
version = '0.1.6',
|
||||
description = 'lookup a key in a deeply nested document of dicts and lists',
|
||||
keywords = 'nested document dictionary dict list lookup schema json xml yaml',
|
||||
long_description = open('README.rst').read(),
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
from unittest import TestCase
|
||||
|
||||
from nested_lookup import nested_lookup
|
||||
from nested_lookup import nested_lookup, get_all_keys
|
||||
|
||||
|
||||
class TestNestedLookup(TestCase):
|
||||
|
||||
|
|
@ -75,3 +76,94 @@ class TestNestedLookup(TestCase):
|
|||
self.assertEqual(2, len(matches['d']))
|
||||
self.assertSetEqual({100,200}, set(matches['d']))
|
||||
|
||||
|
||||
class TestGetAllKeys(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.sample1 = {
|
||||
"hardware_details": {
|
||||
"model_name": "MacBook Pro",
|
||||
"processor_details": {
|
||||
"processor_name": "Intel Core i7",
|
||||
"processor_speed": "2.7 GHz",
|
||||
"core_details": {
|
||||
"total_numberof_cores": "4",
|
||||
"l2_cache(per_core)": "256 KB"
|
||||
}
|
||||
},
|
||||
"total_number_of_cores": "4",
|
||||
"memory": "16 GB",
|
||||
},
|
||||
"os_details": {
|
||||
"product_version": "10.13.6",
|
||||
"build_version": "17G65"
|
||||
},
|
||||
"name": "Test",
|
||||
"date": "YYYY-MM-DD HH:MM:SS"
|
||||
}
|
||||
self.sample2 = {
|
||||
"hardware_details": {
|
||||
"model_name": "MacBook Pro",
|
||||
"processor_details": [{
|
||||
"processor_name": "Intel Core i7",
|
||||
"processor_speed": "2.7 GHz",
|
||||
"core_details": {
|
||||
"total_numberof_cores": "4",
|
||||
"l2_cache(per_core)": "256 KB"
|
||||
}
|
||||
}],
|
||||
"total_number_of_cores": "4",
|
||||
"memory": "16 GB",
|
||||
}
|
||||
}
|
||||
self.sample3 = {
|
||||
"hardware_details": {
|
||||
"model_name": "MacBook Pro",
|
||||
"processor_details": [
|
||||
{
|
||||
"processor_name": "Intel Core i7",
|
||||
"processor_speed": "2.7 GHz",
|
||||
},
|
||||
{
|
||||
"total_numberof_cores": "4",
|
||||
"l2_cache(per_core)": "256 KB"
|
||||
}
|
||||
],
|
||||
"total_number_of_cores": "4",
|
||||
"memory": "16 GB",
|
||||
}
|
||||
}
|
||||
|
||||
def test_sample_data1(self):
|
||||
result = get_all_keys(self.sample1)
|
||||
self.assertEqual(15, len(result))
|
||||
keys_to_verify = [
|
||||
'model_name', 'core_details', 'l2_cache(per_core)',
|
||||
'build_version', 'date'
|
||||
]
|
||||
for key in keys_to_verify:
|
||||
self.assertIn(key, result)
|
||||
|
||||
def test_sample_data2(self):
|
||||
result = get_all_keys(self.sample2)
|
||||
self.assertEqual(10, len(result))
|
||||
keys_to_verify = [
|
||||
'hardware_details', 'processor_speed',
|
||||
'total_numberof_cores', 'memory'
|
||||
]
|
||||
for key in keys_to_verify:
|
||||
self.assertIn(key, result)
|
||||
|
||||
def test_sample_data3(self):
|
||||
result = get_all_keys(self.sample3)
|
||||
self.assertEqual(9, len(result))
|
||||
keys_to_verify = [
|
||||
'processor_details', 'processor_name',
|
||||
'l2_cache(per_core)', 'total_number_of_cores'
|
||||
]
|
||||
for key in keys_to_verify:
|
||||
self.assertIn(key, result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pass
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue