1 | :mod:`waeup.sirp.university.coursecontainer` -- Course containers |
---|
2 | ***************************************************************** |
---|
3 | |
---|
4 | .. module:: waeup.sirp.university.coursecontainer |
---|
5 | |
---|
6 | Containers for courses. |
---|
7 | |
---|
8 | .. :doctest: |
---|
9 | .. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer |
---|
10 | |
---|
11 | |
---|
12 | Content Classes (models and containers) |
---|
13 | ======================================= |
---|
14 | |
---|
15 | :class:`CourseContainer` |
---|
16 | ------------------------ |
---|
17 | |
---|
18 | .. class:: CourseContainer() |
---|
19 | |
---|
20 | Create a course container instance. |
---|
21 | |
---|
22 | .. attribute:: grok.implements(ICourseContainer) |
---|
23 | |
---|
24 | .. attribute:: grok.require('waeup.manageUniversity') |
---|
25 | |
---|
26 | :class:`CourseContainer` instances are instances of |
---|
27 | :class:`grok.Container` that hold and manage instances of |
---|
28 | :class:`waeup.sirp.university.course.Course`. |
---|
29 | |
---|
30 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
31 | >>> mycontainer = CourseContainer() |
---|
32 | >>> mycontainer |
---|
33 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
34 | |
---|
35 | Course containers provide `ICourseContainer`: |
---|
36 | |
---|
37 | >>> from waeup.sirp.university.interfaces import ICourseContainer |
---|
38 | >>> ICourseContainer.providedBy(mycontainer) |
---|
39 | True |
---|
40 | |
---|
41 | We can be sure, that the full interface is supported by the |
---|
42 | CourseContainer class: |
---|
43 | |
---|
44 | >>> from zope.interface.verify import verifyClass |
---|
45 | >>> verifyClass(ICourseContainer, CourseContainer) |
---|
46 | True |
---|
47 | |
---|
48 | As normal ``grok.Container``, :class:`CourseContainer` instances |
---|
49 | can be used like dicts: |
---|
50 | |
---|
51 | >>> list(mycontainer.keys()) |
---|
52 | [] |
---|
53 | |
---|
54 | .. method:: addCourse(course) |
---|
55 | |
---|
56 | Add a course. |
---|
57 | |
---|
58 | *course* must be an instance of |
---|
59 | :class:`waeup.sirp.university.course.Course`: |
---|
60 | |
---|
61 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
62 | >>> mycontainer = CourseContainer() |
---|
63 | >>> mycontainer.addCourse('blah') |
---|
64 | Traceback (most recent call last): |
---|
65 | ... |
---|
66 | TypeError: CourseContainers contain only ICourse instances |
---|
67 | |
---|
68 | The key of added items is the ``code`` attribute: |
---|
69 | |
---|
70 | >>> from waeup.sirp.university.course import Course |
---|
71 | >>> mycourse = Course(code='COURSE1') |
---|
72 | >>> mycontainer.addCourse(mycourse) |
---|
73 | 'Course added (for tests only).' |
---|
74 | >>> list(mycontainer.keys()) |
---|
75 | [u'COURSE1'] |
---|
76 | |
---|
77 | .. method:: clear() |
---|
78 | |
---|
79 | Remove all contained courses: |
---|
80 | |
---|
81 | >>> len(mycontainer.items()) |
---|
82 | 1 |
---|
83 | |
---|
84 | >>> mycontainer.clear() |
---|
85 | >>> len(mycontainer.items()) |
---|
86 | 0 |
---|
87 | |
---|
88 | Utilities |
---|
89 | ========= |
---|
90 | |
---|
91 | :class:`CourseContainerFactory` |
---|
92 | ------------------------------- |
---|
93 | |
---|
94 | .. class:: CourseContainerFactory() |
---|
95 | |
---|
96 | .. attribute:: grok.name(u'waeup.CourseContainer') |
---|
97 | |
---|
98 | .. attribute:: grok.implements(IFactory) |
---|
99 | |
---|
100 | A named utility to deliver new instances of :class:`CourseContainer` |
---|
101 | without the need to import the implementation before: |
---|
102 | |
---|
103 | >>> from zope.component import createObject |
---|
104 | >>> mycoursecontainer = createObject(u'waeup.CourseContainer') |
---|
105 | >>> mycoursecontainer |
---|
106 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
107 | |
---|
108 | The factory complies with the specifications from the |
---|
109 | :class:`IFactory` insterface: |
---|
110 | |
---|
111 | >>> from zope.interface.verify import verifyClass |
---|
112 | >>> from zope.component.interfaces import IFactory |
---|
113 | >>> from waeup.sirp.university.coursecontainer import CourseContainerFactory |
---|
114 | >>> verifyClass(IFactory, CourseContainerFactory) |
---|
115 | True |
---|
116 | |
---|
117 | This means also, that we can get the interfaces of the created |
---|
118 | object from the factory: |
---|
119 | |
---|
120 | >>> coursecontainer_factory = CourseContainerFactory() |
---|
121 | >>> coursecontainer_factory.getInterfaces() |
---|
122 | <implementedBy waeup.sirp.university.coursecontainer.CourseContainer> |
---|
123 | |
---|
124 | |
---|
125 | Examples |
---|
126 | ======== |
---|
127 | |
---|
128 | Getting a course container |
---|
129 | -------------------------- |
---|
130 | |
---|
131 | We can easily create `CourseContainers`: |
---|
132 | |
---|
133 | >>> from waeup.sirp.university.coursecontainer import CourseContainer |
---|
134 | >>> mycontainer = CourseContainer() |
---|
135 | |
---|
136 | |
---|
137 | Another way to get a course container -- without importing the class |
---|
138 | -- is via factories. We registered a factory for course containers |
---|
139 | under the name ``waeup.CourseContainer``. Now we can ask for an object |
---|
140 | by calling the appropriate factory: |
---|
141 | |
---|
142 | >>> from zope.component import createObject |
---|
143 | >>> createObject(u'waeup.CourseContainer') |
---|
144 | <waeup.sirp.university.coursecontainer.CourseContainer object at 0x...> |
---|
145 | |
---|
146 | This way we get a thing that implements ICourseContainer without |
---|
147 | imports or similar. |
---|
148 | |
---|