Browse Source

[#45] Added the __hash__ method implementation for the Model class

Roland Haroutiounian 9 years ago
parent
commit
8a6fdd4c6a
2 changed files with 9 additions and 3 deletions
  1. 2
    2
      EditorialModel/components.py
  2. 7
    1
      EditorialModel/model.py

+ 2
- 2
EditorialModel/components.py View File

@@ -6,6 +6,7 @@
6 6
 import datetime
7 7
 import logging
8 8
 from collections import OrderedDict
9
+import hashlib
9 10
 
10 11
 import EditorialModel.fieldtypes as ftypes
11 12
 from EditorialModel.exceptions import *
@@ -79,8 +80,7 @@ class EmComponent(object):
79 80
     ## @brief Hash function that allows to compare two EmComponent
80 81
     # @return EmComponent+ClassName+uid
81 82
     def __hash__(self):
82
-        return hash(str(self.attr_dump))
83
-        # return "EmComponent"+self.__class__.__name__+str(self.uid)
83
+        return hashlib.md5(str(self.attr_dump).encode('utf-8')).hexdigest()
84 84
 
85 85
     ## @brief Test if two EmComponent are "equals"
86 86
     # @return True or False

+ 7
- 1
EditorialModel/model.py View File

@@ -10,7 +10,7 @@ from EditorialModel.fields import EmField
10 10
 from EditorialModel.types import EmType
11 11
 from EditorialModel.exceptions import *
12 12
 import EditorialModel
13
-
13
+import hashlib
14 14
 
15 15
 ## Manages the Editorial Model
16 16
 class Model(object):
@@ -26,6 +26,12 @@ class Model(object):
26 26
         self._components = {'uids': {}, 'EmClass': [], 'EmType': [], 'EmField': [], 'EmFieldGroup': []}
27 27
         self.load()
28 28
 
29
+    def __hash__(self):
30
+        return hashlib.md5(str({uid: component.__hash__ for uid, component in self._components.items()}).encode('utf-8')).hexdigest()
31
+
32
+    # def __eq__(self, other):
33
+    #     return self.__hash__() == other.__hash__()
34
+
29 35
     @staticmethod
30 36
     ## Given a name return an EmComponent child class
31 37
     # @param class_name str : The name to identify an EmComponent class

Loading…
Cancel
Save