Next / Previous / Contents / Shipman's homepage

11. class KwicRef: Record of one reference to one keyword

An instance of this class records the occurrence of a specific keyword in a specific string. Here is the formal interface.

kwic.py
# - - - - -   c l a s s   K w i c R e f

class KwicRef(object):
    '''Represents one reference to a keyword and its context.

      Exports:
        KwicRef(prefix, word, suffix, userData):
          [ (prefix, word, and suffix are unicode strings) and
            (userData may have any type) ->
              return a new KwicRef instance with those values ]
        .prefix:        [ as passed to constructor, read-only ]
        .word:          [ as passed to constructor, read-only ]
        .suffix:        [ as passed to constructor, read-only ]
        .userData:      [ as passed to constructor, read-only ]
        .__cmp__(self, other):
          [ returns cmp(ref-key(self), ref-key(other)) ]

There is one internal attribute. Rather than rebuilding two comparison keys each time the __cmp__ method is called, we build it once initially. See Section 8.1, “ref-key.

kwic.py
      State/Invariants:
        .__key:   [ ref-key(self) ]
    '''

11.1. KwicRef.__init__(): Constructor

kwic.py
# - - -   K w i c R e f . _ _ i n i t _ _

    def __init__(self, prefix, word, suffix, userData):
        '''Constructor.
        '''
        #-- 1 --
        self.prefix = prefix
        self.word = word
        self.suffix = suffix
        self.userData = userData
        self.__key = "%s|%s|%s" % (word, suffix, prefix)