예제 #1
0
def CHECK_EQ_LIST_UNORDERED(input_list1, input_list2, debug=True):
    '''
    check two lists are equal in ordered way
    '''
    if debug:
        assert islist(input_list1) and islist(
            input_list2), 'input lists are not correct'
    return set(input_list1) == set(input_list2)
예제 #2
0
def construct_dict_from_lists(list_key, list_value, debug=True):
    '''
	construct a distionary from two lists
	'''
    if debug:
        assert islist(list_key) and islist(
            list_value), 'the input key list and value list are not correct'
        assert len(list_key) == len(
            list_value), 'the length of two input lists are not equal'

    return dict(zip(list_key, list_value))
예제 #3
0
def list_reorder(input_list, order_index, debug=True):
    '''
	reorder a list based on a list of index
	'''
    if debug:
        assert islist(input_list) and islist(
            order_index), 'inputs are not two lists'
        assert len(input_list) == len(
            order_index), 'length of input lists is not equal'
        assert all(
            isscalar(index_tmp)
            for index_tmp in order_index), 'the list of order is not correct'

    reordered_list = [
        ordered for whatever, ordered in sorted(zip(order_index, input_list))
    ]
    return reordered_list
예제 #4
0
def remove_list_from_index(list_src, list_index_src, warning=True, debug=True):
    '''
	remove a list "list_to_remove" from a list "list_all_src" based on value
	'''
    if debug:
        assert islist(list_src) and islist(
            list_index_src), 'input lists are not valid'

    input_list = safe_list(list_src, warning=warning, debug=debug)
    index_list = safe_list(list_index_src, warning=warning, debug=debug)
    index_list.sort(reverse=True)
    for item_index in index_list:
        try:
            del input_list[item_index]
        except ValueError:
            if warning:
                print(
                    'Warning!!!!!! Item to remove is not in the list. Remove operation is not done.'
                )

    return input_list
예제 #5
0
def safe_list(input_data, warning=True, debug=True):
	'''
	copy a list to the buffer for use

	parameters:
		input_data:		a list

	outputs:
		safe_data:		a copy of input data
	'''
	if debug: assert islist(input_data), 'the input data is not a list'
	safe_data = copy.copy(input_data)
	return safe_data
예제 #6
0
def scalarlist2floatlist(scalar_list, debug=True):
    '''
	convert a list of scalar to a list of floating number
	'''
    if debug:
        assert islist(scalar_list) and all(
            isscalar(scalar_tmp)
            for scalar_tmp in scalar_list), 'input list is not a scalar list'

    float_list = list()
    for item in scalar_list:
        float_list.append(float(item))
    return float_list
예제 #7
0
def scalarlist2strlist(scalar_list, debug=True):
    '''
	convert a list of scalar to a list of string
	'''
    if debug:
        assert islist(scalar_list) and all(
            isscalar(scalar_tmp)
            for scalar_tmp in scalar_list), 'input list is not a scalar list'

    str_list = list()
    for item in scalar_list:
        str_list.append(str(item))
    return str_list
예제 #8
0
def strlist2floatlist(str_list, warning=True, debug=True):
    '''
	convert a list of string to a list of floating number
	'''
    if debug:
        assert islist(str_list), 'input is not a list'
        assert all(isstring(str_tmp)
                   for str_tmp in str_list), 'input is not a list of string'
    if any(len(str_tmp) == 0 for str_tmp in str_list):
        if warning:
            print(
                'warning: the list of string contains empty element which will be removed before converting to floating number'
            )
        str_list = filter(None, str_list)
    return [float(str_tmp) for str_tmp in str_list]
예제 #9
0
def ord2string(ord_list, debug=True):
    '''
	convert a list of ASCII character to a string
	'''
    if debug:
        assert islist(ord_list) and len(
            ord_list
        ) > 0, 'input should be a list of ord with length larger than 0'
        assert all(
            isinteger(tmp) for tmp in
            ord_list), 'all elements in the list of ord should be integer'

    L = ''
    for o in ord_list:
        L += chr(o)
    return L
예제 #10
0
def floatlist2bytes(float_list, debug=True):
    '''
	convert a list of floating number to bytes
	'''
    if debug:
        assert isfloat(float_list) or (
            islist(float_list)
            and all(isfloat(float_tmp) for float_tmp in float_list)
        ), 'input is not a floating number or a list of floating number'

    # convert a single floating number to a list with one item
    if isfloat(float_list): float_list = [float_list]
    try:
        binary = struct.pack('%sf' % len(float_list), *float_list)
    except ValueError:
        print('Warnings!!!! Failed to convert to bytes!!!!!')

    return binary
예제 #11
0
def onehot2string(onehot, warning=True, debug=True):
    '''
	convert a set of one hot vector to a string
	'''
    if isnparray(onehot):
        onehot.ndim == 2, 'input should be 2-d numpy array'
        onehot = list(onehot)
    elif islist(onehot):
        assert CHECK_EQ_LIST([
            tmp.ndim for tmp in onehot
        ]), 'input list of one hot vector should have same length'
    else:
        assert False, 'unknown error'

    if debug:
        assert all(
            sum(onehot_tmp) == 1 and np.count_nonzero(onehot_tmp) == 1
            for onehot_tmp in
            onehot), 'input numpy array is not a set of one hot vector'
    ord_list = [onehot2ord(onehot_tmp) for onehot_tmp in onehot]
    return ord2string(ord_list)
예제 #12
0
def test_islist():
    input_test = []
    assert islist(input_test)
    input_test = ['']
    assert islist(input_test)
    input_test = [1]
    assert islist(input_test)
    input_test = [1, 2, 3]
    assert islist(input_test)
    input_test = [[], []]
    assert islist(input_test)
    input_test = list()
    assert islist(input_test)

    input_test = 123
    assert islist(input_test) is False
    input_test = False
    assert islist(input_test) is False
    input_test = dict()
    assert islist(input_test) is False
    input_test = 'ss'
    assert islist(input_test) is False
    input_test = np.array(('sss'))
    assert islist(input_test) is False
    input_test = ('syt')
    assert islist(input_test) is False
예제 #13
0
def CHECK_EQ_LIST_SELF(input_list, debug=True):
    '''
	check all elements in a list are equal
	'''
    if debug: assert islist(input_list), 'input is not a list'
    return input_list[1:] == input_list[:-1]
예제 #14
0
def list2tuple(input_list, debug=True):
    '''
	convert a list to a tuple
	'''
    if debug: assert islist(input_list), 'input is not a list'
    return tuple(input_list)
예제 #15
0
def remove_empty_item_from_list(list_to_remove, debug=True):
    '''
	remove an empty string from a list
	'''
    if debug: assert islist(list_to_remove), 'input list is not a list'
    return remove_item_from_list(list_to_remove, '', debug=debug)