1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """C{unique [-c]}
19
20 Input objects are passed to output, removing duplicates. No output is
21 generated until the end of the input stream occurs. However, if the
22 duplicates are known to be consecutive, then specifying C{-c} allows
23 output to be generated sooner. Input order is preserved only if C{-c}
24 is specified.
25
26 """
27
28 import types
29
30 import osh.args
31 import osh.core
32
33 Option = osh.args.Option
34
35
38
39
40 -def unique(consecutive = False):
41 """Input objects are passed to output, removing duplicates. No output
42 is generated until the end of the input stream occurs. However, if the
43 duplicates are known to be consecutive, then setting C{consecutive} to true
44 allows output to be generated sooner. Input order is preserved only if
45 C{consecutive} is true.
46 """
47 args = []
48 if consecutive:
49 args.append(Option('-c'))
50 return _Unique().process_args(*args)
51
53
54 _uniquer = None
55
56
57
58
61
62
63
64
67
69 args = self.args()
70 if args.has_next():
71 self.usage()
72 if args.flag('-c'):
73 self._uniquer = _ConsecutiveUniquer(self)
74 else:
75 self._uniquer = _GeneralUniquer(self)
76
78 self._uniquer.receive(object)
79
81 self._uniquer.receive_complete()
82
83
85
86 _command = None
87 _unique = None
88
90 self._command = command
91 self._unique = {}
92
94 if type(object) == types.ListType:
95 object = tuple(object)
96 self._unique[object] = None
97
99 for key in self._unique.keys():
100 self._command.send(key)
101 self._command.send_complete()
102
104
105 _command = None
106 _current = None
107
110
112 if self._current != object:
113 if self._current is not None:
114 self._command.send(self._current)
115 self._current = object
116
118 if self._current is not None:
119 self._command.send(self._current)
120 self._command.send_complete()
121