def test_hamming(): print("Testing Hamming") h = Hamming(3) my_str = "111100001010" e = h.encode(my_str) print("original string: ", my_str) print("original encoded: ", e) e = e[:-1] + "1" e = "0" + e[1:] print("errored encoded: ", e) d = h.decode(e) print("decoded string: ", d)
def send(word, parity): hamming = Hamming(word, parity) if hamming.word_generate(): print() print('=' * 50) print('Typed word: ', end='') print(word) print('generated word: ', end='') print(hamming.word_distributed) print('=' * 50) print() return True return False
def test_hamming_single_block_encode_only(): # i 0 1 2 3 4 5 6 7 # data = 11 1 0 1 - 1 # coverage a b c # a X X X S - - - - # b X X - - X S - - # c X - X - X - S - # result 1 0 1 0 1 0 1 0 ham = Hamming(block_size=8) in_block = np.unpackbits(np.array([0b1011], dtype=np.uint8)) #4'b1011 bits = ham._encode_block(in_block[4:8]) expected_bits = np.unpackbits(np.array([0b10101010], dtype=np.uint8)) assert (bits == expected_bits).all()
def receive(word, parity): hamming = Hamming(word, parity) if hamming.check_word(): print() print('=' * 50) print(hamming.word_distributed) if hamming.error_counter > 0: print('incorrect word') print('error found in position {}'.format(hamming.error_position)) else: print('correct word') print('=' * 50) print() return True return False
def click_binario(): # Makes sure the input is a 12 bit binary number try: user_input = entry.get() number = int(user_input) is_binary = True for i in range(len(user_input)): if user_input[i] != str(1) and user_input[i] != str(0): is_binary = False break if len(user_input) == 12 and isinstance(number, int) and is_binary: # entry.delete(0, 'end') deci = int(user_input, 2) hexa = hex(deci)[2:].upper() octa = oct(deci)[2:].upper() table.delete(*table.get_children()) table.insert("", 0, values=(user_input, deci, hexa, octa)) table.pack() draw_nrzi = NRZI(user_input, nrzi_canvas) draw_nrzi.draw() Hamming(root, user_input, radio_group.get(), digit_group.get()) else: messagebox.showerror("Error", "El número debe ser binario y de 12 dígitos") except ValueError: messagebox.showerror("Error", "Ingrese un número válido")
def order(self, half, pitchers, ordered): """Sorts pitchers in order of appearance""" actions = half('div', class_='action') for action in actions: if action.h3 and action.h3.text == 'Pitching Substitution' and \ action.p.text[:17] == 'Pitching Change: ': name = action.p.text.replace( 'Pitching Change: ', '').split(' replaces ')[0].strip() name = sub(r'\s+Jr\.\s*', '', name) name = sub(r'\s*(.)\.', r'\1', name) name = sub(r'\s+', r' ', name) name = name.strip() assert len(pitchers) != 0,\ "gameid: {} - pitchers' list is empty".format(self.data['gameid']) closest = Hamming.closest(name, pitchers) ordered.append(closest)
class Protocol: BUFF_SIZE = 4096 def get_data_bytes(self, data): data = data.decode('utf-8') return data.split('/') def make_payload(self, data): lt = self.get_data_bytes(data) l = len(lt) - 1 msg = '' for i in range(l): dta = self.hamming.detect_error(lt[i]) if dta == 0: msg += self.hamming.data_extract(lt[i]) else: msg += self.hamming.data_extract(dta) return msg[::-1] def __init__(self, sockt): self.sock = sockt self.addr = socket.gethostname() self.hamming = Hamming() def write(self, data, addr): data_bytes = self.hamming.make_bytes(data) self.sock.sendto(data_bytes.encode('utf-8'), (self.addr, addr)) def read(self): data, client_addr = self.sock.recvfrom(self.BUFF_SIZE) self.addr_port = client_addr[1] resp = self.make_payload(data) return resp def start(self, server_port): self.sock.bind((self.addr, server_port))
def setUp(self): self.temp = Hamming() try: self.assertRaisesRegex except AttributeError: self.assertRaisesRegex = self.assertRaisesRegexp
class HammingTest(unittest.TestCase): def setUp(self): self.temp = Hamming() try: self.assertRaisesRegex except AttributeError: self.assertRaisesRegex = self.assertRaisesRegexp def test_empty_strands(self): self.assertEqual(self.temp.distance("", ""), 0) def test_single_letter_identical_strands(self): self.assertEqual(self.temp.distance("A", "A"), 0) def test_single_letter_different_strands(self): self.assertEqual(self.temp.distance("G", "T"), 1) def test_long_identical_strands(self): self.assertEqual(self.temp.distance("GGACTGAAATCTG", "GGACTGAAATCTG"), 0) def test_long_different_strands(self): self.assertEqual(self.temp.distance("GGACGGATTCTG", "AGGACGGATTCT"), 9) def test_disallow_first_strand_longer(self): with self.assertRaisesWithMessage(ValueError): self.temp.distance("AATG", "AAA") def test_disallow_second_strand_longer(self): with self.assertRaisesWithMessage(ValueError): self.temp.distance("ATA", "AGTG") def test_disallow_left_empty_strand(self): with self.assertRaisesWithMessage(ValueError): self.temp.distance("", "G") def test_disallow_right_empty_strand(self): with self.assertRaisesWithMessage(ValueError): self.temp.distance("G", "") # Utility functions def assertRaisesWithMessage(self, exception): return self.assertRaisesRegex(exception, r".+") def tearDown(self): self.temp = None
def test_long_different_strands(self): self.assertEqual(hamming.distance("GGACGGATTCTG", "AGGACGGATTCT"), 9)
def __init__(self, sockt): self.sock = sockt self.addr = socket.gethostname() self.hamming = Hamming()
def test_long_identical_strands(self): self.assertEqual(hamming.distance("GGACTGAAATCTG", "GGACTGAAATCTG"), 0)
def test_hamming_multiple_block_encode_only(): ham = Hamming(block_size=16) in_block = bytearray(b"e") bits = ham.encode(in_block) assert len(bits) == 2
def test_disallow_first_strand_longer(self): with self.assertRaisesWithMessage(ValueError): hamming.distance("AATG", "AAA")
from hamming import Hamming data = raw_input('Type the message received: ') h = Hamming() result = h.decode(data) msg = 'Result: ' + result + '\n' if (int(result, 2) != 0): msg += 'There\'s an error on position ' + str(int(result, 2)) + '.\n' correct = h.correct(data, int(result, 2)) msg += 'The correct data should be ' + correct + '.' else: msg += 'The data received is correct.' print msg
from hamming import Hamming data = raw_input('Type the message you to send: ') h = Hamming() print 'The message to be sent with ' + str( h.calculateR(data)) + ' redudancy bits is: ' + h.encode(data)
def test_single_letter_identical_strands(self): self.assertEqual(hamming.distance("A", "A"), 0)
def test_empty_strands(self): self.assertEqual(hamming.distance("", ""), 0)
def test_disallow_right_empty_strand(self): with self.assertRaisesWithMessage(ValueError): hamming.distance("G", "")
def test_disallow_second_strand_longer(self): with self.assertRaisesWithMessage(ValueError): hamming.distance("ATA", "AGTG")
from hamming import Hamming if __name__ == "__main__": palavra = Hamming() palavra.gerador() palavra.decomposicao() palavra.validacao() palavra.colocando_erro() palavra.validacao()
def test_single_letter_different_strands(self): self.assertEqual(hamming.distance("G", "T"), 1)