Skip to content

Commit 551209a

Browse files
committed
ProxylessGateway: creation from existing variables, cast to JSON, string, Pickle, Dynamic object
For #40
1 parent b81e4b6 commit 551209a

File tree

1 file changed

+56
-10
lines changed

1 file changed

+56
-10
lines changed

isc/py/gw/DynamicObject.cls

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
Class isc.py.gw.DynamicObject Extends %RegisteredObject
33
{
44

5-
Property %Name As %String(MAXLEN = 1000);
5+
/// Name of python variable holding the object
6+
Property %Variable As %String(MAXLEN = 1000);
67

8+
/// Python type
79
Property %Type As %String(MAXLEN = 1000);
810

11+
/// Default serialization for methods
912
Property %Serialization As %Integer;
1013

1114
/// do ##class(isc.py.init.Test).Initialize(,1)
@@ -16,21 +19,32 @@ Property %Serialization As %Integer;
1619
/// w obj.age
1720
/// w obj.getAge()
1821
/// w obj.getAgePlus(10)
19-
Method %OnNew(type, variable, serialization As %Integer = {##class(isc.py.Callout).#SerializationStr}, args...) As %Status [ Private, ServerOnly = 1 ]
22+
Method %OnNew(type As %String = "", variable As %String, serialization As %Integer = {##class(isc.py.Callout).#SerializationStr}, args...) As %Status [ Private, ServerOnly = 1 ]
2023
{
21-
set ..%Name = variable
22-
set ..%Type = type
24+
set ..%Variable = variable
2325
set ..%Serialization = serialization
24-
quit ##class(isc.py.Main).ExecuteFunctionArgs(type, variable,,, args...)
26+
27+
if type'="" {
28+
// Create variable of type
29+
set ..%Type = type
30+
set sc = ##class(isc.py.Main).ExecuteFunctionArgs(type, variable,,, args...)
31+
} else {
32+
// Populate variable info
33+
set sc = ##class(isc.py.Main).GetVariableInfo(variable, ,.defined, .type)
34+
throw:'defined ##class(%Exception.General).%New("<VARIABLE>", variable)
35+
set ..%Type = $g(type)
36+
}
37+
38+
quit sc
2539
}
2640

2741
/// Get serialized property value
2842
Method %DispatchGetProperty(property As %String) [ ServerOnly = 1 ]
2943
{
30-
$$$TOE(sc, ##class(isc.py.Main).GetVariableInfo(..%Name _ "." _ property, ..%Serialization, .defined, .type, .length))
44+
$$$TOE(sc, ##class(isc.py.Main).GetVariableInfo(..%Variable _ "." _ property, ..%Serialization, .defined, .type, .length))
3145
throw:'defined ##class(%Exception.General).%New("<PROPERTY>", property)
3246

33-
$$$TOE(sc, ##class(isc.py.Main).SimpleString("zzzproperty=" _ ..%Name _ "." _ property, "zzzproperty", ..%Serialization, .zzzproperty))
47+
$$$TOE(sc, ##class(isc.py.Main).SimpleString("zzzproperty=" _ ..%Variable _ "." _ property, "zzzproperty", ..%Serialization, .zzzproperty))
3448
$$$TOE(sc, ##class(isc.py.Main).SimpleString("del zzzproperty"))
3549

3650
quit zzzproperty
@@ -39,20 +53,52 @@ Method %DispatchGetProperty(property As %String) [ ServerOnly = 1 ]
3953
/// Set python object property
4054
Method %DispatchSetProperty(property As %String, val) [ ServerOnly = 1 ]
4155
{
42-
$$$TOE(sc, ##class(isc.py.Main).GetVariableInfo(..%Name _ "." _ property, ..%Serialization, .defined, .type, .length))
56+
$$$TOE(sc, ##class(isc.py.Main).GetVariableInfo(..%Variable _ "." _ property, ..%Serialization, .defined, .type, .length))
4357
throw:'defined ##class(%Exception.General).%New("<PROPERTY>", property)
4458

45-
set arguments = $lb(..%Name, ##class(isc.py.util.Converter).EscapeString(property), ##class(isc.py.util.Converter).EscapeString(val))
59+
set arguments = $lb(..%Variable, ##class(isc.py.util.Converter).EscapeString(property), ##class(isc.py.util.Converter).EscapeString(val))
4660

4761
$$$TOE(sc, ##class(isc.py.Main).ExecuteFunction("setattr", arguments))
4862
}
4963

5064
/// Call python function
5165
Method %DispatchMethod(method As %String, args...) [ ServerOnly = 1 ]
5266
{
53-
$$$TOE(sc, ##class(isc.py.Main).ExecuteFunctionArgs(..%Name _ "." _ method, ,..%Serialization, .result, args...))
67+
$$$TOE(sc, ##class(isc.py.Main).ExecuteFunctionArgs(..%Variable _ "." _ method, ,..%Serialization, .result, args...))
5468
quit result
5569
}
5670

71+
/// Convert Python object to JSON
72+
Method %ToJSON(ByRef stream As %Stream.Object, useString As %Boolean = {$$$NO}) As %Status [ CodeMode = expression ]
73+
{
74+
##class(isc.py.Main).GetVariableJson(..%Variable, .stream, useString)
75+
}
76+
77+
/// Convert Python object to InterSystems IRIS Dynamic object
78+
Method %ToDynObj(Output obj As %DynamicObject) As %Status
79+
{
80+
set sc = ..%ToJSON(.stream)
81+
quit:$$$ISERR(sc) sc
82+
try {
83+
set obj = {}.%FromJSON(stream)
84+
} catch ex {
85+
set sc = ex.AsStatus()
86+
}
87+
88+
quit sc
89+
}
90+
91+
/// Convert Python object to Pickle or Dill
92+
Method %ToPickle(ByRef stream As %Stream.Object, useString As %Boolean = {$$$NO}, useDill As %Boolean = {$$$NO}) As %Status [ CodeMode = expression ]
93+
{
94+
##class(isc.py.Main).GetVariablePickle(..%Variable, .stream, useString, useDill)
95+
}
96+
97+
/// Convert Python object to string
98+
Method %ToStream(serialization As %Integer = {..%Serialization}, ByRef stream As %Stream.Object, useString As %Boolean = {$$$NO}) As %Status [ CodeMode = expression ]
99+
{
100+
##class(isc.py.Main).GetVariable(..%Variable, serialization, .stream, useString)
101+
}
102+
57103
}
58104

0 commit comments

Comments
 (0)