aeidon/enum.py

Source code for module aeidon.enum from file aeidon/enum.py.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
# -*- coding: utf-8 -*-

# Copyright (C) 2005 Osmo Salomaa
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""
Lists of named constants with integer values.

:class:`EnumerationItem`s are subclassed from :class:`int`, so they are actual
integers. The string value of an item will be the name that it was defined with
in its set. New items can always be added to an enumeration.
"""

__all__ = ("EnumerationItem", "Enumeration",)

# TODO: Dump this code and use the enum module added in Python 3.4?


class EnumerationItem(int):

    """
    Named constant with an integer value.

    :class:`EnumerationItem` can be instantiated without any specified values
    and users need not bother with them. Instantiation with correct values
    harmonous with the rest of the items should be left up to the parent list.
    """

    def __new__(cls, value=0, name="", parent=None):
        """Return integer instance with additional attributes."""
        instance = int.__new__(cls, value)
        instance.name = name
        instance.parent = parent
        return instance

    def __bool__(self):
        """For consistency, always return ``True``."""
        return True

    def __str__(self):
        """Return name as the string representation."""
        return self.name


class Enumeration(list):

    """
    List of named constants with integer values.

    :class:`Enumeration` is an actual :class:`list` where enumeration items are
    stored as both list items and instance attributes. New items should be
    added by setting an instance attribute.

    Typical use to create a new enumeration would be something like::

        fruits = aeidon.Enumeration()
        fruits.APPLE = aeidon.EnumerationItem()
        fruits.MANGO = aeidon.EnumerationItem()
        fruits.APPLE.size = 10
        fruits.MANGO.size = 20

    Note that there is no finalization of an enumeration. New items can always
    be added just by assigning a new attribute to the enumeration. Likewise,
    existing items can always be removed using :func:`delattr`.
    """

    NONE = None

    def __contains__(self, item):
        """Return ``True`` is enumeration contains `item`."""
        return list.__contains__(self, item)

    def __delattr__(self, name):
        """Delete enumeration item and attribute."""
        list.remove(self, getattr(self, name))
        return object.__delattr__(self, name)

    def find_item(self, name, value):
        """Return the first found item with the given attribute `value`."""
        for item in self:
            if getattr(item, name) == value:
                return item
        raise ValueError("Name {} not found".format(repr(name)))

    def __setattr__(self, name, value):
        """Set value of enumeration item with correct attributes."""
        if isinstance(value, EnumerationItem):
            value = value.__class__(len(self), name, self)
            list.append(self, value)
        return object.__setattr__(self, name, value)