Чэнь Сюаньдун, 619/2 группа RecordPlus 15380
Ван Цзыци, 614 группа RecordPlus 15390
t1def Record(slots_str, **readonly_fields):t1def Record(slots_str, **readonly_fields):
22
3    def decorator(cls):3    def decorator(cls):
4        if not hasattr(cls, '__slots__'):4        if not hasattr(cls, '__slots__'):
5            raise TypeError('Class must use __slots__ as its object model')5            raise TypeError('Class must use __slots__ as its object model')
6        new_slots = sorted(set(cls.__slots__) | set(slots_str.split()))6        new_slots = sorted(set(cls.__slots__) | set(slots_str.split()))
7        setattr(cls, '__slots__', new_slots)7        setattr(cls, '__slots__', new_slots)
8        for field in dir(cls):8        for field in dir(cls):
9            if field.startswith('_') is False:9            if field.startswith('_') is False:
10                setattr(cls, field, getattr(cls, field))10                setattr(cls, field, getattr(cls, field))
11        for field, value in readonly_fields.items():11        for field, value in readonly_fields.items():
12            if field.startswith('_') is False:12            if field.startswith('_') is False:
13                setattr(cls, field, value)13                setattr(cls, field, value)
1414
15        def set_obj(self, attr, value):15        def set_obj(self, attr, value):
16            if attr in cls.__slots__:16            if attr in cls.__slots__:
17                setattr(cls, attr, value)17                setattr(cls, attr, value)
18        cls.__setattr__ = set_obj18        cls.__setattr__ = set_obj
1919
20        def iter_obj(self):20        def iter_obj(self):
21            for attr in sorted(set(dir(cls) + cls.__slots__)):21            for attr in sorted(set(dir(cls) + cls.__slots__)):
22                if not attr.startswith('_'):22                if not attr.startswith('_'):
23                    yield attr23                    yield attr
24        cls.__iter__ = iter_obj24        cls.__iter__ = iter_obj
2525
26        def str_obj(self):26        def str_obj(self):
27            parts = []27            parts = []
28            for attr in iter_obj(self):28            for attr in iter_obj(self):
29                value = getattr(self, attr, None)29                value = getattr(self, attr, None)
30                if attr in cls.__slots__ and value is not None:30                if attr in cls.__slots__ and value is not None:
31                    parts.append(f'{attr}={value}')31                    parts.append(f'{attr}={value}')
32                elif value is not None:32                elif value is not None:
33                    parts.append(f'{attr}:{value}')33                    parts.append(f'{attr}:{value}')
34                else:34                else:
35                    parts.append(attr)35                    parts.append(attr)
36            return '|'.join(parts)36            return '|'.join(parts)
37        cls.__str__ = str_obj37        cls.__str__ = str_obj
38        return cls38        return cls
39    return decorator39    return decorator
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op