Skip to content

Commit 89ec4e3

Browse files
add embedded python methods
in dc.sample.PersistentClass
1 parent 01d1e94 commit 89ec4e3

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

src/dc/sample/ObjectScript.cls

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ClassMethod ObjectScript() As %Status
2525
set a=42
2626
write "Hello World! from ",$CLASSMETHOD("dc.sample.ObjectScript","%ClassName",1)," in ObjectScript language",!
2727
write "This is InterSystems IRIS with version ",$zv,!
28-
write "Current time is: "_$zdt($now(),2,,6),!
28+
write "Current time is: "_$zdt($now(),3,,6),!
2929
return a
3030
}
3131

@@ -36,8 +36,7 @@ from datetime import datetime
3636
a=42
3737
print("Hello World! from",iris.cls('dc.sample.ObjectScript')._ClassName(1),"in Python language")
3838
print("This is InterSystems IRIS with version",iris.cls('%SYSTEM.Version').GetVersion())
39-
now = datetime.now()
40-
print("Current time is:",now)
39+
print("Current time is:",datetime.now())
4140
return a
4241
}
4342

src/dc/sample/PersistentClass.cls

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,90 @@ Class dc.sample.PersistentClass Extends %Persistent
44
Property Test As %VarString;
55

66
ClassMethod CreateRecord(ByRef id As %Integer) As %Status
7+
{
8+
set sc=$$$OK
9+
Try {
10+
write "from Python : "
11+
set builtins = ##class(%SYS.Python).Import("builtins")
12+
set id = builtins.list()
13+
#; set id=0
14+
set sc = ..CreateRecordPython(id)
15+
write id."__getitem__"(0)," successfully created with CreateRecordPython",!
16+
write "from ObjectScript : "
17+
set sc = ..CreateRecordObjesctScript(.id)
18+
write id," successfully created with CreateRecordObjesctScript",!
19+
}
20+
Catch ex {
21+
Set tSC=ex.AsStatus()
22+
}
23+
24+
return sc
25+
}
26+
27+
ClassMethod CreateRecordObjesctScript(ByRef id As %Integer) As %Status
728
{
829
set sc=$$$OK
930
set objPC=..%New()
10-
set objPC.Test="Test string"
31+
set objPC.Test="Test string from CreateRecordObjectScript() "_$zdt($now(),3,,6)
1132
set sc=objPC.%Save()
1233
set id=objPC.%Id()
1334
return sc
1435
}
1536

16-
/// opens the record by id and reads its property
37+
ClassMethod CreateRecordPython(id) As %Status [ Language = python ]
38+
{
39+
import iris
40+
from datetime import datetime
41+
42+
objPC=iris.cls('dc.sample.PersistentClass')._New()
43+
objPC.Test="Test string from CreateRecordPython() "+str(datetime.now())
44+
sc=objPC._Save()
45+
id.append(objPC._Id())
46+
return sc
47+
}
48+
1749
ClassMethod ReadProperty(id As %Integer) As %Status
50+
{
51+
52+
Set sc = $$$OK
53+
Try {
54+
write "from Python : "
55+
set sc = ..ReadPropertyPython(id)
56+
write "from ObjectScript : "
57+
set sc = ..ReadPropertyObjectScript(id)
58+
}
59+
Catch ex {
60+
Set tSC=ex.AsStatus()
61+
}
62+
Return sc
63+
}
64+
65+
/// opens the record by id and reads its property
66+
ClassMethod ReadPropertyObjectScript(id As %Integer) As %Status
1867
{
1968
Set sc = $$$OK
2069
#dim obj as dc.sample.PersistentClass
21-
set obj=..%OpenId(id,,.sc)
22-
if $ISOBJECT(obj) write obj.Test
70+
if ..%ExistsId(id) {
71+
set obj=..%OpenId(id,,.sc)
72+
if $ISOBJECT(obj) write obj.Test,!
73+
} else {
74+
write id," is not an existing ID in dc.sample.PersistentClass",!
75+
}
2376
Return sc
2477
}
2578

79+
ClassMethod ReadPropertyPython(id As %Integer) As %Status [ Language = python ]
80+
{
81+
import iris
82+
sc=1
83+
if iris.cls('dc.sample.PersistentClass')._ExistsId(id):
84+
obj=iris.cls('dc.sample.PersistentClass')._OpenId(id)
85+
print(obj.Test)
86+
else:
87+
print(id,'is not an existing ID in dc.sample.PersistentClass')
88+
return sc
89+
}
90+
2691
Storage Default
2792
{
2893
<Data name="PersistentClassDefaultData">

tests/dc/sample/unittests/TestPersistentClass.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Method TestCreateRecord()
88
do $$$AssertStatusOK(status,"CreateRecord")
99
set obj=##class(dc.sample.PersistentClass).%OpenId(id)
1010
if $IsObject(obj) {
11-
set tResults=obj.Test}
11+
set tResults=$extract(obj.Test,1,$LENGTH(tExpected))}
1212
else {set tResults="There is no such record with id="_id}
1313
Do $$$AssertEquals(tResults,tExpected,tExpected_" = "_tResults)
1414
}

0 commit comments

Comments
 (0)