Change calculator example to use new dict syntax for a typeless struct

This commit is contained in:
Jason Paryani
2014-01-13 16:50:02 -08:00
parent e7e95abeed
commit 0930a9fd1b

View File

@@ -53,13 +53,19 @@ def main(host):
print('Evaluating a literal... ', end="") print('Evaluating a literal... ', end="")
# Set up the request. Note the form is 'evaluate' + '_request', where # Make the request. Note we are using the shorter function form (instead
# 'evaluate' is the name of the method we want to call # of evaluate_request), and we are passing a dictionary that represents a
# struct and its member to evaluate
eval_promise = calculator.evaluate({"literal": 123})
# This is equivalent to:
'''
request = calculator.evaluate_request() request = calculator.evaluate_request()
request.expression.literal = 123 request.expression.literal = 123
# Send it, which returns a promise for the result (without blocking). # Send it, which returns a promise for the result (without blocking).
eval_promise = request.send() eval_promise = request.send()
'''
# Using the promise, create a pipelined request to call read() on the # Using the promise, create a pipelined request to call read() on the
# returned object. Note that here we are using the shortened method call # returned object. Note that here we are using the shortened method call
@@ -88,7 +94,8 @@ def main(host):
# Get the "subtract" function from the server. # Get the "subtract" function from the server.
subtract = calculator.getOperator(op='subtract').func subtract = calculator.getOperator(op='subtract').func
# Build the request to evaluate 123 + 45 - 67. # Build the request to evaluate 123 + 45 - 67. Note the form is 'evaluate'
# + '_request', where 'evaluate' is the name of the method we want to call
request = calculator.evaluate_request() request = calculator.evaluate_request()
subtract_call = request.expression.init('call') subtract_call = request.expression.init('call')
subtract_call.function = subtract subtract_call.function = subtract
@@ -110,6 +117,22 @@ def main(host):
print("PASS") print("PASS")
'''
Note: a one liner version of building the previous request (I highly
recommend not doing it this way, but I just wanted to demonstrate it is
possible to set all of the fields with a dictionary):
eval_promise = calculator.evaluate({"call":
{"function": subtract,
"params": [{"call":
{"function": add,
"params":
[{"literal": 123},
{"literal": 45}]
}},
{"literal": 67.0}]}})
'''
'''Make a request to evaluate 4 * 6, then use the result in two more '''Make a request to evaluate 4 * 6, then use the result in two more
requests that add 3 and 5. requests that add 3 and 5.