From 904a2b957df3e4a3ad4540626daede6e81d43e4a Mon Sep 17 00:00:00 2001 From: Ramesh RV Date: Mon, 8 Oct 2018 16:09:19 +0530 Subject: [PATCH 1/3] New feature (Finding occurance) --- README.rst | 23 ++++++++++ nested_lookup/__init__.py | 3 +- nested_lookup/nested_lookup.py | 55 ++++++++++++++++++++++++ setup.py | 2 +- test_nested_loopkup.py | 78 +++++++++++++++++++++++++++++++++- 5 files changed, 158 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 5afc5ca..fff6165 100644 --- a/README.rst +++ b/README.rst @@ -8,6 +8,7 @@ A small Python library which enables: #. key lookups on deeply nested documents. #. fetching all keys from a nested dictionary. +#. get the number of occurances of a key/value from a nested dictionary Documents may be built out of dictionaries (dicts) and/or lists. @@ -50,6 +51,14 @@ quick tutorial >>> get_all_keys(document) ['taco', 'salsa', 'burrito', 'taco'] + >>> from nested_lookup import get_occurance_of_key, get_occurance_of_value + + >>> get_occurance_of_key(document, key='taco') + 2 + + >>> get_occurance_of_value(document, value='42') + 1 + longer tutorial =============== @@ -93,6 +102,20 @@ To get a list of every nested key in a document, run this: ['name', 'email_address', 'other', 'secondary_email', 'EMAIL_RECOVERY', 'email_address'] +To get the number of occurance of the given key/value + +.. code-block:: python + + from nested_lookup import get_occurance_of_key, get_occurance_of_value + + no_of_key_occurance = get_occurance_of_key(my_document, key='email_address') + + print(no_of_key_occurance) # result => 2 + + no_of_value_occurance = get_occurance_of_value(my_document, value='test2@example.com') + + print(no_of_value_occurance) # result => 1 + Next, we could act `wild` and find all the email addresses like this: .. code-block:: python diff --git a/nested_lookup/__init__.py b/nested_lookup/__init__.py index 367b245..1da6552 100644 --- a/nested_lookup/__init__.py +++ b/nested_lookup/__init__.py @@ -1 +1,2 @@ -from .nested_lookup import nested_lookup, get_all_keys +from .nested_lookup import nested_lookup, get_all_keys, get_occurance_of_key,\ + get_occurance_of_value diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index 17de3fd..1e22531 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -62,3 +62,58 @@ def get_all_keys(dictionary): recrusion(dictionary=dictionary) return result_list + + +def get_occurance_of_key(dictionary, key): + """ + Method to get occurance of a key in a nested dictionary + + Args: + dictionary: Nested dictionary + key: Key to search for the occurances + Return: + Number of occurance (Integer) + """ + return _get_occurrence(dictionary=dictionary, item='key', keyword=key) + + +def get_occurance_of_value(dictionary, value): + """ + Method to get occurance of a value in a nested dictionary + + Args: + dictionary: Nested dictionary + value: Value to search for the occurances + Return: + Number of occurance (Integer) + """ + return _get_occurrence(dictionary=dictionary, item='value', keyword=value) + + +def _get_occurrence(dictionary, item, keyword): + """ + Method to get occurance of a key or value in a nested dictionary + + Args: + dictionary: Nested dictionary + item: Mostly (key or value) + keyword: key word to find occurance + Return: + Number of occurance of the given keyword in the dict + """ + occurance = [0] + + def recrusion(dictionary): + if item == 'key': + occurance[0] += 1 if dictionary.get(keyword) else 0 + elif keyword in dictionary.values(): + occurance[0] += dictionary.values().count(keyword) + for key, value in iteritems(dictionary): + if isinstance(value, dict): + recrusion(dictionary=value) + elif isinstance(value, list): + for list_items in value: + recrusion(dictionary=list_items) + + recrusion(dictionary=dictionary) + return occurance[0] diff --git a/setup.py b/setup.py index 15bba6b..36955bf 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,7 @@ with open('requirements.txt', 'r') as f: setup( name = 'nested-lookup', - version = '0.1.6', + version = '0.1.7', 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(), diff --git a/test_nested_loopkup.py b/test_nested_loopkup.py index d02e9dc..447c56f 100644 --- a/test_nested_loopkup.py +++ b/test_nested_loopkup.py @@ -1,6 +1,7 @@ from unittest import TestCase -from nested_lookup import nested_lookup, get_all_keys +from nested_lookup import nested_lookup, get_all_keys, get_occurance_of_key,\ + get_occurance_of_value class TestNestedLookup(TestCase): @@ -160,5 +161,80 @@ class TestGetAllKeys(TestCase): self.assertIn(key, result) +class TestGetOccurance(TestCase): + def setUp(self): + self.sample1 = { + "build_version": { + "model_name": "MacBook Pro", + "build_version": { + "processor_name": "Intel Core i7", + "processor_speed": "2.7 GHz", + "core_details": { + "build_version": "4", + "l2_cache(per_core)": "256 KB" + } + }, + "number_of_cores": "4", + "memory": "256 KB", + }, + "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": "4", + "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": [ + { + "total_number_of_cores": "4", + "processor_speed": "2.7 GHz", + }, + { + "total_number_of_cores": "4", + "l2_cache(per_core)": "256 KB" + } + ], + "total_number_of_cores": "4", + "memory": "16 GB", + } + } + + def test_sample_data1(self): + result = get_occurance_of_key(self.sample1, 'build_version') + self.assertEqual(4, result) + result = get_occurance_of_value(self.sample1, '256 KB') + self.assertEqual(2, result) + + def test_sample_data2(self): + result = get_occurance_of_key(self.sample2, 'core_details') + self.assertEqual(1, result) + result = get_occurance_of_value(self.sample2, '4') + self.assertEqual(3, result) + + def test_sample_data3(self): + result = get_occurance_of_key(self.sample3, 'total_number_of_cores') + self.assertEqual(3, result) + result = get_occurance_of_value(self.sample3, '4') + self.assertEqual(3, result) + + if __name__ == "__main__": pass From 7692fc2754e25b2fc3e530bf9ec2bc70c576a8f0 Mon Sep 17 00:00:00 2001 From: Ramesh RV Date: Mon, 8 Oct 2018 16:26:29 +0530 Subject: [PATCH 2/3] New Feature (finding occurence) - typo correction --- README.rst | 20 ++++++++++---------- nested_lookup/__init__.py | 4 ++-- nested_lookup/nested_lookup.py | 30 +++++++++++++++--------------- test_nested_loopkup.py | 18 +++++++++--------- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/README.rst b/README.rst index fff6165..45fe9cd 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,7 @@ A small Python library which enables: #. key lookups on deeply nested documents. #. fetching all keys from a nested dictionary. -#. get the number of occurances of a key/value from a nested dictionary +#. get the number of occurences of a key/value from a nested dictionary Documents may be built out of dictionaries (dicts) and/or lists. @@ -51,12 +51,12 @@ quick tutorial >>> get_all_keys(document) ['taco', 'salsa', 'burrito', 'taco'] - >>> from nested_lookup import get_occurance_of_key, get_occurance_of_value + >>> from nested_lookup import get_occurence_of_key, get_occurence_of_value - >>> get_occurance_of_key(document, key='taco') + >>> get_occurence_of_key(document, key='taco') 2 - >>> get_occurance_of_value(document, value='42') + >>> get_occurence_of_value(document, value='42') 1 longer tutorial @@ -102,19 +102,19 @@ To get a list of every nested key in a document, run this: ['name', 'email_address', 'other', 'secondary_email', 'EMAIL_RECOVERY', 'email_address'] -To get the number of occurance of the given key/value +To get the number of occurence of the given key/value .. code-block:: python - from nested_lookup import get_occurance_of_key, get_occurance_of_value + from nested_lookup import get_occurence_of_key, get_occurence_of_value - no_of_key_occurance = get_occurance_of_key(my_document, key='email_address') + no_of_key_occurence = get_occurence_of_key(my_document, key='email_address') - print(no_of_key_occurance) # result => 2 + print(no_of_key_occurence) # result => 2 - no_of_value_occurance = get_occurance_of_value(my_document, value='test2@example.com') + no_of_value_occurence = get_occurence_of_value(my_document, value='test2@example.com') - print(no_of_value_occurance) # result => 1 + print(no_of_value_occurence) # result => 1 Next, we could act `wild` and find all the email addresses like this: diff --git a/nested_lookup/__init__.py b/nested_lookup/__init__.py index 1da6552..ff39c13 100644 --- a/nested_lookup/__init__.py +++ b/nested_lookup/__init__.py @@ -1,2 +1,2 @@ -from .nested_lookup import nested_lookup, get_all_keys, get_occurance_of_key,\ - get_occurance_of_value +from .nested_lookup import nested_lookup, get_all_keys, get_occurence_of_key,\ + get_occurence_of_value diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index 1e22531..0173628 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -64,50 +64,50 @@ def get_all_keys(dictionary): return result_list -def get_occurance_of_key(dictionary, key): +def get_occurence_of_key(dictionary, key): """ - Method to get occurance of a key in a nested dictionary + Method to get occurence of a key in a nested dictionary Args: dictionary: Nested dictionary - key: Key to search for the occurances + key: Key to search for the occurences Return: - Number of occurance (Integer) + Number of occurence (Integer) """ return _get_occurrence(dictionary=dictionary, item='key', keyword=key) -def get_occurance_of_value(dictionary, value): +def get_occurence_of_value(dictionary, value): """ - Method to get occurance of a value in a nested dictionary + Method to get occurence of a value in a nested dictionary Args: dictionary: Nested dictionary - value: Value to search for the occurances + value: Value to search for the occurences Return: - Number of occurance (Integer) + Number of occurence (Integer) """ return _get_occurrence(dictionary=dictionary, item='value', keyword=value) def _get_occurrence(dictionary, item, keyword): """ - Method to get occurance of a key or value in a nested dictionary + Method to get occurence of a key or value in a nested dictionary Args: dictionary: Nested dictionary item: Mostly (key or value) - keyword: key word to find occurance + keyword: key word to find occurence Return: - Number of occurance of the given keyword in the dict + Number of occurence of the given keyword in the dict """ - occurance = [0] + occurence = [0] def recrusion(dictionary): if item == 'key': - occurance[0] += 1 if dictionary.get(keyword) else 0 + occurence[0] += 1 if dictionary.get(keyword) else 0 elif keyword in dictionary.values(): - occurance[0] += dictionary.values().count(keyword) + occurence[0] += dictionary.values().count(keyword) for key, value in iteritems(dictionary): if isinstance(value, dict): recrusion(dictionary=value) @@ -116,4 +116,4 @@ def _get_occurrence(dictionary, item, keyword): recrusion(dictionary=list_items) recrusion(dictionary=dictionary) - return occurance[0] + return occurence[0] diff --git a/test_nested_loopkup.py b/test_nested_loopkup.py index 447c56f..0d7eb95 100644 --- a/test_nested_loopkup.py +++ b/test_nested_loopkup.py @@ -1,7 +1,7 @@ from unittest import TestCase -from nested_lookup import nested_lookup, get_all_keys, get_occurance_of_key,\ - get_occurance_of_value +from nested_lookup import nested_lookup, get_all_keys, get_occurence_of_key,\ + get_occurence_of_value class TestNestedLookup(TestCase): @@ -161,7 +161,7 @@ class TestGetAllKeys(TestCase): self.assertIn(key, result) -class TestGetOccurance(TestCase): +class TestGetOccurence(TestCase): def setUp(self): self.sample1 = { "build_version": { @@ -218,21 +218,21 @@ class TestGetOccurance(TestCase): } def test_sample_data1(self): - result = get_occurance_of_key(self.sample1, 'build_version') + result = get_occurence_of_key(self.sample1, 'build_version') self.assertEqual(4, result) - result = get_occurance_of_value(self.sample1, '256 KB') + result = get_occurence_of_value(self.sample1, '256 KB') self.assertEqual(2, result) def test_sample_data2(self): - result = get_occurance_of_key(self.sample2, 'core_details') + result = get_occurence_of_key(self.sample2, 'core_details') self.assertEqual(1, result) - result = get_occurance_of_value(self.sample2, '4') + result = get_occurence_of_value(self.sample2, '4') self.assertEqual(3, result) def test_sample_data3(self): - result = get_occurance_of_key(self.sample3, 'total_number_of_cores') + result = get_occurence_of_key(self.sample3, 'total_number_of_cores') self.assertEqual(3, result) - result = get_occurance_of_value(self.sample3, '4') + result = get_occurence_of_value(self.sample3, '4') self.assertEqual(3, result) From 395ee0185b9c5b01f405f9e5f08429b718cc4663 Mon Sep 17 00:00:00 2001 From: Ramesh RV Date: Mon, 8 Oct 2018 16:31:51 +0530 Subject: [PATCH 3/3] New Feature (finding occurrence) - typo correction --- README.rst | 20 ++++++++++---------- nested_lookup/__init__.py | 4 ++-- nested_lookup/nested_lookup.py | 30 +++++++++++++++--------------- test_nested_loopkup.py | 18 +++++++++--------- 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/README.rst b/README.rst index 45fe9cd..0b09dcb 100644 --- a/README.rst +++ b/README.rst @@ -8,7 +8,7 @@ A small Python library which enables: #. key lookups on deeply nested documents. #. fetching all keys from a nested dictionary. -#. get the number of occurences of a key/value from a nested dictionary +#. get the number of occurrences of a key/value from a nested dictionary Documents may be built out of dictionaries (dicts) and/or lists. @@ -51,12 +51,12 @@ quick tutorial >>> get_all_keys(document) ['taco', 'salsa', 'burrito', 'taco'] - >>> from nested_lookup import get_occurence_of_key, get_occurence_of_value + >>> from nested_lookup import get_occurrence_of_key, get_occurrence_of_value - >>> get_occurence_of_key(document, key='taco') + >>> get_occurrence_of_key(document, key='taco') 2 - >>> get_occurence_of_value(document, value='42') + >>> get_occurrence_of_value(document, value='42') 1 longer tutorial @@ -102,19 +102,19 @@ To get a list of every nested key in a document, run this: ['name', 'email_address', 'other', 'secondary_email', 'EMAIL_RECOVERY', 'email_address'] -To get the number of occurence of the given key/value +To get the number of occurrence of the given key/value .. code-block:: python - from nested_lookup import get_occurence_of_key, get_occurence_of_value + from nested_lookup import get_occurrence_of_key, get_occurrence_of_value - no_of_key_occurence = get_occurence_of_key(my_document, key='email_address') + no_of_key_occurrence = get_occurrence_of_key(my_document, key='email_address') - print(no_of_key_occurence) # result => 2 + print(no_of_key_occurrence) # result => 2 - no_of_value_occurence = get_occurence_of_value(my_document, value='test2@example.com') + no_of_value_occurrence = get_occurrence_of_value(my_document, value='test2@example.com') - print(no_of_value_occurence) # result => 1 + print(no_of_value_occurrence) # result => 1 Next, we could act `wild` and find all the email addresses like this: diff --git a/nested_lookup/__init__.py b/nested_lookup/__init__.py index ff39c13..e2dc9a0 100644 --- a/nested_lookup/__init__.py +++ b/nested_lookup/__init__.py @@ -1,2 +1,2 @@ -from .nested_lookup import nested_lookup, get_all_keys, get_occurence_of_key,\ - get_occurence_of_value +from .nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\ + get_occurrence_of_value diff --git a/nested_lookup/nested_lookup.py b/nested_lookup/nested_lookup.py index 0173628..da8e4c1 100644 --- a/nested_lookup/nested_lookup.py +++ b/nested_lookup/nested_lookup.py @@ -64,50 +64,50 @@ def get_all_keys(dictionary): return result_list -def get_occurence_of_key(dictionary, key): +def get_occurrence_of_key(dictionary, key): """ - Method to get occurence of a key in a nested dictionary + Method to get occurrence of a key in a nested dictionary Args: dictionary: Nested dictionary - key: Key to search for the occurences + key: Key to search for the occurrences Return: - Number of occurence (Integer) + Number of occurrence (Integer) """ return _get_occurrence(dictionary=dictionary, item='key', keyword=key) -def get_occurence_of_value(dictionary, value): +def get_occurrence_of_value(dictionary, value): """ - Method to get occurence of a value in a nested dictionary + Method to get occurrence of a value in a nested dictionary Args: dictionary: Nested dictionary - value: Value to search for the occurences + value: Value to search for the occurrences Return: - Number of occurence (Integer) + Number of occurrence (Integer) """ return _get_occurrence(dictionary=dictionary, item='value', keyword=value) def _get_occurrence(dictionary, item, keyword): """ - Method to get occurence of a key or value in a nested dictionary + Method to get occurrence of a key or value in a nested dictionary Args: dictionary: Nested dictionary item: Mostly (key or value) - keyword: key word to find occurence + keyword: key word to find occurrence Return: - Number of occurence of the given keyword in the dict + Number of occurrence of the given keyword in the dict """ - occurence = [0] + occurrence = [0] def recrusion(dictionary): if item == 'key': - occurence[0] += 1 if dictionary.get(keyword) else 0 + occurrence[0] += 1 if dictionary.get(keyword) else 0 elif keyword in dictionary.values(): - occurence[0] += dictionary.values().count(keyword) + occurrence[0] += dictionary.values().count(keyword) for key, value in iteritems(dictionary): if isinstance(value, dict): recrusion(dictionary=value) @@ -116,4 +116,4 @@ def _get_occurrence(dictionary, item, keyword): recrusion(dictionary=list_items) recrusion(dictionary=dictionary) - return occurence[0] + return occurrence[0] diff --git a/test_nested_loopkup.py b/test_nested_loopkup.py index 0d7eb95..f3e1f4c 100644 --- a/test_nested_loopkup.py +++ b/test_nested_loopkup.py @@ -1,7 +1,7 @@ from unittest import TestCase -from nested_lookup import nested_lookup, get_all_keys, get_occurence_of_key,\ - get_occurence_of_value +from nested_lookup import nested_lookup, get_all_keys, get_occurrence_of_key,\ + get_occurrence_of_value class TestNestedLookup(TestCase): @@ -161,7 +161,7 @@ class TestGetAllKeys(TestCase): self.assertIn(key, result) -class TestGetOccurence(TestCase): +class TestGetOccurrence(TestCase): def setUp(self): self.sample1 = { "build_version": { @@ -218,21 +218,21 @@ class TestGetOccurence(TestCase): } def test_sample_data1(self): - result = get_occurence_of_key(self.sample1, 'build_version') + result = get_occurrence_of_key(self.sample1, 'build_version') self.assertEqual(4, result) - result = get_occurence_of_value(self.sample1, '256 KB') + result = get_occurrence_of_value(self.sample1, '256 KB') self.assertEqual(2, result) def test_sample_data2(self): - result = get_occurence_of_key(self.sample2, 'core_details') + result = get_occurrence_of_key(self.sample2, 'core_details') self.assertEqual(1, result) - result = get_occurence_of_value(self.sample2, '4') + result = get_occurrence_of_value(self.sample2, '4') self.assertEqual(3, result) def test_sample_data3(self): - result = get_occurence_of_key(self.sample3, 'total_number_of_cores') + result = get_occurrence_of_key(self.sample3, 'total_number_of_cores') self.assertEqual(3, result) - result = get_occurence_of_value(self.sample3, '4') + result = get_occurrence_of_value(self.sample3, '4') self.assertEqual(3, result)