Wrapper for a list of objects that allows to create indexes for faster lookups
Project description
ListLookup
Wrapper for faster lookups in a list of objects/dictionaries. ATTENTION Do not modify list once indexes are created!
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("id", lambda d: d['id'], True)
cities.index("country", lambda d: d['country'])
list(cities.lookup(id=1))
>>> [{"id": 1, "country": "us", name: "Atlanta"}]
list(cities.lookup(country="us", preserve_order=True))
>>> [{"id": 1, "country": "us", name: "Atlanta"}, {"id": 2, "country": "us", name: "Miami"}]
list(cities.lookup(id=2, country="uk"))
>>> []
cities.index('name', lambda d: d['name'])
list(cities.lookup(name=lambda val: val.startswith('B'))
>>> [{"id": 3, "country": "uk", name: "Britain"}, {"id": 4, "country": "ca", "name": "Barrie"}]
Case insensitive index
This is not supported out of the box. You need to use same case for index and lookup values. E.g. use .lower()
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("country_ci", lambda d: d['country'].lower())
list(cities.lookup(country_ci="UK".lower()))
>>> [{"id": 3, "country": "uk", name: "Britain"}]
Multiple pointers per item
You can have same record be referenced by multiple values. In the following example "uk" and "uk2" correspond to the same record because they are returned as alternatives during indexing:
from listlookup import ListLookup
cities = ListLookup([
{"id": 1, "country": "us", name: "Atlanta"},
{"id": 2, "country": "us", name: "Miami"},
{"id": 3, "country": "uk", name: "Britain"},
{"id": 4, "country": "ca", "name": "Barrie"},
])
cities.index("country_alt", lambda d: [d['country'], "%s2" % d['country']], multiple=True)
list(cities.lookup(country_alt="uk"))
>>> [{"id": 3, "country": "uk", name: "Britain"}]
list(cities.lookup(country_alt="uk2"))
>>> [{"id": 3, "country": "uk", name: "Britain"}]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
list-lookup-1.0.7.tar.gz
(3.8 kB
view details)
Built Distribution
File details
Details for the file list-lookup-1.0.7.tar.gz
.
File metadata
- Download URL: list-lookup-1.0.7.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7e0f1b584519b621d8d597057e0e1d8349f233df23056451a4f21f9106a1875 |
|
MD5 | 317fde0d520055f637ba78cb950a1d00 |
|
BLAKE2b-256 | 866490aa9a6b18cbfcc59cac900a11083befbf21ea5a98561579ffb1f7886864 |
File details
Details for the file list_lookup-1.0.7-py2.py3-none-any.whl
.
File metadata
- Download URL: list_lookup-1.0.7-py2.py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 277f883cc9449b7450eab2195c3768e22d74d3d914f2ac18e9ad83f661e066ec |
|
MD5 | 7843321db1efad7a2fd496bb3aeaac89 |
|
BLAKE2b-256 | 7f5b39cd180417e1f6b27351ebd3ac2d28e8aedf7c107795f1b80c3be5e5499e |