def sdmx_report(sql, data_frame=None): """ Build the sdmx file. :param sql: Sql text query. :param data_frame: The Pandas dataframe :return: Sdmx. """ col_dict = build_column_dict(data_frame, sql) out_stream = StringIO.StringIO() generic_data = sdmx.GenericDataType() header = sdmx.HeaderType() header.set_Test(False) header.set_Truncated(False) ts = time.time() prepared = datetime.fromtimestamp(ts).strftime('%Y-%m-%dT%H:%M:%S') header.set_Prepared(prepared) sender = sdmx.PartyType() sender.set_id(SENDER) name = sdmx.TextType() name.set_lang(LANGUAGE_CODE) name.set_valueOf_(SENDER_NAME) sender.add_Name(name) header.add_Sender(sender) generic_data.set_Header(header) data_set = sdmx.DataSetType() data_set.set_keyFamilyURI(SENDER_NAME) data = get_data_from_data_frame(data_frame) for r, row in enumerate(data): series = sdmx.SeriesType() series_key = sdmx.SeriesKeyType() obs = sdmx.ObsType() for c in col_dict: v = row[c] val = "%s" % v value = sdmx.ValueType() concept = get_concept(col_dict, c) if concept == 'obsValue': if not val.startswith('*'): obs_value = sdmx.ObsValueType() obs_value.set_value(val) obs.set_ObsValue(obs_value) elif concept is not None: value.set_concept(concept) value.set_value(val.strip()) series_key.add_Value(value) series.set_SeriesKey(series_key) series.add_Obs(obs) data_set.add_Series(series) generic_data.set_DataSet(data_set) generic_data.export(out_stream, 0) value = out_stream.getvalue() value = add_xml_header(value) return value
def rdf_report(sql, title, description, data_frame=None, rdf_format=None): """ Get the rdf report. :param sql: The query. :param title: The query title. :param description: Query description. :param data_frame: The Pandas data frame. :param rdf_format: The desired rdf format. :return: The serialized Rdf. """ g = Graph() data = get_data_from_data_frame(data_frame) title = title.decode('utf-8') dataset = "dataset-%s" % title subject_t_ref = term.URIRef(my_ns[dataset]) source_tables = get_source_tables(sql) g = add_table_triples(description, g, source_tables, title, subject_t_ref) col_dict = build_column_dict(data_frame, sql) slice_t_ref = term.URIRef(my_ns['slice']) g = add_slice_triples(g, subject_t_ref, data_frame, col_dict) g = add_observations_triples(g, data, title, slice_t_ref, col_dict) ser = g.serialize(format=rdf_format) if rdf_format == "xml": ser = add_xml_header(ser) elif rdf_format == "turtle": ser = add_sh_like_header(ser) return ser