Skip to content

Commit 773528f

Browse files
committed
Use pickle for variable serialization
1 parent b35163c commit 773528f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

isc/py/data/Varable.cls

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ Property Name As %String;
88
/// Variable Class
99
Property Type As %String;
1010

11-
/// Variable repr
11+
/// Variable repr serialization
1212
Property Value As %VarString;
1313

14+
/// Variable pickle serialization
15+
Property Pickle As %VarString;
16+
1417
/// Variable JSON serialization
1518
Property JSON As %VarString;
1619

@@ -25,22 +28,24 @@ ClassMethod SaveVariable(name As %String = "", Output variable As isc.py.data.Va
2528
quit:repr="" $$$ERROR($$$GeneralError, $$$FormatText("Variable %1 is empty or initialized", name))
2629

2730
set type = ##class(isc.py.Callout).SimpleString("zzztype=type("_name_").__name__", "zzztype")
28-
do ##class(isc.py.Callout).SimpleString("import json;")
31+
do ##class(isc.py.Callout).SimpleString("import json, pickle;")
32+
2933
if type = "DataFrame" {
30-
//set json = ##class(isc.py.Callout).SimpleString("zzzjson=json.dumps([list("_name_".index), list("_name_"), "_name_".values.tolist()])", "zzzjson")
3134
set json = ##class(isc.py.Callout).SimpleString("zzzjson=" _name _ ".to_json()", "zzzjson")
32-
set repr = ##class(isc.py.Callout).SimpleString("zzzjson=" _name _ ".to_dict()", "zzzjson")
3335
} else {
3436
set json = ##class(isc.py.Callout).SimpleString("zzzjson=json.dumps(" _name _ ")", "zzzjson")
3537
}
3638

39+
set pickle = ##class(isc.py.Callout).SimpleString("zzzjson=pickle.dumps(" _name _ ")", "zzzjson")
40+
3741

3842
do ##class(isc.py.Callout).SimpleString("del zzztype, zzzjson")
3943

4044
set variable = ..%New()
4145
set variable.Name = name
4246
set variable.Value = repr
4347
set variable.Type = type
48+
set variable.Pickle = pickle
4449
set variable.JSON = json
4550
set sc = variable.%Save()
4651

@@ -56,20 +61,15 @@ Method Display(indent As %String = "")
5661
If $l(..Value)<=limit {
5762
Write indent, "Value: ", ..Value, !
5863
} Else {
59-
Write indent, "Value (truncated): ", $e(..Value,1,limit), !
64+
Write indent, "Value (truncated): ", $replace($e(..Value,1,limit),$c(10), $c(13,10)), !
6065
}
6166
write !
6267
}
6368

6469
/// Restore variable for disk.
6570
Method Restore(context As isc.py.data.Context)
6671
{
67-
if ..Type = "DataFrame" {
68-
set pandas = context.Modules.GetAt("pandas")
69-
do ##class(isc.py.Callout).SimpleString(..Name _ "=" _ pandas _".DataFrame.from_dict(" _ ..Value _ ")")
70-
} else {
71-
do ##class(isc.py.Callout).SimpleString(..Name _ "=" _ ..Value)
72-
}
72+
do ##class(isc.py.Callout).SimpleString(..Name _ "=pickle.loads(" _ ..Pickle _ ")")
7373
}
7474

7575
Storage Default
@@ -90,6 +90,9 @@ Storage Default
9090
<Value name="5">
9191
<Value>JSON</Value>
9292
</Value>
93+
<Value name="6">
94+
<Value>Pickle</Value>
95+
</Value>
9396
</Data>
9497
<DataLocation>^isc.py.data.VarableD</DataLocation>
9598
<DefaultData>VarableDefaultData</DefaultData>

0 commit comments

Comments
 (0)