瀏覽代碼

Make it possible to add vertices without adding edges

master
Pete Shadbolt 8 年之前
父節點
當前提交
062eb828d5
共有 7 個文件被更改,包括 43 次插入9 次删除
  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 查看文件

@@ -13,6 +13,12 @@ class GraphState(object):
self.ngbh = defaultdict(set)
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):
""" Add an edge between two vertices in the self """
if not v1 in self.ngbh:


server/server.py → abp/server.py 查看文件

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

state = 0

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

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

def __init__(self, port = 8000):
self.port = port
self.state = None
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):
""" Run in such a way that keyboard interrupts are caught properly """
try:
self.serve_forever()
except KeyboardInterrupt:
"Caught keyboard interrupt"
self.shutdown()

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

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

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

server.shutdown()


+ 13
- 0
examples/visualize.py 查看文件

@@ -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 查看文件


server/main.css → static/main.css 查看文件


server/main.js → static/main.js 查看文件

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

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

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

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

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

+ 2
- 0
tests/test_graph.py 查看文件

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

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

Loading…
取消
儲存