def test_max_is_number(self): for protocol in ("json", "json_verbose"): io = StringIO() w = Writer(io, protocol) w.write([value]) actual_type = type(json.loads(io.getvalue())[0]) self.assertEqual(expected_type, actual_type)
def test_roundtrip(self): in_data = value io = StringIO() w = Writer(io, "json") w.write(in_data) r = Reader("json") out_data = r.read(StringIO(io.getvalue())) self.assertEqual(in_data, out_data)
def to_transit(in_data, protocol='json'): io = StringIO() writer = Writer(io, protocol) writer.register(TestObj, TestObjHandler) writer.write(in_data) return io.getvalue()
def test_reencode_json_verbose(self): io = StringIO() writer = Writer(io, protocol="json_verbose") writer.write(val) s = io.getvalue() io = StringIO(s) reader = Reader(protocol="json_verbose") newval = reader.read(io) self.assertEqual(val, newval)
def test_reencode_msgpack(self): io = BytesIO() writer = Writer(io, protocol="msgpack") writer.write(val) s = io.getvalue() io = BytesIO(s) reader = Reader(protocol="msgpack") newval = reader.read(io) self.assertEqual(val, newval)
def test_write_bool(self): for protocol in ("json", "json_verbose", "msgpack"): io = StringIO() w = Writer(io, protocol) w.write((True, False)) r = Reader(protocol) io.seek(0) out_data = r.read(io) assert out_data[0] == true assert out_data[1] == false
def test_reencode_json(self): io = StringIO() writer = Writer(io, protocol="json") writer.write(val) s = io.getvalue() # Uncomment when debugging to see what payloads fail # print(s) io = StringIO(s) reader = Reader(protocol="json") newval = reader.read(io) self.assertEqual(val, newval)
def write(data): sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.connect(SERVER_ADDRESS) with closing(sock): sockfile = sock.makefile('wrb') with closing(sockfile): Writer(sockfile, TRANSFER_PROTOCOL).write(data) return Reader(TRANSFER_PROTOCOL).read(sockfile)
def sanitySend(self, message): writeHandlers = marshal.getWriteHandlers(localTargets, localResources) writeHandlers.update( { deque: ArrayHandler, numpy.uint32: NumpyIntHandler, numpy.int64: NumpyIntHandler, numpy.float32: NumpyFloatHandler, numpy.ndarray: NumpyArrayHandler, } ) io = StringIO() writer = Writer(io, TRANSIT_ENCODING) for objType, handler in writeHandlers.items(): writer.register(objType, handler) writer.write(message) serialized = str(io.getvalue()) reactor.callFromThread(WebSocketServerProtocol.sendMessage, self, serialized, isBinary=False)
def dump(data, name, workdir='work', backend='pickle'): """Dump a data object.""" if not os.path.exists(workdir): os.makedirs(workdir) if backend == 'pickle': fname = os.path.join(workdir, name + '.pkl') with open(fname, 'w') as f: pickle.dump(data, f, -1) elif backend == 'transit': fname = os.path.join(workdir, name + '.json') from transit.writer import Writer with open(fname, 'w') as f: writer = Writer(f, 'json') writer.register(np.ndarray, NDArrayHandler) writer.register(np.int64, NumpyIntHandler) writer.register(np.float64, NumpyFloatHandler) writer.write(data)
def _request(action, datoms): io = StringIO() writer = Writer(io) writer.write({Keyword("content"): datoms}) r = post("http://127.0.0.1:8080/" + action, data=io.getvalue().encode('utf-8'), headers={ 'Content-Type': "application/transit+json; charset=utf-8", }) try: reader = Reader() data = reader.read(StringIO(r.content)) except: print "ILLEGAL RESPONSE: " try: print r.content except UnboundLocalError: print "NO RESPONSE" pass if not r: raise DatomicError(data[Keyword("content")]) else: return data[Keyword("content")]
def dump(data, name, workdir='work', backend='pickle'): """Dump a data object.""" if not os.path.exists(workdir): os.makedirs(workdir) if backend == 'pickle': fname = os.path.join(workdir, name+'.pkl') with open(fname, 'w') as f: pickle.dump(data, f, -1) elif backend == 'transit': fname = os.path.join(workdir, name+'.json') from transit.writer import Writer with open(fname, 'w') as f: writer = Writer(f, 'json') writer.register(np.ndarray, NDArrayHandler) writer.register(np.int64, NumpyIntHandler) writer.register(np.float64, NumpyFloatHandler) writer.write(data)
def sanitySend(self, message): writeHandlers = marshal.getWriteHandlers(localTargets, localResources) writeHandlers.update({ deque: ArrayHandler, numpy.uint32: NumpyIntHandler, numpy.int64: NumpyIntHandler, numpy.float32: NumpyFloatHandler, numpy.ndarray: NumpyArrayHandler, }) io = StringIO() writer = Writer(io, TRANSIT_ENCODING) for objType, handler in writeHandlers.items(): writer.register(objType, handler) writer.write(message) serialized = str(io.getvalue()) reactor.callFromThread(WebSocketServerProtocol.sendMessage, self, serialized, isBinary=False)
def transit_dumps(x): io = StringIO() Writer(io, 'json').write(x) return io.getvalue()
def write_transit(value): sio = StringIO() writer = Writer(sio, "json") writer.write(value) return sio.getvalue()
def serialize(data): io = StringIO() writer = Writer(io, "msgpack") writer.register(Decimal, DecimalWriteHandler) writer.write(data) return io
def transitify(val, format='json'): io = StringIO() writer = Writer(io, format) # or "json-verbose", "msgpack" writer.write(val) return io.getvalue()
def test_key_not_cached(self): io = StringIO() w = Writer(io, "json_verbose") w.write([{'myKey1': 42}, {'myKey1': 42}]) self.assertEqual(io.getvalue(), u"[{\"myKey1\":42},{\"myKey1\":42}]")
try: from StringIO import StringIO except ImportError: from io import StringIO # In Case we want to use multiple threads try: import thread except ImportError: import _thread as thread import time # Creates the writer io = StringIO() writer = Writer(StringIO(), "json") # Creates the reader reader = Reader("json") # Gets the agent agent = run_student_bot() # Url For the Websocket url = "ws://emscript.regent.edu:3449/chat?name=ChAI&room=ChatTest" # Defines the action to do on reciept of a message def on_message(ws, message): # Uses the transit reader to parse the recieved message
def encode_transit(records): '''Returns the records serialized as Transit/json in utf8''' with StringIO() as buf: writer = Writer(buf, "json") writer.write(records) return buf.getvalue().encode('utf8')
#!/usr/bin/python # -*- coding: utf-8 -*- import websocket import thread import time from transit.writer import Writer from transit.reader import Reader from StringIO import StringIO io = StringIO() writer = Writer(io, "json") def on_message(ws, message): print "message received: ", message def on_error(ws, error): print "error ", error def on_close(ws): print "### closed ###" def on_open(ws): def run(*args): writer.write({"command": "init", "token": "token-1"}) s = io.getvalue()