前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用嵌入式 Python (二)

使用嵌入式 Python (二)

作者头像
用户7741497
发布2022-08-04 13:49:47
5180
发布2022-08-04 13:49:47
举报
文章被收录于专栏:hml_知识记录

在 Python 脚本文件 (.py) 中

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

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

代码语言:javascript
复制
# 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,如下所示:

代码语言:javascript
复制
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

代码语言:javascript
复制
/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 编写的具有类方法的以下类:

代码语言:javascript
复制
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 调用此方法:

代码语言:javascript
复制
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

代码语言:javascript
复制
>>> 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 函数或存储过程,如下所示:

代码语言:javascript
复制
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 模块中的函数。

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

代码语言:javascript
复制
SELECT tzconvert(now(), 'US/Eastern', 'UTC')

该函数返回如下内容:

代码语言:javascript
复制
2021-10-19 15:10:05

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 在 Python 脚本文件 (.py) 中
  • 在 IRIS 类的方法中
    • 在 SQL 函数和存储过程中
    相关产品与服务
    对象存储
    对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档