[Tutor] Mapping to an equivalent / similar object?

Walker Hale IV walker.hale.iv at gmail.com
Fri Jun 5 14:52:59 CEST 2009


On Thu, May 28, 2009 at 5:41 PM, Allen Fowler <allen.fowler at yahoo.com> wrote:
> How should the mapping between the CanonicalFlavor('Vanilla') object and ManufAFlavor('Vanilla') / ManufBFlavor('Vanilla') objects be handled.

Create a dict. The keys are either  CannonicalFlavor objects or just
strings containing the canonical names. The values of the dict are
either sets or lists. Inside one of those sets or lists, you have the
manufactured flavor objects. If there is a natural ordering to the
manufactured flavor objects, such as preferring to get vanilla from
Manufacturer A but chocolate from Manufacturer B, then use lists.
Otherwise use sets.

Example code:

flavor_sources = dict()
flavor_sources['vanilla'] = [ManufAFlavor('Vanilla'), ManufBFlavor('Vanilla')]
flavor_sources['chocolate'] = [ManufBFlavor('Chocolate'),
ManufAFlavor('Chocolate')]

If needed you can customize this in two ways: using objects for the
keyes and using sets for the values.

Note that if you want to use objects for keys, then your class needs
to implement __hash__() and __cmp__(). See
http://www.python.org/doc/2.5.4/ref/customization.html

-- 
Walker Hale <walker.hale.iv at gmail.com>


More information about the Tutor mailing list