示例#1
0
import json

from goodtables import validate
from goodtables.cli import _print_report
from pathlib import Path

root = Path(__file__).parents[1]

metadata = json.load(open(root / "datapackage.json", "r"))

paths = {i["name"]: i["path"] for i in metadata["resources"] if "schema" in i}
schemas = {
    i["name"]: i["schema"]
    for i in metadata["resources"] if "schema" in i
}

for name, path in paths.items():
    report = validate(root / path,
                      schema=schemas[name],
                      table_limit=20,
                      row_limit=600000)
    _print_report(report)
print("\nData summary:\n")
print("Emissions sum w/o EU28: {:d} GgCO₂-equiv.".format(int(
    export.Emissions.sum() - export.Emissions.loc['EUU'].sum())))
print("Percentage sum: {}".format(
    export.Percentage.sum() - export.loc['EUU'].Percentage))
print("Count signatures: {}".format(export.Signature.count()))
print("Count ratified: {}".format(
    export["Ratification"].count()))
ratified = export["Ratification"].notnull()
percentage_sum = (export[ratified].Percentage.sum() -
                  export.loc["EUU"].Percentage)
print("Sum of percentages with ratification w/o EU: {}".format(
    percentage_sum))


def to_int(x):
    if pd.isnull(x):
        return ""
    else:
        return str(int(x))


export.Emissions = export.Emissions.apply(to_int)
export.Year = export.Year.apply(to_int)
export.to_csv(outfile, encoding="UTF-8")

report = validate(root / "datapackage.json")
if report["error-count"] > 0:
    _print_report(report)