def send(self, topic, message): """Publishes a pulse message to the proper exchange.""" if not message: Log.error("Expecting a message") message._prepare() if not self.connection: self.connect() producer = Producer( channel=self.connection, exchange=Exchange(self.settings.exchange, type='topic'), routing_key=topic ) # The message is actually a simple envelope format with a payload and # some metadata. final_data = Dict( payload=message.data, _meta=set_default({ 'exchange': self.settings.exchange, 'routing_key': message.routing_key, 'serializer': self.settings.serializer, 'sent': time_to_string(datetime.datetime.now(timezone(self.settings.broker_timezone))), 'count': self.count }, message.metadata) ) producer.publish(jsons.scrub(final_data), serializer=self.settings.serializer) self.count += 1
def send(self, topic, message): """Publishes a pulse message to the proper exchange.""" if not message: Log.error("Expecting a message") message._prepare() if not self.connection: self.connect() producer = Producer( channel=self.connection, exchange=Exchange(self.settings.exchange, type='topic'), routing_key=topic ) # The message is actually a simple envelope format with a payload and # some metadata. final_data = Data( payload=message.data, _meta=set_default({ 'exchange': self.settings.exchange, 'routing_key': message.routing_key, 'serializer': self.settings.serializer, 'sent': time_to_string(datetime.datetime.now(timezone(self.settings.broker_timezone))), 'count': self.count }, message.metadata) ) producer.publish(jsons.scrub(final_data), serializer=self.settings.serializer) self.count += 1
def pretty_json(value): try: if scrub(value) is None: return "null" elif isinstance(value, basestring): if isinstance(value, str): value = utf82unicode(value) try: return quote(value) except Exception, e: from pyLibrary.debugs.logs import Log try: Log.note("try explicit convert of string with length {{length}}", length= len(value)) acc = [u"\""] for c in value: try: try: c2 = ESCAPE_DCT[c] except Exception, h: c2 = c c3 = unicode(c2) acc.append(c3) except BaseException, g: pass # Log.warning("odd character {{ord}} found in string. Ignored.", ord= ord(c)}, cause=g) acc.append(u"\"") output = u"".join(acc) Log.note("return value of length {{length}}", length= len(output)) return output
def pretty_json(value): try: if scrub(value) is None: return "null" elif isinstance(value, basestring): if isinstance(value, str): value = utf82unicode(value) try: return quote(value) except Exception, e: from pyLibrary.debugs.logs import Log try: Log.note( "try explicit convert of string with length {{length}}", length=len(value)) acc = [u"\""] for c in value: try: try: c2 = ESCAPE_DCT[c] except Exception, h: c2 = c c3 = unicode(c2) acc.append(c3) except BaseException, g: pass # Log.warning("odd character {{ord}} found in string. Ignored.", ord= ord(c)}, cause=g) acc.append(u"\"") output = u"".join(acc) Log.note("return value of length {{length}}", length=len(output)) return output
def encode(self, value, pretty=False): if pretty: return pretty_json(value) try: scrubbed = scrub(value) return unicode(self.encoder.encode(scrubbed)) except Exception, e: from pyLibrary.debugs.logs import Log Log.warning("problem serializing {{type}}", type= repr(value), cause=e) raise e
def encode(self, value, pretty=False): if pretty: return pretty_json(value) try: scrubbed = scrub(value) return unicode(self.encoder.encode(scrubbed)) except Exception, e: from pyLibrary.debugs.exceptions import Except from pyLibrary.debugs.logs import Log e = Except.wrap(e) Log.warning("problem serializing {{type}}", type=_repr(value), cause=e) raise e
def test_json(results, description, method, n): output = [] for case in cases: try: data, count = globals()[case] if "scrub" in description: #SCRUB BEFORE SENDING TO C ROUTINE (NOT FAIR, BUT WE GET TO SEE HOW FAST ENCODING GOES) data = unwrap(scrub(data)) try: example = method(data) if case == "HUGE": example = "<too big to show>" except Exception, e: Log.warning("json encoding failure", cause=e) example = "<CRASH>" t0 = time.time() try: for i in range(n): for i in range(count): output.append(method(data)) duration = time.time() - t0 except Exception: duration = time.time() - t0 summary = { "description": description, "interpreter": platform.python_implementation(), "time": duration, "type": case, "num": n, "count": count, "length": len(output), "result": example } Log.note("{{interpreter}}: {{description}} {{type}} x {{num}} x {{count}} = {{time}} result={{result}}", **summary) results.append(summary) except Exception, e: Log.warning("problem with encoding: {{message}}", {"message": e.message}, e)
def test_minus_inf(self): test = float("-inf") output = pypy_json_encode(test) expecting = cpython_json_encoder(jsons.scrub(test)) self.assertEqual(output, expecting, "expecting "+expecting)
def json_encode(value): """ FOR PUTTING JSON INTO DATABASE (sort_keys=True) dicts CAN BE USED AS KEYS """ return unicode(json_encoder.encode(jsons.scrub(value)))
except Exception, e: from pyLibrary.debugs.logs import Log Log.warning( "problem concatenating string of length {{len1}} and {{len2}}", len1=len("".join(output)), len2=len(p), ) output.append("\n]") return "".join(output) elif hasattr(value, "__json__"): j = value.__json__() if j == None: return " null " # TODO: FIND OUT WHAT CAUSES THIS return pretty_json(json_decoder(j)) elif scrub(value) is None: return "null" elif hasattr(value, "__iter__"): return pretty_json(list(value)) elif hasattr(value, "__call__"): return "null" else: try: if int(value) == value: return str(int(value)) except Exception: pass try: if float(value) == value: return str(float(value))
output.append(indent(p)) except Exception, e: from pyLibrary.debugs.logs import Log Log.warning("problem concatenating string of length {{len1}} and {{len2}}", len1=len("".join(output)), len2=len(p) ) output.append("\n]") return "".join(output) elif hasattr(value, '__json__'): j = value.__json__() if j == None: return " null " # TODO: FIND OUT WHAT CAUSES THIS return pretty_json(json_decoder(j)) elif scrub(value) is None: return "null" elif hasattr(value, '__iter__'): return pretty_json(list(value)) elif hasattr(value, '__call__'): return "null" else: try: if int(value) == value: return str(int(value)) except Exception: pass try: if float(value) == value: return str(float(value))