Next / Previous / Contents / TCC Help System / NM Tech homepage

7.23. UserDatabase.__getitem__(): Implement dictionary get

This method retrieves the string-valued contents for a given user ID, and returns those values as a UserRecord instance.

reader.cgi
# - - -   U s e r D a t a b a s e . _ _ g e t i t e m _ _

    def __getitem__ ( self, userId ):
        '''Return self[userId] as a UserRecord.
        '''
        #-- 1 --
        # [ if self.__db has an entry for key (userId) ->
        #     rawValue  :=  corresponding value
        #   else -> raise KeyError ]
        rawValue  =  self.__db[userId]

The three values—chapter number, persistent flag, and expiration time—are separated by commas.

reader.cgi
        #-- 2 --
        rawChapterNo, rawPersist, rawExpiration  =  rawValue.split(',')

        #-- 3 --
        return UserRecord ( userId, int(rawChapterNo),
                            int(rawPersist), int(rawExpiration) )