Beispiel #1
0
class PackingSystem:
    DEFAULT_PACKET_DATA_SIZE = 4096
    PACKET_HEADER_SIZE = 4

    def __init__(self):
        self.__data_size_in_bytes = PackingSystem.DEFAULT_PACKET_DATA_SIZE
        self.__packet_size_in_bytes = PackingSystem.DEFAULT_PACKET_DATA_SIZE + PackingSystem.PACKET_HEADER_SIZE

        self.__packet_number = 1

        self.__header_size_in_bytes = PackingSystem.PACKET_HEADER_SIZE

    def pack_data(self):
        new_packet = SWPacket(self.__packet_size_in_bytes,
                              self.__data_size_in_bytes,
                              self.__header_size_in_bytes,
                              packet_type=PacketType.DATA)
        new_packet.set_packet_number(self.__packet_number)
        self.__packet_number += 1
        new_packet.store_data(self.__file_reader.read())
        return new_packet

    def open_file(self, source_file):
        self.__file_reader = FileReader(source_file, self.__data_size_in_bytes)
        self.__file_reader.open()

    def close_file(self):
        self.__file_reader.close()

    def get_file_size(self):
        return self.__file_reader.get_file_size_in_bytes()

    def get_current_packet_number(self):
        return self.__packet_number

    def get_data_size_in_bytes(self):
        return self.__data_size_in_bytes

    def get_end_file_packet(self):
        end_packet = SWPacket(self.__packet_size_in_bytes,
                              self.__data_size_in_bytes,
                              self.__header_size_in_bytes,
                              packet_type=PacketType.DATA)

        end_packet.make_end_packet()
        end_packet.set_packet_number(self.__packet_number)

        return end_packet

    def reset(self):
        self.__packet_number = 1

    def set_data_size(self, new_size):
        self.__data_size_in_bytes = new_size
        self.__packet_size_in_bytes = new_size + PackingSystem.PACKET_HEADER_SIZE
class TestFileReader(unittest.TestCase):
    TEST_FILENAME = 'test_file.csv'
    EXPECTED_HEADER = ('header_a', 'header_b')
    EXPECTED_ROWS = (
        ('val_a1', 'val_b1'),
        ('val_a2', 'val_b2'),
        ('val_a3', 'val_b3'),
        ('val_a4', 'val_b4'),
        ('val_a5', 'val_b5'),
    )

    @classmethod
    def setUpClass(cls):
        with open(cls.TEST_FILENAME, 'w') as f:
            csv_writer = csv.writer(f)
            rows = (cls.EXPECTED_HEADER, ) + cls.EXPECTED_ROWS
            for r in rows:
                csv_writer.writerow(r)

    def setUp(self):
        self._csv_file = FileReader(self.TEST_FILENAME, has_header=True)

    def tearDown(self):
        self._csv_file.close()

    @classmethod
    def tearDownClass(cls):
        if os.path.isfile(cls.TEST_FILENAME):
            os.remove(cls.TEST_FILENAME)

    def test_read_first_row(self):
        row = self._csv_file.get_next_row()
        self.assertEqual(tuple(row), self.EXPECTED_ROWS[0])

    def test_header(self):
        self.assertEqual(
            tuple(self._csv_file.header),
            self.EXPECTED_HEADER
        )

    def test_iterable(self):
        for rows, expected in zip(self._csv_file, self.EXPECTED_ROWS):
            self.assertEqual(tuple(rows), expected)
class SpecReader_2():
    def __init__(self, path = None):
        self._reader = FileReader(path)

    def setPath(self, path):
        self._reader.setPath(path)

    def close(self):
        self._reader.close()

    def open(self):
        '''Státuszjelzés:
        False, ha bármely okból nem sikerült a művelet
        True, ha sikerült
        '''

        return self._reader.open()

    def readLine(self):
        '''Státuszjelzés:
        -1, ha bármely okból nem sikerült a művelet
        +1, ha sikerült adatot olvasni
        0,  ha már nincs több adat
        '''

        while True:
            ret_dic = self._reader.readLine()
            if ret_dic['status'] <= 0:
                return ret_dic

            data = ret_dic['data'].rstrip()
            if len(data) == 0: continue

            if data.lstrip()[0] == '#': continue

            return ret_dic
Beispiel #4
0
    args = vars(args)

    dictionary_filename = args["dictionary_filename"]
    queries_filename = args["queries_filename"]
    postings_filename = args["postings_filename"]
    results_filename = args["results_filename"]
    timed = args["timed"]

    queries = read_queries(queries_filename)
    dictionary = read_dictionary_dictionary(dictionary_filename)

    posting_file_reader = FileReader(postings_filename)

    start = time.time()

    search_results = []
    for query in queries:
        query = query.strip('\n')
        search_result = search(query, dictionary, posting_file_reader,
                               term_from_token)
        search_results.append(search_result)

    done = time.time()
    elapsed = done - start

    if timed:
        print "Started at {}, ended at {}, time taken: {}".format(
            start, done, elapsed)

    posting_file_reader.close()
    write_results(results_filename, search_results)