Esempio n. 1
0
    def testSingledispatchArgumentToString(self):
        def argumentToString_tuple(obj):
            return "Dispatching tuple!"

        # Unsupport Python2
        if "singledispatch" not in dir(functools):
            for attr in ("register", "unregister"):
                with self.assertRaises(NotImplementedError):
                    getattr(argumentToString, attr)(
                        tuple, argumentToString_tuple
                    )
            return

        # Prepare input and output
        x = (1, 2)
        default_output = ic.format(x)

        # Register
        argumentToString.register(tuple, argumentToString_tuple)
        assert tuple in argumentToString.registry
        assert str.endswith(ic.format(x), argumentToString_tuple(x))

        # Unregister
        argumentToString.unregister(tuple)
        assert tuple not in argumentToString.registry
        assert ic.format(x) == default_output
Esempio n. 2
0
 def testFormat(self):
     with disableColoring(), captureStandardStreams() as (out, err):
         """comment"""; noop(); ic(  # noqa
             'sup'); noop()  # noqa
     """comment"""; noop(); s = ic.format(  # noqa
         'sup'); noop()  # noqa
     assert s == err.getvalue().rstrip()
  def agent_request(self, other_starts_goals, _, action_req):
    '''Handle an action request from an agent'''
    if self.state is not TransformerIState.WaitAction:
      self.log.warning('Ignoring action request; mode is %s', self.state)
      return
    self.log.info('Received agent request')

    self.conflicts_with.clear()
    action, start_pose = action_req
    self.current_action = action_req
    for step in np.arange(Transformer.WORLD_TICK, action.time, Transformer.WORLD_TICK):
      pose = start_pose + step * action.vel_vec
      pose_array = np.full_like(other_starts_goals, pose)
      # Check that the action doesn't conflict with starts/goals
      if np.linalg.norm(pose_array - other_starts_goals,
                        -np.inf) <= Agent.GOAL_THRESHOLD + Agent.RADIUS:
        # We come too close to some start or goal; reject
        self.log.debug('Agent request conflicts with start or goal; rejecting')
        self.agent.queue.put(
            Msg(SenderTypes.Transformer, MessageTypes.ActionReject, self.get_id(), action_req))
        return

      # Check that the action doesn't conflict with known current/potential actions
      for (tf_id, transformer) in self.transformers.items():
        if test_conflict((action, pose), (ZERO_ACTION, transformer.pose)):
          # We'd run into where an agent currently is
          self.agent.queue.put(
              Msg(SenderTypes.Transformer, MessageTypes.ActionReject, self.get_id(), action_req))
          return

        if transformer.curr_action:
          tf_action, tf_pose = transformer.curr_action
          # if transformer.running:
          #   ticks_elapsed = self.tick_count - transformer.start_tick
          #   tf_pose += tf_action.vel_vec * ticks_elapsed * Transformer.WORLD_TICK

          if test_conflict((action, pose), (tf_action, tf_pose)):
            # The action conflicts with a currently executing or approved action
            self.conflicts_with.add(tf_id)
            self.log.debug('Agent request conflicts with approved action; rejecting')
            self.agent.queue.put(
                Msg(SenderTypes.Transformer, MessageTypes.ActionReject, self.get_id(), action_req))
            return

    # We can skip the reverse schedulability check because this is the special case where actions
    # are exactly reversible
    # Check with the other transformers
    self.need_acknowledgement = set(
        tf_id for tf_id in self.transformers if not self.transformers[tf_id].at_goal)
    if self.need_acknowledgement:
      self.log.debug('Sending out for approval')
      self.send_to_all(
          Msg(SenderTypes.Transformer, MessageTypes.ActionRequest, self.get_id(), action_req))
      self.log.debug('Need approval from %s', ic.format(self.need_acknowledgement))
      self.state = TransformerIState.WaitAcknowledge
    else:
      self.start_action()
Esempio n. 4
0
        def do_icecream(self, arg):
            """ic(ecream) expression
            Icecream print.
            """
            try:
                from icecream import ic

                val = self._getval(arg)
                ic.configureOutput(prefix="🍦 |> ")
                self._print(ic.format(arg, val))
            except BaseException:
                pass