Permalink
Browse files

Added some type hinting.

  • Loading branch information...
javipalanca committed Oct 15, 2018
1 parent afd73ea commit 306e0e948460b28dc902d441ea019545ac515095
Showing with 28 additions and 25 deletions.
  1. +0 −1 requirements_dev.txt
  2. +28 −24 spade/behaviour.py
@@ -19,7 +19,6 @@ pluggy<0.7,>=0.5
python-coveralls==2.9.1
requests==2.19.1
parsel==1.4.0
docutils==0.12
sphinx_rtd_theme==0.4.0
testfixtures==6.3.0
pytest-aiohttp==0.3.0
@@ -6,6 +6,10 @@
from datetime import datetime
import asyncio
from typing import Any, Union
from .message import Message
from .template import Template
now = datetime.now
@@ -53,7 +57,7 @@ def set_agent(self, agent):
self.presence = agent.presence
self.web = agent.web
def set_template(self, template):
def set_template(self, template: Template):
"""
Sets the template that is used to match incoming
messages with this behaviour.
@@ -64,7 +68,7 @@ def set_template(self, template):
"""
self.template = template
def match(self, message):
def match(self, message: Message) -> bool:
"""
Matches a message with the behaviour's template
@@ -79,26 +83,26 @@ def match(self, message):
return self.template.match(message)
return True
def set(self, name, value):
def set(self, name: str, value: Any) -> None:
"""
Stores a knowledge item in the agent knowledge base.
Args:
name (str): name of the item
value (object): value of the item
value (Any): value of the item
"""
self.agent.set(name, value)
def get(self, name):
def get(self, name: str) -> Any:
"""
Recovers a knowledge item from the agent's knowledge base.
Args:
name (str): name of the item
Returns:
object: the object retrieved or None
Any: the object retrieved or None
"""
return self.agent.get(name)
@@ -121,7 +125,7 @@ def start(self):
self.kill(exit_code=e)
await self._step()
def kill(self, exit_code=None):
def kill(self, exit_code: Any = None):
"""
Stops the behaviour
@@ -134,7 +138,7 @@ def kill(self, exit_code=None):
self._exit_code = exit_code
logger.info("Killing behavior {0} with exit code: {1}".format(self, exit_code))
def is_killed(self):
def is_killed(self) -> bool:
"""
Checks if the behaviour was killed by means of the kill() method.
@@ -145,7 +149,7 @@ def is_killed(self):
return self._force_kill.is_set()
@property
def exit_code(self):
def exit_code(self) -> Any:
"""
Returns the exit_code of the behaviour.
It only works when the behaviour is done or killed,
@@ -161,7 +165,7 @@ def exit_code(self):
raise BehaviourNotFinishedException
@exit_code.setter
def exit_code(self, value):
def exit_code(self, value: Any):
"""
Sets a new exit code to the behaviour.
@@ -171,7 +175,7 @@ def exit_code(self, value):
"""
self._exit_code = value
def done(self):
def done(self) -> bool:
"""
Returns True if the behaviour has finished
else returns False
@@ -228,7 +232,7 @@ def done(self):
logger.error("Exception running on_end in behaviour {}: {}".format(self, e))
self.kill(exit_code=e)
async def enqueue(self, message):
async def enqueue(self, message: Message):
"""
Enqueues a message in the behaviour's mailbox
@@ -237,7 +241,7 @@ def done(self):
"""
await self.queue.put(message)
def mailbox_size(self):
def mailbox_size(self) -> int:
"""
Checks if there is a message in the mailbox
@@ -247,7 +251,7 @@ def mailbox_size(self):
"""
return self.queue.qsize()
async def send(self, msg):
async def send(self, msg: Message):
"""
Sends a message.
@@ -264,11 +268,11 @@ def mailbox_size(self):
msg.sent = True
self.agent.traces.append(msg, category=str(self))
async def _xmpp_send(self, msg):
async def _xmpp_send(self, msg: Message):
aioxmpp_msg = msg.prepare()
await self.agent.client.send(aioxmpp_msg)
async def receive(self, timeout=None):
async def receive(self, timeout: float = None) -> Union[Message, None]:
"""
Receives a message for this behaviour.
If timeout is not None it returns the message or "None"
@@ -304,7 +308,7 @@ def __init__(self):
super().__init__()
self._already_executed = False
def done(self):
def done(self) -> bool:
""" """
if not self._already_executed:
self._already_executed = True
@@ -333,12 +337,12 @@ def __init__(self, period, start_at=None):
self._next_activation = now()
@property
def period(self):
def period(self) -> timedelta:
""" Get the period. """
return self._period
@period.setter
def period(self, value):
def period(self, value: float):
"""
Set the period.
@@ -393,7 +397,7 @@ def __init__(self, start_at):
await self.run()
self._timeout_triggered = True
def done(self):
def done(self) -> bool:
""" """
return self._timeout_triggered
@@ -432,7 +436,7 @@ def setup(self):
""" """
pass
def add_state(self, name, state, initial=False):
def add_state(self, name: str, state: str, initial: bool = False):
""" Adds a new state to the FSM.
Args:
@@ -447,7 +451,7 @@ def add_state(self, name, state, initial=False):
if initial:
self.current_state = name
def add_transition(self, source, dest):
def add_transition(self, source: str, dest: str):
""" Adds a transition from one state to another.
Args:
@@ -457,7 +461,7 @@ def add_transition(self, source, dest):
"""
self._transitions[source].append(dest)
def is_valid_transition(self, source, dest):
def is_valid_transition(self, source: str, dest: str) -> bool:
"""
Checks if a transitions is registered in the FSM
@@ -520,7 +524,7 @@ def is_valid_transition(self, source, dest):
"""
raise RuntimeError # pragma: no cover
def to_graphviz(self):
def to_graphviz(self) -> str:
"""
Converts the FSM behaviour structure to Graphviz syntax

0 comments on commit 306e0e9

Please sign in to comment.