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