def _get_fields_key(resource): """Returns the fields key from a resource dict """ if resource['code'] in [HTTP_OK, HTTP_ACCEPTED]: if (MODEL_RE.match(resource_id) or ANOMALY_RE.match(resource_id)): return resource['object']['model']['model_fields'] elif CLUSTER_RE.match(resource_id): return resource['object']['clusters']['fields'] elif CORRELATION_RE.match(resource_id): return resource['object']['correlations']['fields'] elif STATISTICAL_TEST_RE.match(resource_id): return resource['object']['statistical_tests']['fields'] elif SAMPLE_RE.match(resource_id): return dict([(field['id'], field) for field in resource['object']['sample']['fields']]) else: return resource['object']['fields'] return None
def pprint(self, resource, out=sys.stdout): """Pretty prints a resource or part of it. """ if (isinstance(resource, dict) and 'object' in resource and 'resource' in resource): resource_id = resource['resource'] if (SOURCE_RE.match(resource_id) or DATASET_RE.match(resource_id) or MODEL_RE.match(resource_id) or EVALUATION_RE.match(resource_id) or ENSEMBLE_RE.match(resource_id) or CLUSTER_RE.match(resource_id) or ANOMALY_RE.match(resource_id)): out.write("%s (%s bytes)\n" % (resource['object']['name'], resource['object']['size'])) elif PREDICTION_RE.match(resource['resource']): objective_field_name = ( resource['object']['fields'][ resource['object']['objective_fields'][0]]['name']) input_data = {} for key, value in resource['object']['input_data'].items(): try: name = resource['object']['fields'][key]['name'] except KeyError: name = key input_data[name] = value prediction = ( resource['object']['prediction'][ resource['object']['objective_fields'][0]]) out.write("%s for %s is %s\n" % (objective_field_name, input_data, prediction)) out.flush() else: pprint.pprint(resource, out, indent=4)
def pprint(self, resource, out=sys.stdout): """Pretty prints a resource or part of it. """ if (isinstance(resource, dict) and 'object' in resource and 'resource' in resource): resource_id = resource['resource'] if (SOURCE_RE.match(resource_id) or DATASET_RE.match(resource_id) or MODEL_RE.match(resource_id) or EVALUATION_RE.match(resource_id) or ENSEMBLE_RE.match(resource_id) or CLUSTER_RE.match(resource_id) or ANOMALY_RE.match(resource_id)): out.write( "%s (%s bytes)\n" % (resource['object']['name'], resource['object']['size'])) elif PREDICTION_RE.match(resource['resource']): objective_field_name = (resource['object']['fields'][ resource['object']['objective_fields'][0]]['name']) input_data = {} for key, value in resource['object']['input_data'].items(): try: name = resource['object']['fields'][key]['name'] except KeyError: name = key input_data[name] = value prediction = (resource['object']['prediction'][ resource['object']['objective_fields'][0]]) out.write("%s for %s is %s\n" % (objective_field_name, input_data, prediction)) out.flush() else: pprint.pprint(resource, out, indent=4)