|
| 1 | +from tornado import gen |
| 2 | + |
| 3 | +from streamz import Stream |
| 4 | + |
| 5 | +_global_sinks = set() |
| 6 | + |
| 7 | + |
| 8 | +class Sink(Stream): |
| 9 | + |
| 10 | + _graphviz_shape = 'trapezium' |
| 11 | + |
| 12 | + def __init__(self, upstream, **kwargs): |
| 13 | + super().__init__(upstream, **kwargs) |
| 14 | + _global_sinks.add(self) |
| 15 | + |
| 16 | + |
| 17 | +@Stream.register_api() |
| 18 | +class sink(Sink): |
| 19 | + """ Apply a function on every element |
| 20 | +
|
| 21 | + Examples |
| 22 | + -------- |
| 23 | + >>> source = Stream() |
| 24 | + >>> L = list() |
| 25 | + >>> source.sink(L.append) |
| 26 | + >>> source.sink(print) |
| 27 | + >>> source.sink(print) |
| 28 | + >>> source.emit(123) |
| 29 | + 123 |
| 30 | + 123 |
| 31 | + >>> L |
| 32 | + [123] |
| 33 | +
|
| 34 | + See Also |
| 35 | + -------- |
| 36 | + map |
| 37 | + Stream.sink_to_list |
| 38 | + """ |
| 39 | + |
| 40 | + def __init__(self, upstream, func, *args, **kwargs): |
| 41 | + self.func = func |
| 42 | + # take the stream specific kwargs out |
| 43 | + stream_name = kwargs.pop("stream_name", None) |
| 44 | + self.kwargs = kwargs |
| 45 | + self.args = args |
| 46 | + super().__init__(upstream, stream_name=stream_name) |
| 47 | + |
| 48 | + def update(self, x, who=None, metadata=None): |
| 49 | + result = self.func(x, *self.args, **self.kwargs) |
| 50 | + if gen.isawaitable(result): |
| 51 | + return result |
| 52 | + else: |
| 53 | + return [] |
| 54 | + |
| 55 | + |
| 56 | +@Stream.register_api() |
| 57 | +class sink_to_textfile(Sink): |
| 58 | + """ Write elements to a plain text file, one element per line. Type of elements |
| 59 | + must be ``str``. |
| 60 | +
|
| 61 | + Arguments |
| 62 | + --------- |
| 63 | + file: str or file-like |
| 64 | + File to write the elements to. ``str`` is treated as a file name to open. |
| 65 | + If file-like, descriptor must be open in text mode. |
| 66 | + Note that the file descriptor will be closed when sink is destroyed. |
| 67 | + end: str, optional |
| 68 | + This value will be written to the file after each element. |
| 69 | + Defaults to newline character. |
| 70 | + mode: str, optional |
| 71 | + If file is ``str``, file will be opened in this mode. Defaults to ``"a"`` |
| 72 | + (append mode). |
| 73 | +
|
| 74 | + Examples |
| 75 | + -------- |
| 76 | + >>> source = Stream() |
| 77 | + >>> source.map(str).sink_to_file("test.txt") |
| 78 | + >>> source.emit(0) |
| 79 | + >>> source.emit(1) |
| 80 | + >>> print(open("test.txt", "r").read()) |
| 81 | + 0 |
| 82 | + 1 |
| 83 | + """ |
| 84 | + def __init__(self, upstream, file, end="\n", mode="a", **kwargs): |
| 85 | + self._fp = open(file, mode=mode, buffering=1) if isinstance(file, str) else file |
| 86 | + self._end = end |
| 87 | + super().__init__(upstream, ensure_io_loop=True, **kwargs) |
| 88 | + |
| 89 | + def __del__(self): |
| 90 | + self._fp.close() |
| 91 | + |
| 92 | + @gen.coroutine |
| 93 | + def update(self, x, who=None, metadata=None): |
| 94 | + yield self.loop.run_in_executor(None, self._fp.write, x) |
| 95 | + yield self.loop.run_in_executor(None, self._fp.write, self._end) |
0 commit comments