• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 17. Changing Size Information > Step 1: Implementing the Adapter

Step 1: Implementing the Adapter

An adapter is usually a simple class, which is marked by the fact that it takes one object as the constructor argument. This object must provide the “from” interface that is often also listed in the used_for attribute of the class. You need to add the following code in your message.py file:

01 from zope.app.size.interfaces import ISized
02
03 class MessageSized(object):
04
05     implements(ISized)
06     __used_for__ = IMessage
07
08     def __init__(self, message):
09         self._message = message
10
11     def sizeForSorting(self):
12         """See ISized"""
13         return ('item', len(self._message))
14
15     def sizeForDisplay(self):
16         """See ISized"""
17         messages = 0
18         for obj in self._message.values():
19             if IMessage.providedBy(obj):
20                 messages += 1
21
22         attachments = len(self._message)-messages
23
24         if messages == 1: size = u'1 reply'
25         else: size = u'%i replies' %messages
26
27         if attachments == 1: size += u', 1 attachment'
28         else: size += u', %i attachments' %attachments
29
30         return size


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint