Simulink.P​ythonImpor​ter() Assertion Failed Error

2 Ansichten (letzte 30 Tage)
Steven
Steven am 26 Mär. 2024
Beantwortet: Jaimin am 26 Dez. 2024
Hello,
I am currently creating a Python Block module to allow HttpReponses to a remote Web API server for data collection of the model I am currently working on. Now, for some reason when I attempt to convert the script into a Simulink Block with PythonImporter I get an Error on Analzing the script. Here is the script I am currently using.
import requests
import datetime
class TransportModel:
def __init__(self, **args ):
self.dict = args
def v1Set(self, bus, val):
self.dict[f'${bus}v1'] = val
def v2Set(self, bus, val):
self.dict[f'${bus}v2'] = val
def v3Set(self, bus, val):
self.dict[f'${bus}v3'] = val
def c1Set(self, bus, val):
self.dict[f'${bus}c1'] = val
def c2Set(self, bus, val):
self.dict[f'${bus}c2'] = val
def c3Set(self, bus, val):
self.dict[f'${bus}c3'] = val
def p1Set(self, bus, val):
self.dict[f'${bus}p1'] = val
def p2Set(self, bus, val):
self.dict[f'${bus}p2'] = val
def p3Set(self, bus, val):
self.dict[f'${bus}p3'] = val
def tsSet(self, val):
self.dict['timestamp'] = val
def epSet(self, val):
self.dict['elapsed'] = val
def listSet(self, Array):
self.dict.copy(Array)
def v1Get(self, bus):
return self.dict[f'${bus}v1']
def v2Get(self, bus):
return self.dict[f'${bus}v2']
def v3Get(self, bus):
return self.dict[f'${bus}v3']
def c1Get(self, bus):
return self.dict[f'${bus}c1']
def c2Get(self, bus):
return self.dict[f'${bus}c2']
def c3Get(self, bus):
return self.dict[f'${bus}c3']
def p1Get(self, bus):
return self.dict[f'${bus}p1']
def p2Get(self, bus):
return self.dict[f'${bus}p2']
def p3Get(self, bus):
return self.dict[f'${bus}p3']
def tsGet(self):
return self.dict['timestamp']
def epGet(self):
return self.dict['elapsed']
def listGet(self):
return self.dict
def listCount(self):
return len(self.dict)
model = []
tempModel = TransportModel
Url = ""
Max = 0
def __init__(url: str, max: int):
global Url
global Max
Url = url
Max = max
def Bus(Bus: int, c1: int, c2: int, c3: int, p1: int, p2: int, p3: int, v1: int, v2: int, v3: int):
BusModel = {
f'{Bus}v1': v1,
f'{Bus}v2': v2,
f'{Bus}v3': v3,
f'{Bus}c1': c1,
f'{Bus}c2': c2,
f'{Bus}c3': c3,
f'{Bus}p1': p1,
f'{Bus}p2': p2,
f'{Bus}p3': p3
}
BusArray(BusModel)
def BusArray(BusModel: dict[str, int]):
if tempModel.listCount == Max:
model.append(tempModel)
tempModel = TransportModel
Send()
tempModel.listSet(BusModel)
def Send():
model.tsSet(val=datetime.datetime.timestamp)
if (Url == None):
exit
else:
sender = requests.post(Url, json = model)
print(f'{sender.status_code}:{sender.text}')
model = []
#def Csv():
# pass
The script is still a WIP, however I wanted to ensure that it will be imported prior to adding any additional code.

Antworten (1)

Jaimin
Jaimin am 26 Dez. 2024
It seems you are encountering an Assertion Error when importing a Python file using the PythonImporter app. To resolve this, ensure your script has correct syntax, properly manages variable scope, uses methods correctly, and effectively handles errors to avoid common issues.
Kindly refer following code for understanding.
import requests
import datetime
class TransportModel:
def __init__(self, **args):
self.dict = args
def v1Set(self, bus, val):
self.dict[f'{bus}v1'] = val
# Other setters and getters...
def listSet(self, array):
self.dict.update(array)
def listCount(self):
return len(self.dict)
model = []
tempModel = TransportModel()
Url = ""
Max = 0
def initialize(url: str, max_count: int) -> None:
global Url, Max
Url = url
Max = max_count
def Bus(bus: int, c1: int, c2: int, c3: int, p1: int, p2: int, p3: int, v1: int, v2: int, v3: int) -> None:
bus_model = {
f'{bus}v1': v1,
f'{bus}v2': v2,
f'{bus}v3': v3,
f'{bus}c1': c1,
f'{bus}c2': c2,
f'{bus}c3': c3,
f'{bus}p1': p1,
f'{bus}p2': p2,
f'{bus}p3': p3
}
BusArray(bus_model)
def BusArray(bus_model: dict) -> None:
global tempModel
if tempModel.listCount() == Max:
model.append(tempModel)
tempModel = TransportModel()
Send()
tempModel.listSet(bus_model)
def Send() -> str:
global model
if not model:
return "No data to send"
model[-1].tsSet(val=datetime.datetime.now().timestamp())
if not Url:
return "URL not set"
try:
sender = requests.post(Url, json=model)
response = f'{sender.status_code}:{sender.text}'
model = []
return response
except requests.exceptions.RequestException as e:
return f"Request failed: {e}"
# Example function to demonstrate input/output structure
def example_function(input_data: dict) -> dict:
# Process input_data and return a result
result = {"status": "success", "processed_data": input_data}
return result
For more information kindly refer following MathWorks documentation.
I hope this will be helpful.

Produkte


Version

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by