| t | import itertools | t | import itertools |
| import string | | import string |
| | | |
| def slotgen(number): | | def slotgen(number): |
| | | |
| def decorator(cls): | | def decorator(cls): |
| | | |
| def generate_slot_names(count): | | def generate_slot_names(count): |
| if count <= 0: | | if count <= 0: |
| return [] | | return [] |
| length = 1 | | length = 1 |
| while len(string.ascii_lowercase) ** length < count: | | while len(string.ascii_lowercase) ** length < count: |
| length += 1 | | length += 1 |
| names = [] | | names = [] |
| for combo in itertools.product(string.ascii_lowercase, repea | | for combo in itertools.product(string.ascii_lowercase, repea |
| t=length): | | t=length): |
| if len(names) >= count: | | if len(names) >= count: |
| break | | break |
| names.append(''.join(combo)) | | names.append(''.join(combo)) |
| return names | | return names |
| slot_names = generate_slot_names(number) | | slot_names = generate_slot_names(number) |
| class_attrs = {} | | class_attrs = {} |
| for name, value in cls.__dict__.items(): | | for name, value in cls.__dict__.items(): |
| if not name.startswith('__') and name not in slot_names: | | if not name.startswith('__') and name not in slot_names: |
| class_attrs[name] = value | | class_attrs[name] = value |
| class_attrs['__slots__'] = tuple(slot_names) | | class_attrs['__slots__'] = tuple(slot_names) |
| new_class = type(f'Slotted{cls.__name__}', (object,), class_attr | | new_class = type(f'Slotted{cls.__name__}', (object,), class_attr |
| s) | | s) |
| return new_class | | return new_class |
| return decorator | | return decorator |