def test_object_pairs_hook_with_unicode(self): s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), ("qrt", 5), ("pad", 6), ("hoy", 7)] self.assertEqual(json.loads(s), eval(s)) self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p) od = json.loads(s, object_pairs_hook=json.OrderedDict) self.assertEqual(od, json.OrderedDict(p)) self.assertEqual(type(od), json.OrderedDict) # the object_pairs_hook takes priority over the object_hook self.assertEqual(json.loads(s, object_pairs_hook=json.OrderedDict, object_hook=lambda x: None), json.OrderedDict(p))
def get_table(request, table): table_name = request.GET.get('table_name', None) rows = Row.objects.filter(table_name=table_name) # data = serializers.serialize('json', header) header = [] header_dates = [] for row in rows: header.append({ 'id': row.id, 'row_name': row.row_name, 'type': row.type, 'title': row.title }) if row.type == 'date': header_dates.append(row.row_name) cursor = connections['default'].cursor() cursor.execute("SELECT * FROM " + table_name) rows = dictfetchall(cursor) for row in rows: for head in header_dates: row[head] = str(row[head]) response_data = simplejson.OrderedDict([ ('table_name', table_name), ('header', header), ('data', rows), ]) return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
def test_ordered_dict(self): # http://bugs.python.org/issue6105 items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)] s = json.dumps(json.OrderedDict(items)) self.assertEqual( s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
def _asdict(self): result = simplejson.OrderedDict() for key in self.__mapper__.c.keys(): if isinstance(getattr(self, key), datetime): result["x"] = getattr(self, key).timestamp() * 1000 result["timestamp"] = result["x"] else: result[key] = getattr(self, key) return result
def delete_row(request): row_id = request.POST.get('id') table_name = request.POST.get('table_name') cursor = connections['default'].cursor() cursor.execute("DELETE FROM " + table_name + " WHERE id = " + row_id) response_data = simplejson.OrderedDict([ ('table_name', table_name), ('id', row_id), ]) return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
def main(argv): filename = argv[0] all_histograms = json.OrderedDict() for histogram in histogram_tools.from_file(filename): name = histogram.name() parameters = json.OrderedDict() table = { 'boolean': '2', 'flag': '3', 'enumerated': '1', 'linear': '1', 'exponential': '0' } # Use __setitem__ because Python lambdas are so limited. histogram_tools.table_dispatch(histogram.kind(), table, lambda k: parameters.__setitem__('kind', k)) if histogram.low() == 0: parameters['min'] = 1 else: parameters['min'] = histogram.low() try: buckets = histogram.ranges() parameters['buckets'] = buckets parameters['max'] = buckets[-1] parameters['bucket_count'] = len(buckets) except histogram_tools.DefinitionException: continue all_histograms.update({ name: parameters }); if startup_histogram_re.search(name) is not None: all_histograms.update({ "STARTUP_" + name: parameters }) print json.dumps({ 'histograms': all_histograms})
def create(request, table_name): params = request.GET names = [] values = [] for name in params: names.append(name) values.append("'" + params.get(name) + "'") cursor = connections['default'].cursor() cursor.execute("INSERT INTO " + table_name + " (" + ", ".join(names) + ") VALUES (" + ", ".join(values) + ")") response_data = simplejson.OrderedDict([ ('table_name', table_name), ('params', params), ]) return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
def post_row(request): params = dict(request.POST) row_id = params.get('id')[0] table_name = params.get('table_name')[0] del params['table_name'] del params['id'] values = [] for param in params: values.append(param + "='" + params[param][0] + "'") value_string = ", ".join(values) cursor = connections['default'].cursor() cursor.execute("UPDATE " + table_name + " SET " + value_string + " WHERE id = " + row_id) response_data = simplejson.OrderedDict([ ('table_name', table_name), ('id', row_id), ('params', params), ]) return HttpResponse(simplejson.dumps(response_data), content_type="application/json")
def create_input_file(): return IFF.InputFileFactory(json.OrderedDict(),RCE_INPUT_FILES)