From cb5416e2b49ce8b864e21dd22b5a72d250566523 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 23 Jan 2020 10:32:56 -0800 Subject: [PATCH] add my own built-ins for aiter and anext because Python doesn't want to add them.. --- solardash/__init__.py | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/solardash/__init__.py b/solardash/__init__.py index 0151469..e81e73e 100644 --- a/solardash/__init__.py +++ b/solardash/__init__.py @@ -38,6 +38,10 @@ logging.basicConfig(level=logging.DEBUG) from aiohttp import web from RainEagle.parse import LogDir as RELogDir, _cmaiter +# https://twitter.com/encthenet/status/1220412987732787200?s=20 +aiter = lambda x: x.__aiter__() +anext = lambda x: x.__anext__() + class StickyChannel(object): '''A pub/sub style class. Any objects that are posted (via the post method), are received by any async iterators over the instance. @@ -93,7 +97,7 @@ class SolarDataWS(object): async with _cmaiter(self._raineagle.enditer()) as ei: while True: try: - res = await ei.__anext__() + res = await anext(ei) await ws.send_str('ng %.4f' % res.load) except concurrent.futures._base.CancelledError: return @@ -174,9 +178,9 @@ class MiscTest(unittest.TestCase): postmsgtask = loop.create_task(postmsg()) - async with _cmaiter(chan.__aiter__()) as chaniter: + async with _cmaiter(aiter(chan)) as chaniter: order.append('c') - val = await chaniter.__anext__() + val = await anext(chaniter) order.append('d') self.assertEqual(val, dataval) @@ -201,18 +205,18 @@ class MiscTest(unittest.TestCase): postmsgtask = loop.create_task(postmsg()) - async with _cmaiter(chan.__aiter__()) as chaniter, \ - _cmaiter(chan.__aiter__()) as chaniter2: - val = await chaniter.__anext__() - val2 = await chaniter2.__anext__() + async with _cmaiter(aiter(chan)) as chaniter, \ + _cmaiter(aiter(chan)) as chaniter2: + val = await anext(chaniter) + val2 = await anext(chaniter2) await asyncio.sleep(.01) - val3 = await chaniter.__anext__() - val4 = await chaniter2.__anext__() + val3 = await anext(chaniter) + val4 = await anext(chaniter2) - async with _cmaiter(chan.__aiter__()) as chaniter3: - val5 = await chaniter3.__anext__() + async with _cmaiter(aiter(chan)) as chaniter3: + val5 = await anext(chaniter3) self.assertEqual(val, dataval) self.assertEqual(val2, dataval) @@ -240,11 +244,11 @@ class MiscTest(unittest.TestCase): postmsgtask = loop.create_task(postmsg()) - async with _cmaiter(chan.__aiter__()) as chaniter: + async with _cmaiter(aiter(chan)) as chaniter: order.append('c') - val = await chaniter.__anext__() + val = await anext(chaniter) order.append('d') - val2 = await chaniter.__anext__() + val2 = await anext(chaniter) order.append('e') self.assertEqual(val, 5)