New Feature (finding occurrence) - typo correction
This commit is contained in:
parent
7692fc2754
commit
395ee0185b
4 changed files with 36 additions and 36 deletions
20
README.rst
20
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:
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue