Exemplo n.º 1
0
def get_car_data(file_name, normalize=True):
    data = util.read_file(file_name)
    car_data = DataSet(data, list(range(0, 6)), 6, file_name)
    # Convert attribute columns to numeric scheme
    car_data.convert_str_attribute(0, {
        'low': 0,
        'med': 1,
        'high': 2,
        'vhigh': 3
    })
    car_data.convert_str_attribute(1, {
        'low': 0,
        'med': 1,
        'high': 2,
        'vhigh': 3
    })
    car_data.convert_str_attribute(2, {'2': 2, '3': 3, '4': 4, '5more': 5})
    car_data.convert_str_attribute(3, {'2': 2, '4': 4, 'more': 5})
    car_data.convert_str_attribute(4, {'small': 0, 'med': 1, 'big': 2})
    car_data.convert_str_attribute(5, {'low': 0, 'med': 1, 'high': 2})
    car_data.convert_to_float([0, 1, 2, 3, 4, 5])
    # Normalize values.
    if normalize:
        car_data.normalize_z_score(list(range(0, 6)))
    # Randomly shuffle values.
    car_data.shuffle()
    return car_data
Exemplo n.º 2
0
 def _read_gold(self):
     path = os.path.join(self._gold_dir, self._name + self._gold_ext)
     html = util.read_file(path)
     soup = BeautifulSoup(html, 'lxml')
     self._parse_gold(soup)
     for entity_id in self.entities.keys():
         for rank in self.entities[entity_id]:
             self.doc2entity[rank] = entity_id
Exemplo n.º 3
0
def get_wine_data():
    data = util.read_file(WINE_DATA_FILE)
    wine_data = DataSet(data, 11, list(range(0, 11)), WINE_DATA_FILE)
    # Convert all attribute columns to numeric values.
    wine_data.convert_to_float(list(range(0, 12)))
    # Normalize values.
    wine_data.normalize_z_score(list(range(0, 11)))
    # Randomly shuffle values.
    wine_data.shuffle()
    return wine_data
Exemplo n.º 4
0
def get_wine_data(file_name, normalize=True):
    data = util.read_file(file_name)
    wine_data = DataSet(data, list(range(0, 11)), 11, file_name)
    # Convert all attribute columns to numeric values.
    wine_data.convert_to_float(list(range(0, 12)))
    # Normalize values.
    if normalize:
        wine_data.normalize_z_score(list(range(0, 11)))
    # Randomly shuffle values.
    wine_data.shuffle()
    return wine_data
Exemplo n.º 5
0
def get_abalone_data():
    data = util.read_file(ABALONE_DATA_FILE)
    abalone_data = DataSet(data, 8, list(range(0, 8)), ABALONE_DATA_FILE)
    numeric_columns = list(range(1, 9))
    # Convert attribute columns to floats
    abalone_data.convert_to_float(numeric_columns)
    # Normalize values
    abalone_data.normalize_z_score(numeric_columns)
    # Randomly shuffle values.
    abalone_data.shuffle()
    return abalone_data
Exemplo n.º 6
0
def get_machine_data():
    data = util.read_file(MACHINE_DATA_FILE)
    # There is another final column but we probably want to exclude it.
    machine_data = DataSet(data, 8, list(range(0, 8)), MACHINE_DATA_FILE)
    # Convert all columns except the first two to floats, including the class column.
    machine_data.convert_to_float(list(range(2, 9)))
    # Normalize values.
    machine_data.normalize_z_score(list(range(2, 8)))
    # Randomly shuffle values.
    machine_data.shuffle()
    return machine_data
Exemplo n.º 7
0
def get_abalone_data(file_name, normalize=True):
    data = util.read_file(file_name)
    abalone_data = DataSet(data, list(range(1, 8)), 8, file_name)
    # Convert male, female, infant column to numeric values
    # abalone_data.convert_str_attribute(0, {'I': 0, 'F': 1, 'M': 2})
    # Convert attribute columns to floats
    abalone_data.convert_to_float(list(range(1, 9)))
    # Normalize values
    if normalize:
        abalone_data.normalize_z_score(list(range(1, 8)))
    # Randomly shuffle values.
    abalone_data.shuffle()
    return abalone_data
Exemplo n.º 8
0
def get_forest_fires_data():
    data = util.read_file(FOREST_FIRE_DATA_FILE)
    forest_fires_data = DataSet(data, 12, list(range(0, 12)), FOREST_FIRE_DATA_FILE)
    numeric_columns = [0, 1] + list(range(4, 13))
    # Remove the first line, which is the header info.
    forest_fires_data.remove_header(1)
    # Convert applicable columns to floats, including the class column.
    forest_fires_data.convert_to_float([0, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12])
    # Normalize values.
    forest_fires_data.normalize_z_score([0, 1, 4, 5, 6, 7, 8, 9, 10, 11])
    # Randomly shuffle values.
    forest_fires_data.shuffle()
    forest_fires_data.sample(250)
    return forest_fires_data
Exemplo n.º 9
0
def get_car_data():
    data = util.read_file(CAR_DATA_FILE)
    car_data = DataSet(data, 6, list(range(0, 6)), CAR_DATA_FILE)
    # Convert attribute columns to numeric scheme
    car_data.convert_attribute(0, {'low': 0, 'med': 1, 'high': 2, 'vhigh': 3})
    car_data.convert_attribute(1, {'low': 0, 'med': 1, 'high': 2, 'vhigh': 3})
    car_data.convert_attribute(2, {'2': 2, '3': 3, '4': 4, '5more': 5})
    car_data.convert_attribute(3, {'2': 2, '4': 4, 'more': 5})
    car_data.convert_attribute(4, {'small': 0, 'med': 1, 'big': 2})
    car_data.convert_attribute(5, {'low': 0, 'med': 1, 'high': 2})
    numeric_columns = list(range(0, 6))
    # Normalize values.
    car_data.normalize_z_score(numeric_columns)
    # Randomly shuffle values.
    car_data.shuffle()
    return car_data
Exemplo n.º 10
0
def get_segmentation_data():
    data = util.read_file(SEGMENTATION_DATA_FILE)
    # Attribute columns are all numeric
    #  * Attribute #7, vedge-sd, removed because it is a standard deviation.
    #  * Attribute #9, hedge-sd, removed for same reason.
    attr_cols = [1, 2, 3, 4, 5, 6, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
    segmentation_data = DataSet(data, 0, attr_cols, SEGMENTATION_DATA_FILE)
    # Remove the first 5 lines, which is reserved for the header.
    segmentation_data.remove_header(5)
    # Convert all attribute columns to numeric values.
    segmentation_data.convert_to_float(attr_cols)
    # Normalize values.
    segmentation_data.normalize_z_score(attr_cols)
    # Randomly shuffle values.
    segmentation_data.shuffle()
    return segmentation_data
Exemplo n.º 11
0
    def login_pressed(self):
        raw_users = util.read_file('users.txt')
        if raw_users == None:
            users = []
            raw_users = ''
        else:
            users = util.trim(raw_users).split('\n')
        user_lookup = {}
        for user in users:
            name = util.alphanums(util.trim(user[32:]))
            password = user[:32].lower()
            user_lookup[name] = password

        user = util.alphanums(self.username.text)
        password = user_lookup.get(user, None)
        if password == None:
            password = util.md5(str(time.time()) + "leprechauns")
            raw_users += "\n" + password + user
            util.write_file('users.txt', raw_users)
        self.auth_request = network.send_authenticate(user, password)
        self.password = password
Exemplo n.º 12
0
    def login_pressed(self):
        raw_users = util.read_file("users.txt")
        if raw_users == None:
            users = []
            raw_users = ""
        else:
            users = util.trim(raw_users).split("\n")
        user_lookup = {}
        for user in users:
            name = util.alphanums(util.trim(user[32:]))
            password = user[:32].lower()
            user_lookup[name] = password

        user = util.alphanums(self.username.text)
        password = user_lookup.get(user, None)
        if password == None:
            password = util.md5(str(time.time()) + "leprechauns")
            raw_users += "\n" + password + user
            util.write_file("users.txt", raw_users)
        self.auth_request = network.send_authenticate(user, password)
        self.password = password
Exemplo n.º 13
0
def get_forest_fires_data(file_name, normalize=True):
    data = util.read_file(file_name)
    forest_fires_data = DataSet(data, list(range(0, 12)), 12, file_name)
    # Remove the first line, which is the header info.
    forest_fires_data.remove_header(1)
    # Convert string columns (dates) to numerics
    forest_fires_data.convert_str_attribute(
        2, {
            'jan': 1,
            'feb': 2,
            'mar': 3,
            'apr': 4,
            'may': 5,
            'jun': 6,
            'jul': 7,
            'aug': 8,
            'sep': 9,
            'oct': 10,
            'nov': 11,
            'dec': 12
        })
    forest_fires_data.convert_str_attribute(3, {
        'mon': 1,
        'tue': 2,
        'wed': 3,
        'thu': 4,
        'fri': 5,
        'sat': 6,
        'sun': 7
    })
    # Convert applicable columns to floats, including the class column.
    forest_fires_data.convert_to_float(list(range(0, 13)))
    # Normalize values.
    if normalize:
        forest_fires_data.normalize_z_score(list(range(0, 12)))
    # Randomly shuffle values.
    forest_fires_data.shuffle()
    return forest_fires_data
Exemplo n.º 14
0
from src import util

import sys
if sys.version > '3':
    from urllib.request import urlopen
    from urllib.parse import urlencode
else:
    from urllib import urlopen
    from urllib import urlencode
import threading

import time

_server_address = util.read_file('server.txt')


class Request(threading.Thread):
    def __init__(self, action, args, user, password):
        threading.Thread.__init__(self)
        self.setDaemon(True)
        self.args = {'action': action}
        if user != None:
            self.args['user_id'] = user
        if password != None:
            self.args['password'] = password
        for key in args:
            self.args[key] = args[key]
        self.hasresponse = False
        self.response = None
        self.error = False
        self.lock = threading.Lock()
Exemplo n.º 15
0
from src.classes import BoardingPass
from src.util import read_file


def find_missing_in_cnt_range(li, min_id=0):
    for ind, l in enumerate(li):
        if l != min_id + ind:
            return min_id + ind
    return None


if __name__ == "__main__":
    file_path = "input.txt"
    bp_ids = read_file(file_path, strip=True)
    bp_seats = sorted([BoardingPass(bp_id).seat_id for bp_id in bp_ids])
    min_id, max_id = bp_seats[0], bp_seats[-1]
    print(f"PART 1 : {max_id}")

    #seat_range = set(list(range(min_id, max_id+1)))
    #print(seat_range.difference(set(bp_seats)))
    missing = find_missing_in_cnt_range(bp_seats, min_id)
    print(f"PART 2: {missing}")
Exemplo n.º 16
0
def get_three_clusters_data():
    data = util.read_file(THREE_CLUSTERS_DATA_FILE)
    three_clusters_data = ds.DataSet(data, 2, [0, 1], THREE_CLUSTERS_DATA_FILE)
    three_clusters_data.convert_to_float([0, 1])
    three_clusters_data.shuffle()
    return three_clusters_data
Exemplo n.º 17
0
    def transmit(self):
        """ This main function initializes the radios and sends
        all the data gathered from the file. """

        # Read file
        util.compress_file(self.config)
        payload_list = util.read_file(self.config,
                                      self.config.IN_FILEPATH_COMPRESSED)

        # Initialize loop variables and functions
        self.receiver.startListening()
        tx_success = False
        seq_num = 1

        # Send file
        while not tx_success:
            # Sending payload
            for payload in payload_list:
                retransmit = True
                attempt = 0
                while retransmit:
                    util.send_packet(self.sender,
                                     self.build_frame(payload, seq_num))
                    attempt = attempt + 1
                    rx_buffer = []
                    if self.wait_for_ack(self.receiver):
                        self.receiver.read(
                            rx_buffer, self.receiver.getDynamicPayloadSize())
                        crc = rx_buffer[:self.config.CRC_SIZE]
                        seq = int.from_bytes(
                            rx_buffer[self.config.
                                      CRC_SIZE:self.config.CRC_SIZE +
                                      self.config.SEQ_NUM_SIZE],
                            byteorder='big')
                        ack = rx_buffer[self.config.CRC_SIZE +
                                        self.config.SEQ_NUM_SIZE:]
                        seq_ack = rx_buffer[self.config.CRC_SIZE:]
                        if util.check_crc(crc, seq_ack):
                            if seq == seq_num:
                                if bytes(ack) == b'ACK':
                                    retransmit = False
                                    print("Packet number " + str(seq_num) +
                                          " transmitted successfully")
                                    seq_num = seq_num + 1
                                elif bytes(ack) == b'ERROR':
                                    print("        Packet number " +
                                          str(seq_num) +
                                          " transmitted incorrectly")
                                else:
                                    print(
                                        "        Unknown error when transmitting packet number "
                                        + str(seq_num))
                            else:
                                print(
                                    "        Received Out of Order ACK. Received: "
                                    + str(seq) + " Expecting: " + str(seq_num))
                        else:
                            print("        Received incorrect ACK number " +
                                  str(seq_num))
                    else:
                        if not (seq_num == 1 and attempt != 1):
                            print("    Attempt " + str(attempt) +
                                  " to retransmit packet number " +
                                  str(seq_num))

                    if attempt > 1000 and seq_num > 1:
                        print(
                            "Transmission ended after trying to retransmit for more than 1000 times"
                        )
                        return False

            retransmit_final = True
            attempt_final = 0
            while retransmit_final:
                util.send_packet(
                    self.sender, self.build_frame(b'ENDOFTRANSMISSION',
                                                  seq_num))
                attempt_final = attempt_final + 1
                rx_buffer = []
                if self.wait_for_ack(self.receiver):
                    self.receiver.read(rx_buffer,
                                       self.receiver.getDynamicPayloadSize())
                    crc = rx_buffer[:self.config.CRC_SIZE]
                    seq = int.from_bytes(
                        rx_buffer[self.config.CRC_SIZE:self.config.CRC_SIZE +
                                  self.config.SEQ_NUM_SIZE],
                        byteorder='big')
                    ack = rx_buffer[self.config.CRC_SIZE +
                                    self.config.SEQ_NUM_SIZE:]
                    seq_ack = rx_buffer[self.config.CRC_SIZE:]
                    if util.check_crc(crc, seq_ack) and seq == seq_num:
                        if bytes(ack) == b'ACK':
                            retransmit_final = False
                            tx_success = True
                            print("TRANSMISSION SUCCESSFUL")
                else:
                    print("    Attempt " + str(attempt_final) +
                          " to retransmit FINAL packet")
                    if attempt_final > 1000:
                        print(
                            "Program ended after failing to transmit the EOT message"
                        )
                        return False

        # Return true if success
        return True
Exemplo n.º 18
0
from src import util

import sys
if sys.version > '3':
	from urllib.request import urlopen
	from urllib.parse import urlencode
else:
	from urllib import urlopen
	from urllib import urlencode
import threading

import time

_server_address = util.read_file('server.txt')

class Request(threading.Thread):
	def __init__(self, action, args, user, password):
		threading.Thread.__init__(self)
		self.setDaemon(True)
		self.args = {'action': action}
		if user != None:
			self.args['user_id'] = user
		if password != None:
			self.args['password'] = password
		for key in args:
			self.args[key] = args[key]
		self.hasresponse = False
		self.response = None
		self.error = False
		self.lock = threading.Lock()
Exemplo n.º 19
0
import numpy as np
from src.util import find_elements_with_sum, arr_mult, read_file
import pprint

pp = pprint.PrettyPrinter()
if __name__ == "__main__":
    arr = read_file("input.txt", int)

    valid_res = find_elements_with_sum(arr, 2020, 2)
    mult_res = arr_mult(valid_res)
    pp.pprint(f"Part 1: {mult_res}")

    valid_res = find_elements_with_sum(arr, 2020, 3)
    mult_res = arr_mult(valid_res)
    pp.pprint(f"Part 2: {mult_res}")
Exemplo n.º 20
0
from src.util import read_file, parse_password_and_policy, verify_new_pass_validity, verify_old_pass_validity

if __name__ == "__main__":
    lines = read_file("input.txt")
    ## PART 1
    valid_cnt = 0
    for line in lines:
        password, letter_constraint, min_max_len = parse_password_and_policy(
            line)
        valid = verify_old_pass_validity(password, letter_constraint,
                                         min_max_len)
        valid_cnt += valid
    print(f"Num valid passwords = {valid_cnt}")

    ## PART 2
    valid_cnt = 0
    for line in lines:
        password, letter_constraint, min_max_len = parse_password_and_policy(
            line)
        valid = verify_new_pass_validity(password, letter_constraint,
                                         min_max_len)
        valid_cnt += valid
    print(f"Num valid passwords 2 = {valid_cnt}")
Exemplo n.º 21
0
    def transmit(self):
        """ This main function initializes the radios and sends
        all the data gathered from the file. """

        # Read file
        util.compress_file(self.config)
        payload_list = util.read_file(self.config, self.config.IN_FILEPATH_COMPRESSED)

        # Initialize loop variables and functions
        self.receiver.startListening()
        tx_success = False
        payload_tx_success = False
        rcv_seq_num = 0

        # Send file
        while not tx_success:
            first_packet_transmitted = False
            while not first_packet_transmitted:
                util.send_packet(self.sender, self.build_frame(payload_list[0], 1))
                rx_buffer = []
                if self.wait_for_ack(self.receiver):
                    self.receiver.read(rx_buffer, self.receiver.getDynamicPayloadSize())
                    crc = rx_buffer[:self.config.CRC_SIZE]
                    ack_seq_num = int.from_bytes(
                        rx_buffer[self.config.CRC_SIZE:self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE],
                        byteorder='big')
                    ack = rx_buffer[self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE:]
                    seq_ack = rx_buffer[self.config.CRC_SIZE:]
                    if util.check_crc(crc, seq_ack):
                        if bytes(ack) == b'ACK' and ack_seq_num == 1:
                            print("Packet 1 transmitted successfully")
                            rcv_seq_num = 1
                            first_packet_transmitted = True
                        else:
                            print("        Unknown error when transmitting packet number 1")
                    else:
                        print("        Received corrupt ACK")

            while not payload_tx_success:
                chunks_left = len(payload_list) - rcv_seq_num
                if chunks_left >= self.config.BURST_SIZE:
                    burst_size = self.config.BURST_SIZE
                else:
                    burst_size = chunks_left
                for sent_seq in range((rcv_seq_num + 1), (rcv_seq_num + 1) + burst_size):
                    util.send_packet(self.sender, self.build_frame(payload_list[sent_seq - 1], sent_seq))
                rx_buffer = []
                if self.wait_for_ack(self.receiver):
                    self.receiver.read(rx_buffer, self.receiver.getDynamicPayloadSize())
                    crc = rx_buffer[:self.config.CRC_SIZE]
                    ack_seq_num = int.from_bytes(
                        rx_buffer[self.config.CRC_SIZE:self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE],
                        byteorder='big')
                    ack = rx_buffer[self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE:]
                    seq_ack = rx_buffer[self.config.CRC_SIZE:]
                    if util.check_crc(crc, seq_ack):
                        if bytes(ack) == b'ACK':
                            print("Packets " + str(rcv_seq_num + 1) + "-" + str(ack_seq_num)
                                  + " transmitted successfully (" + str(ack_seq_num - rcv_seq_num) + " OK)")
                            rcv_seq_num = ack_seq_num
                            if rcv_seq_num == len(payload_list):
                                payload_tx_success = True
                        else:
                            print("        Unknown error when transmitting packet number " + str(ack_seq_num))
                    else:
                        print("        Received corrupt ACK")
                else:
                    print("    Attempt to retransmit from packet number " + str(rcv_seq_num + 1))

            retransmit_final = True
            attempt_final = 0
            final_seq_num = rcv_seq_num + 1
            while retransmit_final:
                util.send_packet(self.sender, self.build_frame(b'ENDOFTRANSMISSION', final_seq_num))
                attempt_final = attempt_final + 1
                rx_buffer = []
                if self.wait_for_ack(self.receiver):
                    self.receiver.read(rx_buffer, self.receiver.getDynamicPayloadSize())
                    crc = rx_buffer[:self.config.CRC_SIZE]
                    ack_seq_num = int.from_bytes(
                        rx_buffer[self.config.CRC_SIZE:self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE],
                        byteorder='big')
                    ack = rx_buffer[self.config.CRC_SIZE + self.config.SEQ_NUM_SIZE:]
                    seq_ack = rx_buffer[self.config.CRC_SIZE:]
                    if util.check_crc(crc, seq_ack) and ack_seq_num == final_seq_num:
                        if bytes(ack) == b'ACK':
                            retransmit_final = False
                            tx_success = True
                            print("TRANSMISSION SUCCESSFUL")
                else:
                    print("    Attempt " + str(attempt_final) + " to retransmit FINAL packet")
                    if attempt_final > 1000:
                        print("Program ended after failing to transmit the EOT message")
                        return False

        # Return true if success
        return True