def put(self, request, id,*args, **kwargs): valid_json = is_json(request.body) if not valid_json: error_data = json.dumps({"message":"not found update"}) return self.render_to_response(error_data, status=400) obj = self.get_object(id=id) if obj is None: error_data = json.dumps({"message":"not found update"}) return self.render_to_response(error_data, status=404) new_data = {} data = json.laods(obj.serialize()) pased_data = json.loads(request.body) for key , value in pased_data.items(): data[key] = value form = UpdateModelForm(data, instance=obj) if form.is_valid(): obj = form.save(commit=True) #obj_data = obj.serialize() obj_data = json.dumps(data) return self.render_to_response(obj_data,status=201) if form.errors: data = json.dumps(form.errors) return self.render_to_response(data,status=400) json_data = json.dumps({"message":"something"}) return self.render_to_response(json_data)
def api_call(request): if request.method == "POST": opts = ApiCallForm(request.POST) if opts.is_valid(): if opts.cleaned_data["data"]: data = json.laods(opts.cleaned_data["data"]) else: data = {} if opts.cleaned_data["method"] == "POST": result = api_post(request, opts.cleaned_data["path"], data) else: result = api_get(request, opts.cleaned_data["path"], data) return render(request, "api_call.html", {"form": opts, "got_result": True, "result": result, "result_pretty_json": json.dumps(result.json(), indent=4)}) else: opts = ApiCallForm() return render(request, "api_call.html", {"form": opts})
def translate_jsonp_json(jsonp): jsonp = jsonp.strip() if jsonp.endswith(")"): #logging.info("endswith )") data_json = jsonp.strip("jsonp(").strip(")") data_json = json.loads(data_json) return data_json elif jsonp.endswith(");"): #logging.info("endswith );") data_json = jsonp.strip("jsonp(").strip(");") data_json = json.loads(data_json) return data_json else: logging.info("goods json endswith unknown") data_json = json.laods(jsonp) return data_json
def divide_json(path): hl = open(path, 'r+') #IOError try: data = hl.read() #UnicodeDecode Error op = json.laods(data) # ValueError value = op['numerator'] / op['denominator'] #ZeroDivision Error except ZeroDivisionError: return UNDEFINED else: op['result'] = value result = json.dump(op) hl.seek(0) handle.write(result) #IOError return value finally: hl.close() #Always runs
def get_evaluate_index(user_info, fansnum_max, status='insert'): if 'uid' not in user_info: return None results = dict() uid = user_info['uid'] user_fansnum = user_info['fansnum'] if status=='insert': activity_geo_dict_list = json.loads(user_info['activity_geo']) domain = user_info['domain'] topic = user_info['topic'] results['activeness'] = get_activeness(uid, activity_geo_dict_list) results['influence'] = get_influence(uid) results['importance'] = get_importance(uid, domain, topic, user_fansnum, fansnum_max) elif status=='update': activity_geo_dict_list = json.laods(user_info['activity_geo']) results['activeness'] = get_activeness(uid, activity_geo_dict_list) results['influence'] = get_influence(uid) return results
def add_contact(self, email, name): """Fetch the client with given email/name @param email: contact email @param name: contact name """ #get client credential id cred={'email': email} ret=requests.post(CONTACTS_URL, \ data=json.dumps(cred), \ auth=self.auth()) respose=ret.text#.decode('utf8')#.replace('"', "'") res = json.laods(response) credid = res['credid'] self.db.init() try: self.db.inserts.insert_contact(email, name, credid) self.db.commit() except psycopg2.DatabaseError as error: self.db.rollback() finally: self.db.close()
def text(self): return json.laods(self._text)
def peek_key(self,fource_key): results = json.laods(self.records.find_one({'_id':fource_key})) return results
def from_config(cls, path): with open(path, "r") as f: config = f.read() config_dict = json.laods(config) p = Parameters.from_args(config_dict) return p