Show More
@@ -9,16 +9,6 b'' | |||
|
9 | 9 | # the file COPYING, distributed as part of this software. |
|
10 | 10 | #----------------------------------------------------------------------------- |
|
11 | 11 | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # Imports | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | import types | |
|
17 | ||
|
18 | #----------------------------------------------------------------------------- | |
|
19 | # Code | |
|
20 | #----------------------------------------------------------------------------- | |
|
21 | ||
|
22 | 12 | def uniq_stable(elems): |
|
23 | 13 | """uniq_stable(elems) -> list |
|
24 | 14 | |
@@ -32,65 +22,14 b' def uniq_stable(elems):' | |||
|
32 | 22 | return [x for x in elems if x not in seen and not seen.add(x)] |
|
33 | 23 | |
|
34 | 24 | |
|
35 | def sort_compare(lst1, lst2, inplace=1): | |
|
36 | """Sort and compare two lists. | |
|
37 | ||
|
38 | By default it does it in place, thus modifying the lists. Use inplace = 0 | |
|
39 | to avoid that (at the cost of temporary copy creation).""" | |
|
40 | if not inplace: | |
|
41 | lst1 = lst1[:] | |
|
42 | lst2 = lst2[:] | |
|
43 | lst1.sort(); lst2.sort() | |
|
44 | return lst1 == lst2 | |
|
45 | ||
|
46 | ||
|
47 | def list2dict(lst): | |
|
48 | """Takes a list of (key,value) pairs and turns it into a dict.""" | |
|
49 | ||
|
50 | dic = {} | |
|
51 | for k,v in lst: dic[k] = v | |
|
52 | return dic | |
|
53 | ||
|
54 | ||
|
55 | def list2dict2(lst, default=''): | |
|
56 | """Takes a list and turns it into a dict. | |
|
57 | Much slower than list2dict, but more versatile. This version can take | |
|
58 | lists with sublists of arbitrary length (including sclars).""" | |
|
59 | ||
|
60 | dic = {} | |
|
61 | for elem in lst: | |
|
62 | if type(elem) in (types.ListType,types.TupleType): | |
|
63 | size = len(elem) | |
|
64 | if size == 0: | |
|
65 | pass | |
|
66 | elif size == 1: | |
|
67 | dic[elem] = default | |
|
68 | else: | |
|
69 | k,v = elem[0], elem[1:] | |
|
70 | if len(v) == 1: v = v[0] | |
|
71 | dic[k] = v | |
|
72 | else: | |
|
73 | dic[elem] = default | |
|
74 | return dic | |
|
75 | ||
|
76 | ||
|
77 | 25 | def flatten(seq): |
|
78 | 26 | """Flatten a list of lists (NOT recursive, only works for 2d lists).""" |
|
79 | 27 | |
|
80 | 28 | return [x for subseq in seq for x in subseq] |
|
81 | ||
|
82 | ||
|
83 | def get_slice(seq, start=0, stop=None, step=1): | |
|
84 | """Get a slice of a sequence with variable step. Specify start,stop,step.""" | |
|
85 | if stop == None: | |
|
86 | stop = len(seq) | |
|
87 | item = lambda i: seq[i] | |
|
88 | return map(item,xrange(start,stop,step)) | |
|
89 | ||
|
29 | ||
|
90 | 30 | |
|
91 | 31 | def chop(seq, size): |
|
92 | 32 | """Chop a sequence into chunks of the given size.""" |
|
93 | chunk = lambda i: seq[i:i+size] | |
|
94 | return map(chunk,xrange(0,len(seq),size)) | |
|
33 | return [seq[i:i+size] for i in xrange(0,len(seq),size)] | |
|
95 | 34 | |
|
96 | 35 |
@@ -18,8 +18,6 b' Authors:' | |||
|
18 | 18 | # Imports |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | |
|
21 | from IPython.utils.data import list2dict2 | |
|
22 | ||
|
23 | 21 | __all__ = ['Struct'] |
|
24 | 22 | |
|
25 | 23 | #----------------------------------------------------------------------------- |
@@ -370,7 +368,7 b' class Struct(dict):' | |||
|
370 | 368 | add_s = lambda old,new: old + ' ' + new |
|
371 | 369 | |
|
372 | 370 | # default policy is to keep current keys when there's a conflict |
|
373 |
conflict_solve = |
|
|
371 | conflict_solve = dict.fromkeys(self, preserve) | |
|
374 | 372 | |
|
375 | 373 | # the conflict_solve dictionary is given by the user 'inverted': we |
|
376 | 374 | # need a name-function mapping, it comes as a function -> names |
General Comments 0
You need to be logged in to leave comments.
Login now