Ver código fonte

Complete the LeFactory generated code tests, add a __hash__ method for fieldtypes

Yann Weber 9 anos atrás
pai
commit
0be8db4d00
2 arquivos alterados com 13 adições e 2 exclusões
  1. 9
    0
      EditorialModel/fieldtypes/generic.py
  2. 4
    2
      leobject/lefactory.py

+ 9
- 0
EditorialModel/fieldtypes/generic.py Ver arquivo

@@ -82,6 +82,15 @@ class GenericFieldType(object):
82 82
     @staticmethod
83 83
     def module_name(fieldtype_name):
84 84
         return 'EditorialModel.fieldtypes.%s'%(fieldtype_name)
85
+
86
+    ## @brief __hash__ implementation for fieldtypes
87
+    def __hash__(self):
88
+        hash_dats = [ self.__class__.__module__ ]
89
+        for kdic in sorted([k for k in self.__dict__.keys() if not k.startswith('_')]):
90
+            hash_dats.append((kdic, getattr(self, kdic)))
91
+        return hash(tuple(hash_dats))
92
+
93
+
85 94
     
86 95
     ## @brief Transform a value into a valid python representation according to the fieldtype
87 96
     # @param value ? : The value to cast

+ 4
- 2
leobject/lefactory.py Ver arquivo

@@ -31,6 +31,8 @@ class LeFactory(object):
31 31
     # @return name.title()
32 32
     @staticmethod
33 33
     def name2classname(name):
34
+        if not isinstance(name, str):
35
+            raise AttributeError("Argument name should be a str and not a %s"%type(name))
34 36
         return name.title()
35 37
 
36 38
     ## @brief Return a call to a FieldType constructor given an EmField
@@ -64,12 +66,12 @@ class LeFactory(object):
64 66
                 cls_fieldgroup[fieldgroup.name].append(field.name)
65 67
         
66 68
         return """
67
-{name}._fildtypes = {ftypes}
69
+{name}._fieldtypes = {ftypes}
68 70
 {name}._linked_types = {ltypes}
69 71
 {name}._fieldgroups = {fgroups}
70 72
 """.format(
71 73
     name = LeFactory.name2classname(emclass.name),
72
-    ftypes = repr(cls_fields),
74
+    ftypes = "{"+(','.join([ '\n%s:%s'%(repr(f),v) for f,v in cls_fields.items()]))+"}",
73 75
     ltypes = "{"+(','.join(cls_linked_types))+'}',
74 76
     fgroups = repr(cls_fieldgroup)
75 77
 )

Carregando…
Cancelar
Salvar