文章目录

第五章 使用嵌入式 Python (二)

在 Python 脚本文件 (.py) 中

还可以使用 ​​irispython​​​ 命令执行 ​​Python​​ 脚本。

考虑 ​​Windows​​​ 系统上的文件 ​​C:\python\test.py​​,其中包含以下代码:

# print the members of the Fibonacci series that are less than 10
print('Fibonacci series:')
a, b = 0, 1
while a < 10:
print(a, end=' ')
a, b = b, a + b

# import the iris module and show the classes in this namespace
import iris
print('\nInterSystems IRIS classes in this namespace:')
status = iris.cls('%SYSTEM.OBJ').ShowClasses()
print(status)

可以从命令行运行 ​​test.py​​,如下所示:

C:\InterSystems\IRIS\bin>set IRISUSERNAME = <username>

C:\InterSystems\IRIS\bin>set IRISPASSWORD = <password>

C:\InterSystems\IRIS\bin>set IRISNAMESPACE = USER

C:\InterSystems\IRIS\bin>irispython \python\test.py
Fibonacci series:
0 1 1 2 3 5 8
InterSystems IRIS classes in this namespace:
User.Company
User.Person
1

在基于 ​​UNIX​​​ 的系统上,使用 ​​export​​​ 而不是 ​​set​​。

/InterSystems/IRIS/bin$ export IRISUSERNAME=<username>
/InterSystems/IRIS/bin$ export IRISPASSWORD=<password>
/InterSystems/IRIS/bin$ export IRISNAMESPACE=USER
/InterSystems/IRIS/bin$ ./irispython /python/test.py
Fibonacci series:
0 1 1 2 3 5 8
InterSystems IRIS classes in this namespace:
User.Company
User.Person
1

注意:如果运行 ​​import iris​​​ 并看到一条消息说 ​​IRIS_ACCESSDENIED​​​,请启用 ​​%Service_Callin​​​。在管理门户中,转至 ​​System Administration > Security > Services​​​,选择 ​​%Service_CallIn​​,然后选中启用服务框。

在 IRIS 类的方法中

可以使用 ​​Language​​​ 关键字在 ​​IRIS​​​ 类中编写 ​​Python​​​ 方法。然后,可以调用该方法,就像调用用 ​​ObjectScript​​ 编写的方法一样。

例如,使用 ​​Python​​ 编写的具有类方法的以下类:

Class User.EmbeddedPython
{

/// Description
ClassMethod Test() As %Status [ Language = python ]
{
# print the members of the Fibonacci series that are less than 10
print('Fibonacci series:')
a, b = 0, 1
while a < 10:
print(a, end=' ')
a, b = b, a + b

# import the iris module and show the classes in this namespace
import iris
print('\nInterSystems IRIS classes in this namespace:')
status = iris.cls('%SYSTEM.OBJ').ShowClasses()
return status
}

}

可以从 ​​ObjectScript​​ 调用此方法:

USER>set status = ##class(User.EmbeddedPython).Test()
Fibonacci series:
0 1 1 2 3 5 8
InterSystems IRIS classes in this namespace:
User.Company
User.EmbeddedPython
User.Person

USER>write status
1

或来自 ​​Python​​:

>>> import iris
>>> status = iris.cls('User.EmbeddedPython').Test()
Fibonacci series:
0 1 1 2 3 5 8
InterSystems IRIS classes in this namespace:
User.Company
User.EmbeddedPython
User.Person
>>> print(status)
1

在 SQL 函数和存储过程中

还可以通过在 ​​CREATE​​​ 语句中指定参数 ​​LANGUAGE PYTHON​​​ 来使用 ​​Embedded Python​​​ 编写 ​​SQL​​ 函数或存储过程,如下所示:

CREATE FUNCTION tzconvert(dt DATETIME, tzfrom VARCHAR, tzto VARCHAR)
RETURNS DATETIME
LANGUAGE PYTHON
{
from datetime import datetime
from dateutil import parser, tz
d = parser.parse(dt)
if (tzfrom is not None):
tzf = tz.gettz(tzfrom)
d = d.replace(tzinfo = tzf)
return d.astimezone(tz.gettz(tzto)).strftime("%Y-%m-%d %H:%M:%S")
}

该代码使用 ​​Python datetime​​​ 和 ​​dateutil​​ 模块中的函数。

以下 ​​SELECT​​​ 语句调用 ​​SQL​​​ 函数,将当前日期/时间从东部时间转换为协调世界时 (​​UTC​​)。

SELECT tzconvert(now(), 'US/Eastern', 'UTC')

该函数返回如下内容:

2021-10-19 15:10:05