How to derive a class from md5?

Discussion in 'Python' started by Josef Wolf, Jul 13, 2004.

  1. Josef Wolf

    Josef Wolf Guest

    Hello!

    I'd like to create a class to calculate md5's of a file. So I thought, I'd
    derive from the md5 class. I came up with following code:

    #!/usr/bin/python

    import md5

    class md5file(md5):
    def __init__(self, filename):
    md5.__init__(self)
    f=file(filename,"r")
    for l in f:
    self.update(l)
    f.close()

    print md5file("/etc/passwd").hexdigest()

    But running this gives me the following error:

    Traceback (most recent call last):
    File "py/t.py", line 5, in ?
    class md5file(md5):
    TypeError: function takes at most 2 arguments (3 given)

    dir(md5) shows that md5 don't have an __init__() method so I tried to use
    its new(), with the same result. What am I doing wrong here?

    --
    Please visit and sign http://petition-eurolinux.org and http://www.ffii.org
    -- Josef Wolf -- --
     
    Josef Wolf, Jul 13, 2004
    #1
    1. Advertising

  2. Josef Wolf

    Paul Rubin Guest

    Josef Wolf <> writes:
    > dir(md5) shows that md5 don't have an __init__() method so I tried to use
    > its new(), with the same result. What am I doing wrong here?


    md5 isn't set up to be subclassed like that. Better make a new
    md5file class and have an md5 instance inside your md5file instances.
    You can make a __getattr__ method to forward operations on md5file
    instances to the internal md5 object. There's probably also some
    metaclass tricks you could use to make that happen automagically.
     
    Paul Rubin, Jul 13, 2004
    #2
    1. Advertising

  3. Josef Wolf wrote:

    > import md5
    >
    > class md5file(md5):

    ...
    > dir(md5) shows that md5 don't have an __init__() method so I tried to
    > use
    > its new(), with the same result. What am I doing wrong here?


    md5 is a module, not a class!

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ All the people in my neighborhood turn around and get mad and sing
    -- Public Enemy
     
    Erik Max Francis, Jul 13, 2004
    #3
  4. Josef Wolf wrote:
    > Hello!
    >
    > I'd like to create a class to calculate md5's of a file. So I thought, I'd
    > derive from the md5 class. I came up with following code:
    >
    > #!/usr/bin/python
    >
    > import md5
    >
    > class md5file(md5):
    > def __init__(self, filename):
    > md5.__init__(self)
    > f=file(filename,"r")
    > for l in f:
    > self.update(l)
    > f.close()
    >
    > print md5file("/etc/passwd").hexdigest()
    >
    > But running this gives me the following error:
    >
    > Traceback (most recent call last):
    > File "py/t.py", line 5, in ?
    > class md5file(md5):
    > TypeError: function takes at most 2 arguments (3 given)
    >
    > dir(md5) shows that md5 don't have an __init__() method so I tried to use
    > its new(), with the same result. What am I doing wrong here?
    >


    Why subclass? You could just get the md5 object by doing:

    from md5 import md5
    m = md5(open(filename).read())

    Simple is better than complex.

    --
    Shalabh
     
    Shalabh Chaturvedi, Jul 14, 2004
    #4
  5. Shalabh Chaturvedi wrote:

    > Why subclass? You could just get the md5 object by doing:
    >
    > from md5 import md5
    > m = md5(open(filename).read())
    >
    > Simple is better than complex.


    If you really want to mix the old md5 functionality with the new, a
    class containing an md5 instance would be a fine solution.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ Sentimentality is a superstructure covering brutality.
    -- C.G. Jung
     
    Erik Max Francis, Jul 14, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amil
    Replies:
    1
    Views:
    384
    Teemu Keiski
    Nov 20, 2003
  2. Replies:
    9
    Views:
    17,202
    John Salerno
    May 8, 2006
  3. gyan
    Replies:
    3
    Views:
    379
    Ian Collins
    Aug 23, 2006
  4. Tinku
    Replies:
    3
    Views:
    1,216
    Alf P. Steinbach
    Jan 31, 2010
  5. Peter Woodsky

    create a md5 / md5 passwd with a salt

    Peter Woodsky, Nov 20, 2008, in forum: Ruby
    Replies:
    6
    Views:
    240
    Brian Candler
    Nov 21, 2008
Loading...

Share This Page