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