@@ -1280,9 +1280,9 @@ class timed_window_unique(Stream):
12801280 key: Union[Hashable, Callable[[Any], Hashable]]
12811281 Callable that accepts a stream element and returns a unique, hashable
12821282 representation of the incoming data (``key(x)``), or a hashable that gets
1283- the corresponding value of a stream element (``x[key]``). For example,
1284- ``key=lambda x: x["a"]`` would allow only elements with unique ``"a"`` values
1285- to pass through.
1283+ the corresponding value of a stream element (``x[key]``). For example, both
1284+ ``key=lambda x: x["a"]`` and ``key="a"`` would allow only elements with unique
1285+ ``"a"`` values to pass through.
12861286
12871287 .. note:: By default, we simply use the element object itself as the key,
12881288 so that object must be hashable. If that's not the case, a non-default
@@ -1298,39 +1298,37 @@ class timed_window_unique(Stream):
12981298 Examples
12991299 --------
13001300 >>> source = Stream()
1301- >>> stream = source.timed_window_unique(interval=2, keep="first").sink(print)
1302- >>> eles = [1, 2, 1, 3, 1, 3, 3, 2]
1303- >>> for ele in eles:
1301+
1302+ Get unique hashable elements in a window, keeping just the first occurrence:
1303+ >>> stream = source.timed_window_unique(interval=1.0, keep="first").sink(print)
1304+ >>> for ele in [1, 2, 3, 3, 2, 1]:
13041305 ... source.emit(ele)
1305- ... time.sleep(0.6)
13061306 ()
13071307 (1, 2, 3)
1308- (1, 3)
1309- (2,)
13101308 ()
13111309
1312- >>> source = Stream()
1313- >>> stream = source.timed_window_unique(interval=2, keep="last").sink(print)
1314- >>> eles = [1, 2, 1, 3, 1, 3, 3, 2]
1315- >>> for ele in eles:
1310+ Get unique hashable elements in a window, keeping just the last occurrence:
1311+ >>> stream = source.timed_window_unique(interval=1.0, keep="last").sink(print)
1312+ >>> for ele in [1, 2, 3, 3, 2, 1]:
13161313 ... source.emit(ele)
1317- ... time.sleep(0.6)
13181314 ()
1319- (2, 1, 3)
1320- (1, 3)
1321- (2,)
1315+ (3, 2, 1)
13221316 ()
13231317
1324- >>> source = Stream()
1325- >>> stream = source.timed_window_unique(interval=2, key=lambda x: len(x), keep="last").sink(print)
1326- >>> eles = ["f", "fo", "f", "foo", "f", "foo", "foo", "fo"]
1327- >>> for ele in eles:
1318+ Get unique elements in a window by (string) length, keeping just the first occurrence:
1319+ >>> stream = source.timed_window_unique(interval=1.0, key=len, keep="first")
1320+ >>> for ele in ["f", "b", "fo", "ba", "foo", "bar"]:
13281321 ... source.emit(ele)
1329- ... time.sleep(0.6)
13301322 ()
1331- ('fo', 'f', 'foo')
1332- ('f', 'foo')
1333- ('fo',)
1323+ ('f', 'fo', 'foo')
1324+ ()
1325+
1326+ Get unique elements in a window by (string) length, keeping just the last occurrence:
1327+ >>> stream = source.timed_window_unique(interval=1.0, key=len, keep="first")
1328+ >>> for ele in ["f", "b", "fo", "ba", "foo", "bar"]:
1329+ ... source.emit(ele)
1330+ ()
1331+ ('b', 'ba', 'bar')
13341332 ()
13351333 """
13361334 _graphviz_shape = "octagon"
0 commit comments