Next / Previous / Contents / Shipman's homepage

7.4. class BigInfo: The PathInfo subclass

In order to make the file snapshots sort in descending order by size, we could just define a .__cmp__() method in the PathInfo class. However, the bigfiles.py script and the oldfiles.py script need different sorting behavior: the former sorts by size, while the latter sorts by modification timestamp.

So each of these scripts defines a new class, inheriting from PathInfo, that defines a .__cmp__() method that makes the objects sort correctly for that application.

So that a BigInfo instance can display the path name relative to the report's starting directory, its constructor requires an additional argument named basePath, the starting directory's absolute path.

Here's the beginning of the class declaration. Note that the class name is followed by the parent class name in parentheses.

bigfiles.py
#================================================================
# Functions and classes
#----------------------------------------------------------------


# - - - - -   c l a s s   B i g I n f o   - - - - -

class BigInfo(pathinfo.PathInfo):
    """Represents information about one file; sorts by size.

      Exports:
        BigInfo ( path, basePath ):
          [ (path is the path name to a file) and
            (basePath is the path name of some directory above
            path) ->
              return a new BigInfo instance with those values ]
        .__cmp__ ( self, other ):
          [ other is a BigInfo instance ->
              return cmp ( other.size, self.size ) ]
        .__str__ ( self ):
          [ return a string describing self's modification time,
            its size, and its path name relative to basePath ]

      State/Invariants:
        .__basePath:  [ as passed to constructor, read-only ]
    """