Exemple #1
0
def init():
    """Description: Starts up the application normally by asking the user about
    the server they want to connect to"""
    if ui.ask_yes_no(constants.Strings.server_prompt.value):
        domain = ui.ask_string(constants.Strings.server_ip.value)
        if domain is None:
            ui.fatal(constants.Exceptions.invalid_ip.value)
    else:
        domain = "play.pommerman.com:5050"
    ui.info(
        constants.Strings.server_connecting_p1.value,
        ui.yellow,
        constants.Strings.server_connecting_p2.value,
        ui.reset,
        constants.Strings.server_connecting_p3.value,
    )
    network = Network(domain)
    try:
        status = network.server_status()
    except Exception as e:
        ui.fatal(e)
    signal.signal(signal.SIGINT, _exit_handler)
    ui.info(
        constants.Strings.server_connected.value,
        ui.yellow,
        constants.Strings.server_players.value,
        str(status[0]) + ",",
        constants.Strings.server_matches.value,
        status[1],
    )
    intent(network)
Exemple #2
0
def test_ask_yes_no():
    """ Test that you can answer with several types of common answers """
    with mock.patch('builtins.input') as m:
        m.side_effect = ["y", "yes", "Yes", "n", "no", "No"]
        expected_res = [True, True, True, False, False, False]
        for res in expected_res:
            actual = ui.ask_yes_no("coffee?")
            assert actual == res
def change_filter():
    """Changes the search filter for myStream.filter(track=[trump])
    :param:
    :return trump: New keyword or DEFAULT keyword 'trump'"""
    trump = 'trump'  # A variable named trump.
    ui.info_1(ui.yellow, "SEARCH:", ui.blue, ui.standout, "'trump'")
    ans_filter = ui.ask_yes_no("Change search keyword? [Ctrl-C Quits] >>>",
                               default=False)
    if ans_filter == False:
        return trump
    else:
        ui.info_1(ui.red, "Really change?")
        ans_really = ui.ask_yes_no("[ENTER] for No >>>", default=False)
        if ans_really == False:
            return trump
        else:
            trump = ui.ask_string("Enter new search keyword")
            ui.info_3("Starting search for new keyword:", ui.blue, trump)
            return trump
 def ploting(tweets_db=TWEETS_DB, csv_file=CSV_FILE):
     table_name = MyStreamListener.table_name
     plot_question = ui.ask_yes_no("Plot?", default=True)
     if plot_question:
         ui.info(ui.green, "Populating csv file with {}".format(table_name))
         q_four.db_into_csv(TWEETS_DB, CSV_FILE, table_name)
         ui.info_3("Ploting...")
         q_four.frecuency()
         q_four.senti()
     else:
         ui.info(ui.turquoise, "Program Finished")
         exit()
Exemple #5
0
def test_ask_yes_no_wrong_input():
    """ Test that we keep asking when answer does not make sense """
    with mock.patch('builtins.input') as m:
        m.side_effect = ["coffee!", "n"]
        assert ui.ask_yes_no("tea?") is False
        assert m.call_count == 2
Exemple #6
0
def test_ask_yes_no_default():
    """ Test that just pressing enter returns the default value """
    with mock.patch('builtins.input') as m:
        m.side_effect = ["", ""]
        assert ui.ask_yes_no("coffee?", default=True) is True
        assert ui.ask_yes_no("coffee?", default=False) is False
def ask_table(db_file=TWEETS_DB,
              conn=create_conection(),
              table_name=TABLE_NAME):
    """Default table or new table.  Checks if table exists,
    creates one if not with name from user and calls user_ans_tweets().
    :param db_file: DEFAULT database
    :param conn: creates connection()
    :param table_name: name of table
    """
    def ploting(tweets_db=TWEETS_DB, csv_file=CSV_FILE):
        table_name = MyStreamListener.table_name
        plot_question = ui.ask_yes_no("Plot?", default=True)
        if plot_question:
            ui.info(ui.green, "Populating csv file with {}".format(table_name))
            q_four.db_into_csv(TWEETS_DB, CSV_FILE, table_name)
            ui.info_3("Ploting...")
            q_four.frecuency()
            q_four.senti()
        else:
            ui.info(ui.turquoise, "Program Finished")
            exit()

    tables_list = list()
    conn = create_conection()
    cur = conn.cursor()
    cur.execute("SELECT name FROM sqlite_master")
    for table in cur:
        tables = str(table).strip('(').strip(')').strip(',').strip("'")
        tables_list.append(tables)
    if "trump" in tables_list:
        ui.info_1(ui.yellow, ui.bold, "DEFAULT TABLE FOUND! =>", ui.blue,
                  ui.standout, "'trump'")
        new_db_ans = ui.ask_yes_no("Add tweets to table 'trump'?",
                                   default=True)
        if new_db_ans:
            ui.warning("Creating database (If it doesn't exist)")
            create_conection()
            ui.info_2(ui.green, "Accessing Database")
            MyStreamListener.table_name = TABLE_NAME
            user_ans_tweets()
            ploting()
        else:
            choices = ["Create New Table", "Load table", "Plot", "Quit"]
            new = ui.ask_choice("Select: ", choices)
            if new == "Create New Table":
                ui.warning("Tables with the same name will not be created")
                new_table_name = ui.ask_string("Enter new table name:")
                new_table_name = new_table_name.lower()
                new_table_name = new_table_name.replace(" ", "_")
                ui.info("Table name with format:", ui.blue, new_table_name)
                create_ans = ui.ask_yes_no("Create?")
                if create_ans:
                    tweets_table_create(create_conection(), TWEETS_DB,
                                        new_table_name)
                    insert_new_tbl = ui.ask_yes_no(
                        "Insert new tweets into {}?".format(new_table_name))
                    if insert_new_tbl:
                        MyStreamListener.table_name = new_table_name
                        user_ans_tweets()
                        ploting()
                    else:
                        ui.info(ui.bold, ("Program Finished"))
                else:
                    ui.info(ui.bols, "Program Finished")
            elif new == "Load table":
                new_table_name = ui.ask_choice("Select Table to load:",
                                               tables_list)
                MyStreamListener.table_name = new_table_name
                user_ans_tweets()
                ploting()

            elif new == "Plot":
                new_table_name = ui.ask_choice("Select Table to plot:",
                                               tables_list)
                MyStreamListener.table_name = new_table_name
                ploting()

            elif new == "Quit":
                ui.warning("Program Finished")
                exit()
Exemple #8
0
def match(network, room=False, agent=False, ui_en=False):
    """Description: This facilitates playing a match  
    Arguments:  
    * network: An `network`(pommerman.network.ion_client.network) object  
    * room: If String, The room to be created/joined. If False, the public \
room will be joined  
    * agent: The class of the agent should be a derivative of BaseAgent  
    * ui_en: If the UI is enabled or disabled (This also controls if exception \
are raised or not)
    Returns: Array [reward, match_id]"""
    agent = agent()
    if ui_en:
        ui.info(ui.yellow, constants.Strings.server_comm.value)
    try:
        network.join_list(room)
    except Exception as e:
        if ui_en:
            ui.fatal(e)
        raise e
    if ui_en:
        ui.info(constants.Strings.match_variant.value, ui.yellow, network.mode)
        ui.info(ui.yellow, constants.Strings.match_wait.value)
    try:
        network.wait_match()
    except Exception as e:
        if ui_en:
            ui.fatal(e)
        raise e
    if ui_en:
        ui.info(constants.Strings.match_run.value, "#" + network.match_id)
    for mode in pommerman.constants.GameType:
        if mode.name in network.mode:
            agent.init_agent(
                id=0, game_type=mode
            )  # We always use ID as 0 as the server doesn't return it
    while True:
        try:
            match_obj = network.match_get()
        except Exception as e:
            if ui_en:
                ui.fatal(e)
            raise e
        # match_obj[0] is the intent: 0 = OBS, 1 = Agent Dead, 2 = Match End
        if match_obj[0] is 0:
            action = agent.act(match_obj[1], gym.spaces.Discrete(6))
            try:
                network.send_move(action, match_obj[2])
            except Exception as e:
                if ui_en:
                    ui.fatal(e)
                raise e
        elif match_obj[0] is 2:
            agent.episode_end(reward=match_obj[1])
            if ui_en:
                if match_obj[1] == 1:
                    ui.info(constants.Strings.match_won.value)
                if match_obj[1] == -1:
                    ui.info(constants.Strings.match_loss_draw.value)
                ui.info(
                    constants.Strings.match_agent.value,
                    ui.yellow,
                    pommerman.constants.Item(match_obj[2]).name,
                )
            else:
                return [match_obj[1], network.match_id]
            break
    ui.info(constants.Strings.match_replay.value, ui.yellow, network.match_id)
    if ui.ask_yes_no(constants.Strings.match_ask_replay.value):
        replay(network, network.match_id)
    else:
        intent(network)