Creating Set of objects of user defined class in python

Kavit Shah
table = set([])

class GlobeLearningTable(object):
    def __init__(self,mac,port,dpid):

        self.mac = mac
        self.port = port
        self.dpid = dpid

    def add(self):

        global table
        if self not in table:
            table.add(self)

class LearningSwitch(object):
    def __init__ (self, connection, transparent):
       self.connection = connection
       self.transparent = transparent
       self.macToPort = {}
       connection.addListeners(self)
       self.hold_down_expired = _flood_delay == 0

    def _handle_PacketIn (self, event):
       packet = event.parsed
       self.macToPort[packet.src] = event.port # 1
       packet_src = str(packet.src)
       packet_mac = packet_src.upper()
       entry = GlobeLearningTable(packet_mac, event.port, dpid_to_str(self.connection.dpid))
       entry.add()

Problem : entry.add() method adds new object every time it is called and increments the items in the table.

This should not happen because

  1. In the add method I am checking that is that object in the table or not , then I am adding that particular object.
  2. Table is a set which is unordered list, which should not have duplicate objects.

Help: is there any way in this set up I can add the object only when it's not in the table.

Martijn Pieters

You need to implement __eq__ and __hash__ methods to teach Python about how to recognise unique GlobeLearningTable instances.

class GlobeLearningTable(object):
    def __init__(self,mac,port,dpid):
        self.mac = mac
        self.port = port
        self.dpid = dpid

    def __hash__(self):
        return hash((self.mac, self.port, self.dpid))

    def __eq__(self, other):
        if not isinstance(other, type(self)): return NotImplemented
        return self.mac == other.mac and self.port == other.port and self.dpid == other.dpid

Now your object is comparable, and equal objects will also return equal values for __hash__. This lets set and dict objects store your objects efficiently and detect if it is already present:

>>> demo = set([GlobeLearningTable('a', 10, 'b')])
>>> GlobeLearningTable('a', 10, 'b') in demo
True

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating Set of objects of user defined class in python

From Dev

Creating objects in a class in Python

From Dev

Creating SafeArray with user defined type objects

From Dev

Creating a DataFrame out of nested user defined objects

From Dev

Creating a DataFrame out of nested user defined objects

From Dev

User-defined class objects in Java TreeMap

From Dev

Basic python-creating user defined function

From Dev

Creating a user defined class in java just like Integer/String class

From Dev

JS - creating user-defined function for custom js objects

From Dev

Using vector of pairs with user-defined class objects

From Dev

Oracle Stored Procedure for passing user defined class objects

From Dev

android: How to access UI objects from a user defined class

From Dev

How to create a Set of user-defined objects in Javascript with a user-defined equality function?

From Dev

How does Python sort user-defined objects?

From Dev

creating a list of class objects

From Dev

Python - create objects of a class without repeating myself while creating

From Dev

HashMap fails for user defined objects?

From Dev

java User Defined Classes and Objects

From Dev

Is there a way to define list(obj) method on a user defined class in python?

From Dev

Syntax error: missing ';' before '*' when creating a pointer object of a user defined abstract class

From Dev

Creating objects based on user input

From Dev

Creating python objects from objects

From Dev

How to read JSON response containing an array of objects of a user-defined class?

From Dev

(python) Creating objects as modules

From Dev

(python) Creating objects as modules

From Dev

Creating a Class of Objects within an Object

From Dev

Creating new objects with another class

From Dev

Creating an array of objects within a class

From Dev

User defined Key Class for HashMap

Related Related

  1. 1

    Creating Set of objects of user defined class in python

  2. 2

    Creating objects in a class in Python

  3. 3

    Creating SafeArray with user defined type objects

  4. 4

    Creating a DataFrame out of nested user defined objects

  5. 5

    Creating a DataFrame out of nested user defined objects

  6. 6

    User-defined class objects in Java TreeMap

  7. 7

    Basic python-creating user defined function

  8. 8

    Creating a user defined class in java just like Integer/String class

  9. 9

    JS - creating user-defined function for custom js objects

  10. 10

    Using vector of pairs with user-defined class objects

  11. 11

    Oracle Stored Procedure for passing user defined class objects

  12. 12

    android: How to access UI objects from a user defined class

  13. 13

    How to create a Set of user-defined objects in Javascript with a user-defined equality function?

  14. 14

    How does Python sort user-defined objects?

  15. 15

    creating a list of class objects

  16. 16

    Python - create objects of a class without repeating myself while creating

  17. 17

    HashMap fails for user defined objects?

  18. 18

    java User Defined Classes and Objects

  19. 19

    Is there a way to define list(obj) method on a user defined class in python?

  20. 20

    Syntax error: missing ';' before '*' when creating a pointer object of a user defined abstract class

  21. 21

    Creating objects based on user input

  22. 22

    Creating python objects from objects

  23. 23

    How to read JSON response containing an array of objects of a user-defined class?

  24. 24

    (python) Creating objects as modules

  25. 25

    (python) Creating objects as modules

  26. 26

    Creating a Class of Objects within an Object

  27. 27

    Creating new objects with another class

  28. 28

    Creating an array of objects within a class

  29. 29

    User defined Key Class for HashMap

HotTag

Archive