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
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
File details
Details for the file named_sequence-1.0-py3-none-any.whl
.
File metadata
- Download URL: named_sequence-1.0-py3-none-any.whl
- Upload date:
- Size: 4.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b9586930f55a71a193e4aada3e20225fc9bad80104547d21ebb07ab1f977d2b |
|
MD5 | 80606b3cef7b07ff4cedd2334f3a10f0 |
|
BLAKE2b-256 | 02c8d0eb2a5cb94d0a361a25b32b39ef73f5aea7ded61cce7416ba8f785dc738 |