Skip to main content

nindex

Project description

nindex

Classes to provide efficient indexing and slicing operations into a list of objects by certain attribute key.

Installation

pip install ninstall

Usage

  1. Create your list of objects:
class Item():
    def __init__(self, value, name):
        self.value = value
        self.name = name

items = [
    Item(1, "alpha"),
    Item(3, "beta"),
    Item(8, "gamma"),
    Item(22, "delta")
]
  1. Wrap the list in a new Index, and specify which attribute will be the key:
from nindex import Index
itemindex = Index(items, "value")
  1. Quickly look up objects by attribute key:
 # Look for the object with this exact value
item = itemindex[3]
print(item.name)
# 'beta'

# Look for the closest object with less than or equal to this value
item = itemindex.le(20)
print(item.name)
# 'gamma'

# Look for all objects with values in the given range
items = itemindex[2:22] #
for item in items:
    print(item.name)
# 'beta'
# 'gamma'

Limitations

This with only work if:

  • The list is sorted
  • The list doesn't change
  • The keyed attributes don't change
  • The keyed attributes contain no duplicate values

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

nindex-1.0.0.tar.gz (3.3 kB view hashes)

Uploaded Source

Built Distribution

nindex-1.0.0-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page