编写脚本时,可以导入 Python 模块。 如果 语言 字段设置为 Python 2,您只能导入 java.util.Date re 模块。 如果设置为 Python 3 ,那么可以导入以下模块。
  • array
  • base64
  • calendar
  • collections
  • datetime (相当于 Python 2 java.util.Date 模块)
  • email
  • hashlib
  • html2text
  • random
  • regex
  • string
  • 不存在的字段

    在 Python 2 中,尝试访问不存在的上下文对象属性会成功,并返回 None 。 但是,在 Python 3 中,同一操作会产生属性错误,该错误指出字段名称无效。 此行为符合标准 Python 3。 hasattr() 方法可用于检查属性是否存在。 例如,
    if hasattr(incident, 'nonExistentField'):
      log.info(incident.nonExistentField)
    else:
      log.info('Tried to access a field that does not exist')
    该脚本返回以下日志消息。
    INFO: Cannot access a field that does not exist.

    Unicode 支持

    Python 3 支持现成的 Unicode ,这意味着您不再需要显式使用 u 前缀或 unicode() 函数将字符串存储为 Unicode。 如果将现有脚本从 Python 2 转换为 Python 3,那么必须检查并除去这些元素以避免 Python 3 中的 Unicode 相关错误。 有关更多信息,请参阅 Python 3 文档 https://docs.python.org/3.6/howto/unicode.html

    文本对象数据类型

    在 Python 2 和 Python 3中,文本对象数据类型的行为有所不同。 这些差异不一定会破坏从 Python 2 转换为 Python 3 的脚本,但可能令人感兴趣。
    • 在 Python 3 中,无论您使用简单字符串还是帮助程序函数来设置文本区域字段的值,该值都存储为 TextObject。 在 Python 2 中,如果使用简单字符串来设置文本区域字段的值,那么该值将存储为 Unicode 对象。 如果使用帮助程序函数对其进行设置,那么该值将存储为 SimpleTextContentDTO
    • 在 Python 3 中,TextObject 类型的对象的缺省格式为“html”。 仅当通过运行 helper.createPlainText() 来设置字段值时,才会将格式设置为 "text"。 在 Python 2 中,类型为 SimpleTextContentDTO 的对象的格式可以是 TEXT 或 HTML ,具体取决于用于设置值的帮助程序。
    • 您可以通过运行以下脚本并检查日志消息来探索 Python 版本中的差异。
      incident.description = 'set direct'
      log.info(type(incident.description)) 
      incident.description = helper.createPlainText('set using createPlainText helper') 
      log.info(type(incident.description)) 
      incident.description = helper.createRichText('set using createRichText helper') 
      log.info(type(incident.description))