Motivation
In the usual Python library enum
, a subclass of Enum
would create all its members upon class creation. This is usually a large waste of time and reminiscence, particularly if the category accommodates a number of members however solely a fraction of them would ever get referred to as.
As a follow undertaking on metaclasses, I made a decision to implement a lazy model of Enum
. This isn’t meant to be a full-fledged library, so I did not implement many of the options in the usual enum
library, just a few primary functionalities.
In LazyEnum
, a member can be created solely after we ask to and it will be created solely as soon as. For instance, let say MyClass
is a subclass of LazyEnum
. The primary time we name MyClass(1)
an object of sort MyClass
is created. Any subsequent name to MyClass(1)
merely return the identical object. Furthermore, I wish to put some validation on member creation, say we might solely enable n
to be a constructive integer when calling MyClass(n)
.
This concept is impressed by Ethan Furman’s reply to my earlier query right here and in addition a dialogue on Udemy with Fred Baptiste, who’s the teacher of the Python Three Deep Dive sequence.
Earlier than wanting on the code, let me present an instance of how you can use LazyEnum
.
Instance
from lazyenum import LazyEnum
class Product(LazyEnum):
def _validate_identifier_value(product_id):
# particular technique utilized by metaclass for validation
return isinstance(product_id, int) and (1001 <= product_id <= 9999)
COMPANY_NAME = 'Our Instance Firm'
def __init__(self, product_id, product_title):
# no must retailer 'product_id' as occasion attribute
self.product_title = product_title
def __repr__(self):
return f'Product({self.product_id!r}, {self.product_title!r})'
Comment:
LazyEnum
is created by a personal metaclass_LazyEnumMeta
.- The primary non-self parameter of
__init__
is mechanically grabbed by the metaclass and cached, so we needn’t set it as an example attribute. If we writeself.product_id = product_id
, it will increase an error after we attempt to initialize a brand new member. This parameter is named the identifier subject and its worth is named the identifier worth, which uniquely establish every member. - The metaclass would see if there’s a technique named
_validate_identifier_value
and use it for validation. It may be outlined as a static technique or a category technique, but when we outline it as a category technique, we would want to brighten it with@classmethod
. Right here we simply outline it as a static technique.
Allow us to see some instance outputs. First, we will initialize a member as standard and name it by its identifier worth.
>>> prod1 = Product(1001, 'Our Good First Product')
>>> Product(1001)
Product(1001, 'Our Good First Product')
>>> prod1 is Product(1001)
True
We will get the identifier worth by utilizing .identifier_field
or straight calling the occasion attribute (.product_id
on this case). The .identifier_field
would give us a nametuple referred to as Identifier
, whose first entry is the attribute title and the second entry is the worth.
>>> prod1.identifier_field
Identifier(field_name='product_id', worth=1001)
>>> prod1.product_id
1001
Error can be raised if we try to create a brand new member with an present identifier worth. After all, the identical factor would occur if we use an invalid identifier worth.
>>> Product(1001, 'This Is Nonetheless The First Product')
ValueError: Member with identifier worth 1001 already exists. Cannont move further arguments ('This Is Nonetheless The First Product',) or {}.
>>> Product(1, 'Product With Invaild ID')
ValueError: Identifier subject 'product_id' has invalid worth 1.
Within the common Enum
, you may set aliases to a member. Proper now we did not set any alias, however we will achieve this utilizing dot notation and see all aliases of a member utilizing .all_aliases
. We will additionally concurrently create a brand new member and set an alias to it.
>>> prod1.all_aliases
[]
>>> Product.product_one = prod1
>>> Product.first_product = Product.product_one
>>> prod1.all_aliases
['product_one', 'first_product']
>>>
>>> Product.product_two = Product(1002, 'The Wonderful Second Product')
>>> Product.product_two
Product(1002, 'The Wonderful Second Product')
>>> Product(1002).all_aliases
['product_two']
However watch out, we might unintentionally overwrite different class attributes.
>>> Product.COMPANY_NAME
'Our Instance Firm'
>>> Product.COMPANY_NAME = prod1
>>> prod1.all_aliases
['product_one', 'first_product', 'COMPANY_NAME']
>>> Product.COMPANY_NAME
Product(1001, 'Our Good First Product')
>>>
>>> Product.COMPANY_NAME = 'Our Instance Firm'
>>> prod1.all_aliases
['product_one', 'first_product']
We will change occasion attributes that aren’t the identifier subject. Trying to vary the identifier subject would increase an error.
>>> prod1.product_title = 'First Product With New Identify'
>>> prod1
Product(1001, 'First Product With New Identify')
>>> prod1.product_id = 2001
AttributeError: cannot set attribute
We will iterate over the category members.
>>> Product(1003, 'Even Extra Wonderful Third Product')
Product(1003, 'Even Extra Wonderful Third Product')
>>> for prod in Product: print(prod)
Product(1001, 'First Product With New Identify')
Product(1002, 'The Wonderful Second Product')
Product(1003, 'Even Extra Wonderful Third Product')
>>> len(Product)
3
Lastly, the category has properties .identifier_value_map
and .alias_to_member_map
, which assist examine all members. Observe that we did not set any alias to Product(1003)
.
>>> Product.identifier_value_map
mappingproxy({
1001: Product(1001, 'First Product With New Identify'),
1002: Product(1002, 'The Wonderful Second Product'),
1003: Product(1003, 'Even Extra Wonderful Third Product')
})
>>> Product.alias_to_member_map
mappingproxy(OrderedDict([
('product_one', Product(1001, 'First Product With New Name')),
('first_product', Product(1001, 'First Product With New Name')),
('product_two', Product(1002, 'The Amazing Second Product'))
]))
The Code
Right here is the code.
# lazyenum.py
from collections import namedtuple, OrderedDict
from sorts import MappingProxyType
_Identifier = namedtuple('Identifier', 'field_name worth')
def _get_identifier_value(self):
# use this operate to monkey patch the category
id_map = sort(self)._object_id_to_value_map
return id_map[id(self)]
class _LazyEnumMeta(sort):
def __new__(mcls, title, bases, attrs):
attrs['_object_id_to_value_map'] = {}
attrs['_identifier_value_map'] = {}
attrs['_alias_to_member_map'] = OrderedDict()
cls = tremendous().__new__(mcls, title, bases, attrs)
# seize the primary parameter title from the __init__ technique
# then inject it to the category as a read-only property
id_name = cls.__init__.__code__.co_varnames[1]
cls._identifier_field_name = id_name
setattr(cls, id_name, property(_get_identifier_value))
return cls
def __call__(cls, worth, *args, **kwargs):
# depend on the category to offer the validation technique
if not cls._validate_identifier_value(worth):
increase ValueError(f'Identifier subject {cls._identifier_field_name!r} '
f'has invalid worth {worth!r}.')
# create a brand new memeber iff no present member has the identical identifier worth
if worth not in cls._identifier_value_map:
new_member = tremendous().__call__(worth, *args, **kwargs)
cls._object_id_to_value_map[id(new_member)] = worth
cls._identifier_value_map[value] = new_member
elif args or kwargs:
increase ValueError(f'Member with identifier worth {worth!r} already exists. '
f'Cannont move further arguments {args} or {kwargs}.')
return cls._identifier_value_map[value]
def __contains__(cls, different):
return different in cls._identifier_value_map.values()
def __len__(cls):
return len(cls._identifier_value_map)
def __iter__(cls):
yield from cls._identifier_value_map.values()
def __setattr__(cls, attr_name, attr_value):
if attr_name in cls._alias_to_member_map:
del cls._alias_to_member_map[attr_name]
# examine if we're setting title to a category member
if attr_value in cls:
cls._alias_to_member_map[attr_name] = attr_value
tremendous().__setattr__(attr_name, attr_value)
def __delattr__(cls, attr_name):
if attr_name in cls._alias_to_member_map:
del cls._alias_to_member_map[attr_name]
tremendous().__delattr__(attr_name)
@property
def identifier_value_map(cls):
return MappingProxyType(cls._identifier_value_map)
@property
def alias_to_member_map(cls):
return MappingProxyType(cls._alias_to_member_map)
class LazyEnum(metaclass=_LazyEnumMeta):
# the primary two strategies function the defaults if a subclass did not present them
# to keep away from error when _LazyEnumMeta makes an attempt to make use of these two strategies
def _validate_identifier_value(worth):
return True
def __init__(self, identifier_value):
move
@property
def identifier_field(self):
id_name = sort(self)._identifier_field_name
return _Identifier(id_name, getattr(self, id_name))
@property
def all_aliases(self):
pairs = sort(self)._alias_to_member_map.objects()
return [alias for alias, member in pairs if member is self]
Questions
1.
The above code does not work nicely with dataclasses. If we write
from lazyenum import LazyEnum
from dataclasses import dataclass
@dataclass
class Product(LazyEnum):
def _validate_identifier_value(product_id):
return isinstance(product_id, int) and (1001 <= product_id <= 9999)
product_id : int
product_title : str
then sort the next within the console:
>>> prod1 = Product(1001, 'First Product')
>>> prod1.product_id = 2001
>>> Product(2001)
TypeError: __init__() lacking 1 required positional argument: 'product_title'
>>> Product(1001)
Product(product_id=2001, product_title='First Product')
We will change the product_id
however the member remains to be recognized by the outdated worth! How can I repair this?
- Other than the difficulty of dataclasses, is there any drawback within the above code? The place can I make enhancements?