Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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


Release history Release notifications

Download files

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

Files for list-property, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size list_property-0.0.1.tar.gz (5.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page