Python ujson.dumps() Examples
The following are 30
code examples of ujson.dumps().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ujson
, or try the search function
.
Example #1
Source File: mgmtbus.py From minemeld-core with Apache License 2.0 | 6 votes |
def _merge_status(self, nodename, status): currstatus = self._status.get(nodename, None) if currstatus is not None: if currstatus.get('clock', -1) > status.get('clock', -2): LOG.error('old clock: {} > {} - dropped'.format( currstatus.get('clock', -1), status.get('clock', -2) )) return self._status[nodename] = status try: source = nodename.split(':', 2)[2] self.SR.publish( 'mm-engine-status.'+source, ujson.dumps({ 'source': source, 'timestamp': int(time.time())*1000, 'status': status }) ) except: LOG.exception('Error publishing status')
Example #2
Source File: response.py From sanic with MIT License | 6 votes |
def json( body, status=200, headers=None, content_type="application/json", dumps=json_dumps, **kwargs, ): """ Returns response object with body in json format. :param body: Response data to be serialized. :param status: Response code. :param headers: Custom Headers. :param kwargs: Remaining arguments that are passed to the json encoder. """ return HTTPResponse( dumps(body, **kwargs), headers=headers, status=status, content_type=content_type, )
Example #3
Source File: rtx.py From txTrader with MIT License | 6 votes |
def handle_accounts(self, rows): rows = json.loads(rows) if rows: self.accounts = list(set([self.make_account(row) for row in rows])) self.accounts.sort() self.account_request_pending = False self.WriteAllClients('accounts: %s' % json.dumps(self.accounts)) self.update_connection_status('Up') for cb in self.account_request_callbacks: cb.complete(self.accounts) for cb in self.set_account_callbacks: self.output('set_account: processing deferred response.') self.process_set_account(cb.id, cb) else: self.handle_initial_account_failure('initial account query returned no data')
Example #4
Source File: bottle.py From silvia-pi with MIT License | 6 votes |
def apply(self, callback, route): dumps = self.json_dumps if not self.json_dumps: return callback def wrapper(*a, **ka): try: rv = callback(*a, **ka) except HTTPResponse as resp: rv = resp if isinstance(rv, dict): #Attempt to serialize, raises exception on failure json_response = dumps(rv) #Set content type only if serialization successful response.content_type = 'application/json' return json_response elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict): rv.body = dumps(rv.body) rv.content_type = 'application/json' return rv return wrapper
Example #5
Source File: rtx.py From txTrader with MIT License | 6 votes |
def format_results(self, results): #print('format_results: label=%s results=%s' % (self.label, results)) if self.label == 'account_data': results = self.format_account_data(results) elif self.label == 'positions': results = self.format_positions(results) elif self.label == 'orders': results = self.format_orders(results) elif self.label == 'tickets': results = self.format_tickets(results) elif self.label=='executions': results = self.format_executions(results) elif self.label == 'order_status': results = self.format_orders(results, self.id) elif self.label == 'barchart': results = self.api.format_barchart(results) return json.dumps(results)
Example #6
Source File: __main__.py From pydata2019-nlp-system with Apache License 2.0 | 6 votes |
def redis(self, stream='comments'): r : redis.Redis = redis.Redis(host=REDIS_HOST) try: s = self.sub.stream s = s.comments if stream == 'comments' else s.submissions f = extract_comment if stream == 'comments' else extract_post for c in tqdm(s()): msg = f(c) r.publish(stream, ujson.dumps(msg)) author = msg['author'] key = f'{stream}:{author}' r.incr(key) r.expire(key, 3600*24) except APIException as e: print(e) self.__retry() except ClientException as e: print(e) self.__retry()
Example #7
Source File: __main__.py From pydata2019-nlp-system with Apache License 2.0 | 6 votes |
def redis(self, stream='comments'): r : redis.Redis = redis.Redis(host=REDIS_HOST) try: s = self.sub.stream s = s.comments if stream == 'comments' else s.submissions f = extract_comment if stream == 'comments' else extract_post for c in tqdm(s()): msg = f(c) text = replace_urls(msg['text']) sentences = message_to_sentences(text) for s in sentences: r.publish(stream, ujson.dumps(s)) author = msg['author'] key = f'{stream}:{author}' r.incr(key) r.expire(key, 3600*24) except APIException as e: print(e) self.__retry() except ClientException as e: print(e) self.__retry()
Example #8
Source File: remote.py From universe with MIT License | 6 votes |
def send_message(self, method, body, headers): id = self._message_id self._message_id += 1 new_headers = { 'message_id': id, 'sent_at': time.time(), } if headers: new_headers.update(headers) payload = { 'method': method, 'body': body, 'headers': new_headers, } # This is a bit ugly, but decide how much we care if (method != 'v0.reply.control.ping' and 'parent_message_id' in new_headers) or\ method == 'v0.connection.close': logger.info('Sending rewarder message: %s', payload) else: logger.debug('Sending rewarder message: %s', payload) self.sendMessage(ujson.dumps(payload).encode('utf-8'), False)
Example #9
Source File: ner_sent.py From pydata2019-nlp-system with Apache License 2.0 | 6 votes |
def model(message): sentence = json.loads(message) doc = nlp(sentence) entities = nlp(sentence).ents if len(entities) == 0: return None ner = dict( sentence=sentence, entities=[str(e) for e in entities] ) cat, score = predict_sentiment(ner['sentence']) output = [dict( entity=entity, sentiment=cat, score=score ) for entity in ner['entities']] return json.dumps(output)
Example #10
Source File: _table_creator.py From sqlitebiter with MIT License | 6 votes |
def __require_rename_table(self, src_con: SimpleSQLite, src_table_name: str) -> bool: if not self.__dst_con.has_table(src_table_name): return False lhs = self.__dst_con.schema_extractor.fetch_table_schema(src_table_name).as_dict() rhs = src_con.schema_extractor.fetch_table_schema(src_table_name).as_dict() if lhs != rhs: self.__logger.debug( dedent( """\ require rename '{table}' because of src table and dst table has a different schema with the same table name: dst-schema={dst_schema} src-schema={src_schema} """ ).format( table=src_table_name, src_schema=json.dumps(lhs, indent=4, ensure_ascii=False), dst_schema=json.dumps(rhs, indent=4, ensure_ascii=False), ) ) return True return False
Example #11
Source File: _ipynb_converter.py From sqlitebiter with MIT License | 6 votes |
def convert(self) -> Set[str]: if not self.__metadata: self._logger.debug("metadata not found") return set() self.__convert_kernelspec() self.__convert_language_info() self.__convert_kv() if self.__metadata: self._logger.debug( "cannot convert: {}".format( json.dumps(self.__metadata, indent=4, ensure_ascii=False) ) ) return self._changed_table_name_set
Example #12
Source File: app.py From iris-relay with BSD 2-Clause "Simplified" License | 6 votes |
def on_get(self, req, resp): # Username verified in auth middleware username = req.context['user'] access_token = hashlib.sha256(os.urandom(32)).hexdigest() encrypted_token = self.fernet.encrypt(access_token.encode('utf8')) exp = time.time() + self.access_ttl connection = db.connect() cursor = connection.cursor() try: cursor.execute('''INSERT INTO `access_token` (`user_id`, `key`, `expiration`) VALUES ((SELECT `id` FROM `target` WHERE `name` = %s AND `type_id` = (SELECT `id` FROM `target_type` WHERE `name` = 'user')), %s, %s)''', (username, encrypted_token, exp)) connection.commit() key_id = cursor.lastrowid finally: cursor.close() connection.close() resp.body = ujson.dumps({'token': access_token, 'key_id': key_id, 'expiry': exp})
Example #13
Source File: mandrill.py From backend with GNU General Public License v2.0 | 6 votes |
def call(self, url, params=None): '''Actually make the API call with the given params - this should only be called by the namespace methods - use the helpers in regular usage like m.tags.list()''' if params is None: params = {} params['key'] = self.apikey params = json.dumps(params) self.log('POST to %s%s.json: %s' % (ROOT, url, params)) start = time.time() r = self.session.post('%s%s.json' % (ROOT, url), data=params, headers={'content-type': 'application/json', 'user-agent': 'Mandrill-Python/1.0.55'}) try: remote_addr = r.raw._original_response.fp._sock.getpeername() # grab the remote_addr before grabbing the text since the socket will go away except: remote_addr = (None, None) #we use two private fields when getting the remote_addr, so be a little robust against errors response_body = r.text complete_time = time.time() - start self.log('Received %s in %.2fms: %s' % (r.status_code, complete_time * 1000, r.text)) self.last_request = {'url': url, 'request_body': params, 'response_body': r.text, 'remote_addr': remote_addr, 'response': r, 'time': complete_time} result = json.loads(response_body) if r.status_code != requests.codes.ok: raise self.cast_error(result) return result
Example #14
Source File: test_recorder.py From indy-plenum with Apache License 2.0 | 6 votes |
def test_get_list_from_recorder(recorder): msg1, frm1 = 'm1', 'f1' msg2, frm2 = 'm2', 'f2' msg3, to1, to11 = 'm3', 't1', 't11' # Decrease resolution recorder.TIME_FACTOR = 1 time.sleep(1) recorder.add_outgoing(msg3, to1, to11) recorder.add_incoming(msg1, frm1) recorder.add_incoming(msg2, frm2) recorder.add_disconnecteds('a', 'b', 'c') for k, v in recorder.store.iterator(include_value=True): assert v.decode() == json.dumps([ [Recorder.OUTGOING_FLAG, 'm3', 't1', 't11'], [Recorder.INCOMING_FLAG, 'm1', 'f1'], [Recorder.INCOMING_FLAG, 'm2', 'f2'], [Recorder.DISCONN_FLAG, 'a', 'b', 'c'] ])
Example #15
Source File: RPGBot.py From RPGBot with GNU General Public License v3.0 | 6 votes |
def update_stats(self): url = "https://bots.discord.pw/api/bots/{}/stats".format(self.user.id) while not self.is_closed(): payload = json.dumps(dict(server_count=len(self.guilds))).encode() headers = {'authorization': self._auth[1], "Content-Type": "application/json"} async with self.session.post(url, data=payload, headers=headers) as response: await response.read() url = "https://discordbots.org/api/bots/{}/stats".format(self.user.id) payload = json.dumps(dict(server_count=len(self.guilds))).encode() headers = {'authorization': self._auth[2], "Content-Type": "application/json"} async with self.session.post(url, data=payload, headers=headers) as response: await response.read() await asyncio.sleep(14400)
Example #16
Source File: test_traced_writer.py From minemeld-core with Apache License 2.0 | 6 votes |
def test_writer(self): config = {} comm = comm_mock.comm_factory(config) store = traced_mock.store_factory() writer = minemeld.traced.writer.Writer(comm, store, 'TESTTOPIC') self.assertEqual(comm.sub_channels[0]['topic'], 'TESTTOPIC') self.assertEqual(comm.sub_channels[0]['allowed_methods'], ['log']) writer.log(0, log='testlog') self.assertEqual(store.writes[0]['timestamp'], 0) self.assertEqual(store.writes[0]['log'], ujson.dumps({'log': 'testlog'})) writer.stop() writer.log(0, log='testlog') self.assertEqual(len(store.writes), 1) writer.stop() # just for coverage
Example #17
Source File: xmpp.py From minemeld-core with Apache License 2.0 | 6 votes |
def _xmpp_publish(self, cmd, data=None): if data is None: data = '' payload_xml = sleekxmpp.xmlstream.ET.Element('mm-command') command_xml = sleekxmpp.xmlstream.ET.SubElement(payload_xml, 'command') command_xml.text = cmd seqno_xml = sleekxmpp.xmlstream.ET.SubElement(payload_xml, 'seqno') seqno_xml.text = '%s' % self.sequence_number data_xml = sleekxmpp.xmlstream.ET.SubElement(payload_xml, 'data') data_xml.text = ujson.dumps(data) result = self._xmpp_client['xep_0060'].publish( self.pubsub_service, self.node, payload=payload_xml ) LOG.debug('%s - xmpp publish: %s', self.name, result) self.sequence_number += 1 self.statistics['xmpp.published'] += 1
Example #18
Source File: autofocus.py From minemeld-core with Apache License 2.0 | 6 votes |
def _build_iterator(self, now): if self.api_key is None or self.label is None: raise RuntimeError( '%s - api_key or label not set, poll not performed' % self.name ) body = { 'label': self.label, 'panosFormatted': True } af = pan.afapi.PanAFapi( hostname=self.hostname, verify_cert=self.verify_cert, api_key=self.api_key ) r = af.export(data=ujson.dumps(body)) r.raise_for_status() return r.json.get('export_list', [])
Example #19
Source File: protocol.py From sockjs with Apache License 2.0 | 5 votes |
def messages_frame(messages): return FRAME_MESSAGE + json.dumps(messages, **kwargs) # Handler messages # ---------------------
Example #20
Source File: text.py From polyglot with GNU General Public License v3.0 | 5 votes |
def to_json(self, *args, **kwargs): '''Return a json representation (str) of this blob. Takes the same arguments as json.dumps. .. versionadded:: 0.5.1 ''' try: import ujson as json except ImportError: import json return json.dumps(self.serialized, *args, **kwargs)
Example #21
Source File: recorder.py From indy-plenum with Apache License 2.0 | 5 votes |
def add_start_time(data_directory, tm): if not os.path.isdir(data_directory): os.makedirs(data_directory) file_name = os.path.join(data_directory, 'start_times') if not os.path.isfile(file_name): start_times = [] else: with open(file_name, 'r') as f: start_times = json.loads(f.read()) if len(start_times[-1]) != 2: raise RuntimeError('Wrongly formatted start_times file') start_times.append([tm, ]) with open(file_name, 'w+') as f: f.write(json.dumps(start_times))
Example #22
Source File: webserver.py From txTrader with MIT License | 5 votes |
def render(self, request): self.request = request user = self.request.getUser() password = self.request.getPassword() if user == self.root.api.username and password == self.root.api.password: return Resource.render(self, request) else: request.setResponseCode(http.UNAUTHORIZED) return json.dumps({'status': 'Unauthorized'})
Example #23
Source File: recorder.py From indy-plenum with Apache License 2.0 | 5 votes |
def add_to_store(self, key, val): try: existing = self.store.get(key) if isinstance(existing, (bytes, bytearray)): existing = existing.decode() existing = json.loads(existing) except KeyError: existing = [] self.store.put(key, json.dumps([*existing, val]))
Example #24
Source File: bottle.py From silvia-pi with MIT License | 5 votes |
def __init__(self, json_dumps=json_dumps): self.json_dumps = json_dumps
Example #25
Source File: queryprocessor.py From minemeld-core with Apache License 2.0 | 5 votes |
def _core_run(self): LOG.debug("Query %s started", self.uuid) SR = redis.StrictRedis.from_url( self.redis_url ) line_generator = self.store.iterate_backwards( self.uuid, self.starting_timestamp, self.starting_counter ) try: num_generated_lines = 0 while num_generated_lines < self.num_lines: line = next(line_generator, None) if not line: break gevent.sleep(0) if 'log' not in line: SR.publish('mm-traced-q.'+self.uuid, ujson.dumps(line)) continue if self._check_query(line['log']): SR.publish('mm-traced-q.'+self.uuid, ujson.dumps(line)) num_generated_lines += 1 SR.publish( 'mm-traced-q.'+self.uuid, '{"msg": "Loaded %d lines"}' % num_generated_lines ) finally: SR.publish('mm-traced-q.'+self.uuid, '<EOQ>') LOG.info("Query %s finished - %d", self.uuid, num_generated_lines)
Example #26
Source File: logstash.py From minemeld-core with Apache License 2.0 | 5 votes |
def _send_logstash(self, message, source=None, indicator=None, value=None): now = datetime.datetime.now() fields = { '@timestamp': now.isoformat()+'Z', '@version': 1, 'logstash_output_node': self.name, 'message': message } if indicator is not None: fields['@indicator'] = indicator if source is not None: fields['@origin'] = source if value is not None: fields.update(value) if 'last_seen' in fields: last_seen = datetime.datetime.fromtimestamp( float(fields['last_seen'])/1000.0 ) fields['last_seen'] = last_seen.isoformat()+'Z' if 'first_seen' in fields: first_seen = datetime.datetime.fromtimestamp( float(fields['first_seen'])/1000.0 ) fields['first_seen'] = first_seen.isoformat()+'Z' try: self._connect_logstash() self._ls_socket.sendall(ujson.dumps(fields)+'\n') except: self._ls_socket = None raise self.statistics['message.sent'] += 1
Example #27
Source File: redis.py From minemeld-core with Apache License 2.0 | 5 votes |
def _add_indicator(self, score, indicator, value): if self.length() >= self.max_entries: self.statistics['drop.overflow'] += 1 return with self.SR.pipeline() as p: p.multi() p.zadd(self.redis_skey, score, indicator) if self.store_value: p.hset(self.redis_skey_value, indicator, json.dumps(value)) result = p.execute()[0] self.statistics['added'] += result
Example #28
Source File: table.py From minemeld-core with Apache License 2.0 | 5 votes |
def set_custom_metadata(self, metadata=None): if metadata is None: self.db.delete(CUSTOM_METADATA) return cmetadata = ujson.dumps(metadata) self.db.put(CUSTOM_METADATA, cmetadata)
Example #29
Source File: rewarder_client.py From universe with MIT License | 5 votes |
def send(self, method, body, headers=None, expect_reply=False): if headers is None: headers = {} if self._closed: error_message = "Can't send message to closed connection" if self._close_message: error_message += ": {}".format(self._close_message) e = error.Error(error_message) if expect_reply: return defer.fail(e) else: raise e id = self._message_id self._message_id += 1 new_headers = { 'message_id': id, 'sent_at': time.time(), } new_headers.update(headers) payload = { 'method': method, 'body': body, 'headers': new_headers, } extra_logger.info('[%s] Sending message to rewarder: %s', self.factory.label, payload) self.sendMessage(ujson.dumps(payload).encode('utf-8'), False) if expect_reply: d = defer.Deferred() self._requests[id] = (payload, d) return d else: return None
Example #30
Source File: simple_server.py From sanic with MIT License | 5 votes |
def handle(request): return web.Response( body=json.dumps({"test": True}).encode("utf-8"), content_type="application/json", )