def pretty_data(data, name='', title=''): pretty = PrettyTable() if title: pretty.get_string(title=title) if not name: pretty.header = False if isinstance(data, dict): head = [name + '_k', name + '_v'] pretty.field_names = head for k, v in data.items(): if isinstance(v, (str)): v = pretty_data(json.loads(v)) if is_valid_json(v) else v else: v = pretty_data(v) pretty.add_row([k, v]) elif isinstance(data, (set, list)): pretty.field_names = [name + '_i', name + '_v'] for k, v in enumerate(data): if isinstance(v, (str)): v = pretty_data(json.loads(v)) if is_valid_json(v) else v else: v = pretty_data(v) pretty.add_row([k, v]) else: pretty = data return pretty
def _pretty_data(self, data, name='※', se='_'): pretty = PrettyTable() if isinstance(data, dict): head = [name + '_k', name + '_v'] pretty.field_names = head for k, v in data.items(): if isinstance(v, dict): v = self._pretty_data(v) elif isinstance(v, (set, list)): v = self._pretty_data(v) elif isinstance(v, (unicode, str)): v = self._pretty_data( json.loads(v)) if is_valid_json(v) else v pretty.add_row([k, v]) elif isinstance(data, (set, list, str)): pretty.field_names = [name + '_i', name + '_v'] for k, v in enumerate(data): if isinstance(v, dict): v = self._pretty_data(v) elif isinstance(v, (set, list, str)): v = self._pretty_data(v) elif isinstance(v, (unicode, str)): v = self._pretty_data( json.loads(v)) if is_valid_json(v) else v pretty.add_row([k, v]) else: pretty = data return pretty
def call_163(self, ep, method='post', token='', params=None): if method not in ('post', 'get'): print('不受支持的http方法') return if isinstance(params, dict): params = json.dumps(params) data = {} enc = { 'params': self.enc_params(params), 'encSecKey': self.get_enckey() } data.update(enc) data = config.tet_data token = token or self.token print('data={}'.format(data)) ret = getattr(requests, method)('http://music.163.com/' + ep, params={ 'csrf_token': token }, data=data) print(ret) if not is_valid_json(ret.text): return ret.text return ret.json()
def typeahead(): res_obj = [] try: query_value = util.get_arg_value("query", request.method) if query_value == "": res_obj = {"error_list":[{"error_code": "missing-query-key-in-query-json"}]} elif util.is_valid_json(query_value) == False: res_obj = {"error_list":[{"error_code": "invalid-query-json"}]} else: query_obj = json.loads(query_value) util.trim_object(query_obj) field_list_one = ["glytoucan_ac", "motif_name", "enzyme_uniprot_canonical_ac", "glycan_pmid", "enzyme"] field_list_two = ["uniprot_canonical_ac", "uniprot_id", "refseq_ac", "protein_name", "gene_name", "pathway_id", "pathway_name", "disease_name","disease_id", "go_id", "go_term", "protein_pmid"] tmp_obj_one, tmp_obj_two = [], [] if query_obj["field"] in field_list_one: tmp_obj_one = apilib.glycan_typeahead(query_obj, config_obj) if query_obj["field"] in field_list_two: tmp_obj_two = apilib.protein_typeahead(query_obj, config_obj) if "error_list" in tmp_obj_one: res_obj = tmp_obj_one elif "error_list" in tmp_obj_two: res_obj = tmp_obj_two else: res_obj = tmp_obj_one + tmp_obj_two except Exception, e: res_obj = errorlib.get_error_obj("typeahead", traceback.format_exc(), path_obj)
def idmapping_list(): res_obj = {} try: query_value = util.get_arg_value("query", request.method) if query_value == "": res_obj = {"error_list":[{"error_code": "missing-query-key-in-query-json"}]} elif util.is_valid_json(query_value) == False: res_obj = {"error_list":[{"error_code": "invalid-query-json"}]} else: query_obj = json.loads(query_value) util.trim_object(query_obj) res_obj = util.get_cached_records_direct(query_obj, config_obj) except Exception, e: res_obj = errorlib.get_error_obj("idmapping_list", traceback.format_exc(), path_obj)
def glycan_to_biosynthesis_enzymes(): res_obj = {} try: query_value = util.get_arg_value("query", request.method) if query_value == "": res_obj = {"error_list":[{"error_code": "missing-query-key-in-query-json"}]} elif util.is_valid_json(query_value) == False: res_obj = {"error_list":[{"error_code": "invalid-query-json"}]} else: query_obj = json.loads(query_value) util.trim_object(query_obj) res_obj = apilib.glycan_to_biosynthesis_enzymes(query_obj, config_obj) except Exception, e: res_obj = errorlib.get_error_obj("glycan_to_biosynthesis_enzymes_direct", traceback.format_exc(), path_obj)
def protein_search(): res_obj = {} try: query_value = util.get_arg_value("query", request.method) if query_value == "": res_obj = {"error_list":[{"error_code": "missing-query-key-in-query-json"}]} elif util.is_valid_json(query_value) == False: res_obj = {"error_list":[{"error_code": "invalid-query-json"}]} else: query_obj = json.loads(query_value) util.trim_object(query_obj) res_obj = apilib.protein_search(query_obj, config_obj) except Exception, e: res_obj = errorlib.get_error_obj("protein_search", traceback.format_exc(), path_obj)
def categorized_typeahead(): res_obj = [] try: query_value = util.get_arg_value("query", request.method) if query_value == "": res_obj = {"error_list":[{"error_code": "missing-query-key-in-query-json"}]} elif util.is_valid_json(query_value) == False: res_obj = {"error_list":[{"error_code": "invalid-query-json"}]} else: query_obj = json.loads(query_value) util.trim_object(query_obj) tmp_obj = apilib.categorized_typeahead(query_obj, config_obj) if "error_list" in tmp_obj: res_obj = tmp_obj else: res_obj = tmp_obj except Exception, e: res_obj = errorlib.get_error_obj("categorized_typeahead", traceback.format_exc(), path_obj)