first commit, working with tests.
new file: .gitignore new file: README.rst new file: __init__.py new file: nested_lookup.py new file: setup.py new file: test_nested_loopkup.py
This commit is contained in:
commit
b530c0a586
6 changed files with 111 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
__pycache__*
|
||||
*.pyc
|
||||
22
README.rst
Normal file
22
README.rst
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
nested_lookup
|
||||
#############
|
||||
|
||||
A small library for python which enables key lookups on deeply nested documents.
|
||||
|
||||
Documents may be built out of dictionaries (dicts) and/or lists.
|
||||
|
||||
:author: Russell Ballestrini
|
||||
|
||||
:license: Public Domain
|
||||
|
||||
quick start
|
||||
===========
|
||||
|
||||
install
|
||||
-------
|
||||
|
||||
tutorial
|
||||
--------
|
||||
|
||||
|
||||
|
||||
0
__init__.py
Normal file
0
__init__.py
Normal file
23
nested_lookup.py
Normal file
23
nested_lookup.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
def nested_lookup(key, document):
|
||||
"""Lookup a key in a nested document, return a list of values"""
|
||||
return list(_nested_lookup(key, document))
|
||||
|
||||
def _nested_lookup(key, document):
|
||||
"""Lookup a key in a nested document, yield a value"""
|
||||
if isinstance(document, list):
|
||||
for d in document:
|
||||
for result in _nested_lookup(key, d):
|
||||
yield result
|
||||
|
||||
if isinstance(document, dict):
|
||||
for k, v in document.iteritems():
|
||||
if k == key:
|
||||
yield v
|
||||
elif isinstance(v, dict):
|
||||
for result in _nested_lookup(key, v):
|
||||
yield result
|
||||
elif isinstance(v, list):
|
||||
for d in v:
|
||||
for result in _nested_lookup(key, d):
|
||||
yield result
|
||||
|
||||
26
setup.py
Normal file
26
setup.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# installation: pip install nested-lookup
|
||||
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name = 'nested-lookup',
|
||||
version = '0.0.1',
|
||||
description = 'lookup a key in a deeply nested document of dicts and lists',
|
||||
keywords = 'nested document dictionary dict list lookup schema json xml',
|
||||
long_description = open('README.rst').read(),
|
||||
|
||||
author = 'Russell Ballestrini',
|
||||
author_email = 'russell@ballestrini.net',
|
||||
url = 'https://github.com/russellballestrini/nested-lookup',
|
||||
|
||||
platforms = ['All'],
|
||||
license = 'Public Domain',
|
||||
|
||||
py_modules = ['nested_lookup'],
|
||||
include_package_data = True,
|
||||
)
|
||||
|
||||
# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools
|
||||
|
||||
# built and uploaded to pypi with this:
|
||||
# python setup.py sdist bdist_egg register upload
|
||||
38
test_nested_loopkup.py
Normal file
38
test_nested_loopkup.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from unittest import TestCase
|
||||
|
||||
from nested_lookup import nested_lookup
|
||||
|
||||
class TestNestedLookup(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.subject_dict = {'a':1,'b':{'d':100},'c':{'d':200}}
|
||||
|
||||
def test_nested_lookup(self):
|
||||
results = nested_lookup('d', self.subject_dict)
|
||||
self.assertEqual(2, len(results))
|
||||
self.assertIn(100, results)
|
||||
self.assertIn(200, results)
|
||||
self.assertSetEqual({100,200}, set(results))
|
||||
|
||||
def test_nested_lookup_wrapped_in_list(self):
|
||||
results = nested_lookup('d', [{}, self.subject_dict, {}])
|
||||
self.assertEqual(2, len(results))
|
||||
self.assertIn(100, results)
|
||||
self.assertIn(200, results)
|
||||
self.assertSetEqual({100,200}, set(results))
|
||||
|
||||
def test_nested_lookup_wrapped_in_list_in_dict_in_list(self):
|
||||
results = nested_lookup('d', [{}, {'H' : [self.subject_dict]} ])
|
||||
self.assertEqual(2, len(results))
|
||||
self.assertIn(100, results)
|
||||
self.assertIn(200, results)
|
||||
self.assertSetEqual({100,200}, set(results))
|
||||
|
||||
def test_nested_lookup_wrapped_in_list_in_list(self):
|
||||
results = nested_lookup('d', [ {}, [self.subject_dict, {}] ])
|
||||
self.assertEqual(2, len(results))
|
||||
self.assertIn(100, results)
|
||||
self.assertIn(200, results)
|
||||
self.assertSetEqual({100,200}, set(results))
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue