Skip to content

Instantly share code, notes, and snippets.

@brianz brianz/actions.py
Last active Sep 15, 2016

Embed
What would you like to do?
import servant.fields
from servant.exceptions import ActionError
from servant.exceptions import ServantException
from servant.service.actions import Action
class AddAction(Action):
number1 = servant.fields.IntField(
required=True,
)
number2 = servant.fields.IntField(
required=True,
)
result = servant.fields.IntField(
in_response=True,
)
def run(self, **kwargs):
self.result = self.number1 + self.number2
class SubtractAction(AddAction):
def run(self, **kwargs):
self.result = self.number1 - self.number2
class DivideAction(Action):
numerator = servant.fields.DecimalField(
required=True,
in_response=True,
)
denominator = servant.fields.DecimalField(
required=True,
in_response=True,
)
quotient = servant.fields.DecimalField(
in_response=True,
)
def run(self, **kwargs):
if self.denominator == 0:
raise ActionError('Cannot divide by zero')
self.quotient = self.numerator / self.denominator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.