Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- import time
- def run(code):
- for i, line in enumerate(code.split('\n')):
- l = Line(line, i)
- asyncio.ensure_future(l.run())
- class Line:
- def __init__(self, code, line):
- self.code = code
- self.line = line
- self.char_count = {i: v for i, v in enumerate(self.code)}
- async def execute(self, cmd, start, end):
- if cmd == 'p':
- text = self.code[start:end]
- if '))' in text:
- text = text.replace('))', '\n')
- print(text, end='')
- elif cmd == 't':
- await asyncio.sleep(float(self.code[start:end]))
- elif cmd == 'v':
- name, value = self.code[start:end].split('/')
- setattr(self, name, value)
- async def run(self):
- for index, char in self.char_count.items():
- if char == ']':
- # Search for start of function
- found_ends = 0
- new_index = index
- while True:
- new_index -= 1
- if self.char_count[new_index] == '[':
- if found_ends:
- found_ends -= 1
- continue
- else:
- await self.execute(self.code[new_index - 1], new_index + 1, index)
- break
- elif self.char_count[new_index] == ']':
- found_ends += 1
- if __name__ == '__main__':
- code = """
- t[2]p[Hello]
- p[World]
- """
- loop = asyncio.get_event_loop()
- run(code)
- loop.run_forever()
- # l = Line("p[Hello World p[Cooctus]]", 0)
- # l.run()
Add Comment
Please, Sign In to add comment