473,503 Members | 2,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

custom classes in sets

How can i make my custom class an element of a set?

class Cfile:
def __init__(s,path): s.path = path

def __eq__(s,other):
print 'inside equals'
return not os.popen('cmp %s %s' % (s.path,other.path)).read()

def __hashcode__(s): return s.path.__hashcode__()

the idea is that it accepts file paths and construct a set of unique
files (the command "cmp" compares files byte by byte.),the files can
have different paths but the same content

but the method __eq__ is never called

Jul 18 '05 #1
3 2350
vegetax wrote:
How can i make my custom class an element of a set?

class Cfile:
def __init__(s,path): s.path = path

def __eq__(s,other):
print 'inside equals'
return not os.popen('cmp %s %s' % (s.path,other.path)).read()

def __hashcode__(s): return s.path.__hashcode__()

the idea is that it accepts file paths and construct a set of unique
files (the command "cmp" compares files byte by byte.),the files can
have different paths but the same content

but the method __eq__ is never called


Seems to be called fine for me:

py> class Cfile:
.... def __eq__(self, other):
.... print 'inside equals'
.... return False
.... def __hash__(self):
.... return 0
....
py> {Cfile():1, Cfile():2}
inside equals
{<__main__.Cfile instance at 0x01166490>: 1, <__main__.Cfile instance at
0x01166760>: 2}

Note that __eq__ won't be called if the hashes are different:

py> class Cfile:
.... hash = 0
.... def __eq__(self, other):
.... print 'inside equals'
.... return False
.... def __hash__(self):
.... Cfile.hash += 1
.... return Cfile.hash
....
py> {Cfile():1, Cfile():2}
{<__main__.Cfile instance at 0x01166918>: 1, <__main__.Cfile instance at
0x011668A0>: 2}

Steve
Jul 18 '05 #2
Steven Bethard wrote:
vegetax wrote:
How can i make my custom class an element of a set?

class Cfile:
def __init__(s,path): s.path = path

def __eq__(s,other):
print 'inside equals'
return not os.popen('cmp %s %s' % (s.path,other.path)).read()

def __hashcode__(s): return s.path.__hashcode__()

the idea is that it accepts file paths and construct a set of unique
files (the command "cmp" compares files byte by byte.),the files can
have different paths but the same content

but the method __eq__ is never called


Seems to be called fine for me:

py> class Cfile:
... def __eq__(self, other):
... print 'inside equals'
... return False
... def __hash__(self):
... return 0
...
py> {Cfile():1, Cfile():2}
inside equals
{<__main__.Cfile instance at 0x01166490>: 1, <__main__.Cfile instance at
0x01166760>: 2}

Note that __eq__ won't be called if the hashes are different:


I just tried and it wont be called =(, so how can i generate a hash code for
the CFile class? note that the comparitions(__eq__) are done based on the
contents of a file using the command 'cmp', i guess thats not posible but
thanks.


Jul 18 '05 #3
vegetax wrote:
Steven Bethard wrote:
vegetax wrote:
How can i make my custom class an element of a set?

class Cfile:
def __init__(s,path): s.path = path

def __eq__(s,other):
print 'inside equals'
return not os.popen('cmp %s %s' % (s.path,other.path)).read()

def __hashcode__(s): return s.path.__hashcode__()

the idea is that it accepts file paths and construct a set of unique files (the command "cmp" compares files byte by byte.),the files can have different paths but the same content

but the method __eq__ is never called

[snip]
I just tried and it wont be called =(, so how can i generate a hash code for the CFile class? note that the comparitions(__eq__) are done based on the contents of a file using the command 'cmp', i guess thats not posible but thanks.

Let me suggest that, if your idea is to get a set of files all with
unique file contents, comparing a file byte-by-byte with each file
already in the set is going to be absurdly inefficient.

Instead, I recommend comparing md5 (or sha) digest. The idea is, you
read in each file once, calculate an md5 digest, and compare the
digests instead of the file contents.

.. import md5
..
.. class Cfile:
.. def __init__(self,path):
.. self.path = path
.. self.md5 = md5.new().update(open(path).read()).digest()
.. def __eq__(self,other):
.. return self.md5 == other.md5
.. def __hash__(self):
.. return hash(self.md5)

This is kind of hackish (not to mention untested). You would probably
do better to mmap the file (see the mmap module) rather than read it.

And, in case you're wondering: yes it is theoretically possible for
different files to have the same md5. However, the chances are
microscopic. (Incidentally, the SCons build system uses MD5 to decide
if a file has been modified.)
--
CARL BANKS

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1894
by: Pierre Barbier de Reuille | last post by:
Ok, I first want to stress that I looked through the newsgroups archives (on Google) for an answer to my question, but I didn't find it. It's about the interface of the set classes as defined in...
0
1900
by: Sundown | last post by:
I am trying to create a custom button control for the web that, when clicked, disables and changes the text of itself and a bunch of other controls (in the collection). My goal is to end up with a...
0
1303
by: downwitch | last post by:
Hi all, I'm trying to get my head around custom events, and I can't. I can use custom classes and collections out the wazoo, but I just don't get what events are for, or how they work. I've...
7
1729
by: Julian Jelfs | last post by:
Hi, I had an aspx pag in .Net 1.1 with a label on it. As such I had a code behind page with a declaration for that label. When I convert to Asp.Net 2.0 the code behind is converted to a...
5
2511
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called...
3
5734
by: Fred | last post by:
Hi, I want to be able to read and write to the custom properties of any Office type file. In Windows explorer I can do this manually by right clicking on a file selecting 'properties' from the...
2
2418
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
2
1344
by: Sumit | last post by:
Hi All... What is the best way of binding a custom object that is returned via a Web Service? From what Ive seen, the proxy class generated by wsdl.exe doesnt include properties for the members...
1
1580
by: leodippolito | last post by:
Dear sirs, I am using custom wrappers to primitive types in my classes, so I can have some flags when working with the database ("undefined" and "null") .. So instead of: public class...
0
2131
by: webmaster | last post by:
Hi all, I'm tearing my hair out with this one. I have successfully implemented by own RadioButtonList in order to provide additional functionality and a DIV rather than TABLE-based layout in...
0
7063
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3146
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
366
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.