Source code

Download source archive ↓

src/openpyxl/descriptors/slots.py

1# Metaclass for mixing slots and descriptors
2# From "Programming in Python 3" by Mark Summerfield Ch.8 p. 383
3
4class AutoSlotProperties(type):
5
6    def __new__(mcl, classname, bases, dictionary):
7        slots = list(dictionary.get("__slots__", []))
8        for getter_name in [key for key in dictionary if key.startswith("get_")]:
9            name = getter_name
10            slots.append("__" + name)
11            getter = dictionary.pop(getter_name)
12            setter = dictionary.get(setter_name, None)
13            if (setter is not None
14                and isinstance(setter, collections.Callable)):
15                del dictionary[setter_name]
16            dictionary[name] = property(getter. setter)
17            dictionary["__slots__"] = tuple(slots)
18            return super().__new__(mcl, classname, bases, dictionary)