Skip to content

Commit

Permalink
[blackboard] client exists() method implemented (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
stonier authored Oct 11, 2019
1 parent 68c38e9 commit 2400680
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release Notes
Forthcoming
-----------
* [blackboard] global ``Blackboard.set`` staticmethod
* [blackboard] client ``exists()`` method, `#238 <https://github.com/splintered-reality/py_trees/pull/238>`_

1.3.x (2019-10-03)
------------------
Expand Down
16 changes: 16 additions & 0 deletions py_trees/blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,22 @@ def set(self, name: str, value: typing.Any, overwrite: bool=True) -> bool:
except AttributeError: # when the object doesn't have the attributes
return False

def exists(self, name: str) -> bool:
"""
Check if the specified variable exists on the blackboard.
Args:
name: name of the variable to get, can be nested, e.g. battery.percentage
Raises:
AttributeError: if the client does not have read access to the variable
"""
try:
unused_value = self.get(name)
return True
except KeyError:
return False

def get(self, name: str) -> typing.Any:
"""
Method based accessor to the blackboard variables (as opposed to simply using
Expand Down
20 changes: 20 additions & 0 deletions tests/test_blackboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ def test_delayed_register_key():
assert(bar.other == 1)


def test_key_exists():
console.banner("Nested Read")
with create_blackboards() as (foo, unused_bar):
print("foo.exists('dude') [{}][{}]".format(foo.exists("dude"), True))
assert(foo.exists("dude"), True)
with nose.tools.assert_raises_regexp(AttributeError, "does not have read/write access"):
print("Expecting Attribute Error with substring 'does not have read/write access'")
print("foo.exists('dude_not_here') [{}][{}]".format(foo.exists("dude_not_here"), False))
assert(foo.exists("dude_not_here"), False)


def test_nested_exists():
console.banner("Nested Read")
with create_blackboards() as (foo, unused_bar):
print("foo.exists('motley.nested') [{}][{}]".format(foo.exists("foo.nested"), True))
assert(foo.exists("motley.nested"), True)
print("foo.exists('motley.not_here') [{}][{}]".format(foo.exists("foo.not_here"), False))
assert(foo.exists("motley.not_here"), False)


def test_nested_read():
console.banner("Nested Read")
with create_blackboards() as (foo, unused_bar):
Expand Down

0 comments on commit 2400680

Please sign in to comment.