Python ujson.loads() Examples

The following are code examples for showing how to use ujson.loads(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 7 votes vote down vote up
def test_integrity_error_handling_with_foreign_key(self, post_method, stream, session):
        stream.feed_data(ujson.dumps([{'m2_id': 1}]).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'params': {'m2_id': 1},
            'database message': {
                'message':  'Cannot add or update a child row: '
                            'a foreign key constraint fails '
                            '(`swaggerit_test`.`model1_swagger`, '
                            'CONSTRAINT `model1_swagger_ibfk_1` FOREIGN '
                            'KEY (`m2_id`) REFERENCES `model2_swagger` '
                            '(`id`))',
                'code': 1452
            }
        } 
Example 2
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 7 votes vote down vote up
def test_model_base_error_handling_with_patch_and_with_nested_remove(self, patch_method, post_method, stream, session):
        stream.feed_data(b'[{}]')
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', body=stream, headers={'content-type': 'application/json'})
        await post_method(request, session)

        stream = asyncio.StreamReader(loop=session.loop)
        body = {'model2_': {'id': 1, '_operation': 'remove'}}
        stream.feed_data(ujson.dumps(body).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', path_params={'id': 1}, body=stream, headers={'content-type': 'application/json'})
        resp = await patch_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': [body],
            'message': "Can't execute nested 'remove' operation"
        } 
Example 3
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 7 votes vote down vote up
def test_model_base_error_handling_with_patch_and_with_nested_update(self, patch_method, post_method, stream, session):
        stream.feed_data(b'[{}]')
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', body=stream, headers={'content-type': 'application/json'})
        await post_method(request, session)

        stream = asyncio.StreamReader(loop=session.loop)
        body = {'model2_': {'id': 1, '_operation': 'update'}}
        stream.feed_data(ujson.dumps(body).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', path_params={'id': 1}, body=stream, headers={'content-type': 'application/json'})
        resp = await patch_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': [body],
            'message': "Can't execute nested 'update' operation"
        } 
Example 4
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 6 votes vote down vote up
def test_model_base_error_handling_with_patch_and_with_nested_delete(self, patch_method, post_method, stream, session):
        stream.feed_data(b'[{}]')
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', body=stream, headers={'content-type': 'application/json'})
        await post_method(request, session)

        stream = asyncio.StreamReader(loop=session.loop)
        body = {'model2_': {'id': 1, '_operation': 'delete'}}
        stream.feed_data(ujson.dumps(body).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/1/', 'patch', path_params={'id': 1}, body=stream, headers={'content-type': 'application/json'})
        resp = await patch_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': [body],
            'message': "Can't execute nested 'delete' operation"
        } 
Example 5
Project: aurora   Author: carnby   File: models.py    MIT License 6 votes vote down vote up
def from_tweets(cls, tweets, metadata=None, **kwargs):
        """
        :param tweets: a iterable of tweets
        :param kwargs: extra attributes to be considered for inclusion. should be json serializable.
        :return:
        """

        tl = cls()

        json_tweets = json.loads(serializers.serialize("json", tweets));
        for key, values in kwargs.items():
            if len(values) != len(json_tweets):
                continue

            for tweet, value in zip(json_tweets, values):
                tweet['fields'][key] = value

        tl.save()

        json_repr = {'metadata': metadata, 'tweets': json_tweets, 'pk': tl.pk, 'created_at': tl.datetime.isoformat()}
        tl.json = json.dumps(json_repr)
        tl.save()
        return tl 
Example 6
Project: aiograph   Author: aiogram   File: api.py    MIT License 6 votes vote down vote up
def upload_from_url(self, url, filename=None, content_type=None, full=True):
        form = aiohttp.FormData(quote_fields=False)

        if filename is None:
            filename = 'file'

        async with self.session.get(url) as response:
            form.add_field(secrets.token_urlsafe(8),
                           response.content,
                           filename=filename,
                           content_type=content_type)

            async with self.session.post(self.format_service_url('/upload'), data=form) as r:
                result = await r.json(loads=self._json_deserialize)

        if isinstance(result, dict) and 'error' in result:
            raise exceptions.NoFilesPassed()

        item = result[0]['src']

        if full:
            return self.format_service_url(item)
        return item 
Example 7
Project: MnemonicReader   Author: HKUST-KnowComp   File: utils.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def load_data(args, filename, skip_no_answer=False):
    """Load examples from preprocessed file.
    One example per line, JSON encoded.
    """
    # Load JSON lines
    with open(filename) as f:
        examples = [json.loads(line) for line in f]

    # Make case insensitive?
    if args.uncased_question or args.uncased_doc:
        for ex in examples:
            if args.uncased_question:
                ex['question'] = [w.lower() for w in ex['question']]
                ex['question_char'] = [w.lower() for w in ex['question_char']]
            if args.uncased_doc:
                ex['document'] = [w.lower() for w in ex['document']]
                ex['document_char'] = [w.lower() for w in ex['document_char']]

    # Skip unparsed (start/end) examples
    if skip_no_answer:
        examples = [ex for ex in examples if len(ex['answers']) > 0]
    return examples 
Example 8
Project: tomodachi   Author: kalaspuff   File: config.py    MIT License 6 votes vote down vote up
def parse_config_files(config_files: List[str]) -> Optional[Dict]:
    if not config_files:
        return None
    if isinstance(config_files, str):
        config_files = [config_files]

    configuration = {}  # type: Dict

    for config_file in config_files:
        with open(config_file) as f:
            data = f.read()
            json_data = ujson.loads(data)

            configuration = merge_dicts(configuration, json_data)

    return configuration 
Example 9
Project: tributary   Author: timkpaine   File: ws.py    Apache License 2.0 6 votes vote down vote up
def AsyncWebSocket(url, json=False, wrap=False):
    async def _listen(url, json, wrap):
        async with websockets.connect(url) as websocket:
            async for x in websocket:
                if isinstance(x, StreamNone):
                    continue
                elif not x or isinstance(x, StreamEnd):
                    break

                if json:
                    x = load_json(x)
                if wrap:
                    x = [x]
                yield x

    return _wrap(_listen, dict(url=url, json=json, wrap=wrap), name='WebSocket') 
Example 10
Project: cyrapidjson   Author: thedrow   File: test_benchmarks.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_double_performance_float_precision(benchmark):
    print("\nArray with 256 doubles:")
    name = 'rapidjson (precise)'
    serialize = rapidjson.dumps
    deserialize = rapidjson.loads

    ser_data, des_data = benchmark(run_client_test,
                                   name, serialize, deserialize,
                                   data=doubles,
                                   iterations=50000,
                                   )
    msg = "%-11s serialize: %0.3f  deserialize: %0.3f  total: %0.3f" % (
        name, ser_data, des_data, ser_data + des_data
    )

    print(msg) 
Example 11
Project: grover   Author: rowanz   File: dedupe_crawl.py    Apache License 2.0 6 votes vote down vote up
def _thread(self, obj_key):
        article_list = []

        with NamedTemporaryFile(mode='w+b', dir='/home/ubuntu/temp2/') as packet_temp:
            s3client.download_fileobj('periodista', obj_key, packet_temp)
            packet_temp.seek(0)

            with open(packet_temp.name, 'r') as fin:
                for l in fin:
                    article = json.loads(l)

                    # Preprocessing could go here
                    _fix_notfound_authors(article)
                    _fix_photos(article)
                    article_list.append(article)
        return article_list 
Example 12
Project: grover   Author: rowanz   File: dedupe_crawl.py    Apache License 2.0 6 votes vote down vote up
def _iterate_through_archivedotorg(bucket_name):
    with NamedTemporaryFile(mode='w+b', dir='/home/ubuntu/temp2/') as packet_temp:
        s3client.download_fileobj(bucket_name, 'archivedotorg.jsonl', packet_temp)
        packet_temp.seek(0)

        with open(packet_temp.name, 'r') as fin:
            for l in fin:
                article = json.loads(l)
                article['split'] = _get_split(article['domain'])
                if article['split'] == 'ignore':
                    article['split'] = 'train'

                # Preprocessing could go here
                _fix_notfound_authors(article)
                _fix_photos(article)
                if _is_definitely_unique(article):
                    yield article 
Example 13
Project: grover   Author: rowanz   File: prepare_lm_data.py    Apache License 2.0 6 votes vote down vote up
def article_iterator(encoder, final_desired_size=1025):
    """ Iterate through the provided filename + tokenize"""
    assert os.path.exists(args.input_fn)
    with open(args.input_fn, 'r') as f:
        for l_no, l in enumerate(f):
            if l_no % args.num_folds == args.fold:
                article = json.loads(l)
                article['input_ids'] = tokenize_for_grover_training(encoder, article, desired_size=final_desired_size,
                                                                    unconditional_prob=.35)
                article['inst_index'] = (l_no // args.num_folds)
                if article['inst_index'] < 100:
                    print('---\nINPUT{}. {}\n---\nTokens: {}\n'.format(article['inst_index'],
                                                                       detokenize(encoder, article['input_ids']),
                                                                       article['input_ids']
                                                                       ), flush=True)
                if len(article['input_ids']) == 0:
                    continue
                yield article 
Example 14
Project: sanic-jsonrpc   Author: mon4ter   File: test_specification.py    MIT License 6 votes vote down vote up
def test_post(test_cli, in_: str, out: str):
    response = await test_cli.post('/post', data=in_)

    if response.headers['content-type'] == 'application/json':
        left = await response.json()

        if not isinstance(left, list):
            left = [left]

        right = loads(out)

        if not isinstance(right, list):
            right = [right]

        assert lists_equal_unordered(left, right)
    else:
        assert await response.text() == out 
Example 15
Project: sanic-jsonrpc   Author: mon4ter   File: test_specification.py    MIT License 6 votes vote down vote up
def test_ws(test_cli, in_: List[str], out: List[str]):
    ws = await test_cli.ws_connect('/ws')

    for data in in_:
        await ws.send_str(data)

    left = []

    while True:
        try:
            left.append(loads(await ws.receive_str(timeout=0.01)))
        except TimeoutError:
            await ws.close()
            break

    right = [loads(s) for s in out]
    assert lists_equal_unordered(left, right) 
Example 16
Project: DoubanMovieTool   Author: frostnotfall   File: data_funcs.py    MIT License 6 votes vote down vote up
def subject_suggest(name):
    with utils.my_opener().get(
            f'https://movie.douban.com/j/subject_suggest?q={parse.quote(name)}') as html_data:
        json_data = ujson.loads(html_data.read().decode('utf-8'))
        suggest_result_list = list()
        for i in json_data:
            suggest_result = dict()
            if i['type'] == 'movie':
                suggest_result['type'] = 'movie'
                suggest_result['title'] = f'''{str(i['title'])}({str(i['year'])})'''
            elif i['type'] == 'celebrity':
                suggest_result['type'] = 'actor'
                suggest_result['title'] = str(i['title'])
            suggest_result['thumb_url'] = i['img']
            suggest_result['description'] = i['sub_title']
            suggest_result['id'] = i['id']
            suggest_result_list.append(suggest_result)
    return suggest_result_list 
Example 17
Project: falcon-api   Author: Opentopic   File: mongoengine.py    MIT License 6 votes vote down vote up
def get_queryset(self, req, resp):
        query_term = self.get_param_or_post(req, self.PARAM_TEXT_QUERY)
        search = self.get_param_or_post(req, self.PARAM_SEARCH)
        if search:
            try:
                req.params['__raw__'] = json.loads(search)
            except ValueError:
                raise HTTPBadRequest('Invalid attribute',
                                     'Value of {} filter attribute is invalid'.format(self.PARAM_SEARCH))
        order = self.get_param_or_post(req, self.PARAM_ORDER)
        queryset = self.objects_class.objects(**req.params)
        if query_term is not None:
            queryset = queryset.search_text(query_term)
        if order:
            queryset = queryset.order_by(order)
        return queryset 
Example 18
Project: falcon-api   Author: Opentopic   File: base.py    MIT License 6 votes vote down vote up
def get_param_totals(self, req):
        """
        Gets the totals and total_count params and normalizes them into a single list.

        :param req: Falcon request
        :type req: falcon.request.Request

        :return: total expressions
        :rtype: list
        """
        totals = self.get_param_or_post(req, self.PARAM_TOTALS, [])
        if totals:
            if isinstance(totals, str):
                totals = json.loads(totals)
            if isinstance(totals, dict):
                totals = [totals]
            else:
                totals = list(map(lambda x: x if isinstance(x, dict) else {x: None}, totals))
        total_count = self.get_param_or_post(req, self.PARAM_TOTAL_COUNT)
        if total_count and not list(filter(lambda x: 'count' in x, totals)):
            totals.append({'count': None})
        return totals 
Example 19
Project: AutoDL   Author: tanguofu   File: request.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json_loads(data):
            # on Python 3.5 json.loads only supports str not bytes
            return json.loads(data.decode()) 
Example 20
Project: AutoDL   Author: tanguofu   File: request.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def load_json(self, loads=json_loads):
        try:
            self.parsed_json = loads(self.body)
        except Exception:
            if not self.body:
                return None
            raise InvalidUsage("Failed when parsing body as json")

        return self.parsed_json 
Example 21
Project: sslyze-api   Author: snehesht   File: tasks.py    MIT License 5 votes vote down vote up
def scan_cli(hostname, port):
    cmd = ['python','-m','sslyze','--regular',hostname+':'+str(port), '--json_out=-']
    proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    return json.loads(out) 
Example 22
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 5 votes vote down vote up
def test_integrity_error_handling_with_duplicated_key(self, post_method, stream, session):
        stream.feed_data(ujson.dumps([{'id': 1}, {'id': 1}]).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'params': [{'id': 1, 'm2_id': None}, {'id': 1, 'm2_id': None}],
            'database message': {
                'message': "Duplicate entry '1' for key 'PRIMARY'",
                'code': 1062
            }
        } 
Example 23
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 5 votes vote down vote up
def test_json_validation_error_handling(self, post_method, stream, session):
        stream.feed_data(b'"test"')
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': 'test',
            'message': "'test' is not of type 'array'. Failed validating instance for schema['type']",
            'schema': {
                'type': 'array'
            }
        } 
Example 24
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 5 votes vote down vote up
def test_json_error_handling(self, post_method, stream, session):
        stream.feed_data(b'test')
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': 'test',
            'message': "Unexpected character found when decoding 'true'"
        } 
Example 25
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 5 votes vote down vote up
def test_model_base_error_handling_with_post_and_with_nested_delete(self, post_method, stream, session):
        body = [{'model2_': {'id': 1, '_operation': 'delete'}}]
        stream.feed_data(ujson.dumps(body).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': body,
            'message': "Can't execute nested 'delete' operation"
        } 
Example 26
Project: swaggerit   Author: dutradda   File: test_method_errors.py    MIT License 5 votes vote down vote up
def test_model_base_error_handling_with_post_and_with_nested_update(self, post_method, stream, session):
        body = [{'model2_': {'id': 1, '_operation': 'update'}}]
        stream.feed_data(ujson.dumps(body).encode())
        stream.feed_eof()
        request = SwaggerRequest('/model1/', 'post', body=stream, headers={'content-type': 'application/json'})
        resp = await post_method(request, session)

        assert resp.status_code == 400
        assert ujson.loads(resp.body) == {
            'instance': body,
            'message': "Can't execute nested 'update' operation"
        } 
Example 27
Project: swaggerit   Author: dutradda   File: method.py    MIT License 5 votes vote down vote up
def _cast_body(self, body):
        body = (await body.read()).decode()
        try:
            return (ujson.loads(body), None)
        except Exception as error:
            return body, error 
Example 28
Project: swaggerit   Author: dutradda   File: _swaggerit_meta.py    MIT License 5 votes vote down vote up
def _format_definitions_names(obj, model_name):
    definitions = obj.__swagger_json__.get('definitions', {})
    definitions_keys = list(definitions.keys())
    for def_name in definitions_keys:
        definitions['{}.{}'.format(model_name, def_name)] = definitions.pop(def_name)

    schema = ujson.dumps(obj.__swagger_json__, escape_forward_slashes=False)
    schema = re.sub(r'("\$ref":"#/definitions/)([^/"]+)', r'\1{}.\2'.format(model_name), schema)
    obj.__swagger_json__ = ujson.loads(schema) 
Example 29
Project: swaggerit   Author: dutradda   File: binds.py    MIT License 5 votes vote down vote up
def _bulk_dict(self, doc_type, dict_, type_):
        body = []
        for key, value in dict_.items():
            key = key.decode() if isinstance(key, bytes) else key
            body.append({type_: {'_id': key}})
            value = ujson.loads(value)

            if type_ == 'update':
                body.append({'doc': value})
            elif type_== 'create':
                body.append(value)

        return await self._client.bulk(body, index=self._index, doc_type=doc_type) 
Example 30
Project: swaggerit   Author: dutradda   File: sqlalchemy_redis.py    MIT License 5 votes vote down vote up
def _get_many(cls, session, ids, todict, kwargs):
        if not todict or session.redis_bind is None:
            filters = cls.build_filters_by_ids(ids)
            insts = cls._build_query(session, kwargs).filter(filters).all()

            if todict:
                return [inst.todict() for inst in insts]
            else:
                return insts

        model_redis_key = type(cls).get_key(cls, '_'.join(kwargs.keys()))
        ids_redis_keys = [cls.get_instance_key(id_, id_.keys()) for id_ in ids]
        objs = await session.redis_bind.hmget(model_redis_key, *ids_redis_keys)
        ids_not_cached = [id_ for i, (id_, obj) in enumerate(zip(ids, objs)) if obj is None]
        objs = [ujson.loads(obj) for obj in objs if obj is not None]

        if ids_not_cached:
            await session.redis_bind.sadd(cls.get_filters_names_key(), model_redis_key)
            filters = cls.build_filters_by_ids(ids_not_cached)
            instances = cls._build_query(session).filter(filters).all()
            if instances:
                items_to_set = {
                    cls.get_instance_key(inst): ujson.dumps(inst.todict()) for inst in instances}
                await session.redis_bind.hmset_dict(model_redis_key, items_to_set)

                for inst in instances:
                    inst_ids = cls.get_instance_ids_map(inst, ids[0].keys())
                    index = ids_not_cached.index(inst_ids)
                    objs.insert(index, inst.todict())

        return objs 
Example 31
Project: swaggerit   Author: dutradda   File: json_builder.py    MIT License 5 votes vote down vote up
def _build_boolean(cls, value):
        value = ujson.loads(value)
        if not isinstance(value, bool):
            raise ValueError(value)
        return value 
Example 32
Project: aurora   Author: carnby   File: views.py    MIT License 5 votes vote down vote up
def convert_to_dicts(queryset, single=False):
    if not single:
        return json.loads(serializers.serialize("json", queryset))
    else:
        result = json.loads(serializers.serialize("json", queryset))
        if result:
            return result[0]
        return None


# TODO: this should be a decorator? 
Example 33
Project: aurora   Author: carnby   File: importer.py    MIT License 5 votes vote down vote up
def __call__(self, filename):
        """
        filter_func is applied to single tweet jsons
        post_func is applied to the whole list of imported pairs (user, tweet)
        """

        pending = []
        self.total_tweet_count = 0

        with gzip.open(filename, 'rt') as f:
            logging.info('Filename: {0} (step={1})'.format(filename, self.step))

            for l in f:
                l = l.strip()

                if not l:
                    continue

                try:
                    json_data = json.loads(l)
                except ValueError as e:
                    logging.debug('Value Error: {0}'.format(e))
                    continue

                pending.append(json_data)

                if len(pending) >= self.step:
                    self.batch_import(pending)
                    pending = []

        if pending:
            self.batch_import(pending)

        logging.info('Filename {0} processed! {1} tweets accepted.'.format(filename, self.total_tweet_count)) 
Example 34
Project: micropython-barebones   Author: reapzor   File: __init__.py    MIT License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 35
Project: micropython-barebones   Author: reapzor   File: uurequests.py    MIT License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 36
Project: micropython-barebones   Author: reapzor   File: __init__.py    MIT License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 37
Project: micropython-barebones   Author: reapzor   File: uurequests.py    MIT License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 38
Project: trelliolibs   Author: quikmile   File: helpers.py    MIT License 5 votes vote down vote up
def json_serializer(data, fields=()):
    for field in fields:
        if data.get(field):
            data[field] = json.loads(data[field])
    return data 
Example 39
Project: trelliolibs   Author: quikmile   File: middlewares.py    MIT License 5 votes vote down vote up
def pre_request(self, service, request, *args, **kwargs):
        user_id = request.headers.get('x-user-id', None)
        user_subs = request.headers.get('x-user-subs', '')
        subscriptions = request.headers.get('x-subscriptions', '')

        if not user_id and not user_subs:
            rbac_client = RBACTCPClient()
            response = await wait_for(rbac_client.verify_token(request.headers.get('authorization')))
            user_id = response.get('user_id')
            user_subs = response.get('user_subs')

        request.__setitem__('subscriptions', json.loads(subscriptions))
        request.__setitem__('user_id', user_id)
        request.__setitem__('user_subs', json.loads(user_subs)) 
Example 40
Project: Gnip-Tweet-Evaluation   Author: twitterdev   File: analysis.py    MIT License 5 votes vote down vote up
def deserialize_tweets(line_generator):
    """ 
    Generator function to manage JSON deserialization
    """
    for line in line_generator:
        try:
            yield json.loads(line)
        except ValueError:
            continue 
Example 41
Project: sanic   Author: huge-success   File: test_request_data.py    MIT License 5 votes vote down vote up
def test_custom_context_old(app):
    @app.middleware("request")
    def store(request):
        try:
            request["foo"]
        except KeyError:
            pass
        request["user"] = "sanic"
        sidekick = request.get("sidekick", "tails")  # Item missing -> default
        request["sidekick"] = sidekick
        request["bar"] = request["sidekick"]
        del request["sidekick"]

    @app.route("/")
    def handler(request):
        return json(
            {
                "user": request.get("user"),
                "sidekick": request.get("sidekick"),
                "has_bar": "bar" in request,
                "has_sidekick": "sidekick" in request,
            }
        )

    request, response = app.test_client.get("/")

    assert response.json == {
        "user": "sanic",
        "sidekick": None,
        "has_bar": True,
        "has_sidekick": False,
    }
    response_json = loads(response.text)
    assert response_json["user"] == "sanic"
    assert response_json.get("sidekick") is None 
Example 42
Project: sanic   Author: huge-success   File: test_request_data.py    MIT License 5 votes vote down vote up
def test_app_injection(app):
    expected = random.choice(range(0, 100))

    @app.listener("after_server_start")
    async def inject_data(app, loop):
        app.injected = expected

    @app.get("/")
    async def handler(request):
        return json({"injected": request.app.injected})

    request, response = app.test_client.get("/")

    response_json = loads(response.text)
    assert response_json["injected"] == expected 
Example 43
Project: gnocchi   Author: gnocchixyz   File: amqp1d.py    Apache License 2.0 5 votes vote down vote up
def on_message(self, event):
        json_message = ujson.loads(event.message.body)
        timestamp = utils.dt_in_unix_ns(utils.utcnow())
        measures_by_host_and_name = sorted((
            (message["host"],
             self._serialize_identifier(index, message),
             value)
            for message in json_message
            for index, value in enumerate(message["values"])
        ))
        for (host, name), values in itertools.groupby(
                measures_by_host_and_name, key=lambda x: x[0:2]):
            measures = (incoming.Measure(timestamp, v[2]) for v in values)
            self.processor.add_measures(host, name, measures) 
Example 44
Project: AppServer   Author: skytoup   File: tests.py    MIT License 5 votes vote down vote up
def response_normal_check(response):
    assert response.status == 200
    assert ujson.loads(response.text).get('ok') is True 
Example 45
Project: AppServer   Author: skytoup   File: tests.py    MIT License 5 votes vote down vote up
def get_apps(app_type, page):
    _, response = app.test_client.get('/apps/{}/page/{}?t={}'.format(app_type, page, time.time()))
    response_normal_check(response)
    json = ujson.loads(response.text)
    assert json.get('datas') is not None
    return response 
Example 46
Project: AppServer   Author: skytoup   File: test_app.py    MIT License 5 votes vote down vote up
def test_app():
    # 上传
    upload()

    # 获取上传的app
    response = get_apps('all', 1)
    app_json = ujson.loads(response.text)
    apps = app_json.get('datas')
    assert len(apps) != 0
    app = apps[0]
    app_id = app.get('id')
    assert app_id is not None

    # 修改app信息
    name = 'name_1'
    short_chain = 'xxxxxok'
    detail = 'hahaha'
    put_app_detail(app_id, 'name_1', 'xxxxxok', 'hahaha')
    app_detail = get_app_detail(app_id)
    app_detail_json = ujson.loads(app_detail.text)

    # 获取app详情
    app_detail_data = app_detail_json.get('datas')
    assert app_detail_data.get('name') == name
    assert app_detail_data.get('short_chain') == short_chain
    assert app_detail_data.get('detail') == detail

    # 获取app版本
    response = get_app_versions(app_id, 1)
    app_versions_json = ujson.loads(response.text)
    app_versions_data = app_versions_json.get('datas')
    assert len(app_versions_data) != 0
    app_version = app_versions_data[0]
    version_id = app_version.get('id')

    # 删除app版本
    del_app_version(app_id, version_id)

    # 删除app
    del_app(app_id) 
Example 47
Project: aiograph   Author: aiogram   File: api.py    MIT License 5 votes vote down vote up
def upload(self, *files, full=True) -> List[str]:
        to_be_closed = []
        form = aiohttp.FormData(quote_fields=False)
        try:
            for file in files:
                if isinstance(file, tuple):
                    if len(file) == 2:
                        filename, fileobj = file
                        content_type = None
                    elif len(file) == 3:
                        filename, fileobj, content_type = file
                    else:
                        raise ValueError('Tuple must have exactly 2 or 3 elements: filename, fileobj, content_type')
                elif isinstance(file, (str, Path)):
                    fileobj = open(file, 'rb')
                    to_be_closed.append(fileobj)
                    filename = os.path.basename(file)
                    content_type = None
                else:
                    fileobj = file
                    filename = _guess_filename(file)
                    content_type = None

                form.add_field(secrets.token_urlsafe(8), fileobj, filename=filename, content_type=content_type)

            async with self.session.post(self.format_service_url('/upload'), data=form) as response:
                result = await response.json(loads=self._json_deserialize)
        finally:
            for item in to_be_closed:
                item.close()

        if isinstance(result, dict) and 'error' in result:
            raise exceptions.NoFilesPassed()

        if full:
            return [self.format_service_url(item['src']) for item in result if 'src' in item]
        return [item['src'] for item in result if 'src' in item] 
Example 48
Project: aiograph   Author: aiogram   File: api.py    MIT License 5 votes vote down vote up
def request(self, method: str, *, path: Optional[str] = None, payload: Optional[dict] = None):
        url = self.format_api_url(method, path)
        async with self.session.post(url, data=payload) as response:
            json_data = await response.json(loads=self._json_deserialize)

            if not json_data.get('ok') and 'error' in json_data:
                error_text = json_data['error']
                raise exceptions.TelegraphError.detect(error_text)
        return json_data['result'] 
Example 49
Project: micropython-sonoff-switch   Author: kfricke   File: sonoff_switch.py    MIT License 5 votes vote down vote up
def handle_subscription(topic, payload):
    global demanded_relay_state
    try:
        payload = ujson.loads(payload.decode('utf-8'))
    except ValueError:
        payload = {}
    if 'state' in payload.keys():
        demanded_relay_state = not payload['state'] 
Example 50
Project: ripe-atlas-tools   Author: RIPE-NCC   File: report.py    GNU General Public License v3.0 5 votes vote down vote up
def _get_results_from_file(self, using_regular_file):
        """
        We need to get the first result from the source in order to detect the
        type.  Additionally, if the source is actually one great big JSON list,
        then we need to parse it so we iterate over the results since there's no
        newline characters.
        """

        self.file = sys.stdin
        if using_regular_file:
            self.file = open(self.arguments.from_file)

        # Pop the first line off the source stack.  This may very well be a Very
        # Large String and cause a memory explosion, but we like to let our
        # users shoot themselves in the foot.
        sample = next(self.file)

        # Re-attach the line back onto the iterable so we don't lose anything
        results = itertools.chain([sample], self.file)

        # In the case of the Very Large String, we parse out the JSON here
        if sample.startswith("["):
            results = json.loads("".join(results))
            sample = results[0]  # Reassign sample to an actual result

        return results, sample 
Example 51
Project: tomodachi   Author: kalaspuff   File: json_base.py    MIT License 5 votes vote down vote up
def parse_message(cls, payload: str, **kwargs: Any) -> Union[Dict, Tuple]:
        message = ujson.loads(payload)

        protocol_version = message.get('metadata', {}).get('protocol_version')
        message_uuid = message.get('metadata', {}).get('message_uuid')
        timestamp = message.get('metadata', {}).get('timestamp')

        if message.get('metadata', {}).get('data_encoding') == 'raw':
            data = message.get('data')
        elif message.get('metadata', {}).get('data_encoding') == 'base64_gzip_json':
            data = ujson.loads(zlib.decompress(base64.b64decode(message.get('data').encode('utf-8'))).decode('utf-8'))

        return {
            'service': {
                'name': message.get('service', {}).get('name'),
                'uuid': message.get('service', {}).get('uuid')
            },
            'metadata': {
                'message_uuid': message.get('metadata', {}).get('message_uuid'),
                'protocol_version': message.get('metadata', {}).get('protocol_version'),
                'timestamp': message.get('metadata', {}).get('timestamp'),
                'topic': message.get('metadata', {}).get('topic'),
                'data_encoding': message.get('metadata', {}).get('data_encoding')
            },
            'data': data
        }, message_uuid, timestamp 
Example 52
Project: tomodachi   Author: kalaspuff   File: aws_sns_sqs_service_with_credentials_with_custom_protocol.py    MIT License 5 votes vote down vote up
def parse_message(cls, payload: str) -> Union[Dict, Tuple]:
        message = ujson.loads(payload)
        return message, None, None 
Example 53
Project: tomodachi   Author: kalaspuff   File: amqp_service_with_credentials_with_custom_protocol.py    MIT License 5 votes vote down vote up
def parse_message(cls, payload: str) -> Union[Dict, Tuple]:
        message = ujson.loads(payload)
        return message, None, None 
Example 54
Project: decaNLP   Author: salesforce   File: convert_to_logical_forms.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write_logical_forms(greedy, args):
    data_dir = os.path.join(args.data, 'wikisql', 'data')
    path = os.path.join(data_dir, 'dev.jsonl') if 'valid' in args.evaluate else os.path.join(data_dir, 'test.jsonl')
    table_path = os.path.join(data_dir, 'dev.tables.jsonl') if 'valid' in args.evaluate else os.path.join(data_dir, 'test.tables.jsonl')
    with open(table_path) as tables_file:
        tables = [json.loads(line) for line in tables_file]
        id_to_tables = {x['id']: x for x in tables}

    examples = []
    with open(path) as example_file:
        for line in example_file:
            entry = json.loads(line)
            table = id_to_tables[entry['table_id']]
            sql = entry['sql']
            header = table['header']
            a = repr(Query.from_dict(entry['sql'], table['header']))
            ex = {'sql': sql, 'header': header, 'answer': a, 'table': table}
            examples.append(ex)

    with open(args.output, 'a') as f:
        count = 0
        correct = 0
        text_answers = []
        for idx, (g, ex) in enumerate(zip(greedy, examples)):
            count += 1
            text_answers.append([ex['answer'].lower()])
            try:
                lf = to_lf(g, ex['table'])
                f.write(json.dumps(correct_format(lf)) + '\n')
                gt = ex['sql']
                conds = gt['conds']
                lower_conds = []
                for c in conds:
                    lc = c
                    lc[2] = str(lc[2]).lower()
                    lower_conds.append(lc)
                gt['conds'] = lower_conds
                correct += lf == gt
            except Exception as e:
                f.write(json.dumps(correct_format({})) + '\n') 
Example 55
Project: adaptivemd   Author: markovmodel   File: dictify.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def callable_from_dict(c_dict):
        """
        Turn a dictionary back in a callable function or class

        Used for conversion from JSON

        Parameters
        ----------
        c_dict : dict
            the dictionary that contains the information

        Returns
        -------
        callable
            the reconstructed callable function or class

        """
        c = None

        if c_dict is not None:
            if '_marshal' in c_dict:
                if ObjectJSON.allow_marshal:
                    code = marshal.loads(base64.b64decode(c_dict['_marshal']))
                    c = types.FunctionType(code, globals(), code.co_name)

            elif '_module' in c_dict:
                module = c_dict['_module']
                packages = module.split('.')
                if packages[0] in ObjectJSON.safe_modules:
                    imp = importlib.import_module(module)
                    c = getattr(imp, c_dict['_name'])

        return c 
Example 56
Project: adaptivemd   Author: markovmodel   File: dictify.py    GNU Lesser General Public License v2.1 5 votes vote down vote up
def from_json(self, json_string):
        simplified = ujson.loads(json_string)
        return self.build(simplified)

    # def unit_to_json(self, unit):
    #     simple = self.unit_to_dict(unit)
    #     return self.to_json(simple)
    #
    # def unit_from_json(self, json_string):
    #     return self.unit_from_dict(self.from_json(json_string)) 
Example 57
Project: early-fakenews-detection   Author: yumere   File: seq_dataset.py    MIT License 5 votes vote down vote up
def dataset_test(file_path):
    from torch.utils.data import DataLoader
    import ujson as json
    with open(file_path, "rt", encoding="utf-8") as f:
        data = [json.loads(d) for d in f]

    dataset = SeqDataset(data)
    loader = DataLoader(dataset, batch_size=16, shuffle=True, drop_last=True, num_workers=0, collate_fn=SeqDataset.collate_fn)

    for i, j in enumerate(loader):
        if i == 100:
            break

        print(j) 
Example 58
Project: tributary   Author: timkpaine   File: input.py    Apache License 2.0 5 votes vote down vote up
def ws(url, callback, json=False, wrap=False):
    ws = create_connection(url)
    for x in run(ws.recv):
        if isinstance(x, StreamNone):
            continue
        elif not x or isinstance(x, StreamEnd):
            break

        if json:
            x = load_json(x)
        if wrap:
            x = [x]
        callback(x) 
Example 59
Project: tributary   Author: timkpaine   File: input.py    Apache License 2.0 5 votes vote down vote up
def kafka(callback, servers, group, topics, json=False, wrap=False, interval=1):
    c = Consumer({
        'bootstrap.servers': servers,
        'group.id': group,
        'default.topic.config': {
            'auto.offset.reset': 'smallest'
        }
    })

    if not isinstance(topics, list):
        topics = [topics]
    c.subscribe(topics)

    def _listen(consumer, json, wrap, interval):
        while True:
            msg = consumer.poll(interval)

            if msg is None:
                continue
            if msg.error():
                if msg.error().code() == KafkaError._PARTITION_EOF:
                    continue
                else:
                    break

            msg = msg.value().decode('utf-8')

            if not msg:
                break
            if json:
                msg = load_json(msg)
            if wrap:
                msg = [msg]
            callback(msg) 
Example 60
Project: tributary   Author: timkpaine   File: kafka.py    Apache License 2.0 5 votes vote down vote up
def AsyncKafka(servers, group, topics, json=False, wrap=False, interval=1):
    c = Consumer({
        'bootstrap.servers': servers,
        'group.id': group,
        'default.topic.config': {
            'auto.offset.reset': 'smallest'
        }
    })

    if not isinstance(topics, list):
        topics = [topics]
    c.subscribe(topics)

    async def _listen(consumer, json, wrap, interval):
        while True:
            msg = consumer.poll(interval)

            if msg is None:
                continue
            if msg.error():
                if msg.error().code() == KafkaError._PARTITION_EOF:
                    continue
                else:
                    print(msg.error())
                    break

            msg = msg.value().decode('utf-8')

            if not msg:
                break
            if json:
                msg = ujson.loads(msg)
            if wrap:
                msg = [msg]
            yield msg

    return _wrap(_listen, dict(consumer=c, json=json, wrap=wrap, interval=interval), name='Kafka') 
Example 61
Project: deoplete-fsharp   Author: callmekohei   File: deoplete-fsharp.py    MIT License 5 votes vote down vote up
def gather_candidates(self, context):

        try:

            if self.standby == False:
                return [ '=== can not initialize deopletefs ===' ]
            else:
                post_data = {
                      "Row"      : context['position'][1]
                    , "Col"      : context['complete_position'] - 1
                    , "Line"     : context['input']
                    , "FilePath" : self.filePath
                    , "Source"   : '\n'.join( getlines( self.vim ) )
                    , "Init"     : 'false'
                }

                self.util.send(json.dumps(post_data))
                s = (self.util.read())[0]
                s = base64.b64decode(s)
                s = s.decode(encoding='utf-8')
                lst = s.split("\n")

                return [
                    {
                          "word": json_data['word']
                        , "info": '\n'.join( functools.reduce ( lambda a , b : a + b , json_data['info'] ) )
                    }
                    for json_data in [ json.loads(s) for s in lst ]
                ]

        except Exception as e:
            return [ str(e) ] 
Example 62
Project: iris-relay   Author: linkedin   File: app.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def process_api_response(content):
    try:
        j = ujson.loads(content)
        if 'app_response' in j:
            return j['app_response']
    except ValueError:
        logger.exception('Failed parsing json from api')

    return 'Invalid response from API server: ' + content 
Example 63
Project: iris-relay   Author: linkedin   File: app.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def on_post(self, req, resp):
        data = ujson.loads(req.context['body'].decode('utf-8'))
        data['username'] = req.context['user']
        result = self.iris.post('devices', data)
        if result.status == 400:
            raise falcon.HTTPBadRequest('Bad Request', '')
        elif result.status != 201:
            logger.error('Unknown response from API: %s: %s', result.status, result.data)
            raise falcon.HTTPInternalServerError('Internal Server Error', 'Unknown response from the api')
        resp.status = falcon.HTTP_201 
Example 64
Project: sanic-jsonrpc   Author: mon4ter   File: jsonrpc.py    MIT License 5 votes vote down vote up
def _parse_json(json: AnyStr) -> Union[Dict, List[Dict], Response]:
        try:
            return loads(json)
        except (TypeError, ValueError):
            return _response(error=PARSE_ERROR) 
Example 65
Project: blinker-mpy   Author: blinker-iot   File: Blinker.py    MIT License 5 votes vote down vote up
def print(self, key, value = None, uint = None):

        if value is None:
            if bProto.isFormat:
                return
            data = str(key)
            BlinkerMpy._print(self, data)
        else:
            key = str(key)
            # if not uint is None:
            #     value = str(value) + str(uint)
            # data = json_encode(key, value)
            # data = {}
            if bProto.isFormat == False:
                bProto.isFormat = True
                bProto.autoFormatFreshTime = millis()
            
            if (millis() - bProto.autoFormatFreshTime) < 100 :
                bProto.autoFormatFreshTime = millis()

            buffer = {}

            if bProto.sendBuf is not '' :
                buffer = ujson.loads(bProto.sendBuf)
            buffer[key] = value
            bProto.sendBuf = ujson.dumps(buffer)
            # # bProto.sendBuf[key] = value

            # # BLINKER_LOG_ALL("key: ", key, ", value: ", bProto.sendBuf[key])
            BLINKER_LOG_ALL("sendBuf: ", bProto.sendBuf) 
Example 66
Project: blinker-mpy   Author: blinker-iot   File: Blinker.py    MIT License 5 votes vote down vote up
def checkAutoFormat(self):
        if bProto.isFormat :
            if (millis() - bProto.autoFormatFreshTime) >= 100 :
                # payload = {}
                # for key in bProto.sendBuf :
                #     BLINKER_LOG_ALL(key, ", ", bProto.sendBuf[key])
                BLINKER_LOG_ALL("auto format: ", ujson.loads(bProto.sendBuf))
                BlinkerMpy._print(self, ujson.loads(bProto.sendBuf))
                bProto.sendBuf = ''
                bProto.isFormat = False 
Example 67
Project: blinker-mpy   Author: blinker-iot   File: urequests.py    MIT License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 68
Project: blinker-mpy   Author: blinker-iot   File: BlinkerWiFi.py    MIT License 5 votes vote down vote up
def on_message(topic, msg):
    BLINKER_LOG_ALL('payload: ', msg)
    data = ujson.loads(msg) 
Example 69
Project: DoubanMovieTool   Author: frostnotfall   File: data_funcs.py    MIT License 5 votes vote down vote up
def movie_search(movie_name):
    with utils.my_opener().get(
            f'https://api.douban.com/v2/movie/search?tag={parse.quote(movie_name)}') as html_data:
        json_data = ujson.loads(html_data.read().decode('utf-8'))

    subjects = json_data['subjects']
    movie_list = list()
    movie_id_list = list()
    range_len_subjects = range(len(subjects))

    for i in range_len_subjects:
        movie_list.append(subjects[i]['title'])
        movie_id_list.append(f'''movie {subjects[i]['id']}''')

    return movie_list, movie_id_list 
Example 70
Project: OpenUniverse   Author: yuwui   File: session_manager.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def get_session(address: Address) -> Dict[str, Any]:
        return ujson.loads(SessionManager.db.get(address[0] + ':' + str(address[1]))) 
Example 71
Project: falcon-api   Author: Opentopic   File: elasticsearch.py    MIT License 5 votes vote down vote up
def get_queryset(self, req, resp):
        query = self.get_base_query(req, resp)
        conditions = {}
        if 'doc' in req.context:
            conditions = dict(req.context['doc'])
            # ignore any special params except SEARCH and ORDER
            for param in self.get_special_params():
                conditions.pop(param, None)
        conditions.update(req.params)
        if self.PARAM_SEARCH in conditions:
            search = conditions.pop(self.PARAM_SEARCH)
            try:
                conditions.update(json.loads(search) if isinstance(search, str) else search)
            except ValueError:
                raise HTTPBadRequest('Invalid attribute',
                                     'Value of {} filter attribute is invalid'.format(self.PARAM_SEARCH))

        order = conditions.pop(self.PARAM_ORDER, None)
        if not order:
            return self.filter_by(query, conditions)

        if isinstance(order, str):
            if (order[0] == '{' and order[-1] == '}') or (order[0] == '[' and order[-1] == ']'):
                try:
                    order = json.loads(order)
                except ValueError:
                    # not valid json, ignore and try to parse as an ordinary list of attributes
                    pass
        if not isinstance(order, list) and not isinstance(order, dict):
            order = [order]
        order_expressions = self._build_order_expressions(order)
        if order_expressions:
            query = query.sort(*order_expressions)
        return self.filter_by(query, conditions, order_criteria=order_expressions) 
Example 72
Project: falcon-api   Author: Opentopic   File: json_middleware.py    MIT License 5 votes vote down vote up
def process_request(self, req, resp):
        """
        Converts request input data from JSON to a dict.
        :param req: Falcon request
        :type req: falcon.request.Request

        :param resp: Falcon response
        :type resp: falcon.response.Response
        """
        # req.stream corresponds to the WSGI wsgi.input environ variable,
        # and allows you to read bytes from the request body.
        #
        # See also: PEP 3333
        if req.content_length in (None, 0):
            # Nothing to do
            return

        body = req.stream.read()
        if not body:
            raise falcon.HTTPBadRequest('Empty request body',
                                        'A valid JSON document is required.')

        try:
            req.context['doc'] = json.loads(body.decode('utf-8'))

        except (ValueError, UnicodeDecodeError):
            raise falcon.HTTPError(falcon.HTTP_753,
                                   'Malformed JSON',
                                   'Could not decode the request body. The '
                                   'JSON was incorrect or not encoded as '
                                   'UTF-8.') 
Example 73
Project: CRUMBS   Author: wildtreetech   File: urequests.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def json(self):
        import ujson
        return ujson.loads(self.content) 
Example 74
Project: tomatolib   Author: im-tangmi   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def patch_json():
    json.__name__ = 'ujson'
    json.dumps = ujson.dumps
    json.loads = ujson.loads 
Example 75
Project: diplomacy   Author: diplomacy   File: api.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def list_games_with_players_in_cd(self):
        """ Lists the game on the standard map where a player is in CD (civil disorder)
            and the bots needs to submit orders

            :return: List of :class:`.GameIdCountryId` tuples  [(game_id, country_id), (game_id, country_id)]
        """
        route = 'players/cd'
        url = '%s?%s' % (API_WEBDIPLOMACY_NET, urlencode({'route': route}))
        return_val = []

        # Sending request
        try:
            response = yield self._send_get_request(url)
        except HTTP_ERRORS as err:
            LOGGER.error('Unable to connect to server. Error raised is: "%s"', repr(err))
            return return_val

        # 200 - Response OK
        if response.code == 200 and response.body:
            try:
                list_games_players = json.loads(response.body.decode('utf-8'))
            except (TypeError, ValueError):
                LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8'))
                return return_val
            for game_player in list_games_players:
                return_val += [GameIdCountryId(game_id=game_player['gameID'], country_id=game_player['countryID'])]

        # Error Occurred
        else:
            LOGGER.warning('ERROR during "%s". Error code: %d. Body: %s.', route, response.code, response.body)

        # Returning
        return return_val 
Example 76
Project: diplomacy   Author: diplomacy   File: api.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def list_games_with_missing_orders(self):
        """ Lists of the game on the standard where the user has not submitted orders yet.

            :return: List of :class:`.GameIdCountryId` tuples  [(game_id, country_id), (game_id, country_id)]
        """
        route = 'players/missing_orders'
        url = '%s?%s' % (API_WEBDIPLOMACY_NET, urlencode({'route': route}))
        return_val = []

        # Sending request
        try:
            response = yield self._send_get_request(url)
        except HTTP_ERRORS as err:
            LOGGER.error('Unable to connect to server. Error raised is: "%s"', repr(err))
            return return_val

        # 200 - Response OK
        if response.code == 200 and response.body:
            try:
                list_games_players = json.loads(response.body.decode('utf-8'))
            except (TypeError, ValueError):
                LOGGER.warning('ERROR during "%s". Unable to load JSON: %s.', route, response.body.decode('utf-8'))
                return return_val
            for game_player in list_games_players:
                return_val += [GameIdCountryId(game_id=game_player['gameID'], country_id=game_player['countryID'])]

        # Error Occurred
        else:
            LOGGER.warning('ERROR during "%s". Error code: %d. Body: %s.', route, response.code, response.body)

        # Returning
        return return_val 
Example 77
Project: aurora   Author: carnby   File: views.py    MIT License 4 votes vote down vote up
def record_event(request):
    check_referer(request)

    ua = agentparse(request.META.get('HTTP_USER_AGENT', ''))
    print(ua)

    if ua.is_bot:
        raise PermissionDenied()

    session = request.session

    if 'experimental_group' not in session:
        raise PermissionDenied()

    if not 'source_app' in request.POST:
        print('no source app in POST', request.POST)
        raise Http404

    source_app = request.POST.get('source_app', None)
    if not source_app or not source_app in ('aurora', 'portraits'):
        print('invalid app', source_app)
        raise Http404

    if not 'user_events' in request.POST:
        print('no user events in POST', request.POST)
        raise Http404

    print('POST', request.POST)

    user_events_str = str(request.POST['user_events'])

    try:
        user_events = json.loads(user_events_str)
    except ValueError:
        raise Http404

    print(session.session_key)
    print(session['experimental_group'])

    for event in user_events:
        print(source_app, 'event', event)
        try:
            interaction = InteractionEvent(session=session.session_key,
                             experimental_group=session['experimental_group'][source_app],
                             source_app=source_app,
                             key=event['name'],
                             data=json.dumps(event),
                             reported_datetime=event['client_datetime'])
            interaction.add_request_meta(request, ua=ua, mobile=(ua.is_mobile or ua.is_tablet or bool(request.GET.get('mobile_version', False))))
            interaction.save()
        except KeyError:
            continue

    return render_json(request, {'json': True}) 
Example 78
Project: iris-relay   Author: linkedin   File: app.py    BSD 2-Clause "Simplified" License 4 votes vote down vote up
def on_post(self, req, resp):
        """
        Accept slack's message from interactive buttons
        """
        try:
            req.context['body'] = req.context['body'].decode('utf-8')
            form_post = falcon.uri.parse_query_string(req.context['body'])
            payload = ujson.loads(form_post['payload'])
            if not self.valid_token(payload['token']):
                logger.error('Invalid token sent in the request.')
                raise falcon.HTTPUnauthorized('Access denied',
                                              'Not a valid auth token')
            try:
                msg_id = int(payload['callback_id'])
            except KeyError as e:
                logger.error(e)
                logger.error('callback_id not found in the json payload.')
                raise falcon.HTTPBadRequest('Bad Request', 'Callback id not found')
            except ValueError as e:
                logger.error(e)
                logger.error('Callback ID not an integer: %s', payload['callback_id'])
                raise falcon.HTTPBadRequest('Bad Request', 'Callback id must be int')
            data = {'msg_id': msg_id,
                    'source': payload['user']['name'],
                    'content': payload['actions'][0]['name']}
            endpoint = self.config['iris']['hook']['slack']
            try:
                result = self.iclient.post(endpoint, data)
            except MaxRetryError as e:
                logger.error(e.reason)
                return
            if result.status == 400:
                raise falcon.HTTPBadRequest('Bad Request', '')
            elif result.status != 200:
                raise falcon.HTTPInternalServerError('Internal Server Error', 'Unknown response from the api')
            else:
                content = process_api_response(result.data)
                self.return_slack_message(resp, content)
            return
        except Exception:
            logger.exception('Unable to read payload from slack. Our post body: %s', req.context['body'])
            raise falcon.HTTPBadRequest('Bad Request', 'Unable to read the payload from slack') 
Example 79
Project: blinker-mpy   Author: blinker-iot   File: Blinker.py    MIT License 4 votes vote down vote up
def parse(self):
        data = bProto.msgBuf
        if not data:
            return
        try:
            data = ujson.loads(data)
            BLINKER_LOG(data)
            # if not isinstance(data, dict):
            #     raise TypeError()
            for key, value in data.items():
                if key in bProto.Buttons:
                    bProto.isRead = False
                    bProto.Buttons[key].func(data[key])
                elif key in bProto.Sliders:
                    bProto.isRead = False
                    bProto.Sliders[key].func(data[key])
                # elif key in bProto.Toggles:
                #     bProto.isRead = False
                #     bProto.Toggles[key].func(data[key])
                elif key in bProto.RGB:
                    bProto.isRead = False
                    BLINKER_LOG(bProto.RGB[key])
                    bProto.RGB[key].func(data[key][R], data[key][G], data[key][B], data[key][BR])
                elif key in bProto.Joystick:
                    bProto.isRead = False
                    bProto.Joystick[key].func(data[key][J_Xaxis], data[key][J_Yaxis])
                elif key == BLINKER_CMD_AHRS:
                    # bProto.isAvail = False
                    bProto.isRead = False
                    bProto.Ahrs[Yaw] = data[key][Yaw]
                    bProto.Ahrs[Pitch] = data[key][Pitch]
                    bProto.Ahrs[Roll] = data[key][Roll]
                    bProto.Ahrs[AHRS_state] = True
                    # BLINKER_LOG(bProto.Ahrs)
                elif key == BLINKER_CMD_GPS:
                    bProto.isRead = False
                    bProto.GPS[LONG] = str(data[key][LONG])
                    bProto.GPS[LAT] = str(data[key][LAT])

                elif key == BLINKER_CMD_GET and data[key] == BLINKER_CMD_VERSION:
                    bProto.isRead = False
                    BlinkerMpy.print(self, BLINKER_CMD_VERSION, BLINKER_VERSION)

                elif key == BLINKER_CMD_GET and data[key] == BLINKER_CMD_STATE:
                    bProto.isRead = False
                    BlinkerMpy.heartbeat(self)
        
        except ValueError:
            pass
        except TypeError:
            pass
        finally:
            if bProto.isRead:
                # bProto.isAvail = 
                if bProto.dataFunc :
                    bProto.dataFunc(data)
                # bProto.isAvail = False 
Example 80
Project: falcon-api   Author: Opentopic   File: sqlalchemy.py    MIT License 4 votes vote down vote up
def get_queryset(self, req, resp, db_session=None, limit=None):
        """
        Return a query object used to fetch data.

        :param req: Falcon request
        :type req: falcon.request.Request

        :param resp: Falcon response
        :type resp: falcon.response.Response

        :param db_session: SQLAlchemy session
        :type db_session: sqlalchemy.orm.session.Session

        :param limit: max number of records fetched
        :type limit: int | None

        :return: a query from `object_class`
        """
        query = self.get_eager_queryset(req, resp, db_session, limit)
        conditions = {}
        if 'doc' in req.context:
            conditions = dict(req.context['doc'])
            # ignore any special params except SEARCH and ORDER
            for param in self.get_special_params():
                conditions.pop(param, None)

        conditions.update(req.params)
        if self.PARAM_SEARCH in conditions:
            search = conditions.pop(self.PARAM_SEARCH)
            try:
                conditions.update(json.loads(search) if isinstance(search, str) else search)
            except ValueError:
                raise HTTPBadRequest('Invalid attribute',
                                     'Value of {} filter attribute is invalid'.format(self.PARAM_SEARCH))

        order = conditions.pop(self.PARAM_ORDER, None)
        if not order:
            primary_keys = inspect(self.objects_class).primary_key
            return self.filter_by(query, conditions).order_by(*primary_keys)

        if isinstance(order, str):
            if (order[0] == '{' and order[-1] == '}') or (order[0] == '[' and order[-1] == ']'):
                try:
                    order = json.loads(order)
                except ValueError:
                    # not valid json, ignore and try to parse as an ordinary list of attributes
                    pass
        if not isinstance(order, list) and not isinstance(order, dict):
            order = [order]
        return self.filter_by(query, conditions, order)