Browse Source

Make it possible to add vertices without adding edges

master
Pete Shadbolt 8 years ago
parent
commit
062eb828d5
7 changed files with 43 additions and 9 deletions
  1. +6
    -0
      abp/graph.py
  2. +20
    -8
      abp/server.py
  3. +13
    -0
      examples/visualize.py
  4. +0
    -0
      static/index.html
  5. +0
    -0
      static/main.css
  6. +2
    -1
      static/main.js
  7. +2
    -0
      tests/test_graph.py

+ 6
- 0
abp/graph.py View File

@@ -13,6 +13,12 @@ class GraphState(object):
self.ngbh = defaultdict(set) self.ngbh = defaultdict(set)
self.vops = defaultdict(int) self.vops = defaultdict(int)


def add_vertex(self, v):
""" Add a vertex if it doesn't already exist """
if not v in self.ngbh:
self.ngbh[v] = set()
self.vops[v] = clifford.by_name["hadamard"]

def add_edge(self, v1, v2): def add_edge(self, v1, v2):
""" Add an edge between two vertices in the self """ """ Add an edge between two vertices in the self """
if not v1 in self.ngbh: if not v1 in self.ngbh:


server/server.py → abp/server.py View File

@@ -7,51 +7,63 @@ import json
import threading import threading
import time import time


state = 0

class VizHandler(SimpleHTTPRequestHandler): class VizHandler(SimpleHTTPRequestHandler):
""" Handles requests to the server """ """ Handles requests to the server """
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
SimpleHTTPRequestHandler.__init__(self, *args, **kwargs) SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)


def get_state(self): def get_state(self):
""" Get the current graph state """
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', 'application/json') self.send_header('Content-Type', 'application/json')
self.end_headers() self.end_headers()
state = self.server.state
self.wfile.write(json.dumps({"state":"{}".format(state)})) self.wfile.write(json.dumps({"state":"{}".format(state)}))
def do_GET(self, *args, **kwargs): def do_GET(self, *args, **kwargs):
""" Someone belled the server """
parsed_path = urlparse.urlparse(self.path) parsed_path = urlparse.urlparse(self.path)
print parsed_path.path
if parsed_path.path == "/state": if parsed_path.path == "/state":
return self.get_state() return self.get_state()
else: else:
return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs) return SimpleHTTPRequestHandler.do_GET(self, *args, **kwargs)


class VizServer(SocketServer.TCPServer):
""" Runs the server in a new thread """
class Server(SocketServer.TCPServer):
""" Serves the good stuff """
allow_reuse_address = True allow_reuse_address = True

def __init__(self, port = 8000): def __init__(self, port = 8000):
self.port = port self.port = port
self.state = None
SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler) SocketServer.TCPServer.__init__(self, ("127.0.0.1", self.port), VizHandler)


def update(self, state):
""" Update the in-memory state """
self.state = state

def run(self): def run(self):
""" Run in such a way that keyboard interrupts are caught properly """
try: try:
self.serve_forever() self.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
"Caught keyboard interrupt"
self.shutdown() self.shutdown()


def start(self): def start(self):
""" Start in a new thread """
thread = threading.Thread(None, self.run) thread = threading.Thread(None, self.run)
thread.daemon = True thread.daemon = True
thread.start() thread.start()
print "Go to 127.0.0.0:{}".format(self.port) print "Go to 127.0.0.0:{}".format(self.port)


if __name__ == '__main__': if __name__ == '__main__':
server = VizServer()
server = Server()
server.start() server.start()

i=0
while True: while True:
state += 1
server.update(i)
i += 1
time.sleep(1) time.sleep(1)

server.shutdown() server.shutdown()


+ 13
- 0
examples/visualize.py View File

@@ -0,0 +1,13 @@
from abp.server import Server
import time

server = Server()
server.start()

i=0
while True:
server.update(i)
i += 1
time.sleep(1)

server.shutdown()

server/index.html → static/index.html View File


server/main.css → static/main.css View File


server/main.js → static/main.js View File

@@ -1,9 +1,11 @@
var body; var body;
var state;


function poll() { function poll() {
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();


xhr.onload=function() { xhr.onload=function() {
state = JSON.parse(xhr.response);
soft_console.innerHTML = "\n" + xhr.responseText; soft_console.innerHTML = "\n" + xhr.responseText;
}; };


@@ -17,6 +19,5 @@ function poll() {
} }


window.onload = function () { window.onload = function () {
console.log("booting");
setInterval(poll, 1000); setInterval(poll, 1000);
} }

+ 2
- 0
tests/test_graph.py View File

@@ -67,3 +67,5 @@ def test_stress():
g.add_edge(i, i + 1) g.add_edge(i, i + 1)
assert time.clock() - t < .5 assert time.clock() - t < .5


def test_cz():
""" Test CZ gate """

Loading…
Cancel
Save