This example demonstrates how you can define useful functions in a Python module at project level and reference those functions from other Python code in the project.
NOTE: Use this example with SAS Event Stream Processing 2024.02 and later.
For more information about how to install and use example projects, see Using the Examples.
The Python window in this project performs division calculations and logs an error message when there is an attempt to divide a number by zero. To log the error, the Python window references a Python module that defines the logging functionality.
The python_module.csv file contains information about calculations.
The following figure shows the diagram of the project:
-
Open the project in SAS Event Stream Processing Studio and click on the toolbar. Project-level properties are displayed in the right pane.
-
Expand Modules.
-
In the Python Modules section, double-click the logger row.
-
In the Edit Python Code Module window, explore the Python code.
Step Python Code Section Import the esp module, which facilitates the use of SAS Event Stream Processing functions with Python. import esp
Declare the Logger class and initialize the _context attribute. class Logger: def __init__(self,context): self._context = context if context != None else "mylogger"
Define the logging functions that are used to create log messages at different severity levels. These functions use a SAS Event Stream Processing function called esp.logMessage. def error(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="error",line=line) def warn(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="warn",line=line) def fatal(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="fatal",line=line) def info(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="info",line=line) def debug(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="debug",line=line) def trace(self,msg,line = None): esp.logMessage(logcontext=self._context,message=msg,level="trace",line=line)
The Source window streams information from the python_module.csv file to the Python window.
Explore the settings for this window:
- Select the Source window on the workspace.
- To examine the window's output schema, on the right toolbar, click . Observe the following fields:
id
: This is the ID that is assigned to each calculation. It is also selected as the Key.dividend
: This is a number that is to be divided by the number in thedivisor
field.divisor
: This is a number that is used as the divisor in the calculation.quotient
: This is the result of the division.
- Click .
The Python window performs division calculations.
Explore the settings for this window:
-
Select the Python window in the workspace.
-
In the right pane, expand Python Settings.
- To increase the efficiency of the Python code processing, you can specify which fields to copy from the source file and which fields to use in the Python code.
- The Fields to copy field shows that the fields
id
anddividend
, anddivisor
are copied from the source file. - The Fields to use in Python code field shows that the fields
dividend
anddivisor
are used in the Python code.
- The Fields to copy field shows that the fields
- The Events function field shows the Python function that is used (in this case, the
create
function).
- To increase the efficiency of the Python code processing, you can specify which fields to copy from the source file and which fields to use in the Python code.
-
Scroll down in the right pane, to view the Python code that performs the calculations for this example:
Step Python Code Section Import the Logger module. from logger import Logger
Define a function called create and set three variables to represent an event and information about the dividend and the divisor. def create(data,context): e = {} dividend = data["dividend"] divisor = data["divisor"]
Check whether the divisor is zero. If so, use the Logger class (defined in the Python module) to log an error message. if (divisor == 0): logger = Logger("modules.example") logger.error("division by 0, " + str(dividend) + "/" + str(divisor)) return None
If the divisor is not zero, perform the calculation and return a SAS Event Stream Processing event. e["quotient"] = dividend / divisor return(e)
When you test the project in SAS Event Stream Processing Studio, the results for each window appear in separate tabs. The following figure shows the Python tab. The python_module.csv file contains four events, but only three events appear in the Python tab. The fourth event, where the number five was to be divided by zero, has caused the following error to be logged in the Log pane: division by 0, 5/0
In this simple example, the Python module is referenced from one location. In a real-life scenario, creating a Python module is most useful when you want to reference common Python code from multiple locations.
For more information, see SAS Help Center: Using Python Modules and SAS Help Center: ESP Server Log Debugging.