Skip to content

Commit

Permalink
Merge pull request #16 from SylvainGuilbaud/embedded-python
Browse files Browse the repository at this point in the history
Embedded python
  • Loading branch information
evshvarov authored Aug 17, 2023
2 parents 979dc52 + bebd3ef commit fe53816
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/dc/sample/ObjectScript.cls
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ClassMethod ObjectScript() As %Status
set a=42
write "Hello World! from ",$CLASSMETHOD("dc.sample.ObjectScript","%ClassName",1)," in ObjectScript language",!
write "This is InterSystems IRIS with version ",$zv,!
write "Current time is: "_$zdt($now(),2,,6),!
write "Current time is: "_$zdt($now(),3,,6),!
return a
}

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

Expand Down
72 changes: 68 additions & 4 deletions src/dc/sample/PersistentClass.cls
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,89 @@ Class dc.sample.PersistentClass Extends %Persistent
Property Test As %VarString;

ClassMethod CreateRecord(ByRef id As %Integer) As %Status
{
set sc=$$$OK
Try {
write "from Python : "
set builtins = ##class(%SYS.Python).Import("builtins")
set id = builtins.list()
set sc = ..CreateRecordPython(id)
write id."__getitem__"(0)," successfully created with CreateRecordPython",!
write "from ObjectScript : "
set sc = ..CreateRecordObjesctScript(.id)
write id," successfully created with CreateRecordObjesctScript",!
}
Catch ex {
Set tSC=ex.AsStatus()
}

return sc
}

ClassMethod CreateRecordObjesctScript(ByRef id As %Integer) As %Status
{
set sc=$$$OK
set objPC=..%New()
set objPC.Test="Test string"
set objPC.Test="Test string from CreateRecordObjectScript() "_$zdt($now(),3,,6)
set sc=objPC.%Save()
set id=objPC.%Id()
return sc
}

/// opens the record by id and reads its property
ClassMethod CreateRecordPython(id) As %Status [ Language = python ]
{
import iris
from datetime import datetime

objPC=iris.cls('dc.sample.PersistentClass')._New()
objPC.Test="Test string from CreateRecordPython() "+str(datetime.now())
sc=objPC._Save()
id.append(objPC._Id())
return sc
}

ClassMethod ReadProperty(id As %Integer) As %Status
{

Set sc = $$$OK
Try {
write "from Python : "
set sc = ..ReadPropertyPython(id)
write "from ObjectScript : "
set sc = ..ReadPropertyObjectScript(id)
}
Catch ex {
Set tSC=ex.AsStatus()
}
Return sc
}

/// opens the record by id and reads its property
ClassMethod ReadPropertyObjectScript(id As %Integer) As %Status
{
Set sc = $$$OK
#dim obj as dc.sample.PersistentClass
set obj=..%OpenId(id,,.sc)
if $ISOBJECT(obj) write obj.Test
if ..%ExistsId(id) {
set obj=..%OpenId(id,,.sc)
if $ISOBJECT(obj) write obj.Test,!
} else {
write id," is not an existing ID in dc.sample.PersistentClass",!
}
Return sc
}

ClassMethod ReadPropertyPython(id As %Integer) As %Status [ Language = python ]
{
import iris
sc=1
if iris.cls('dc.sample.PersistentClass')._ExistsId(id):
obj=iris.cls('dc.sample.PersistentClass')._OpenId(id)
print(obj.Test)
else:
print(id,'is not an existing ID in dc.sample.PersistentClass')
return sc
}

Storage Default
{
<Data name="PersistentClassDefaultData">
Expand Down
2 changes: 1 addition & 1 deletion tests/dc/sample/unittests/TestPersistentClass.cls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Method TestCreateRecord()
do $$$AssertStatusOK(status,"CreateRecord")
set obj=##class(dc.sample.PersistentClass).%OpenId(id)
if $IsObject(obj) {
set tResults=obj.Test}
set tResults=$extract(obj.Test,1,$LENGTH(tExpected))}
else {set tResults="There is no such record with id="_id}
Do $$$AssertEquals(tResults,tExpected,tExpected_" = "_tResults)
}
Expand Down

0 comments on commit fe53816

Please sign in to comment.