Python 2 and Python 3 differences
You must use Python 3 to write new scripts, or to edit existing scripts. For more information about the differences between Python 2 and Python 3, see What's New in Python 3.0 in the Python documentation.
If you are familiar with writing scripts in Python 2, there are some changes in Python 3 that you should know about before you convert your SOAR scripts.
Importing modules
arraybase64bs4calendarcollectionsdatetimeemailenumhashlibhtmlhtml2text-
json randomreregexstringtimexml
all()any()bytearray()bytes()classmethodstaticmethodtype()
Types of errors
ResilientSecurityExceptionoccurs if a script attempts to breach the security restrictions.ResilientMemoryLimitExceptionoccurs if the script tries to allocate more than the maximum amount of RAM allowed (64 MB).
'from' keyword
The from keyword in Python 3 clashes with the from attribute in
the email message context object. For this reason, the use of emailmessage.from is
not supported in Python 3 scripts and needs to be replaced by
emailmessage.sender.
Nonexistent fields
None. However, in Python 3, the same operation produces an attribute error,
which states that the field name is invalid. This behavior is in line with standard Python 3. The
hasattr() method can be used to check whether an attribute exists. For
example,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 support
Python 3 supports Unicode out of the box, which means that you no longer need to explicitly use
the u prefix or the unicode() function to store a string as
Unicode. If you convert existing scripts from Python 2 to Python 3, you must check and remove these
elements to avoid unicode-related errors in Python 3. For more information, see the Python 3
documentation https://docs.python.org/3.6/howto/unicode.html.
Text object data types
-
In Python 2, if you use a simple string to set the value of a text-area field, the value is stored as a Unicode object. If you set it using a helper function, the value is stored as a
SimpleTextContentDTO.In Python 3, whether you use a simple string or a helper function to set the value of a text-area field, the value is stored as a TextObject.
-
In Python 2, the format for objects of type
SimpleTextContentDTOcan be TEXT or HTML, depending on the helper that is used to set the value.In Python 3, the default format for objects of type TextObject is ‘html’. The format is set to 'text' only if the field value is set by running
helper.createPlainText().
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))