Beispiel #1
0
    def test_delete_one(self):
        self.__save_records()
        records = Observation.find(self.dataset)
        self.assertNotEqual(records, [])

        row = self.__decode(records[0])

        Observation.delete(self.dataset, row[INDEX])
        new_records = Observation.find(self.dataset)

        # Dump to avoid problems with nan != nan.
        self.assertEqual(dump_mongo_json(records[1:]),
                         dump_mongo_json(new_records))
Beispiel #2
0
    def _dump_or_error(self, obj, error_message=DEFAULT_ERROR_MESSAGE, callback=False):
        """Dump JSON or return error message, potentially with callback.

        If `obj` is None `error_message` is returned and the HTTP status code
        is set to 400. Otherwise the HTTP status code is set to
        `success_status_code`. If `callback` exists, the returned string is
        wrapped in the callback for JSONP.

        :param obj: Data to dump as JSON using BSON encoder.
        :param error_message: Error message to return is object is None.
        :param callback: Callback string to wrap obj in for JSONP.

        :returns: A JSON string wrapped with callback if callback is not False.
        """
        if obj is None:
            obj = {self.ERROR: error_message}

        result = obj if isinstance(obj, basestring) else dump_mongo_json(obj)
        self.__add_cors_headers()

        return "%s(%s)" % (str(callback), result) if callback else result
    def _dump_or_error(self,
                       obj,
                       error_message=DEFAULT_ERROR_MESSAGE,
                       callback=False):
        """Dump JSON or return error message, potentially with callback.

        If `obj` is None `error_message` is returned and the HTTP status code
        is set to 400. Otherwise the HTTP status code is set to
        `success_status_code`. If `callback` exists, the returned string is
        wrapped in the callback for JSONP.

        :param obj: Data to dump as JSON using BSON encoder.
        :param error_message: Error message to return is object is None.
        :param callback: Callback string to wrap obj in for JSONP.

        :returns: A JSON string wrapped with callback if callback is not False.
        """
        if obj is None:
            obj = {self.ERROR: error_message}

        result = obj if isinstance(obj, basestring) else dump_mongo_json(obj)
        self.__add_cors_headers()

        return '%s(%s)' % (str(callback), result) if callback else result
Beispiel #4
0
 def to_json(self):
     """Convert DataFrame to a list of dicts, then dump to JSON."""
     jsondict = self.to_jsondict()
     return dump_mongo_json(jsondict)
Beispiel #5
0
def df_to_json(df):
    """Convert DataFrame to a list of dicts, then dump to JSON."""
    jsondict = df_to_jsondict(df)
    return dump_mongo_json(jsondict)
Beispiel #6
0
    def test_encode_no_dataset(self):
        records = self.__save_records()

        for record in records:
            encoded = Observation.encode(record)
            self.assertEqual(dump_mongo_json(encoded), dump_mongo_json(record))
Beispiel #7
0
def df_to_json(df):
    """Convert DataFrame to a list of dicts, then dump to JSON."""
    jsondict = df_to_jsondict(df)
    return dump_mongo_json(jsondict)