Skip to content

Commit

Permalink
Update FullScriptSupport example with table load
Browse files Browse the repository at this point in the history
  • Loading branch information
josefinestal committed Dec 28, 2017
1 parent ca17e8b commit 8b8f52c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def GetCapabilities(self, request, context):
# Set values for pluginIdentifier and pluginVersion
capabilities = SSE.Capabilities(allowScript=True,
pluginIdentifier='Full Script Support - Qlik',
pluginVersion='v1.0.0-beta1')
pluginVersion='v1.1.0')

return capabilities

Expand Down
Binary file modified examples/python/FullScriptSupport/SSE_Full_Script_Support.qvf
Binary file not shown.
Binary file not shown.
66 changes: 37 additions & 29 deletions examples/python/FullScriptSupport/ScriptEval_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,32 @@ def EvaluateScript(self, header, request, context):
logging.info('EvaluateScript: {} ({} {}) {}'
.format(header.script, arg_types, ret_type, func_type))

aggr = (func_type == FunctionType.Aggregation)

# Check if parameters are provided
if header.params:
# Create an empty list if tensor function
if aggr:
all_rows = []
all_rows = []

# Iterate over bundled rows
for request_rows in request:
# Iterate over rows
for row in request_rows.rows:
# Retrieve parameters
params = self.get_arguments(context, arg_types, row.duals, header)
all_rows.append(params)

# First element in the parameter list should contain the data of the first parameter.
all_rows = [list(param) for param in zip(*all_rows)]

if arg_types == ArgType.Mixed:
param_datatypes = [param.dataType for param in header.params]
for i, datatype in enumerate(param_datatypes):
if datatype == SSE.DUAL:
# For easier access to the numerical and string representation of duals, in the script, we
# split them to two list. For example, if the first parameter is dual, it will contain two lists
# the first one being the numerical representation and the second one the string.
all_rows[i] = [list(datatype) for datatype in zip(*all_rows[i])]

if aggr:
# Append value to list, for later aggregation
all_rows.append(params)
else:
# Evaluate script row wise
yield self.evaluate(header.script, ret_type, params=params)

# Evaluate script based on data from all rows
if aggr:
# First element in the parameter list should contain the data of the first parameter.
params = [list(param) for param in zip(*all_rows)]
if arg_types == ArgType.Mixed:
# Each parameter list should contain two lists, the first one consisting of numerical
# representations and the second one, the string representations.
params = [[list(datatype) for datatype in zip(*param)] for param in params]
yield self.evaluate(header.script, ret_type, params=params)
logging.debug('Received data from Qlik (args): {}'.format(all_rows))
yield self.evaluate(header.script, ret_type, params=all_rows)

else:
# No parameters provided
Expand Down Expand Up @@ -159,7 +154,17 @@ def get_return_type(header):
return ReturnType.Undefined

@staticmethod
def evaluate(script, ret_type, params=[]):
def get_duals(result, ret_type):
if isinstance(result, str) or not hasattr(result, '__iter__'):
result = [result]
# Transform the result to an iterable of Dual data
if ret_type == ReturnType.String:
duals = [SSE.Dual(strData=col) for col in result]
elif ret_type == ReturnType.Numeric:
duals = [SSE.Dual(numData=col) for col in result]
return iter(duals)

def evaluate(self, script, ret_type, params=[]):
"""
Evaluates a script with given parameters and construct the result to a Row of duals.
:param script: script to evaluate
Expand All @@ -168,14 +173,17 @@ def evaluate(script, ret_type, params=[]):
:return: a RowData of string dual
"""
# Evaluate script
print(params)
result = eval(script, {'args': params, 'numpy': numpy})
logging.debug('Result: {}'.format(result))

# Transform the result to an iterable of Dual data
if ret_type == ReturnType.String:
duals = iter([SSE.Dual(strData=result)])
elif ret_type == ReturnType.Numeric:
duals = iter([SSE.Dual(numData=result)])
bundledRows = SSE.BundledRows()
if isinstance(result, str) or not hasattr(result, '__iter__'):
# A single value is returned
bundledRows.rows.add(duals=self.get_duals(result, ret_type))
else:
for row in result:
# note that each element of the result should represent a row
bundledRows.rows.add(duals=self.get_duals(row, ret_type))

return SSE.BundledRows(rows=[SSE.Row(duals=duals)])
return bundledRows

4 changes: 2 additions & 2 deletions examples/python/test/test_fullscriptsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_getcapabilities(self):

assert capabilities.allowScript is True
assert capabilities.pluginIdentifier == 'Full Script Support - Qlik'
assert capabilities.pluginVersion == 'v1.0.0-beta1'
assert capabilities.pluginVersion == 'v1.1.0'

def test_evaluatescript(self):
"""
Expand All @@ -44,7 +44,7 @@ def test_evaluatescript(self):
"""
params = to_numeric_parameters('num1', 'num2')

header = SSE.ScriptRequestHeader(script='args[0] + args[1]',
header = SSE.ScriptRequestHeader(script='sum(args[0] + args[1])',
functionType=SSE.TENSOR,
returnType=SSE.NUMERIC,
params=params)
Expand Down

0 comments on commit 8b8f52c

Please sign in to comment.