1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 import sys
17 import os
18 import string
19 import collections
20 import itertools
21 import copy
22
24 """ a list of list """
25
27 """ return all the combinatory possibility in a list """
28
29 if not self:
30 return [[]]
31
32 len_vector=[]
33 status_vector=[]
34 for i in range(0,len(self)):
35 status_vector.append(0)
36 if type(self[i])==list:
37 len_vector.append(len(self[i]))
38 else:
39 len_vector.append(1)
40
41 solution=[]
42 while 1:
43
44 solution.append(self.extract_vector(status_vector))
45 curent_pos=0
46 while 1:
47
48 if status_vector[curent_pos]==len_vector[curent_pos]-1:
49
50 curent_pos+=1
51 if(curent_pos==len(status_vector)):
52
53 break
54 else:
55
56 status_vector[curent_pos]+=1
57 for i in range(0,curent_pos):
58 status_vector[i]=0
59 break
60 if (curent_pos==len(status_vector)):
61 break
62
63 return solution
64
66 """ return the list of element corresponding at position in pos_list """
67
68 if len(pos_list)!=len(self):
69 return
70
71 solution=[]
72
73 for i in range(0,len(pos_list)):
74 solution.append(self[i][pos_list[i]])
75
76 return solution
77
78
80 """ return all permutation of list where each element belongs to corresponding list of the multilist
81
82 example: multilist=[ [a,b,c],[a,b,d],[c,d] ]
83 list=[a,b,c]
84 ->return=[ [a,b,c],[b,a,c] ]
85
86 multilist=[ [a,b,c],[a,b,d],[a,c,d] ]
87 list=[a,b,d]
88 ->return=[ [a,b,d],[b,a,d],[b,d,a] ]
89
90 option: if the list is made of object, but multilist are data of this object,
91 you can use opt to check if list[i].opt in self[i]
92 """
93
94 if len(list)!=len(self):
95 return
96
97 perm_pos=permutate(list)
98 sol=[]
99 for perm in perm_pos:
100 find=1
101 for i in range(0,len(list)):
102 if opt=='':
103 if perm[i] not in self[i]:
104 find=0
105 break
106 else:
107 value=eval('perm[i].'+opt)
108 if value not in self[i]:
109 find=0
110 break
111 if find:
112 sol.append(perm)
113 return sol
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
132 """permutate a sequence and return a list of the permutations"""
133
134 if not seq:
135 return [seq]
136 else:
137 temp = []
138 for k in range(len(seq)):
139 part = seq[:k] + seq[k+1:]
140
141 for m in permutate(part):
142 temp.append(seq[k:k+1] + m)
143
144 return temp
145
160
207
208
209
210
211
212
213
214
215
216
217
218
219
220
222 """read a file returning the lines in reverse order for each call of readline()
223 This actually just reads blocks (4096 bytes by default) of data from the end of
224 the file and returns last line in an internal buffer. I believe all the corner
225 cases are handled, but never can be sure..."""
226
227
229 while len(self.data) == 1 and ((self.blkcount * self.blksize) < self.size):
230 self.blkcount = self.blkcount + 1
231 line = self.data[0]
232 try:
233 self.seek(-self.blksize * self.blkcount, 2)
234 self.data = string.split(self.read(self.blksize) + line, '\n')
235 except IOError:
236 self.seek(0)
237 self.data = string.split(self.read(self.size - (self.blksize * (self.blkcount-1))) + line, '\n')
238
239 if len(self.data) == 0:
240 return ""
241
242
243
244 line = self.data[-1]
245 self.data = self.data[:-1]
246 return line + '\n'
247
248 - def __init__(self, filepos, blksize=4096):
249 """initialize the internal structures"""
250
251
252 self.size = os.stat(filepos)[6]
253
254 self.blksize = blksize
255
256 self.blkcount = 1
257 file.__init__(self, filepos, 'rb')
258
259
260 if self.size > self.blksize:
261 self.seek(-self.blksize * self.blkcount, 2)
262 self.data = string.split(self.read(self.blksize), '\n')
263
264
265 if not self.data[-1]:
266
267 self.data = self.data[:-1]
268
269
270
271
272
273
274
275
277 """ """
278
279 assert isinstance(pid_list, list)
280
281 assert isinstance(bjet_is_jet, bool) or bjet_is_jet in [0,1]
282
283 list_id = []
284 for i,pid in enumerate(pid_list):
285 if abs(pid) in [1,2,3,4]:
286 list_id.append('j')
287 elif abs(pid) == 5:
288 if bjet_is_jet:
289 list_id.append('j')
290 else:
291 list_id.append('b')
292 elif abs(pid) in [12,14,16,18,1000022,1000023,1000025,1000035]:
293 list_id.append('%s_%s' % (i, pid))
294 else:
295 list_id.append(pid)
296
297
298 return get_all_permutations(list_id)
299
301 """ """
302
303
304 nb_cat = collections.defaultdict(list)
305 for i,cat in enumerate(cat_list):
306 nb_cat[cat].append(i+1)
307 cat_names = nb_cat.keys()
308
309 iterator = dict([(cat, itertools.permutations(value))
310 for cat,value in nb_cat.items()])
311
312 permutations = []
313 current = 0
314
315 current_value = dict([(cat, list(it.next())) for cat,it in iterator.items()])
316
317 while current < len(iterator):
318
319 perm = []
320 curr = copy.deepcopy(current_value)
321 for cat in cat_list:
322 perm.append(curr[cat].pop(0))
323 permutations.append(perm)
324
325 while current < len(iterator):
326 cat = cat_names[current]
327 it = iterator[cat]
328 try:
329 new_val = it.next()
330 except StopIteration:
331 iterator[cat] = itertools.permutations(nb_cat[cat])
332 current_value[cat] = list(iterator[cat].next())
333 current +=1
334 else:
335 current_value[cat] = list(new_val)
336 current = 0
337 break
338
339 return permutations
340