Browse Source

add my own built-ins for aiter and anext because Python doesn't want

to add them..
main
John-Mark Gurney 5 years ago
parent
commit
cb5416e2b4
1 changed files with 18 additions and 14 deletions
  1. +18
    -14
      solardash/__init__.py

+ 18
- 14
solardash/__init__.py View File

@@ -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)


Loading…
Cancel
Save