8.13 实现数据模型的类型约束
在这个问题中,你需要在对某些实例属性赋值时进行检查。所以你要自定义属性赋值函数,这种情况下最好使用描述器。
下面的代码使用描述器实现了一个系统类型和赋值验证框架:
这些类就是你要创建的数据模型或类型系统的基础构建模块。下面就是我们实际定义的各种不同的数据类型:
- class Integer(Typed):
- expected_type = int
- class UnsignedInteger(Integer, Unsigned):
- pass
- class Float(Typed):
- expected_type = float
- class UnsignedFloat(Float, Unsigned):
- pass
- class String(Typed):
- expected_type = str
- class SizedString(String, MaxSized):
- pass
然后使用这些自定义数据类型,我们定义一个类:
然后测试这个类的属性赋值约束,可发现对某些属性的赋值违法了约束是不合法的:
- >>> s.name
- 'ACME'
- >>> s.shares = 75
- >>> s.shares = -10
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "example.py", line 17, in __set__
- super().__set__(instance, value)
- File "example.py", line 23, in __set__
- raise ValueError('Expected >= 0')
- ValueError: Expected >= 0
- >>> s.price = 'a lot'
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "example.py", line 16, in __set__
- raise TypeError('expected ' + str(self.expected_type))
- TypeError: expected <class 'float'>
- >>> s.name = 'ABRACADABRA'
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "example.py", line 17, in __set__
- super().__set__(instance, value)
- File "example.py", line 35, in __set__
- raise ValueError('size must be < ' + str(self.size))
- ValueError: size must be < 8
- >>>
另外一种方式是使用元类:
- # A metaclass that applies checking
- def __new__(cls, clsname, bases, methods):
- # Attach attribute names to the descriptors
- for key, value in methods.items():
- if isinstance(value, Descriptor):
- value.name = key
- return type.__new__(cls, clsname, bases, methods)
- # Example
- class Stock2(metaclass=checkedmeta):
- name = SizedString(size=8)
- shares = UnsignedInteger()
- price = UnsignedFloat()
- def __init__(self, name, shares, price):
- self.name = name
- self.shares = shares
- self.price = price
本节使用了很多高级技术,包括描述器、混入类、 的使用、类装饰器和元类。不可能在这里一一详细展开来讲,但是可以在8.9、8.18、9.19小节找到更多例子。但是,我在这里还是要提一下几个需要注意的点。
首先,在 Descriptor
基类中你会看到有个 set()
方法,却没有相应的 方法。如果一个描述仅仅是从底层实例字典中获取某个属性值的话,那么没必要去定义 get()
方法。
所有描述器类都是基于混入类来实现的。比如 Unsigned
和 要跟其他继承自 Typed
类混入。这里利用多继承来实现相应的功能。
混入类的一个比较难理解的地方是,调用 super()
函数时,你并不知道究竟要调用哪个具体类。你需要跟其他类结合后才能正确的使用,也就是必须合作才能产生效果。
所有方法中,类装饰器方案应该是最灵活和最高明的。首先,它并不依赖任何其他新的技术,比如元类。其次,装饰器可以很容易的添加或删除。
最后,装饰器还能作为混入类的替代技术来实现同样的效果;
- # Decorator for applying type checking
- def Typed(expected_type, cls=None):
- if cls is None:
- return lambda cls: Typed(expected_type, cls)
- super_set = cls.__set__
- def __set__(self, instance, value):
- if not isinstance(value, expected_type):
- raise TypeError('expected ' + str(expected_type))
- super_set(self, instance, value)
- cls.__set__ = __set__
- return cls
- # Decorator for unsigned values
- def Unsigned(cls):
- super_set = cls.__set__
- def __set__(self, instance, value):
- if value < 0:
- raise ValueError('Expected >= 0')
- super_set(self, instance, value)
- cls.__set__ = __set__
- return cls
- # Decorator for allowing sized values
- def MaxSized(cls):
- def __init__(self, name=None, **opts):
- if 'size' not in opts:
- raise TypeError('missing size option')
- super_init(self, name, **opts)
- cls.__init__ = __init__
- super_set = cls.__set__
- def __set__(self, instance, value):
- if len(value) >= self.size:
- raise ValueError('size must be < ' + str(self.size))
- super_set(self, instance, value)
- cls.__set__ = __set__
- return cls
- # Specialized descriptors
- @Typed(int)
- class Integer(Descriptor):
- pass
- @Unsigned
- class UnsignedInteger(Integer):
- pass
- @Typed(float)
- class Float(Descriptor):
- pass
- @Unsigned
- class UnsignedFloat(Float):
- pass
- @Typed(str)
- class String(Descriptor):
- pass
- @MaxSized
- class SizedString(String):
这种方式定义的类跟之前的效果一样,而且执行速度会更快。设置一个简单的类型属性的值,装饰器方式要比之前的混入类的方式几乎快100%。现在你应该庆幸自己读完了本节全部内容了吧?^_^
原文: