Guest User

Untitled

a guest
Nov 18th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. import asyncio
  2. import time
  3.  
  4.  
  5. def run(code):
  6. for i, line in enumerate(code.split('\n')):
  7. l = Line(line, i)
  8. asyncio.ensure_future(l.run())
  9.  
  10.  
  11. class Line:
  12. def __init__(self, code, line):
  13. self.code = code
  14. self.line = line
  15. self.char_count = {i: v for i, v in enumerate(self.code)}
  16.  
  17. async def execute(self, cmd, start, end):
  18. if cmd == 'p':
  19. text = self.code[start:end]
  20. if '))' in text:
  21. text = text.replace('))', '\n')
  22. print(text, end='')
  23. elif cmd == 't':
  24. await asyncio.sleep(float(self.code[start:end]))
  25. elif cmd == 'v':
  26. name, value = self.code[start:end].split('/')
  27. setattr(self, name, value)
  28.  
  29. async def run(self):
  30. for index, char in self.char_count.items():
  31. if char == ']':
  32. # Search for start of function
  33. found_ends = 0
  34. new_index = index
  35. while True:
  36. new_index -= 1
  37. if self.char_count[new_index] == '[':
  38. if found_ends:
  39. found_ends -= 1
  40. continue
  41. else:
  42. await self.execute(self.code[new_index - 1], new_index + 1, index)
  43. break
  44. elif self.char_count[new_index] == ']':
  45. found_ends += 1
  46.  
  47.  
  48. if __name__ == '__main__':
  49. code = """
  50. t[2]p[Hello]
  51. p[World]
  52. """
  53. loop = asyncio.get_event_loop()
  54. run(code)
  55. loop.run_forever()
  56. # l = Line("p[Hello World p[Cooctus]]", 0)
  57. # l.run()
Add Comment
Please, Sign In to add comment