Bonjour à tous,
Quelle différence(s) entre mes classes obj et Coord empêche(nt) d'utiliser le generator "for" comme il se doit?

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# *-* coding:UTF-8 *-*
 
class Coord(tuple):
	def __new__(cls,*arg):
		r=tuple.__new__(cls,arg[0]) if len(arg)==1 else tuple.__new__(cls,arg)
		return r
	def __init__(self,*arg):
		self.__coord=arg[0] if len(arg)==1 else list(arg)
		self.__dim=len(self.coord)
        @property
	def coord(self):
		return self.__coord
        @property
	def dim(self):
		return self.__dim
        @coord.setter
	def coord(self,add):
		print "Erreur!!"
        @coord.deleter
	def coord(self):
		print "Erreur!!"
 
	#Surcharges
	def __str__(self):
		return 'Coord'+str(tuple(self.coord))
	def __hash__(self):
		v=0
		pw=self.dim-1
		for c in self.__coord:
			v+=c*10**pw
			pw-=1
		return v
	def __add__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			gt=self if self.dim >= other.dim else other
			lt=other if other.dim <= self.dim else self
#			print ("gt=%s, lt=%s" % (gt,lt))
			ad=gt.dim-lt.dim
			val=()
			resized=lt.coord
			if ad!=0:
				for c in xrange(ad):
					resized+=0,
			for v1,v2 in zip(gt.coord,resized):
				val+=v1+v2,
 
			return Coord(val)
	def __sub__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:	
			gt=self if self.dim >= other.dim else other
			lt=other if other.dim <= self.dim else self
			fct=-1 if other.dim>self.dim else 1
			ad=gt.dim-lt.dim
			val=()
			resized=lt.coord
			if ad!=0:
				for c in xrange(ad):
					resized+=0,
			for v1,v2 in zip(gt.coord,resized):
				val+=(v1-v2)*fct,
 
			return Coord(val)
	def __mul__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			gt=self if self.dim >= other.dim else other
			lt=other if other.dim <= self.dim else self
			ad=gt.dim-lt.dim
			val=()
			resized=lt.coord
			if ad!=0:
				for c in xrange(ad):
					resized+=0,
			for v1,v2 in zip(gt.coord,resized):
				val+=v1*v2,
 
			return Coord(val)
	def __eq__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!"
		else:
			return self.coord==other.coord
	def __ne__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!"
		else:
			return self.coord!=other.coord
	def __gt__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			return self.__hash__()>other.__hash__()
	def __ge__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			return self.__hash__()>=other.__hash__()
	def __lt__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			return self.__hash__()<other.__hash__()
	def __le__(self,other):
		if not isinstance(other,Coord):
			print "Erreur!!"
		else:
			return self.__hash__()<=other.__hash__()
 
def step(c_min,c_max):
	#TODO test de validite des arguments
 
	t_min,t_max=list(c_min.coord),list(c_max.coord)
	l_min,l_max,ret_list,mods=[],[],[],[]
	for v1,v2 in zip(t_min,t_max):
		l_min.append(min([v1,v2]))
		ret_list.append(min([v1,v2]))
		l_max.append(max([v1,v2]))
 
#	print "l_min-->",l_min,"\nl_max-->",l_max,"\nret_list-->",ret_list
 
	max_click,count=1,1
 
	for mn,mx in zip(l_min,l_max):
		mods.append(abs(max_click))
		max_click*=abs(mx-mn)+1
 
#	print "max_click-->",max_click,"\nmods-->",mods
	while count<=max_click:
 
		for i in xrange(len(l_min)):
			if not count%mods[i]:
				#print ("not %d%%%d = %s" % (count,mods[i],not count%mods[i]))
				ret_list[i]=ret_list[i]+1 if ret_list[i]<l_max[i] else l_min[i]
				#print ("ret_list[%d]<l_max[%d] = %s  l_min[%d] = %s" % (i,i,ret_list[i]<l_max[i],i,l_min[i]))
				#print ("ret_list[%d] = %s" % (i,ret_list[i]))
 
		yield Coord(ret_list)
		count+=1
 
class obj(object):
	oid=1
	def __init__(self):
		self.oid=obj.oid
		obj.oid+=1
	def __str__(self):
		return ("obj.%d" % self.oid)
	def __repr__(self):
		return ("obj.%d" % self.oid)
	def __hash__(self):
		return self.oid
 
def robj(mx):
	count=1
	while count<=mx:
		yield obj()
		count+=1
 
class mini_field():
	def __init__(self):
		self.__d={}
		for c in step(Coord(1,1),Coord(1,4)):
			self.__d[c]=c
		for o in robj(4):				
			self.__d[o]=o
        @property
	def d(self):
		return self.__d
 
if __name__=='__main__':
	f=mini_field()
	for k in f.d:
		print k,f.d[k]
	print f.d.keys()
Voici ce que j'obtiens en sortie:
obj.1 obj.1
obj.2 obj.2
obj.3 obj.3
obj.4 obj.4
Coord(1, 1) Coord(1, 1)
Coord(1, 1) Coord(1, 1)
Coord(1, 1) Coord(1, 1)
Coord(1, 1) Coord(1, 1)
[obj.1, obj.2, obj.3, obj.4, (1, 1), (1, 2), (1, 3), (1, 4)]
Je ne retrouve plus les clés de type Coord en passant par un generator...