Next / Previous / Contents / Shipman's homepage

10. class KwicWord: All references to one keyword

An instance of this class is a container for all the KwicRef instances that record occurrences of a given keyword. Here is the formal interface.

kwic.py
# - - - - -   c l a s s   K w i c W o r d

class KwicWord(object):
    '''Container for all the references to one keyword.

      Exports:
        KwicWord(word):
          [ word is a unicode keyword ->
              return a new, empty KwicWord instance for word ]
        .word:             [ as passed to constructor, read-only ]
        .add(prefix, suffix, userData):
          [ (prefix and suffix are unicode strings) and
            (userData may be any type) ->
              self  :=  self with a new KwicRef added for
                  prefix=(prefix), word=self.word, suffix=(suffix),
                  and userData=(userData) ]
        .getKey():   [ return self.word.upper() ]

See Section 8.1, “ref-key.

kwic.py
        .genRefs():
          [ generate references in self as a sequence of KwicRef
            instances, in ascending order by ref-key(self) ]

Here are the internal attributes. The way we store the references is dictated by the need to generate them in the order specified by the KwicRef.__cmp__ method. Hence, all we need to do is put the references into a list, and the .genRefs() method can use the sorted() iterator to produce them in the correct order.

kwic.py
      State/Invariants:
        .__refList:
          [ a list containing self's references as KwicRef instances ]

Also, so that the .getKey() method doesn't have to rebuild the key for each call, we do it once initially and store it in .__key.

kwic.py
        .__key:     [ self.word.upper() ]
    '''

10.1. KwicWord.__init__(): Constructor

Not much to do here except to copy the constructor argument and set up an empty .__refList.

kwic.py
# - - -   K w i c W o r d . _ _ i n i t _ _

    def __init__(self, word):
        '''Constructor
        '''
        self.word = word
        self.__refList = []
        self.__key = word.upper()