Skip to content

Commit 2627eb0

Browse files
authored
Proxyless Gateway docs
1 parent 07a9a75 commit 2627eb0

File tree

1 file changed

+120
-6
lines changed

1 file changed

+120
-6
lines changed

Gateway.md

Lines changed: 120 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,39 @@ There are three main parts:
66
- Proxyless Gateway
77
- Proxy Gateway
88

9-
109
# Execute function
1110

12-
Consists of `ExecuteFunction` method from `isc.py.Main` class. Signature:
13-
- `function` - name of function to invoke. Can be nested, i.e. `random.randint`
11+
Executes function by name. This API consists of two methods:
12+
- `ExecuteFunction`
13+
- `ExecuteFunctionArgs`
14+
15+
The difference between them is caller signature. `ExecuteFunction` accepts %List, %Collection.AbstractArray and JSON object separated into positional and keyword arguments. `ExecuteFunctionArgs` accepts `args...` and parses them into positional and keyword arguments. After that `ExecuteFunctionArgs` calls `ExecuteFunction`.
16+
17+
It is caller responsibility to escape argument values. Use `isc.py.util.Converter` class to escape:
18+
- string
19+
- boolean
20+
- date
21+
- time
22+
- timestamp
23+
24+
## ExecuteFunction
25+
26+
`ExecuteFunction` method from `isc.py.Main` class. Signature:
27+
- `function` - name of function to invoke. Can be nested, i.e. `random.randint`
28+
- `variable` - name of python variable to write result to.
1429
- `positionalArguments` - positional argumets for Python function. Can be one of:
1530
+ `$lb(val1, val2, ..., valN)`
1631
+ `%Collection.AbstractIterator` object
1732
+ JSON array
18-
- keywordArguments - keyword argumets for Python function. Can be one of:
33+
- `keywordArguments` - keyword argumets for Python function. Can be one of:
1934
+ `$lb($lb(name1, val1), $lb(name2, val2), ..., $lb(nameN, valN))`
2035
+ `%Collection.AbstractArray` object
2136
+ flat JSON object
2237
- `serialization` - how to serialize result
2338
- `result` - write result into this variable
2439

40+
All arguments besides `function` are optional.
41+
2542
Here's an example of how it works:
2643

2744
```
@@ -35,7 +52,6 @@ set posDynamic = [1, 100]
3552
3653
for positionalArguments = posList,posCollection,posDynamic {
3754
set sc = ##class(isc.py.Main).ExecuteFunction(random _ ".randint", positionalArguments,,,.result)
38-
3955
write result,!
4056
}
4157
@@ -47,7 +63,6 @@ set kwDynamic = { "a": 1, "b": 100}
4763
4864
for kwArguments = kwList,kwCollection,kwDynamic {
4965
set sc = ##class(isc.py.Main).ExecuteFunction(random _ ".randint", ,kwArguments,,.result)
50-
5166
write result,!
5267
}
5368
@@ -62,3 +77,102 @@ set kwDynamic = {"name":(##class(isc.py.util.Converter).EscapeString("Alice")),
6277
set sc = ##class(isc.py.Main).ExecuteFunction("str.format", posList, kwDynamic,,.result)
6378
write result,!
6479
```
80+
81+
## ExecuteFunctionArgs
82+
83+
`ExecuteFunctionArgs` method from `isc.py.Main` class. Signature:
84+
- `function` - name of function to invoke. Can be nested, i.e. `random.randint`
85+
- `variable` - name of python variable to write result to.
86+
- `serialization` - how to serialize result
87+
- `result` - write result into this variable
88+
- `args...` - function arguments.
89+
90+
`ExecuteFunctionArgs` attempts to determine correct positional and keyword arguments from function signature (if available). It is recommended to call `ExecuteFunction` directly if `ExecuteFunctionArgs` is unable to construct a correct argument spec (and open an issue). Example:
91+
92+
```
93+
set sc = ##class(isc.py.Main).ImportModule("random", ,.random)
94+
set sc = ##class(isc.py.Main).ExecuteFunctionArgs(random _ ".randint", , ,.result, 1, 100)
95+
write result,!
96+
97+
set string = ##class(isc.py.util.Converter).EscapeString("Positional: {0}, {1}, {2}, {3}")
98+
set arg1 = ##class(isc.py.util.Converter).EscapeString("Hello")
99+
set arg2 = ##class(isc.py.util.Converter).EscapeString("World")
100+
set arg3 = ##class(isc.py.util.Converter).EscapeString("Alice")
101+
set arg4 = ##class(isc.py.util.Converter).EscapeString("Bob")
102+
set sc = ##class(isc.py.Main).ExecuteFunctionArgs("str.format",,,.result, string, arg1, arg2, arg3, arg4)
103+
write result,!
104+
105+
set string = ##class(isc.py.util.Converter).EscapeString("Positional: {0} {1}! Keyword: {name}, {name2}")
106+
set arg1 = ##class(isc.py.util.Converter).EscapeString("Hello")
107+
set arg2 = ##class(isc.py.util.Converter).EscapeString("World")
108+
set kwargs = "**" _ {"name":"Alice","name2":"Bob"}.%ToJSON()
109+
set sc = ##class(isc.py.Main).ExecuteFunctionArgs("str.format",,, .result, string, arg1, arg2, kwargs)
110+
write result,!
111+
```
112+
113+
# Proxyless Gateway
114+
115+
Proxyless gateway allows user to bind Python variables to InterSystems IRIS variables.
116+
This allows user to:
117+
- Get/Set object properties
118+
- Call object methods
119+
- Serialize variable to: Str, Repr, Pickle, Dill, JSON, Dynamic Object.
120+
121+
Example.
122+
123+
1. Load Python class `Person`: `do ##class(isc.py.init.Test).Initialize(,1)`
124+
125+
Note: here's `Person` class definition for reference:
126+
```
127+
class Person(object):
128+
def __init__(self, name, age, city):
129+
self.name = name
130+
self.age = age
131+
self.city = city
132+
def getAge(self):
133+
return self.age
134+
def getAgePlus(self, add):
135+
return self.age + add
136+
```
137+
138+
2. Create Proxy variable: `set obj = ##class(isc.py.gw.DynamicObject).%New("Person", "p1", , "'Ed'", "25", "'Test'")`
139+
In this call we create Python variable `p1` of `Person` class and pass three methods to constructor `'Ed'`, `25` and `'Test'`.
140+
141+
3. Now we can interact with the object, let's get and set some properties:
142+
```
143+
write obj.name
144+
set obj.name="Bob"
145+
write obj.name
146+
write obj.age
147+
```
148+
4. We can set some new properties too (unlike `ExecuteFunction` values are escaped automatically if `%EscapeOnSet` property is 1, which is default. You can also set properties to other dynamic objects. In that case unsecaped python variable name would be used):
149+
```
150+
set obj.pet = "Dog"
151+
write obj.pet
152+
```
153+
154+
5. And we can call object methods:
155+
156+
```
157+
write obj.getAge()
158+
write obj.getAgePlus(10)
159+
```
160+
161+
6. Finally we can convert object:
162+
```
163+
set sc = obj.%ToJSON(.json)
164+
set sc = obj.%ToDynObj(.dynObj)
165+
set sc = obj.%ToPickle(.pickle)
166+
set sc = obj.%ToStream(,.stream)
167+
```
168+
169+
To create proxy object from existing proxy object just skip type argument:
170+
```
171+
kill obj
172+
set p1 = ##class(isc.py.gw.DynamicObject).%New(, "p1")
173+
```
174+
175+
# Proxy Gateway
176+
177+
Under development.
178+

0 commit comments

Comments
 (0)