def search(self, pif_query): """ Run a PIF query against Citrination. :param pif_query: :class:`.PifQuery` to execute. :return: :class:`.PifSearchResult` object with the results of the query. """ if pif_query.size is None and pif_query.from_index is None: total = 1; time = 0.0; hits = []; first = True while len(hits) < min(total, 10000): if first: first = False else: sleep(3) sub_query = deepcopy(pif_query) sub_query.from_index = len(hits) partial_results = self.search(sub_query) total = partial_results.total_num_hits time += partial_results.took if partial_results.hits is not None: hits.extend(partial_results.hits) return PifSearchResult(hits=hits, total_num_hits=total, took=time) response = requests.post(self.pif_search_url, data=pif.dumps(pif_query), headers=self.headers) if response.status_code != requests.codes.ok: raise RuntimeError('Received ' + str(response.status_code) + ' response: ' + str(response.reason)) return PifSearchResult(**keys_to_snake_case(response.json()['results']))
def search(self, pif_query): """ Run a PIF query against Citrination. :param pif_query: :class:`.PifQuery` to execute. :return: :class:`.PifSearchResult` object with the results of the query. """ if pif_query.size is None and pif_query.from_index is None: total = 1 time = 0.0 hits = [] first = True while len(hits) < min(total, 10000): if first: first = False else: sleep(3) sub_query = deepcopy(pif_query) sub_query.from_index = len(hits) partial_results = self.search(sub_query) total = partial_results.total_num_hits time += partial_results.took if partial_results.hits is not None: hits.extend(partial_results.hits) return PifSearchResult(hits=hits, total_num_hits=total, took=time) response = requests.post(self.pif_search_url, data=pif.dumps(pif_query), headers=self.headers) if response.status_code != requests.codes.ok: raise RuntimeError('Received ' + str(response.status_code) + ' response: ' + str(response.reason)) return PifSearchResult( **keys_to_snake_case(response.json()['results']))
def search(self, pif_query): """ Run a PIF query against Citrination. :param pif_query: :class:`.PifQuery` to execute. :return: :class:`.PifSearchResult` object with the results of the query. """ response = requests.post(self.pif_search_url, data=pif.dumps(pif_query), headers=self.headers) if response.status_code != requests.codes.ok: raise RuntimeError('Received ' + str(response.status_code) + ' response: ' + str(response.reason)) return PifSearchResult(**keys_to_snake_case(response.json()['results']))
def pif_multi_search(self, multi_query): """ Run each in a list of PIF queries against Citrination. :param multi_query: :class:`MultiQuery` object to execute. :return: :class:`PifMultiSearchResult` object with the results of the query. """ failure_message = "Error while making PIF multi search request" response_dict = self._get_success_json( self._post(routes.pif_multi_search, data=json.dumps(multi_query, cls=QueryEncoder), failure_message=failure_message)) return PifMultiSearchResult(**keys_to_snake_case(response_dict['results']))
def _get_object(class_, obj): """ Helper function that returns an object, or if it is a dictionary, initializes it from class_. :param class_: Class to use to instantiate object. :param obj: Object to process. :return: One or more objects. """ if isinstance(obj, list): return [Pio._get_object(class_, i) for i in obj] elif isinstance(obj, dict): return class_(**keys_to_snake_case(obj)) else: return obj
def _get_object(class_, obj): """ Helper function that returns an object, or if it is a dictionary, initializes it from class_. :param class_: Class to use to instantiate object. :param obj: Object to process. :return: One or more objects. """ if isinstance(obj, list): return [Serializable._get_object(class_, i) for i in obj] elif isinstance(obj, dict): return class_(**keys_to_snake_case(obj)) else: return obj
def _search_internal(self, returning_query, result_class): if result_class == PifSearchResult: route = routes.pif_search failure_message = "Error while making PIF search request" elif result_class == DatasetSearchResult: route = routes.dataset_search failure_message = "Error while making dataset search request" response_json = self._get_success_json(self._post( route, data=json.dumps(returning_query, cls=QueryEncoder), failure_message=failure_message)) return result_class(**keys_to_snake_case(response_json['results']))
def _dict_to_pio(d): """ Convert a single dictionary object to a Physical Information Object. :param d: Dictionary to convert. :return: Single object derived from :class:`.Pio`. """ d = keys_to_snake_case(d) if 'category' not in d: raise ValueError('Dictionary does not contains a category field: ' + ', '.join(d.keys())) elif d['category'] == 'system': return System(**d) elif d['category'] == 'system.chemical': return ChemicalSystem(**d) elif d['category'] == 'system.chemical.alloy': # Legacy support return Alloy(**d) elif d['category'] == 'system.chemical.alloy.phase': # Legacy support return ChemicalSystem(**d) raise ValueError('Dictionary does not contain a valid top-level category: ' + str(d['category']))
def _dict_to_pio(d, class_=None): """ Convert a single dictionary object to a Physical Information Object. :param d: Dictionary to convert. :param class_: Subclass of :class:`.Pio` to produce, if not unambiguous :return: Single object derived from :class:`.Pio`. """ d = keys_to_snake_case(d) if class_: return class_(**d) if 'category' not in d: raise ValueError('Dictionary does not contains a category field: ' + ', '.join(d.keys())) elif d['category'] == 'system': return System(**d) elif d['category'] == 'system.chemical': return ChemicalSystem(**d) elif d['category'] == 'system.chemical.alloy': # Legacy support return Alloy(**d) elif d['category'] == 'system.chemical.alloy.phase': # Legacy support return ChemicalSystem(**d) raise ValueError('Dictionary does not contain a valid top-level category: ' + str(d['category']))