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