Skip to content

Update race_behaviours.py - #2

Open
Margravegit wants to merge 1 commit into
jazzz:developmentfrom
Margravegit:patch-2
Open

Margravegit wants to merge 1 commit into
jazzz:developmentfrom
Margravegit:patch-2

Conversation

@Margravegit

Copy link
Copy Markdown

Added fullstop behavior

Added fullstop behavior
@jazzz

jazzz commented Mar 15, 2018

Copy link
Copy Markdown
Owner

Looking Good!

Comment thread src/race_behaviours.py

async def update(self):
while True:
await asyncio.sleep(0.1)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works great, with co-routines we can simplify things by removing the conditional logic. In a coroutine when you 'await' you are suspending the function, so it resumes where it left off.

async def update():
    n = 255
    while True:
        for x in range(n+1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)
        for x in range(n-1,0,-1):
            await asyncio.sleep(0.1)
            self.color = Color(x,0,0)

Better yet we could also do something like if you wanted to reuse the functionality.

async def update():
    async for x in thereAndBackIncrementor(0,10):
        self.color = Color(x,0,0)
        await asyncio.sleep(0.1)


async def thereAndBackIncrementor(firstVal,lastVal):
    for x in range(lastVal + 1):
        yield x
    for x in range(lastVal - 1, firstVal, -1):
        yield x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants