def dataReceived(self, data): """ Take data coming from the server and put it into the queue for the client We are also doing a bit of checking to combine auth messages. """ if self.data_buffer: data = self.data_buffer + data self.data_buffer = '' try: data = json.loads(data) except ValueError: # We probably just didn't get all of it self.data_buffer = data return self.outgoing_queue.put(json.dumps(data)) jData = data if "auth" in jData and not self.authPacket: self.authPacket = jData elif "auth" in jData and self.authPacket: self.authPacket.update(jData['auth']) self.hud_queue.put(json.dumps(self.authPacket)) else: self.hud_queue.put(json.dumps(data))
def testEncodeUnicodeBMP(self): s = '\U0001f42e\U0001f42e\U0001F42D\U0001F42D' # 🐮🐮🐭🐭 encoded = ultrajson.dumps(s) encoded_json = json.dumps(s) if len(s) == 4: self.assertEqual(len(encoded), len(s) * 12 + 2) else: self.assertEqual(len(encoded), len(s) * 6 + 2) self.assertEqual(encoded, encoded_json) decoded = ultrajson.loads(encoded) self.assertEqual(s, decoded) # ultrajson outputs an UTF-8 encoded str object if PY3: encoded = ultrajson.dumps(s, ensure_ascii=False) else: encoded = ultrajson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = json.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = ultrajson.loads(encoded) self.assertEqual(s, decoded)
def output_html(data, code=200, headers=None): if type(data) is dict: data = dumps(data) resp = make_response(data, code) resp.headers.extend(headers or {}) resp.headers['Content-Type'] = 'text/html; charset=utf-8' return resp
def testEncodeSymbols(self): s = '\u273f\u2661\u273f' # ✿♡✿ encoded = ultrajson.dumps(s) encoded_json = json.dumps(s) self.assertEqual(len(encoded), len(s) * 6 + 2) # 6 characters + quotes self.assertEqual(encoded, encoded_json) decoded = ultrajson.loads(encoded) self.assertEqual(s, decoded) # ultrajson outputs an UTF-8 encoded str object if PY3: encoded = ultrajson.dumps(s, ensure_ascii=False) else: encoded = ultrajson.dumps(s, ensure_ascii=False).decode("utf-8") # json outputs an unicode object encoded_json = json.dumps(s, ensure_ascii=False) self.assertEqual(len(encoded), len(s) + 2) # original length + quotes self.assertEqual(encoded, encoded_json) decoded = ultrajson.loads(encoded) self.assertEqual(s, decoded)
def test_encodeBlist(self): try: from blist import blist except ImportError: return b = blist(list(range(10))) c = ultrajson.dumps(b) d = ultrajson.loads(c) self.assertEqual(10, len(d)) for x in range(10): self.assertEqual(x, d[x])
def test_encodeNullCharacter(self): input = "31337 \x00 1337" output = ultrajson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ultrajson.decode(output)) input = "\x00" output = ultrajson.encode(input) self.assertEqual(input, json.loads(output)) self.assertEqual(output, json.dumps(input)) self.assertEqual(input, ultrajson.decode(output)) self.assertEqual('" \\u0000\\r\\n "', ultrajson.dumps(" \u0000\r\n "))
def output_json(data, code, headers=None): """Makes a Flask response with a JSON encoded body""" settings = current_app.config.get('RESTFUL_JSON', {}) # If we're in debug mode, and the indent is not set, we set it to a # reasonable value here. Note that this won't override any existing value # that was set. We also set the "sort_keys" value. if current_app.debug: settings.setdefault('indent', 4) settings.setdefault('sort_keys', True) # always end the json dumps with a new line # see https://github.com/mitsuhiko/flask/pull/1262 dumped = dumps(data, **settings) + "\n" resp = make_response(dumped, code) resp.headers.extend(headers or {}) return resp
def create_config_dir(self): config_dirname = os.path.dirname(self.config_path) if not os.path.exists(config_dirname): os.makedirs(os.path.basename(config_dirname)) # Real configuration file: if not os.path.exists(self.config_path): self.config = {} with open(self.config_path, 'w') as file_obj: file_obj.write(ultrajson.dumps(self.config)) else: with open(self.config_path) as file_obj: self.config = ultrajson.loads(file_obj.read()) # Virtual dir: cfg_dir = ConfigDir(self.control_dir, 'config', self.config, self) self.control_dir.add_child(cfg_dir) self.config_dir = cfg_dir return cfg_dir
parser.add_argument("--key", help="https://cloud.google.com/console", required=True) args = parser.parse_args() key = args.key playlist = "PLTcojYWKm_xq4mQ7K0uRo0q_ecLlE9Efk" # Create client with dev key client = youtube.YTClient(key) # Single playlist info playlist_info = client.get_playlist_info(playlist) print "PLAYLIST INFO --> ", json.dumps(playlist_info), "\n\n" # Videos for playlist videos = client.get_videos_from_playlist(playlist) print "PLAYLIST VIDEOS --> ", json.dumps(videos), "\n\n" # For each video for video in videos.get("items", []): # Grab video id vid_id = video["contentDetails"]["videoId"] # Grab video info based on id video_info = client.get_video_info(vid_id) print "VIDEO ID --> ", vid_id, "\n\nINFO --> ", json.dumps( video_info), "\n\n"
def ultrajsonEnc(): x = ultrajson.dumps(encodeData)
def test_WriteFalse(self): self.assertEqual("false", ultrajson.dumps(False))
def test_sortKeys(self): data = {"a": 1, "c": 1, "b": 1, "e": 1, "f": 1, "d": 1} sortedKeys = ultrajson.dumps(data, sort_keys=True) self.assertEqual(sortedKeys, '{"a":1,"b":1,"c":1,"d":1,"e":1,"f":1}')
def test_WriteArrayOfSymbolsFromTuple(self): self.assertEqual("[true,false,null]", ultrajson.dumps((True, False, None)))
def test_WriteArrayOfSymbolsFromList(self): self.assertEqual("[true,false,null]", ultrajson.dumps([True, False, None]))
def test_WriteNull(self): self.assertEqual("null", ultrajson.dumps(None))
from ginf import GinfAPI from ginf.helpers import safeget def get_params(): parser = argparse.ArgumentParser(description='predict location') parser.add_argument("--redis-service", type=str, default='localhost:6379') parser.add_argument("--always-predict", action='store_true') parser.add_argument("--always-dirty", action='store_true') return parser.parse_args() if __name__ == "__main__": args = get_params() ginf_api = GinfAPI(args.redis_service) for i,line in enumerate(sys.stdin): try: source = re.sub('id:twitter.com:', '', safeget(json.loads(line), 'actor.id')) loc, mode = ginf_api.get_user_loc(source, always_predict=args.always_predict, always_dirty=args.always_dirty) if loc: loc.update({"source" : source, "mode" : mode}) print json.dumps(loc) except: print >> sys.stderr, 'error: %s' % line if not i % 1000: print >> sys.stderr, '%d records complete' % i
def testWriteEscapedString(self): self.assertEqual('"\\u003cimg src=\'\\u0026amp;\'\\/\\u003e"', ultrajson.dumps("<img src='&'/>", encode_html_chars=True))
def test_encodeDecodeLongDecimal(self): sut = {'a': -528656961.4399388} encoded = ultrajson.dumps(sut, double_precision=15) ultrajson.decode(encoded)
def persist_config(self): s = ultrajson.dumps(self.config) with open(self.config_path, 'w') as file_obj: file_obj.write(s)
def test_WriteTrue(self): self.assertEqual("true", ultrajson.dumps(True))