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.

viz.py 1.1KB

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