class Compress(object): def __init__(self): self.__fifo = Fifo() self.__output = [] self.__output.append(LZO_MAGIC) def __yield_output(self): output = ''.join(self.__output) self.__output = [] return output def compress(self, data): self.__fifo.write(data) while True: block = self.__fifo.read(MAX_BLOCK) if not block: break data = lzo1x_1_compress(block) if len(data) >= len(block): self.__output.append(pack(_BLOCK_HEADER_FORMAT, len(block))) self.__output.append(block) else: self.__output.append(pack(_BLOCK_HEADER_FORMAT, len(data) | 0x8000)) self.__output.append(data) return self.__yield_output()
class PatientsLine: def __init__(self, patience_rate=0): self.corona_plus_patients = Fifo(patience_rate, "plus patients") self.corona_minus_patients = Fifo(patience_rate, "minus patients") def elapse_time(self): self.corona_minus_patients.elapse_time() self.corona_plus_patients.elapse_time() def add_to_line(self, patient): if patient.corona_test_result == "+": self.corona_plus_patients.add_to_line(patient) else: self.corona_minus_patients.add_to_line(patient) def get_next_patient(self): next_patient = self.corona_plus_patients.get_next_patient() if next_patient == None: next_patient = self.corona_minus_patients.get_next_patient() return next_patient def get_plus_patients_length(self): return len(self.corona_plus_patients.line) def get_minus_patients_length(self): return len(self.corona_minus_patients.line) def get_line_length(self): return self.get_plus_patients_length( ) + self.get_minus_patients_length()
class Decompress(object): STATE_MAGIC = 0 STATE_BLOCK_HEADER = 1 STATE_BLOCK = 2 def __init__(self): self.__buff = lzo_buffer(MAX_BLOCK) self.__fifo = Fifo() self.__output = [] self.state = self.STATE_MAGIC def __yield_output(self): output = ''.join(self.__output) self.__output = [] return output def decompress(self, data): self.__fifo.write(data) while True: # magic if self.state == self.STATE_MAGIC: magic = self.__fifo.readblock(len(LZO_MAGIC)) if not magic: break if magic != LZO_MAGIC: raise Exception('Bad magic: %s' % magic) self.state = self.STATE_BLOCK_HEADER # block header if self.state == self.STATE_BLOCK_HEADER: header = self.__fifo.readblock(_BLOCK_HEADER_LENGTH) if not header: break self.__block_length = unpack(_BLOCK_HEADER_FORMAT, header)[0] self.__block_compressed = self.__block_length & 0x8000 self.__block_length ^= self.__block_compressed if self.__block_length: self.state = self.STATE_BLOCK # block if self.state == self.STATE_BLOCK: block = self.__fifo.readblock(self.__block_length) if not block: break if self.__block_compressed: decomp = lzo1x_decompress(block, self.__buff) self.__output.append(decomp) else: self.__output.append(block) self.state = self.STATE_BLOCK_HEADER return self.__yield_output()
def __init__(self, name, neighbors=None): self.name = name self.holder = None self.using = False self.request_q = Fifo() self.asked = False self.is_recovering = False self.is_working = False self.neighbors_states = {} self.neighbors = neighbors if neighbors else [] self.consumer = Consumer(self.name, self._handle_message) self.publisher = Publisher(self.name)
def kill(self): """ Simulates a node crash Clears its state Then call recover method """ self.holder = None self.using = False self.is_working = False self.request_q = Fifo() self.asked = False self.neighbors_states = {} self._recover()
def __init__(self, buss_id, address): self.gyro = Gyroscope(buss_id, address) self.gyro_full_scale = 245 self.acc = Accelerometer(buss_id, address) self.acc_full_scale = 2 self.fifo = Fifo(buss_id, address) self.temp = Temperature(buss_id, address)
def main(): number_of_operations = int(input('How many operations? ')) book = Book(Operations(Fifo()), number_of_operations) while book.can_add_operation is not False: book.add_operation(input('Input the operation: ')) for x in range(0, number_of_operations): result = book.execute() if result is not None: print(result)
def test_should_the_ouput_equal_14(): book = Book(Operations(Fifo()), 4) book.add_operation('1 80') book.add_operation('1 14') book.add_operation('2') book.add_operation('3') book.execute() book.execute() book.execute() assert book.execute() == 14
def init(pru_bin): pypruss.modprobe(1024) ddr_addr = pypruss.ddr_addr() ddr_size = pypruss.ddr_size() print "DDR memory address is 0x%x and the size is 0x%x"%(ddr_addr, ddr_size) fifo = Fifo(ddr_addr, ddr_size) pypruss.init() pypruss.open(0) pypruss.pruintc_init() pypruss.pru_write_memory(0,0,[ddr_addr]) pypruss.exec_program(0, pru_bin) return fifo
class Minimu: VERSION = 'minimu9v5' def __init__(self, buss_id, address): self.gyro = Gyroscope(buss_id, address) self.gyro_full_scale = 245 self.acc = Accelerometer(buss_id, address) self.acc_full_scale = 2 self.fifo = Fifo(buss_id, address) self.temp = Temperature(buss_id, address) def enable(self, odr=104): self.gyro.enable(odr) self.gyro_full_scale = self.gyro.get_full_scale_selection() self.acc.enable(odr) self.acc_full_scale = self.acc.get_full_scale_selection() self.fifo.enable(odr) def disable(self): self.gyro.disable() self.acc.disable() self.fifo.disable() def read_fifo_raw(self): data = np.array(self.fifo.get_data(), dtype=np.int) return data def read_fifo(self): data = np.array(self.fifo.get_data(), dtype=np.double) try: data[:, :3] *= 1 except IndexError: sleep(0.1) data = np.array(self.fifo.get_data(), dtype=np.double) data[:, :3] *= self.gyro_full_scale data[:, -3:] *= self.acc_full_scale data[data > 0] /= 32767 data[data < 0] /= 32768 return data def read_temperature(self): return self.temp.get_temperature()
import sys from fifo import Fifo from lru import LRU from opt import Opt def read_file(file_name): with open(file_name, "r") as f: result = map(lambda line: int(line.replace("\n", "")), f.readlines()) return result if __name__ == "__main__": args = sys.argv slots = args[1] file_name = args[2] entries = list(read_file(file_name)) fifo = Fifo(int(slots)) lru = LRU(int(slots)) opt = Opt(int(slots), entries) for index, entrie in enumerate(entries): fifo.add(entrie) lru.add(entrie) opt.add(entrie, index) print( "{} quadros, {} refs: FIFO: {} PFs, LRU: {} PFs, OPT: {} PFs" .format(slots, len(entries), fifo.falts, lru.falts, opt.falts))
In = fifoFull(11) In.displayCountAndElements() print"\n" In = fifoFull(22) In.displayCountAndElements() print"\n" In = fifoFull(33) In.displayCountAndElements() print"\n" print "\n\nOut 1 : ",In.firstOut() In.displayCountAndElements() print"\n" print "\nOut 2 : ",In.firstOut() In.displayCountAndElements() print"\n" print "\nOut 3 : ",In.firstOut() In.displayCountAndElements() print"\n" In = Fifo(66) In.displayCountAndElements() print"\n" In = Fifo(77) In.displayCountAndElements() #############################################################################################
def test_should_burst_exceptions_if_the_number_of_operations_greater_than_or_equal_to_1( ): with raises(ValueError): Book(Operations(Fifo()), -1)
def test_should_burst_exceptions_if_try_execute_with_number_of_operations_less_than_to_0( ): book = Book(Operations(Fifo()), 4) with raises(ValueError, match='Exceeded number of operations allowed'): for x in range(0, 10): book.add_operation('3')
from saa7113 import saa7113 from fifo import Fifo from buffer import Buffer # Use Broadcom pin numbering GPIO.setmode(GPIO.BCM) # Globals packetSize=42 # The input stream packet size. Does not include CRI and FC # Setup saa7120() saa7113() # Objects fifo=Fifo() # buffer stuff head=0 tail=0 BUFFERS = 4 buf = [0] * BUFFERS for i in range(BUFFERS): buf[i]=Buffer() countdown=BUFFERS-1 if countdown<1: countdown=1 GPIO_FLD=22 #define GPIO_FLD 3 -> Broadcom 22 GPIO_CSN=24 #define GPIO_CSN 5 -> Broadcom 24
def test_should_burst_exceptions_if_the_number_of_operations_less_than_or_equal_to_100000( ): with raises(ValueError): Book(Operations(Fifo()), 100001)
class Node: """ Node in the sense of Raymond's algorithm Implements the magical algorithm """ def __init__(self, name, neighbors=None): self.name = name self.holder = None self.using = False self.request_q = Fifo() self.asked = False self.is_recovering = False self.is_working = False self.neighbors_states = {} self.neighbors = neighbors if neighbors else [] self.consumer = Consumer(self.name, self._handle_message) self.publisher = Publisher(self.name) def _assign_privilege(self): """ Implementation of ASSIGN_PRIVILEGE from Raymond's algorithm """ if self.holder == "self" and not self.using and not self.request_q.empty( ): self.holder = self.request_q.get() self.asked = False if self.holder == "self": self.using = True self._enter_critical_section() self._exit_critical_section() else: self.publisher.send_request(self.holder, MSG_PRIVILEGE) def _make_request(self): """ Implementation of MAKE_REQUEST from Raymond's algorithm """ if self.holder != "self" and not self.request_q.empty( ) and not self.asked: self.publisher.send_request(self.holder, MSG_REQUEST) self.asked = True def _assign_privilege_and_make_request(self): """ Calls assign_privilege and make_request sleep(x) allows to display what is happening """ if not self.is_recovering: time.sleep(PROPAGATION_DELAY) self._assign_privilege() self._make_request() def ask_for_critical_section(self): """ When the node wants to enter the critical section """ self.request_q.push("self") self._assign_privilege_and_make_request() def kill(self): """ Simulates a node crash Clears its state Then call recover method """ self.holder = None self.using = False self.is_working = False self.request_q = Fifo() self.asked = False self.neighbors_states = {} self._recover() def _recover(self): """ Implements Raymond's recovering process """ self.is_recovering = True time.sleep(RECOVER_TIMEOUT) for neighbor in self.neighbors: self.publisher.send_request(neighbor, MSG_RESTART) def _receive_request(self, sender): """ When the node receives a request from another """ self.request_q.push(sender) self._assign_privilege_and_make_request() def _receive_privilege(self): """ When the node receives the privilege from another """ self.holder = "self" self._assign_privilege_and_make_request() def _enter_critical_section(self): """ Does stuff to simulate critical section """ self.is_working = True with open("working_proof.txt", "a") as f: f.write(self.name + "\n") time.sleep(WORK_TIME) self.is_working = False def _exit_critical_section(self): """ When the node exits the critical section """ self.using = False self._assign_privilege_and_make_request() def _handle_message(self, ch, method, properties, body): """ Callback for the RabbitMQ consumer Messages are sent with 'node_name.type' routing keys and 'sender' as body """ sender = method.routing_key.split(".")[0] message_type = method.routing_key.split(".")[2] logging.info("## Received %s from %s" % (message_type, sender)) if message_type == MSG_REQUEST: self._receive_request(sender) elif message_type == MSG_PRIVILEGE: self._receive_privilege() elif message_type == MSG_INITIALIZE: self.initialize_network(sender) elif message_type == MSG_RESTART: self._send_advise_message(sender) elif message_type == MSG_ADVISE: message = body.decode("UTF-8") self._receive_advise_message(sender, message) def _receive_advise_message(self, sender, message): """ When the node receives an advise message from another """ state = make_tuple(message) self.neighbors_states[sender] = state if len(self.neighbors_states) == len(self.neighbors): self._finalize_recover() def _finalize_recover(self): """ Finalize recovering process """ # Determine holder for neighbor, state in self.neighbors_states.items(): if not state[0]: self.holder = neighbor break if (not self.holder or self.holder == "self"): # Privilege may be received while recovering self.holder = "self" # Determine asked self.asked = False else: self.asked = self.neighbors_states[self.holder][2] # Rebuild request_q for neighbor, state in self.neighbors_states.items(): if state[0] and state[1] and not neighbor in self.request_q: self.request_q.push(neighbor) self.is_recovering = False self._assign_privilege_and_make_request() def _send_advise_message(self, recovering_node): """ Sends X - Y relationship state: (HolderY == X, AskedY, X in Request_qY) """ state = ( self.holder == recovering_node, self.asked, recovering_node in self.request_q, ) self.publisher.send_request(recovering_node, MSG_ADVISE, str(state)) def initialize_network(self, init_sender=None): """ When initializing, send initialize messages to neighbors BUT the one which sent it to the node (if it exists) """ neighbors = self.neighbors.copy() if init_sender: neighbors.remove(init_sender) self.holder = init_sender else: self.holder = "self" for neighbor in neighbors: self.publisher.send_request(neighbor, MSG_INITIALIZE)
def __init__(self): self.__buff = lzo_buffer(MAX_BLOCK) self.__fifo = Fifo() self.__output = [] self.state = self.STATE_MAGIC
def test_add_patients(self): fifo = Fifo() fifo.add_to_line(Patient("+", 3)) fifo.add_to_line(Patient("+", 6)) fifo.add_to_line(Patient("+", 1)) fifo.add_to_line(Patient("+", 2)) fifo.add_to_line(Patient("+", 4)) self.assertEqual(5, len(fifo.line)) fifo.elapse_time() self.assertEqual(4, len(fifo.line)) fifo.elapse_time() self.assertEqual(3, len(fifo.line)) fifo.elapse_time() self.assertEqual(2, len(fifo.line)) fifo.elapse_time() self.assertEqual(1, len(fifo.line)) fifo.elapse_time() self.assertEqual(1, len(fifo.line)) fifo.elapse_time() self.assertEqual(0, len(fifo.line))
def __init__(self, patience_rate=0): self.corona_plus_patients = Fifo(patience_rate, "plus patients") self.corona_minus_patients = Fifo(patience_rate, "minus patients")
def __init__(self): self.__fifo = Fifo() self.__output = [] self.__output.append(LZO_MAGIC)
def test_patience_time(self): fifo = Fifo() fifo.add_to_line(Patient("+", 2)) fifo.add_to_line(Patient("+", 3)) fifo.add_to_line(Patient("+", 5)) fifo.elapse_time() self.assertEqual(1, fifo.get_next_patient().patience_in_minutes) fifo.elapse_time() fifo.elapse_time() self.assertEqual(2, fifo.get_next_patient().patience_in_minutes) self.assertEqual(0, len(fifo.line)) self.assertIsNone(fifo.get_next_patient())
from fifo import Fifo from aleatory import Aleatory from lfu import Lfu from lru import Lru import sys if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: python3 main.py algorithm cache_size") exit(1) with open("file.txt") as file_: if sys.argv[1] == "fifo": func = Fifo(int(sys.argv[-1])) elif sys.argv[1] == "aleatory": func = Aleatory(int(sys.argv[-1])) elif sys.argv[1] == "lfu": func = Lfu(int(sys.argv[-1])) elif sys.argv[1] == "lru": func = Lru(int(sys.argv[-1])) for line in file_: normalized_line = [x.strip() for x in line.split(",")] for item in normalized_line: func.process(item) print(func)
def queue(): return Fifo()
def test_empty_fifo(self): fifo = Fifo() self.assertEqual(1, fifo.elapse_time()) self.assertIsNone(fifo.get_next_patient())