Commit 50ae877c authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common component:

- Fixed object factory methods on Optical Links
parent 6e42db53
Loading
Loading
Loading
Loading
+20 −22
Original line number Diff line number Diff line
@@ -14,30 +14,28 @@

import copy

def convert_to_dict(single_val:int)->dict:
def convert_to_dict(single_val: int, start_point: int = 0, width: int = None) -> dict:
    slot = dict()
    bin_num = bin(single_val)
    sliced_num=bin_num[2:]
    for i in range(len(sliced_num)):
        slot[str(i+1)]=int(sliced_num[i])
    sliced_num = bin(single_val)[2:]
    if width is not None:
        sliced_num = sliced_num.zfill(width)
    for i, bit in enumerate(sliced_num):
        slot[str(start_point + i)] = int(bit)
    return slot

def correct_slot(dic: dict) -> dict:
    _dict = copy.deepcopy(dic)
    keys_list = list(_dict.keys())
    if len(keys_list) < 20:
        num_keys = [int(i) for i in keys_list]
        if num_keys[-1] != 20:
            missed_keys = []
            diff = 20 - len(num_keys)
            #print(f"diff {diff}")
            for i in range(diff+1):
                missed_keys.append(num_keys[-1]+i)
            #print(f"missed_keys {missed_keys}")
            for key in missed_keys :
                _dict[key]=1
            #print(f"result {_dict}")
    return _dict
def correct_slot(dic: dict, width: int = None) -> dict:
    corrected = copy.deepcopy(dic)
    if len(corrected) == 0:
        return corrected

    normalized = {int(key): int(value) for key, value in corrected.items()}
    max_slot = max(normalized.keys())
    max_range = width if width is not None else max_slot + 1

    for slot_idx in range(max_range):
        normalized.setdefault(slot_idx, 1)

    return {str(key): normalized[key] for key in sorted(normalized.keys())}


## To be deleted , needed now for development purpose ##