Skip to content

Commit fe53816

Browse files
authored
Merge pull request #16 from SylvainGuilbaud/embedded-python
Embedded python
2 parents 979dc52 + bebd3ef commit fe53816

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

src/dc/sample/ObjectScript.cls

Lines changed: 3 additions & 4 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

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

src/dc/sample/PersistentClass.cls

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,89 @@ 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 sc = ..CreateRecordPython(id)
14+
write id."__getitem__"(0)," successfully created with CreateRecordPython",!
15+
write "from ObjectScript : "
16+
set sc = ..CreateRecordObjesctScript(.id)
17+
write id," successfully created with CreateRecordObjesctScript",!
18+
}
19+
Catch ex {
20+
Set tSC=ex.AsStatus()
21+
}
22+
23+
return sc
24+
}
25+
26+
ClassMethod CreateRecordObjesctScript(ByRef id As %Integer) As %Status
727
{
828
set sc=$$$OK
929
set objPC=..%New()
10-
set objPC.Test="Test string"
30+
set objPC.Test="Test string from CreateRecordObjectScript() "_$zdt($now(),3,,6)
1131
set sc=objPC.%Save()
1232
set id=objPC.%Id()
1333
return sc
1434
}
1535

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

78+
ClassMethod ReadPropertyPython(id As %Integer) As %Status [ Language = python ]
79+
{
80+
import iris
81+
sc=1
82+
if iris.cls('dc.sample.PersistentClass')._ExistsId(id):
83+
obj=iris.cls('dc.sample.PersistentClass')._OpenId(id)
84+
print(obj.Test)
85+
else:
86+
print(id,'is not an existing ID in dc.sample.PersistentClass')
87+
return sc
88+
}
89+
2690
Storage Default
2791
{
2892
<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)