Multiple return, reports, errors
The previous examples work well enough for simple calculations, but they do not take advantage of all of the features of ArupCompute.
To take advantage of these features we need to return an object with appropriately named fields.
Add the below code to your file to define the appropriate objects.
Python and the ArupCompute system are case-sensitive, so ensure things are named exactly as below.
class ArupComputeResultItem:
def __init__(self):
self.symbol = None
self.value = None
class ArupComputeResult:
def __init__(self):
self.errors = []
self.warnings = []
self.remarks = []
self.arupComputeResultItems = []
self.arupComputeReport = None
self.arupComputeReport_HTML = None
Multiple return
Often we may want to calculate many things at once and return them to the user.
To do this we can populate the ArupComputeResultItems
field with a list of values.
The ArupCompute system relies on the variable symbol to match up everywhere to ensure correct operation.
def multiplyandPower(x: float, y: float) -> ArupComputeResult:
"""Multiply and power
Works out what two number multiplied together is, as well as one raised to the power of the other
Args:
x: The base number
y: The number to multiply x by, and raise x to
Returns:
m: Numbers multiplied together
p: x raised to power of y
"""
acr = ArupComputeResult()
ri_mul = ArupComputeResultItem()
ri_mul.value = x * y
ri_mul.symbol = "m"
acr.arupComputeResultItems.append(ri_mul)
ri_pow = ArupComputeResultItem()
ri_pow.value = x ** y
ri_pow.symbol = "p"
acr.arupComputeResultItems.append(ri_pow)
return acr
Errors, warnings, and remarks
Sometimes we may want to raise specific warning messages to alert the user. ArupCompute uses a three-level system that are displayed in special ways throughout the ecosystem. For example when an error is raised it will turn the Grasshopper components red.
An example function that uses all the features is below.
def ewr(x: float) -> ArupComputeResult:
"""Errors, warnings and remarks
Showcases the errors, warnings and remarks system of ArupCompute. Enter a number and receive your judgement!
Args:
x: The number to test
Returns:
j: Your judgement
"""
acr = ArupComputeResult()
judgement = ""
if(x < 0):
judgement = "Oh dear, you entered a negative number!"
acr.errors.append(judgement)
if(x == 0):
judgement = "You entered 0, just on the edge!"
acr.warnings.append(judgement)
if(x > 0):
judgement = "Well done you entered a positive number!"
acr.remarks.append(judgement)
ri = ArupComputeResultItem()
ri.value = judgement
ri.symbol = "j"
acr.arupComputeResultItems.append(ri)
return acr
Report output
ArupCompute lets you output reports in two different formats: plain text, and HTML. The HTML is more widely used, although the plain text report can be useful in environments that dont support HTML styling (e.g. to output directly in command line programs, or to print directly into Excel cells).
An example function is provided below:
def greet(name: str) -> ArupComputeResult:
"""Greet
Greets you
Args:
name: Your name
Returns:
g: The greeting you have received
"""
greeting = f'Great to see you {name}!'
greetingResult = ArupComputeResultItem()
greetingResult.value = greeting
greetingResult.symbol = "g"
acr = ArupComputeResult()
acr.arupComputeResultItems.append(greetingResult)
acr.arupComputeReport = greeting
acr.arupComputeReport_HTML = f'<p>{greeting}</p>'
return acr
Which in the web-ui produces a very plain looking report, but with the full flexibility of HTML at your fingertips you can produce almost anything that you need.