From 89ec4e3ae4ab6d6f37f95e7eedad637b19487879 Mon Sep 17 00:00:00 2001 From: SylvainGuilbaud Date: Thu, 17 Aug 2023 13:14:47 +0200 Subject: [PATCH 1/3] add embedded python methods in dc.sample.PersistentClass --- src/dc/sample/ObjectScript.cls | 5 +- src/dc/sample/PersistentClass.cls | 73 ++++++++++++++++++- .../sample/unittests/TestPersistentClass.cls | 2 +- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/src/dc/sample/ObjectScript.cls b/src/dc/sample/ObjectScript.cls index 22965dd..4bdfd7d 100644 --- a/src/dc/sample/ObjectScript.cls +++ b/src/dc/sample/ObjectScript.cls @@ -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 } @@ -36,8 +36,7 @@ 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("Current time is:",datetime.now()) return a } diff --git a/src/dc/sample/PersistentClass.cls b/src/dc/sample/PersistentClass.cls index cb12802..dc034ca 100644 --- a/src/dc/sample/PersistentClass.cls +++ b/src/dc/sample/PersistentClass.cls @@ -4,25 +4,90 @@ 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 id=0 + 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 { diff --git a/tests/dc/sample/unittests/TestPersistentClass.cls b/tests/dc/sample/unittests/TestPersistentClass.cls index d084d78..d66a545 100644 --- a/tests/dc/sample/unittests/TestPersistentClass.cls +++ b/tests/dc/sample/unittests/TestPersistentClass.cls @@ -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) } From 7555e232893d193943a628732d7faf2d9fb3a8e0 Mon Sep 17 00:00:00 2001 From: SylvainGuilbaud Date: Thu, 17 Aug 2023 13:21:01 +0200 Subject: [PATCH 2/3] iris.system.Version.GetVersion() --- src/dc/sample/ObjectScript.cls | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dc/sample/ObjectScript.cls b/src/dc/sample/ObjectScript.cls index 4bdfd7d..785f437 100644 --- a/src/dc/sample/ObjectScript.cls +++ b/src/dc/sample/ObjectScript.cls @@ -35,7 +35,7 @@ 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()) +print("This is InterSystems IRIS with version",iris.system.Version.GetVersion()) print("Current time is:",datetime.now()) return a } From bebd3ef414115c89e502a1d3b8f81e15c5bcc197 Mon Sep 17 00:00:00 2001 From: SylvainGuilbaud Date: Thu, 17 Aug 2023 13:42:23 +0200 Subject: [PATCH 3/3] removed a commented line --- src/dc/sample/PersistentClass.cls | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dc/sample/PersistentClass.cls b/src/dc/sample/PersistentClass.cls index dc034ca..77ce561 100644 --- a/src/dc/sample/PersistentClass.cls +++ b/src/dc/sample/PersistentClass.cls @@ -10,7 +10,6 @@ ClassMethod CreateRecord(ByRef id As %Integer) As %Status write "from Python : " set builtins = ##class(%SYS.Python).Import("builtins") set id = builtins.list() - #; set id=0 set sc = ..CreateRecordPython(id) write id."__getitem__"(0)," successfully created with CreateRecordPython",! write "from ObjectScript : "