第五章 使用嵌入式 Python (二)
在 Python 腳本文件 (.py) 中
還可以使用 irispython 命令執(zhí)行 Python 腳本。
考慮 Windows 系統(tǒng)上的文件 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 的系統(tǒng)上,使用 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。在管理門戶中,轉(zhuǎn)至 System Administration > Security > Services,選擇 %Service_CallIn,然后選中啟用服務(wù)框。
在 IRIS 類的方法中
可以使用 Language 關(guān)鍵字在 IRIS 類中編寫 Python 方法。然后,可以調(diào)用該方法,就像調(diào)用用 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 調(diào)用此方法:
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 函數(shù)和存儲過程中
還可以通過在 CREATE 語句中指定參數(shù) LANGUAGE PYTHON 來使用 Embedded Python 編寫 SQL 函數(shù)或存儲過程,如下所示:
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 模塊中的函數(shù)。
以下 SELECT 語句調(diào)用 SQL 函數(shù),將當(dāng)前日期/時間從東部時間轉(zhuǎn)換為協(xié)調(diào)世界時 (UTC)。
SELECT tzconvert(now(), 'US/Eastern', 'UTC')
該函數(shù)返回如下內(nèi)容:
2021-10-19 15:10:05