Implementing a Data Model or Type System
Problem
You want to define various kinds of data structures and want to enforce constraints on the values that are allowed to be assigned to certain attributes.
Solution
In this problem, you are basically faced with the task of placing checks or assertions on the setting of certain instance attributes. To do this, you need to customize the setting of attributes on a per-attribute basis; you should use descriptors.
The following code illustrates the use of descriptors to implement a system type and value checking framework:
# Base class. Uses a descriptor to set a value
class
Descriptor
(
object
):
def
__init__
(
self
,
name
=
None
,
**
opts
):
self
.
name
=
name
for
key
,
value
in
opts
.
items
():
setattr
(
self
,
key
,
value
)
def
__set__
(
self
,
instance
,
value
):
instance
.
__dict__
[
self
.
name
]
=
value
# Descriptor for enforcing types
class
Typed
(
Descriptor
):
expected_type
=
type
(
None
)
def
__set__
(
self
,
instance
,
value
):
if
not
isinstance
(
value
,
self
.
expected_type
):
raise
TypeError
(
'expected '
+
str
(
self
.
expected_type
))
super
()
.
__set__
(
instance
,
value
)
# Descriptor for enforcing values
class
Unsigned
(
Descriptor
):
def
__set__
(
self
,
instance
,
value
):
if
value
<
0
:
raise
ValueError
(
'Expected >= 0'
)
super
()
.
__set__
(
instance
,
value
)
class
MaxSized
(
Descriptor
):
def
__init__
(
self
,
name ...
Get Implementing a Data Model or Type System now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.