0 votes
47 views

I wanna use Python 3,0 to replace one flow with another one. As many of my processes contain this exchange flow. When I operate the below codes, it doesn't work. Please who could give me some suggestions why it failed:

import olca_schema as o

import pandas as pd

import olca_ipc as olca

import uuid

import math

from datetime import datetime

from matplotlib import pyplot as plt

import matplotlib.mlab as mlab

from matplotlib import rcParams

import matplotlib.patches as mpatches

import seaborn as sns

params = {'mathtext.default': 'regular' }

client = olca.Client(8080)

client

# Read the Excel file into a DataFrame

df = pd.read_excel(r'C:\Users\jx331\Desktop\Ofwat\Python_OpenLCA\input_python.xlsx', sheet_name='input')

# Iterate through each row in the DataFrame

for index, row in df.iterrows():

    target_uuid = row['UUID']  # Assuming 'UUID' is the column containing UUIDs in your DataFrame

    target_flow_name = row['Flow_name']  # Target flow name to change

    

    try:

        # Retrieve the process based on the UUID

        process = client.get(o.Process, target_uuid)

        if process is not None:

            # Find the exchange corresponding to the target flow name

            exchange = next((e for e in process.exchanges if e.flow.name == target_flow_name), None)  

            if exchange is not None:

                # Find the target exchange with the given UID

                target_exchange = next((e for e in process.exchanges if e.flow.uid == "9d23fe2a-b3f8-4ee2-9dcb-5f5cede20bd6"), None)

                if target_exchange is not None:

                    # Update the flow of the exchange with the new target exchange

                    exchange.flow = target_exchange.flow

                else:

                    print(f"Target exchange not found for UID: 9d23fe2a-b3f8-4ee2-9dcb-5f5cede20bd6")

            else:

                print(f"Exchange not found for UUID: {target_uuid} and flow name: {target_flow_name}")

        else:

            print(f"Process not found for UUID: {target_uuid}")

            

    except Exception as e:

        print(f"Failed to retrieve process for UUID: {target_uuid}, Error: {e}")

        # Skip to the next iteration if process retrieval fails

        continue

Erro:

Failed to retrieve process for UUID: bd74e0bd-58f7-473e-b41f-f05d99b2bc41, Error: 'Ref' object has no attribute 'uid'
Failed to retrieve process for UUID: 620a992e-058e-48c2-add4-f6eb6f35c600, Error: 'Ref' object has no attribute 'uid'
Failed to retrieve process for UUID: bb9a82b4-fcfd-4e82-afa4-0b4ba492c9ec, Error: 'Ref' object has no attribute 'uid'

in openLCA by (120 points)

1 Answer

0 votes
by (2.7k points)

Hi Jerry,

I have not tested it, but from the error "Error: 'Ref' object has no attribute 'uid'" I guess that the attribute uid is called on an object that does not have it. Looking into your code, there is, in fact, a call to it: "e.flow.uid". You should use "e.flow.id" instead (cf. schema).

Regards,

François

...