Skip to main content

list like object allowing selection by name and index

Project description

named_sequence - list like object allowing selection by name and index

This package contains the class NamedSequence.
This class reproduces the behaviour of a list (from which it inherits) but most list special functions such as _getitem_ allow name and index as argument.
Methods formerly returning list (such as slicing with _getitem_ or copy) now return a NamedSequence instance.

NamedSequence instances requires an additional argument : the name of the method of the items to get their name.

Basic usage

Instanciation

from named_sequence import NamedSequence

class NamedItem(object):  # Used only to illusrate the behaviour
    def __init__(self, name):
        self.__name = name

    def get_name(self):
        return self.__name

the_instance = NamedSequence((NamedItem("a"), NamedItem("b")), "get_name")

getting or setting items

a1 = the_instance["a"]
a2 = the_instance[0]
a1 is a2
>>> True

the_instance["b"] = NamedItem("a")
the_instance["b"] = NamedItem("d")  # the name "b" no more exists
>>> KeyError: "No item named 'b' !

the_instance["a"] = NamedItem("a2")  # When name are present several time, the fist occurence is considered
for elem in the_instance:
    print(elem.get_name())
>>> a2
>>> a

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

named_sequence-1.0-py3-none-any.whl (4.6 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