Near Final PEP 247 - API for Cryptographic Hash Functions

Oleg Broytmann phd at phd.pp.ru
Fri Sep 21 06:35:06 EDT 2001


On Thu, Sep 20, 2001 at 12:20:41PM -0400, Andrew Kuchling wrote:
>     http://python.sourceforge.net/peps/pep-0247.html

Why not to define __repr__ and __str__?

   __repr__ = hexdigest

   def str(self):
      return "%s(%s)" % (self.__class__.__name__, self.hexdigest())

What about processing files? I think it is very common operation, so why
not stuff it here?

   BUFSIZE = 16*1024
   def process_file(self, f):
      if type(f) == type(''): # If f is string - use it as file's name
         infile = open(f, 'r')
      else:
         infile = f           # else assume it is file or file-like object

      try:
         while 1:
            buf = infile.read(BUFSIZE)
            if not buf: break
            self.update(buf)

      finally:
         if type(f) == type(''): # If f was opened - close it
            infile.close()

Oleg.
---- 
     Oleg Broytmann            http://phd.pp.ru/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.




More information about the Python-list mailing list