Defining Events
Before defining events on API, you need to define events on the DolphinDB server.
The following DolphinDB script defines an Event class "TestEvent", i.e., event to be processed by DolphinDB CEP engine.
class TestEvent {
a :: INT
b :: DECIMAL32(3)
c :: DOUBLE VECTOR
def TestEvent(a_, b_, c_) {
a = a_;
b = b_;
c = c_;
}
}
Then, in Python API, create a child class "MyEvent" of Event
, which has the same schema as "TestEvent".
from dolphindb.cep import Event
from dolphindb.typing import Scalar, Vector
import dolphindb.settings as keys
class MyEvent(Event):
a: Scalar[keys.DT_INT]
b: Scalar[keys.DT_DECIMAL32, 3]
c: Vector[keys.DT_DOUBLE]
Attributes Declaration
The data type and form of an attribute can be declared using classes Scalar
and Vector
in dolphindb.typing
. Note that only scalar and vector (excluding AnyVector and ArrayVector) are supported in the current version (3.0.0.0).
Scalar
Two ways to declare an attribute as a scalar:
Scalar[keys.DT_TYPE, (exparam)]
- DT_TYPE is the data type constant provided in
dolphindb.settings
, e.g.,Scalar[keys.DT_INT]
,Scalar[keys.DT_STRING]
. - exparam specifies the number of decimal places for data of Decimal type, e.g.,
Scalar[keys.DT_DECIMAL32, 3]
.
- DT_TYPE is the data type constant provided in
Scalar[DATA_TYPE, (exparam)]
- DT_TYPE is a data type ID, e.g.,
Scalar[4]
,Scalar[37, 3]
. For mappings of data type and ID, see DolphinDB Data Types.
- DT_TYPE is a data type ID, e.g.,
Examples:
Declare an attribute as an int scalar.
from dolphindb.settings as keys
class TestEvent(Event):
a: Scalar[keys.DT_INT] # or a: Scalar[4]
Declare an attribute as a Decimal scalar with 3 decimal places.
class TestEvent(Event):
a: Scalar[keys.DT_DECIMAL32, 3] # or a: Scalar[37, 3]
Vector
Two ways to declare an attribute as a vector, similar to how scalars are declared:
Vector[keys.DT_TYPE, (exparam)]
Vector[DATA_TYPE, (exparam)]
Examples:
from dolphindb.settings as keys
class TestEvent(Event):
a: Vector[keys.DT_INT] # or a: Vector[4]
b: Vector[keys.DT_DECIMAL32, 3] # or b: Vector[37, 3]
Constructor
Default Constructor
Events inherited from the Event
class have default constructor. There are two ways to construct instances: using positional or keyword arguments.
Take the "MyEvent" defined above as an example:
Using Positional-Only Arguments
print(MyEvent(1, Decimal("1.234"), [1.1, 2.2]))
# output
MyEvent({'a': 1, 'b': Decimal('1.234'), 'c': [1.1, 2.2]})
Using Keyword-Only Arguments
print(MyEvent(a=1, b=Decimal("1.234"), c=[1.1, 2.2]))
# output
MyEvent({'a': 1, 'b': Decimal('1.234'), 'c': [1.1, 2.2]})
Using Both Positional and Keyword Arguments
print(MyEvent(1, Decimal("1.234"), c=[1.1, 2.2]))
# output
MyEvent({'a': 1, 'b': Decimal('1.234'), 'c': [1.1, 2.2]})
Defining the Constructor
You can also choose to create a constructor when defining the class "MyEvent".
class MyEvent(Event):
...
def __init__(self):
self.a = 1
self.b = Decimal("1.234")
self.c = [1.1, 2.2]
print(MyEvent())
# output
MyEvent({'a': 1, 'b': Decimal('1.234'), 'c': [1.1, 2.2]})
Event Name
The event name defaults to the class name. For example, the event name for "MyEvent" is MyEvent by default. If you want to change the name, use the _event_name
attribute:
class MyEvent(Event):
_event_name = "TestName"
...
The output event name will be changed to "TestName".
print(MyEvent(1, Decimal("1.234"), [1.1, 2.2]))
# output
TestName({'a': 1, 'b': Decimal('1.234'), 'c': [1.1, 2.2]})