Skip to content

Commit ce919f6

Browse files
committed
Context/Variable serialization. Now uses streams
1 parent 7ee29b5 commit ce919f6

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

isc/py/data/Context.cls

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ Method PopulateHistory() As %Status
4444
/// Get modules and their aliases
4545
Method PopulateModules() As %Status
4646
{
47+
#dim sc As %Status = $$$OK
4748
set modules = ##class(isc.py.Callout).SimpleString("zzzmodules=json.dumps(list(zzzmodulesfunc()))", "zzzmodules")
4849
do ##class(isc.py.Callout).SimpleString("del zzzmodules")
49-
5050
set modules = {}.%FromJSON(modules)
51+
5152
set iterator = modules.%GetIterator()
5253
while iterator.%GetNext(.key, .value) {
5354
set module = value.%Get(0)
5455
set alias = value.%Get(1)
5556
continue:module="builtins"
5657
do ..Modules.SetAt(alias, module)
5758
}
59+
60+
quit sc
5861
}
5962

6063
/// Get variables and their values.
@@ -66,6 +69,7 @@ Method PopulateVariables() As %Status
6669
do ##class(isc.py.Callout).SimpleString("del zzzvars")
6770

6871
set variables = {}.%FromJSON(variables)
72+
6973
set iterator = variables.%GetIterator()
7074
while iterator.%GetNext(.key, .variable) {
7175
#dim varObj As isc.py.data.Varable
@@ -93,15 +97,20 @@ Method Init()
9397
}
9498

9599
/// Save Python context on disk
100+
/// context - resulting context
101+
/// maxLength - maximum length of saved variable. If veriable serialization is longer than that, it would be ignored. Overrides mask. Set to 0 to get them all.
102+
/// mask - commaseparated list of variables to save. Special symbols * and ? are recognized.
96103
/// set sc=##class(isc.py.data.Context).SaveContext()
97-
ClassMethod SaveContext(Output context As isc.py.data.Context, verbose As %Boolean = {$$$NO}) As %Status
104+
ClassMethod SaveContext(Output context As isc.py.data.Context, maxLength As %Integer = {$$$MaxStringLength}, mask As %String = "*", verbose As %Boolean = {$$$NO}) As %Status
98105
{
99106
kill context
100107
set context = ..%New()
101108
do context.Init()
102109
//do obj.PopulateHistory()
103-
do context.PopulateModules()
104-
do context.PopulateVariables()
110+
set sc = context.PopulateModules()
111+
quit:$$$ISERR(sc) sc
112+
set sc = context.PopulateVariables()
113+
quit:$$$ISERR(sc) sc
105114
do:verbose context.Display()
106115

107116
quit context.%Save()

isc/py/data/Variable.cls

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,55 @@
22
Class isc.py.data.Variable Extends %Persistent
33
{
44

5+
Parameter DisplayLimit = 100;
6+
57
/// Variable Name
68
Property Name As %String;
79

810
/// Variable Class
911
Property Type As %String;
1012

1113
/// Variable repr serialization
12-
Property Value As %VarString;
14+
Property Value As %Stream.GlobalCharacter;
1315

1416
/// Variable pickle serialization
15-
Property Pickle As %VarString;
17+
Property Pickle As %Stream.GlobalCharacter;
1618

1719
/// Variable JSON serialization
18-
Property JSON As %VarString;
20+
Property JSON As %Stream.GlobalCharacter;
1921

2022
/// Save variable on disk.
2123
/// zw ##class(isc.py.data.Variable).SaveVariable()
22-
ClassMethod SaveVariable(name As %String = "", Output variable As isc.py.data.Variable) As %Status
24+
ClassMethod SaveVariable(variable As %String = "", Output obj As isc.py.data.Variable) As %Status
2325
{
24-
kill variable
25-
quit:name="" $$$ERROR($$$GeneralError, "Variable name can't be empty")
26-
27-
set repr = ##class(isc.py.Callout).SimpleString(,name, 1)
28-
quit:repr="" $$$ERROR($$$GeneralError, $$$FormatText("Variable %1 is empty or initialized", name))
26+
#include Ensemble
27+
kill obj
28+
quit:variable="" $$$ERROR($$$GeneralError, "Variable name can't be empty")
2929

30-
set type = ##class(isc.py.Callout).SimpleString("zzztype=type("_name_").__name__", "zzztype")
31-
do ##class(isc.py.Callout).SimpleString("import json, pickle;")
30+
//$$$TRACE(variable)
3231

33-
if type = "DataFrame" {
34-
set json = ##class(isc.py.Callout).SimpleString("zzzjson=" _name _ ".to_json()", "zzzjson")
35-
} elseif type="DatetimeIndex" {
36-
/// TODO
37-
} else {
38-
set json = ##class(isc.py.Callout).SimpleString("zzzjson=json.dumps(" _name _ ")", "zzzjson")
39-
}
32+
set sc = ##class(isc.py.Main).GetVariableInfo(variable, ##class(isc.py.Callout).#SerializationRepr, .defined, .type, .length)
33+
quit:$$$ISERR(sc) sc
34+
quit:'defined $$$ERROR($$$GeneralError, "Variable '" _ variable _ "' is not defined")
4035

41-
set pickle = ##class(isc.py.Callout).SimpleString("zzzjson=pickle.dumps(" _name _ ")", "zzzjson")
36+
#dim repr, pickle, json As %Stream.GlobalCharacter
37+
38+
set sc = ##class(isc.py.Main).GetVariable(variable, ##class(isc.py.Callout).#SerializationRepr, .repr)
39+
quit:$$$ISERR(sc) sc
4240

41+
set sc = ##class(isc.py.Main).GetVariableJson(variable, .json)
42+
quit:$$$ISERR(sc) sc
4343

44-
do ##class(isc.py.Callout).SimpleString("del zzztype, zzzjson")
44+
set sc = ##class(isc.py.Main).GetVariablePickle(variable, .pickle)
45+
quit:$$$ISERR(sc) sc
4546

46-
set variable = ..%New()
47-
set variable.Name = name
48-
set variable.Value = repr
49-
set variable.Type = type
50-
set variable.Pickle = pickle
51-
set variable.JSON = $g(json)
52-
set sc = variable.%Save()
47+
set obj = ..%New()
48+
set obj.Name = variable
49+
set obj.Value = repr
50+
set obj.Type = type
51+
set obj.Pickle = pickle
52+
set obj.JSON = json
53+
set sc = obj.%Save()
5354

5455
quit sc
5556
}
@@ -58,21 +59,24 @@ ClassMethod SaveVariable(name As %String = "", Output variable As isc.py.data.Va
5859
Method Display(indent As %String = "")
5960
{
6061
write indent, "Name: ", ..Name, !
61-
write indent, "Type: ", ..Type, !
62-
set limit = 100
63-
If $l(..Value)<=limit {
64-
Write indent, "Value: ", ..Value, !
65-
} Else {
66-
Write indent, "Value (truncated): ", $replace($e(..Value,1,limit),$c(10), $c(13,10)), !
67-
}
68-
write !
62+
write indent, "Type: ", ..Type, !
63+
write indent, "Value", $case(..Value.Size<=..#DisplayLimit, $$$YES:"", $$$NO:" (truncated)"), ": ", ..Value.Read(..#DisplayLimit), !, !
6964
}
7065

7166
/// Restore variable from disk.
7267
/// context - variable owner Python context object.
73-
Method Restore(context As isc.py.data.Context)
68+
Method Restore(context As isc.py.data.Context) As %Status
7469
{
75-
do ##class(isc.py.Callout).SimpleString(..Name _ "=" _ context.%PickleAlias _ ".loads(" _ ..Pickle _ ")")
70+
#dim sc As %Status = $$$OK
71+
if ..Pickle.Size > 0 {
72+
set stream = ##class(%Stream.TmpCharacter).%New()
73+
do stream.Write(context.%PickleAlias _ ".loads(")
74+
do stream.CopyFrom(..Pickle)
75+
do stream.Write(")")
76+
set sc = ##class(isc.py.Main).ExcuteCode(stream, ..Name)
77+
}
78+
79+
quit sc
7680
}
7781

7882
Storage Default

0 commit comments

Comments
 (0)