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

7.21. class UserDatabase

This class encapsulates all of the gdbm protocol for saving and retrieving user records. It is a container class for UserRecord instances; see Section 7.28, “class UserRecord. Its interface is dictionary-like, with user ID values as the keys.

reader.cgi
# - - - - -   c l a s s   U s e r D a t a b a s e

class UserDatabase:
    '''Represents the file of user records.

      Exports:
        UserDatabase():
          [ file DB_NAME can be opened read-write ->
              return a UserDatabase instance representing that file ]
        .__getitem__ ( self, userId ):
          [ if self contains an entry for user (userId) ->
              return that user's data as a UserRecord instance
            else -> raise KeyError ]
        .__setitem__ ( self, userId, userRecord ):
          [ (userId is a string) and
            (userRecord is a UserRecord instance) ->
              self  :=  self with userRecord stored under key (userId) ]
        .__delitem__ ( self, userId ):
          [ userId is a string ->
              self  :=  self with nothing stored under key (userId) ]
        .cleanup():
          [ self  :=  self minus all entries whose expiration is
                      in the past ]
        .close():
          [ self  :=  self closed ]

      State/Invariants:
        .__db:  [ a gdbm.gdbm instance connected to file DB_NAME ]
    '''