Skip to main content

editabletuple

Overview

This module provides the editabletuple() and editableobject() functions.

The editabletuple() function is used tor creating classes with a fixed sequence of fields, similar to a namedtuple, except editable.

Each instance of a class created by the editabletuple() function's fields can be accessed by index et[i] (or by slice), or by fieldname et.name. Although fields can be read and written, they cannot be added or deleted. Since instances are mutable they can't be used in sets or as dict keys.

If you provide a validator, it will be used when new instances are created and updated.

The editableobject() function creates classes very similar to those created by editabletuple(). The essential difference is that editableobject()'s class's instances don't support indexing or iteration, so support only fieldname access. They also have an addtional totuple property (not needed for editabletuple()s since tuple(et) is sufficient due to their iteration support).

See the function docstrings for examples and more about the editabletuple and editableobject APIs.

To install just use python3 -m pip install editabletuple. (See PyPI.)

Or just copy the editabletuple.py file which is self-contained and depends only on the standard library.

Examples

Example #1: no defaults; no validator

>>> Options = editabletuple('Options', 'maxcolors shape zoom restore')
>>> options = Options(5, 'square', 0.9, True)
>>> options
Options(maxcolors=5, shape='square', zoom=0.9, restore=True)
>>> options.maxcolors = 7
>>> options[-1] = False
>>> options[2] -= 0.1
>>> options
Options(maxcolors=7, shape='square', zoom=0.8, restore=False)

Example #2: with defaults but no validator

>>> Rgb = editabletuple('Rgb', 'red green blue', defaults=(0, 0, 0))
>>> black = Rgb()
>>> black
Rgb(red=0, green=0, blue=0)
>>> navy = Rgb(blue=128)
>>> navy
Rgb(red=0, green=0, blue=128)
>>> violet = Rgb(238, 130, 238)
>>> violet
Rgb(red=238, green=130, blue=238)

Example #3: with defaults and a validator

If you provide a validator function, it will be called whenever an attempt is made to set a value, whether at construction time or later by et[i] = value or et.fieldname = value. It is passed an attribute name and an attribute value. It should check the value and either return the value (or an acceptable alternative value) which will be the one actually set, or raise a ValueError.

>>> def validate_rgba(name, value):
...     if name == 'alpha':
...         if not (0.0 <= value <= 1.0):
...             return 1.0 # silently default to opaque
...     elif not (0 <= value <= 255):
...         raise ValueError(f'color value must be 0-255, got {value}')
...     return value # must return a valid value or raise ValueError
>>>
>>> Rgba = editabletuple('Rgba', 'red', 'green', 'blue', 'alpha',
...                      defaults=(0, 0, 0, 1.0), validator=validate_rgba)
>>> black = Rgba()
>>> black
Rgba(red=0, green=0, blue=0, alpha=1.0)
>>> seminavy = Rgba(blue=128, alpha=0.5)
>>> seminavy
Rgba(red=0, green=0, blue=128, alpha=0.5)
>>> violet = Rgba(238, 130, 238, alpha=2.5) # alpha too big
>>> violet
Rgba(red=238, green=130, blue=238, alpha=1.0)
>>>
>>> color = Rgba(green=99)
>>> color
Rgba(red=0, green=99, blue=0, alpha=1.0)
>>> assert color.green == 99
>>> color.red = 128
>>> assert color[2] == 0
>>> color[2] = 240
>>> assert color[2] == 240
>>> color[-1] = 0.5
>>> color
Rgba(red=128, green=99, blue=240, alpha=0.5)
>>> color[1] = 299
Traceback (most recent call last):
    ...
ValueError: color value must be 0-255, got 299
>>> color.blue = -65
Traceback (most recent call last):
    ...
ValueError: color value must be 0-255, got -65

These examples—and several others—are in the module's function's docstrings.

API

def editabletuple(classname, *fieldnames, defaults=None, validator=None, doc=None):

Creates a new class called classname with the given fieldnames, optional defaults, optional validator, and optional doc docstring.

Instances of the class behave almost exactly like collections.namedtuple's except that fields may be set as well as get using their index position or fieldname. They support len(), in, the comparison operators, and are iterable—which means they can be converted to a list or tuple by passing to either's eponymous factory function. They also provide an .asdict property, and also an update() method that accepts name=value arguments.

def editableobject(classname, *fieldnames, defaults=None, validator=None, doc=None):

Creates a new class called classname with the given fieldnames, optional defaults, optional validator, and optional doc docstring.

Instances of the class have fields which can get and set by fieldname. They support the comparison operators and .astuple and .asdict properties, the former returning a tuple of the instance's values, the latter a dict of fielname-value items. It also has an update() method that accepts name=value arguments.

Notes

I can't work out how to make editabletuple and editableobject instances picklable. Patches or suggestions on how to do this would be welcome.


License: GPLv3

Metadata

Release files for editabletuple 1.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for editabletuple 1.5.3
File Size Uploaded
editabletuple-1.5.3.tar.gz 20.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for editabletuple 1.5.3
File Interpreter ABI Platform
editabletuple-1.5.3-py3-none-any.whl Python 3 none any Details

Total release size: 40.4 kB

Release files / editabletuple-1.5.3.tar.gz

Download URL editabletuple-1.5.3.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
688bf2ddd1c88bdb9c0e845a87720f7b6065c9bf9bee8dc9c86e3ea4f7f49fdc
BLAKE2b-256 checksum
How to use checksums
1adb3b432f93d633da854d76577d2754c3a80a759f4db138861eefbaf5d02152
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

Release files / editabletuple-1.5.3-py3-none-any.whl

Download URL editabletuple-1.5.3-py3-none-any.whl
Size 20.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
805821e4befb8b0538737a3e6c7a057a7fc2906a2c8a80e85d818b3cbcca51f1
BLAKE2b-256 checksum
How to use checksums
561529e717087078f9612d231ad0f9ea3a66b0b5ae8eb5a0d312b01f819ccac0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.4.2 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.9.2

Release history Release notifications | RSS feed

This release

1.5.3 This release

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.5

2 release files

1.4.4

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page