예제 #1
0
 def modify_exchanges_output(self, exchanges, key):
     db_changed = {key[0]}
     for exc in exchanges:
         db_changed.add(exc['output'][0])
         if exc['type'] == 'production':
             new_exc = Exchange()
             new_exc._data = copy.deepcopy(exc._data)
             new_exc['type'] = 'technosphere'
             new_exc['output'] = key
             new_exc.save()
         else:
             exc['output'] = key
             exc.save()
     for db in db_changed:
         signals.database_changed.emit(db)
예제 #2
0
def create_new_exchange(in_key: tuple,
                        out_key: tuple,
                        data: dict,
                        exc_type: str = "technosphere") -> Exchange:
    # Override default exc_type if inputdb is biosphere
    if in_key[0] == bw.config.biosphere:
        exc_type = "biosphere"
    data["input"] = in_key
    data["output"] = out_key
    data["type"] = exc_type
    e = Exchange(**data)
    return e
예제 #3
0
def structure_exchanges(data: List[dict], super_db: str, deltas: set) -> List[Exchange]:
    """Take a list of dictionaries and structure them into a list of Exchange
    objects, adjusted for the superstructure database.
    """
    def alter_data(d):
        amount = d.get("amount")
        d["amount"] = amount if d["type"] == "production" else 0
        key = d["input"]
        d["input"] = (super_db, key[1]) if key[0] in deltas else key
        key = d["output"]
        d["output"] = (super_db, key[1]) if key[0] in deltas else key
        return d

    altered_exchanges = map(alter_data, data)
    new_exchanges = [Exchange(**exc) for exc in altered_exchanges]
    return new_exchanges
예제 #4
0
def swap_exchange_activities(data: dict, super_db: str,
                             delta_db: str) -> Exchange:
    """Take the exchange data and replace one or two activities inside with
    new ones containing the same information.

    This works best with activities constructed like those of ecoinvent.
    """
    in_key = data.get("input", ("", ))
    out_key = data.get("output", ("", ))
    if in_key[0] == delta_db:
        data["input"] = (super_db, in_key[1])
    if out_key[0] == delta_db:
        data["output"] = (super_db, out_key[1])
    # Constructing the Exchange this way will cause a new row to be written
    e = Exchange(**data)
    return e