| 1 | waeup.sirp.hostel.HostelContainer |
|---|
| 2 | ********************************* |
|---|
| 3 | |
|---|
| 4 | Containers for hostels. |
|---|
| 5 | |
|---|
| 6 | .. :doctest: |
|---|
| 7 | .. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer |
|---|
| 8 | |
|---|
| 9 | Getting a hostel container |
|---|
| 10 | ========================== |
|---|
| 11 | |
|---|
| 12 | We can easily create `HostelContainers`:: |
|---|
| 13 | |
|---|
| 14 | >>> from waeup.sirp.hostel.hostelcontainer import HostelContainer |
|---|
| 15 | >>> mycontainer = HostelContainer() |
|---|
| 16 | |
|---|
| 17 | Hostel containers provide ``IHostelContainer``: |
|---|
| 18 | |
|---|
| 19 | >>> from waeup.sirp.interfaces import IHostelContainer |
|---|
| 20 | >>> IHostelContainer.providedBy(mycontainer) |
|---|
| 21 | True |
|---|
| 22 | |
|---|
| 23 | Another way to get a hostel container -- without importing the class |
|---|
| 24 | -- is via factories. We registered a factory for hostel containers |
|---|
| 25 | under the name ``waeup.HostelContainer``. So we can ask for an object |
|---|
| 26 | by calling the appropriate factory: |
|---|
| 27 | |
|---|
| 28 | >>> from zope.component import createObject |
|---|
| 29 | >>> createObject(u'waeup.HostelContainer') |
|---|
| 30 | <waeup.sirp.hostel.hostelcontainer.HostelContainer object at 0x...> |
|---|
| 31 | |
|---|
| 32 | This way we get a thing that implements IHostelContainer without |
|---|
| 33 | imports or similar. |
|---|
| 34 | |
|---|
| 35 | We can be sure, that the full interface is supported by the |
|---|
| 36 | HostelContainer class:: |
|---|
| 37 | |
|---|
| 38 | >>> from zope.interface.verify import verifyClass |
|---|
| 39 | >>> verifyClass(IHostelContainer, HostelContainer) |
|---|
| 40 | True |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | Storing things in hostel containers |
|---|
| 44 | ==================================== |
|---|
| 45 | |
|---|
| 46 | We can, of course, store things in a hostel container. But when we |
|---|
| 47 | really store an object, then it must be a hostel:: |
|---|
| 48 | |
|---|
| 49 | >>> mycontainer.addHostel(42) |
|---|
| 50 | Traceback (most recent call last): |
|---|
| 51 | ... |
|---|
| 52 | TypeError: HostelContainers contain only IHostel instances |
|---|
| 53 | |
|---|
| 54 | Okay, so we have to get a hostel first:: |
|---|
| 55 | |
|---|
| 56 | >>> from waeup.sirp.hostel.hostel import Hostel |
|---|
| 57 | >>> myhostel = Hostel() |
|---|
| 58 | |
|---|
| 59 | We can add this hostel to our container:: |
|---|
| 60 | |
|---|
| 61 | >>> mycontainer.addHostel(myhostel) |
|---|
| 62 | '0' |
|---|
| 63 | |
|---|
| 64 | We get back the key, under which the hostel was stored. It will be |
|---|
| 65 | some string, but there is no guarantee at all, how this key looks |
|---|
| 66 | like. It might be a string of integers, a name or whatever; you cannot |
|---|
| 67 | know before. |
|---|