Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

38 lignes
1.1KB

  1. """
  2. Allows us to visualize the state in a browser
  3. """
  4. import atexit
  5. import threading
  6. import time
  7. from websocket import create_connection
  8. from graphstate import GraphState
  9. import json
  10. class VisibleGraphState(GraphState):
  11. """ Overloads the graph state with methods for sending to the browser over a websocket """
  12. def __init__(self, *args, **kwargs):
  13. """ Constructor """
  14. GraphState.__init__(self, *args, **kwargs)
  15. self.ws = create_connection("ws://localhost:5000")
  16. atexit.register(self.shutdown)
  17. def shutdown(self):
  18. """ The client should shut down automatically on close """
  19. self.update()
  20. self.ws.close()
  21. def to_json(self):
  22. """ We override to_json() so that we send the whole `ngbh` structure in JS-friendly form """
  23. ngbh = {a: {b: True for b in self.ngbh[a]}
  24. for a in self.ngbh}
  25. return {"vops": self.vops, "ngbh": ngbh, "meta": self.meta}
  26. def update(self):
  27. """ Call this function when you are ready to send data to the browser """
  28. data = json.dumps(self.to_json())
  29. self.ws.send(data)