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)
```
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 | RSS feed
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 details)
File details
Details for the file list_property-0.0.1.tar.gz
.
File metadata
- Download URL: list_property-0.0.1.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40d8728905dfd66b8aedc6b486c0d138b31fb517ebf8228c707ad16494e22756 |
|
MD5 | c63d0a810218c3d5c23917f3f3354eca |
|
BLAKE2b-256 | 26142b17d528304a7120368dd5fc722c255c7435613d8468104ed31bf103814f |