Exemplo n.º 1
0
def start_connection(destination: str, port_num: str) -> socket.socket:
    '''
    function to start a connection to a server.

    returns the client tcp socket
    '''
    # create a tuple containing the host and port
    server_address = (destination, int(port_num))

    # create a random 16 letter string to use as a connection ID
    connection_id = ''.join(
        random.choice(string.ascii_lowercase) for i in range(16))

    # print that we are starting a connection to the server
    print("starting connection", connection_id, "to", server_address)

    # create a TCP socket (nonblocking)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(False)
    sock.connect_ex(
        server_address
    )  # like connect, but returns an error code rather than raising an exception when an error occurs

    events = selectors.EVENT_READ | selectors.EVENT_WRITE
    data = types.SimpleNamespace(addr=server_address, inb=b"", outb=b"")
    sel.register(sock, events, data=data)
    return sock
Exemplo n.º 2
0
def start_connection(destination: str, port_num: str,
                     conn_id: str) -> socket.socket:
    '''
    function to start a connection to a server.

    returns the client tcp socket
    '''
    # create a tuple containing the host and port
    server_address = (destination, int(port_num))

    # print that we are starting a connection to the server
    print(f'starting connection to: {server_address}')

    # create a TCP socket (nonblocking)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setblocking(False)
    sock.connect_ex(
        server_address
    )  # like connect, but returns an error code rather than raising an exception when an error occurs

    data = types.SimpleNamespace(connid=conn_id,
                                 addr=server_address,
                                 messages=[],
                                 inb=b"",
                                 outb=b"")
    DEFAULT_SELECTOR.register(sock, EVENTS, data=data)
    return sock
Exemplo n.º 3
0
def accept_wrapper(sock: socket.socket) -> None:
    '''
    wrapper function to accept a socket connection 
    and register with the default selector
    '''
    conn, addr = sock.accept()  # Should be ready to read
    print("accepted connection from", addr)
    GLOBAL_CONNECTIONS[str(len(GLOBAL_CONNECTIONS))] = (*addr, sock, 'server')
    conn.setblocking(False)
    data = types.SimpleNamespace(addr=addr, inb=b"", outb=b"")
    events = selectors.EVENT_READ | selectors.EVENT_WRITE
    sel.register(conn, events, data=data)
Exemplo n.º 4
0
def accept_wrapper(sock: socket.socket) -> None:
    '''
    wrapper function to accept a socket connection 
    and register with the default selector
    '''
    conn, addr = sock.accept()  # Should be ready to read
    print("accepted connection from", addr)
    conn_id = str(len(GLOBAL_CONNECTIONS))
    GLOBAL_CONNECTIONS[conn_id] = (addr[0], addr[1], conn, 'server')
    conn.setblocking(False)
    data = types.SimpleNamespace(connid=conn_id,
                                 addr=addr,
                                 messages=[],
                                 inb=b"",
                                 outb=b"")
    DEFAULT_SELECTOR.register(conn, EVENTS, data=data)
Exemplo n.º 5
0
def _send(connection_id: str, *argv):
    '''
    Command Name:
    send
    Description:
    send <connection id.> <message> (For example, send 3 Oh! This project is a piece of cake). This will
    send the message to the host on the connection that is designated by the number 3 when command “list” is
    used. The message to be sent can be up-to 100 characters long, including blank spaces. On successfully
    executing the command, the sender should display “Message sent to <connection id>” on the screen. On
    receiving any message from the peer, the receiver should display the received message along with the
    sender information.
    '''
    # get the socket at the connection id
    destination, port_num, sock, _ = GLOBAL_CONNECTIONS.get(connection_id)
    message_to_send = ' '.join([arg for arg in argv])
    DEFAULT_SELECTOR.modify(sock,
                            events=EVENTS,
                            data=types.SimpleNamespace(
                                connid=connection_id,
                                addr=(destination, port_num),
                                messages=[message_to_send.encode()],
                                inb=b"",
                                outb=b""))
def word_frequency_gui(wti_index, corpus):

    treaty_time_groups = wti_index.get_treaty_time_groupings()

    lw = lambda w: widgets.Layout(width=w)

    include_pos_tags = ['ADJ', 'VERB', 'ADV', 'NOUN', 'PROPN']
    weighting_options = {'Count': 'count', 'Frequency': 'freq'}
    normalize_options = {'': False, 'Lemma': 'lemma', 'Lower': 'lower'}
    pos_options = include_pos_tags
    default_include_pos = ['NOUN', 'PROPN']
    frequent_words = [
        x[0].lower() for x in textacy_utility.get_most_frequent_words(
            corpus, 100, include_pos=default_include_pos)
    ]

    group_by_options = {
        treaty_time_groups[k]['title']: k
        for k in treaty_time_groups
    }
    output_type_options = [
        ('Sample', 'table'),
        ('Rank', 'rank'),
        ('Excel', 'excel'),
    ]
    ngrams_options = {'-': None, '1': [1], '1,2': [1, 2], '1,2,3': [1, 2, 3]}
    party_preset_options = wti_index.get_party_preset_options()
    parties_options = [
        x for x in wti_index.get_countries_list() if x != 'ALL OTHER'
    ]

    gui = types.SimpleNamespace(
        progress=widgets.IntProgress(value=0,
                                     min=0,
                                     max=5,
                                     step=1,
                                     description='',
                                     layout=lw('98%')),
        parties=widgets.SelectMultiple(description='Parties',
                                       options=parties_options,
                                       value=[],
                                       rows=7,
                                       layout=lw('200px')),
        party_preset=widgets_config.dropdown('Presets',
                                             party_preset_options,
                                             None,
                                             layout=lw('200px')),
        ngrams=widgets.Dropdown(description='n-grams',
                                options=ngrams_options,
                                value=None,
                                layout=lw('200px')),
        min_word=widgets.Dropdown(description='Min length',
                                  options=[1, 2, 3, 4],
                                  value=1,
                                  layout=lw('200px')),
        normalize=widgets.Dropdown(description='Normalize',
                                   options=normalize_options,
                                   value='lemma',
                                   layout=lw('200px')),
        weighting=widgets.Dropdown(description='Weighting',
                                   options=weighting_options,
                                   value='freq',
                                   layout=lw('200px')),
        include_pos=widgets.SelectMultiple(description='POS',
                                           options=pos_options,
                                           value=default_include_pos,
                                           rows=7,
                                           layout=lw('150px')),
        stop_words=widgets.SelectMultiple(description='STOP',
                                          options=frequent_words,
                                          value=list([]),
                                          rows=7,
                                          layout=lw('200px')),
        group_by_column=widgets.Dropdown(description='Group by',
                                         value='signed_year',
                                         options=group_by_options,
                                         layout=lw('200px')),
        output_type=widgets.Dropdown(description='Output',
                                     value='rank',
                                     options=output_type_options,
                                     layout=lw('200px')),
        compute=widgets.Button(description='Compute',
                               button_style='Success',
                               layout=lw('120px')),
        display_score=widgets.ToggleButton(description='Display score',
                                           icon='check',
                                           value=False,
                                           layout=lw('120px')),
        output=widgets.Output(layout={'border': '1px solid black'}),
        file_suffix=widgets.Text(
            value='',
            placeholder='(optional id)',
            description='ID:',
            disabled=False,
            layout=lw('200px'),
            tooltip="Optional plain text id that will be added to filename."))

    boxes = widgets.VBox([
        gui.progress,
        widgets.HBox([
            widgets.VBox([
                gui.normalize,
                gui.ngrams,
                gui.weighting,
                gui.group_by_column,
                gui.output_type,
            ]),
            widgets.VBox([
                gui.parties,
                gui.party_preset,
            ]),
            gui.include_pos,
            widgets.VBox([
                gui.stop_words,
                gui.file_suffix,
            ]),
            widgets.VBox([
                gui.display_score,
                gui.compute,
            ],
                         layout=widgets.Layout(align_items='flex-end')),
        ]), gui.output
    ])

    display(boxes)

    def on_party_preset_change(change):  # pylint: disable=W0613
        if gui.party_preset.value is None:
            return
        gui.parties.value = gui.parties.options if 'ALL' in gui.party_preset.value else gui.party_preset.value

    gui.party_preset.observe(on_party_preset_change, names='value')

    def pos_change_handler(*args):
        with gui.output:
            gui.compute.disabled = True
            selected = set(gui.stop_words.value)
            frequent_words = [
                x[0].lower() for x in textacy_utility.get_most_frequent_words(
                    corpus,
                    100,
                    normalize=gui.normalize.value,
                    include_pos=gui.include_pos.value,
                    #weighting=gui.weighting.value
                )
            ]

            gui.stop_words.options = frequent_words
            selected = selected & set(gui.stop_words.options)
            gui.stop_words.value = list(selected)
            gui.compute.disabled = False

    gui.include_pos.observe(pos_change_handler, 'value')
    gui.weighting.observe(pos_change_handler, 'value')

    def compute_callback_handler(*_args):
        gui.output.clear_output()
        with gui.output:
            try:
                gui.compute.disabled = True
                df_counts = compute_list_of_most_frequent_words(
                    corpus=corpus,
                    gui=gui,
                    target=gui.normalize.value,
                    treaty_time_groups=treaty_time_groups,
                    group_by_column=gui.group_by_column.value,
                    parties=gui.parties.value,
                    weighting=gui.weighting.value,
                    include_pos=gui.include_pos.value,
                    stop_words=set(gui.stop_words.value),
                    display_score=gui.display_score.value)
                display_list_of_most_frequent_words(gui, df_counts)
            except Exception as ex:
                logger.error(ex)
                raise
            finally:
                gui.progress.value = 0
                gui.compute.disabled = False

    gui.compute.on_click(compute_callback_handler)
    return gui