def __execute(self): audience = self.audience # Pick a person to gaze at initially if isinstance(audience, Entity): person = audience with self.lock: if not self.preempt_requested: self.gaze_ah = self.robot.gaze(person.head) else: if isinstance(audience, list): audience = Query(audience) results = audience.sort_increasing(lambda p: p.distance_to(self.robot)).execute() if len(results) > 0: person = results[0] self.current_gazee = person with self.lock: if not self.preempt_requested: self.gaze_ah = self.robot.gaze(person.head) if self.gaze_ah is not None: self.robot.wait(self.gaze_ah) with self.lock: if not self.preempt_requested: self.say_ah = self.robot.say(self.sentence)
def test_select_deferred(self): a = TracingGenerator() self.assertEqual(a.trace, []) b = Query(a).select_where(lambda x: x % 3 == 0) self.assertEqual(a.trace, []) c = b.take(2).execute() self.assertEqual(a.trace, [0, 1, 2, 3])
def test_to_list(self): a = [ 27, 74, 18, 48, 57, 97, 76, 20, 91, 8, 80, 59, 20, 32, 58, 12, 74, 78, 4 ] b = Query(a).execute() self.assertEqual(a, b)
def test_then_by_descending(self): a = [ 'sort', 'these', 'words', 'by', 'length', 'and', 'then', 'lexicographically' ] b = Query(a).sort_increasing(len).then_decreasing().execute() c = [ 'by', 'and', 'then', 'sort', 'words', 'these', 'length', 'lexicographically' ] self.assertEqual(b, c)
def test_order_by(self): a = [ 27, 74, 18, 48, 57, 97, 76, 20, 91, 8, 80, 59, 20, 32, 58, 12, 74, 78, 4 ] b = Query(a).sort_increasing().execute() c = [ 4, 8, 12, 18, 20, 20, 27, 32, 48, 57, 58, 59, 74, 74, 76, 78, 80, 91, 97 ] self.assertEqual(b, c)
def test_order_by_stability(self): a = [1, 2, 3] b = [1, 2, 3] c = [4, 5, 6] d = [4, 5, 6] e = [7, 5, 4] f = [7, 5, 4] self.assertTrue(a is not b) self.assertTrue(c is not d) self.assertTrue(e is not f) s = [a, b, c, d, e, f] for pre_perm in itertools.permutations(s): pre_index_a = index_by_identity(pre_perm, a) pre_index_b = index_by_identity(pre_perm, b) pre_index_c = index_by_identity(pre_perm, c) pre_index_d = index_by_identity(pre_perm, d) pre_index_e = index_by_identity(pre_perm, e) pre_index_f = index_by_identity(pre_perm, f) pre_order_a_b = sgn(pre_index_a - pre_index_b) pre_order_c_d = sgn(pre_index_c - pre_index_d) pre_order_e_f = sgn(pre_index_e - pre_index_f) post_perm = Query(pre_perm).sort_increasing().execute() post_index_a = index_by_identity(post_perm, a) post_index_b = index_by_identity(post_perm, b) post_index_c = index_by_identity(post_perm, c) post_index_d = index_by_identity(post_perm, d) post_index_e = index_by_identity(post_perm, e) post_index_f = index_by_identity(post_perm, f) post_order_a_b = sgn(post_index_a - post_index_b) post_order_c_d = sgn(post_index_c - post_index_d) post_order_e_f = sgn(post_index_e - post_index_f) self.assertEqual(pre_order_a_b, post_order_a_b) self.assertEqual(pre_order_c_d, post_order_c_d) self.assertEqual(pre_order_e_f, post_order_e_f)
def test_of_type_int(self): a = ['one', 2, 3, 'four', 'five', 6, 'seven', 8, 9, 'ten'] b = Query(a).select_type(int).execute() c = [2, 3, 6, 8, 9] self.assertEqual(b, c)
def test_order_by_descending(self): a = [1, 9, 7, 2, 5, 4, 6, 3, 8, 10] b = Query(a).sort_decreasing().execute() c = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] self.assertEqual(b, c)
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import rospy from hri_api.entities import Person, World from hri_api.query import Query from nao_hri import Nao, Gesture world = World() robot = Nao() people = Query(world).select_type(Person) closest_person = people.sort_decreasing(lambda p: p.distance_to(robot)).take(1) rate = rospy.Rate(2) while not rospy.is_shutdown(): result = closest_person.execute() if len(result) > 0: break rate.sleep() ah0 = robot.gesture(Gesture.PointRarm, target=result[0].head) ah1 = robot.say_to('Hello I can see you!', closest_person) ah2 = robot.gesture(Gesture.WaveLarm) robot.wait(ah1, ah2, ah0)
def test_select_not_callable(self): a = range(0, 100) self.assertRaises(TypeError, lambda: Query(a).select_where("not callable"))
def test_select_infinite(self): a = infinite() b = Query(a).select_where(lambda x: x % 5 == 0).take(3).execute() c = [0, 5, 10] self.assertEqual(b, c)
def test_then_by_descending_key(self): a = ['sort', 'using', 'third', 'letter', 'then', 'second'] b = Query(a).sort_increasing(lambda x: x[2]).then_decreasing( lambda y: y[1]).execute() c = ['second', 'then', 'using', 'third', 'sort', 'letter'] self.assertEqual(b, c)
def test_select(self): a = range(0, 100) b = Query(a).select_where(lambda x: x % 3 == 0).execute() c = list(range(0, 100, 3)) self.assertEqual(b, c)
def test_take_too_many(self): a = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] b = Query(a).take(10).execute() c = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] self.assertEqual(b, c)
def test_then_by_descending_not_callable(self): a = ['sort', 'using', 'third', 'letter', 'then', 'second'] b = Query(a).sort_increasing(lambda x: x[2]) self.assertRaises(TypeError, lambda: b.then_decreasing("not callable"))
def test_take_zero(self): a = ['a', 'b', 'c'] b = Query(a).take(0).execute() c = [] self.assertEqual(b, c)
def test_to_list_identity(self): a = [27, 74, 18, 48, 57, 97] b = Query(a).execute() self.assertTrue(a is b)
def test_order_by_not_callable(self): a = ['Sort', 'words', 'by', 'length'] self.assertRaises(TypeError, lambda: Query(a).sort_increasing("not callable"))
def test_order_by_key(self): a = ['Sort', 'words', 'by', 'length'] b = Query(a).sort_increasing(len).execute() c = ['by', 'Sort', 'words', 'length'] self.assertEqual(b, c)
def test_order_by2(self): a = [1, 9, 7, 2, 5, 4, 6, 3, 8, 10] b = Query(a).sort_increasing().execute() c = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] self.assertEqual(b, c)
def test_take_negative(self): a = ['a', 'b', 'c'] b = Query(a).take(-1).execute() c = [] self.assertEqual(b, c)
def test_of_type_str(self): a = ['one', 2, 3, 'four', 'five', 6, 'seven', 8, 9, 'ten'] d = Query(a).select_type(str).execute() e = ['one', 'four', 'five', 'seven', 'ten'] self.assertEqual(d, e)
def test_take_five(self): a = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] b = Query(a).take(5).execute() c = ['a', 'b', 'c', 'd', 'e'] self.assertEqual(b, c)
def test_of_type_tuple(self): a = ['one', 2, 3, 'four', 3.2, 'five', 6, 'seven', 8, 9, 'ten', 9.4] d = Query(a).select_type((str, int)).execute() e = ['one', 2, 3, 'four', 'five', 6, 'seven', 8, 9, 'ten'] self.assertEqual(d, e)
def test_take_from_infinite(self): b = Query(infinite()).take(5).execute() c = [0, 1, 2, 3, 4] self.assertEqual(b, c)
def test_of_type_not_type(self): a = ['one', 2, 3, 'four', 'five', 6, 'seven', 8, 9, 'ten'] self.assertRaises(TypeError, lambda: Query(a).execute(7))
def test_non_iterable(self): self.assertRaises(TypeError, lambda: Query(5))