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

第五章 使用嵌入式 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 datetimedateutil 模塊中的函數(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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容