Skip to content

Commit e5f9ddb

Browse files
committed
Simpler format for creation
1 parent 6ff1d92 commit e5f9ddb

File tree

2 files changed

+34
-32
lines changed

2 files changed

+34
-32
lines changed

examples/document.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,23 @@ class Document(BaseWithId):
1010

1111
def __init__(self, **kwargs):
1212

13-
self.allowed_children = collections.OrderedDict(
14-
[
15-
("sections", ("The Sections", Section)),
16-
]
17-
)
18-
19-
#self.add_allowed_child("sections", "The sections of the document", Section)
20-
21-
22-
self.allowed_fields = collections.OrderedDict(
23-
[
24-
("title", ("Document title", str)),
25-
(
26-
"ISBN",
27-
(
28-
"International Standard Book Number",
29-
int,
30-
),
31-
),
32-
]
33-
)
13+
self.add_allowed_child("sections", "The sections of the document", Section)
14+
15+
self.add_allowed_field("title", "Document title", str)
16+
self.add_allowed_field("ISBN", "International Standard Book Number", int)
3417

3518
super(Document, self).__init__(**kwargs)
3619

20+
print('Created:: %s'%(self))
21+
3722

3823
class Section(BaseWithId):
3924

4025
_definition = "A model of a section of the document"
4126

4227
def __init__(self, **kwargs):
4328

44-
self.allowed_children = collections.OrderedDict(
45-
[
46-
("paragraphs", ("The paragraphs", Paragraph)),
47-
]
48-
)
29+
self.add_allowed_child("paragraphs", "The paragraphs", Paragraph)
4930

5031
super(Section, self).__init__(**kwargs)
5132

@@ -56,14 +37,11 @@ class Paragraph(Base):
5637

5738
def __init__(self, **kwargs):
5839

59-
self.allowed_fields = collections.OrderedDict(
60-
[
61-
("contents", ("Paragraph contents", str)),
62-
]
63-
)
40+
self.add_allowed_field("contents", "Paragraph contents", str)
6441

6542
super(Paragraph, self).__init__(**kwargs)
6643

44+
6745
doc = Document(id="MyBook")
6846
doc.title = "My life in Python"
6947

modelspec/BaseTypes.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,29 @@ def get_id(self):
6969
def get_type(self):
7070
return self.__class__.__name__
7171

72+
73+
def add_allowed_child(self, name, description, type_):
74+
75+
if self.allowed_children is None:
76+
self.allowed_children = collections.OrderedDict([])
77+
78+
self.allowed_children[name] = (description, type_)
79+
80+
81+
def add_allowed_field(self, name, description, type_):
82+
if self.allowed_fields is None:
83+
self.allowed_fields = collections.OrderedDict([])
84+
85+
self.allowed_fields[name] = (description, type_)
86+
7287
def __getattr__(self, name):
7388

74-
# if verbose: print_v(" > Checking the value of attribute %s in: %s..."%(name,self.get_id()))
7589

7690
if name == "id" and not "id" in self.allowed_fields:
7791
return None
7892

93+
if verbose: print_v(" > Checking the value of attribute %s in: %s..."%(name,'?'))
94+
7995
if name in self.__dict__:
8096
return self.__dict__[name]
8197

@@ -87,6 +103,12 @@ def __getattr__(self, name):
87103
self.__dict__["allowed_children"] = collections.OrderedDict()
88104
return self.__dict__["allowed_children"]
89105

106+
if name == "add_allowed_child":
107+
return self.add_allowed_child
108+
109+
if name == "add_allowed_field":
110+
return self.add_allowed_field
111+
90112
if name in self.allowed_fields:
91113
if not name in self.fields:
92114
return None
@@ -101,6 +123,7 @@ def __getattr__(self, name):
101123

102124
return None
103125

126+
104127
@classmethod
105128
def _is_evaluable_expression(cls, value):
106129
if not hasattr(value, "__name__"):
@@ -486,6 +509,7 @@ def __init__(self, **kwargs):
486509

487510
super(BaseWithId, self).__init__(**kwargs)
488511

512+
489513
def get_id(self):
490514
if len(self.fields) == 0:
491515
return "???"

0 commit comments

Comments
 (0)