示例#1
0
def import_example_data():
    db = Database("temp-example-db")
    if db.name not in databases:
        db.register()
    db.write(db_data)
    db.process()

    method = Method(("static GWP", ))
    if method.name not in methods:
        method.register()
    method.write(static_cfs)
    method.process()

    dynamic_method = DynamicIAMethod("static GWP")
    if dynamic_method.name not in dynamic_methods:
        dynamic_method.register()
    dynamic_method.write({x[0]: x[1] for x in static_cfs})
    dynamic_method.to_worst_case_method(("static GWP", "worst case"))

    dynamic_method = DynamicIAMethod("dynamic GWP")
    if dynamic_method.name not in dynamic_methods:
        dynamic_method.register()
    dynamic_method.write(dynamic_cfs)
    dynamic_method.to_worst_case_method(("dynamic GWP", "worst case"))

    dynamic_method = DynamicIAMethod("discounted dynamic GWP")
    if dynamic_method.name not in dynamic_methods:
        dynamic_method.register()
    dynamic_method.write(dynamic_discounted_cfs)
    dynamic_method.to_worst_case_method(
        ("discounted dynamic GWP", "worst case"))
示例#2
0
def relink_exchanges_existing_db(db: bw.Database, old: str,
                                 other: bw.Database) -> None:
    """Relink exchanges after the database has been created/written.

    This means possibly doing a lot of sqlite update calls.
    """
    if old == other.name:
        print("No point relinking to same database.")
        return
    assert db.backend == "sqlite", "Relinking only allowed for SQLITE backends"
    assert other.backend == "sqlite", "Relinking only allowed for SQLITE backends"

    duplicates, candidates = {}, {}
    altered = 0

    for ds in other:
        key = activity_hash(ds, DEFAULT_FIELDS)
        if key in candidates:
            duplicates.setdefault(key, []).append(ds)
        else:
            candidates[key] = ds.key

    with sqlite3_lci_db.transaction() as transaction:
        try:
            # Only do relinking on external biosphere/technosphere exchanges.
            for i, exc in enumerate(
                    exc for act in db for exc in act.exchanges()
                    if exc.get("type") in {"biosphere", "technosphere"}
                    and exc.input[0] == old):
                # Use the input activity to generate the hash.
                key = activity_hash(exc.input, DEFAULT_FIELDS)
                if key in duplicates:
                    raise StrategyError(
                        format_nonunique_key_error(exc.input, DEFAULT_FIELDS,
                                                   duplicates[key]))
                elif key in candidates:
                    exc["input"] = candidates[key]
                    altered += 1
                exc.save()
                if i % 10000 == 0:
                    # Commit changes every 10k exchanges.
                    transaction.commit()
        except (StrategyError, ValidityError) as e:
            print(e)
            transaction.rollback()
    # Process the database after the transaction is complete.
    #  this updates the 'depends' in metadata
    db.process()
    print(
        "Relinked database '{}', {} exchange inputs changed from '{}' to '{}'."
        .format(db.name, altered, old, other.name))