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