blob: dfa03e03c7783202ac5b1ac2553ab0c8802a6ac6 (
plain)
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
|
# Copyright: 2005 Gentoo Foundation
# Author(s):
# License: GPL2
# $Header: /local/data/ulm/cvs/history/var/cvsroot/gentoo-src/portage/portage/util/lists.py,v 1.1 2005/07/20 14:33:12 ferringb Exp $
def unique(s):
"""lifted from python cookbook, credit: Tim Peters
Return a list of the elements in s in arbitrary order, sans duplicates"""
n = len(s)
if n == 0:
return []
u = {}
# assume all elements are hashable, if so, it's linear
try:
for x in s:
u[x] = 1
except TypeError: del u
else: return u.keys()
# so much for linear. abuse sort.
try:
t = list(s)
t.sort()
except TypeError: del t
else:
assert n > 0
last = t[0]
lasti = i = 1
while i < n:
if t[i] != last:
t[lasti] = last = t[i]
lasti += 1
i+= 1
return t[:lasti]
# blah. back to original portage.unique_array
u = []
for x in s:
if x not in s:
u.append(x)
return u
def iterflatten(l):
"""collapse [(1),2] into [1,2]"""
iters = [iter(l)]
while iters:
try:
while True:
x = iters[-1].next()
if isinstance(x, list) or isinstance(x, tuple):
iters.append(iter(x))
break
yield x
except StopIteration:
iters.pop(-1)
|