Skip to main content

Library for lists indexes having associated names like namedtuple and property. Includes list_property and namedlist functions.

Project description

# list_property
Module for making list properties or named lists


## list_property - function
```python
import list_property

class Person(list):
first_name = list_property(0)
last_name = list_property(1)
middle_initial = list_property(2, '')

p = Person(("John", "Smith"))
assert p == ["John", "Smith"]

assert p.first_name == 'John'
assert p[0] == 'John'
assert p.last_name == 'Smith'
assert p[1] == 'Smith'
assert p.middle_initial == ''
try:
assert p[2] == ''
raise AssertionError('Index 2 was not set and there should be an IndexError!')
except IndexError:
pass # Success

p.first_name = "Hello"
p.last_name = 'World!'
p.middle_initial = 'T'
assert p.first_name == 'Hello'
assert p[0] == 'Hello'
assert p.last_name == 'World!'
assert p[1] == 'World!'
assert p.middle_initial == 'T'
assert p[2] == 'T' # Note: p[2] is now set
```
This class also works like a property decorator

```python
import list_property

class Person(list):
@list_property(0)
def first_name(self):
try:
return self[0]
except:
return 'anonymous'

middle_initial = list_property(2)

@middle_initial.setter
def middle_initial(self, value):
self[2] = str(value)[0].upper()
```


## namedlist
Named list factory function like named tuple

```python
from list_property import namedlist, NamedList

Person = namedlist('Person', 'first_name last_name middle_initial', {'middle_initial': 'T'})

p = Person('John', 'Smith')
assert p.first_name == 'John'
assert p[0] == 'John'
assert p.last_name == 'Smith'
assert p[1] == 'Smith'
assert p.middle_initial == 'T'
assert p[2] == 'T'
assert isinstance(p, list)
assert isinstance(p, NamedList)
```

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_property-0.0.1.tar.gz (5.2 kB view hashes)

Uploaded Source

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