Пример #1
0
def _next_combination(binlist):
    '''
        >>> binlist = [1, 0, 0, 1, 1]
        >>>
        >>> _next_combination(binlist)
        [1, 0, 1, 0, 1]
        >>>
        >>> _next_combination(binlist)
        [1, 0, 1, 1, 0]
        >>> _next_combination(binlist)
        [1, 1, 0, 1, 0]
        >>> _next_combination(binlist)
        [1, 1, 1, 0, 0]
        >>> _next_combination(binlist)
        >>>
        >>> binlist = [1, 0, 1, 1, 0]
        >>> _next_combination(binlist)
        [1, 1, 0, 1, 0]
        >>> _next_combination(binlist)
        [1, 1, 1, 0, 0]
        >>> _next_combination(binlist)
        >>>
    '''
    lngth = len(binlist)
    last = binlist[-1]
    if (last == 1):
        last_zero_index = elel.find_fst_index_eq_via_reversing(0, binlist)
        if (last_zero_index == None):
            return (None)
        else:
            pass
        next_one_index = last_zero_index + 1
        elel.swap(last_zero_index, next_one_index, binlist)
        #
        hl = binlist[:next_one_index + 1]
        tl = binlist[next_one_index + 1:]
        tl = _get_minbinlist(tl)
        binlist = elel.concat(hl, tl)
        #
    else:
        last_one_index = elel.find_fst_index_eq_via_reversing(1, binlist)
        prev_zero_index = elel.find_fst_index_eq_via_reversing(
            0, binlist[:last_one_index])
        if (prev_zero_index == None):
            return (None)
        else:
            pass
        prev_one_index = prev_zero_index + 1
        elel.swap(prev_zero_index, prev_one_index, binlist)
        #
        hl = binlist[:prev_one_index + 1]
        tl = binlist[prev_one_index + 1:]
        tl = _get_minbinlist(tl)
        binlist = elel.concat(hl, tl)
        #
    return (binlist)
Пример #2
0
def _repl_cols(df,poses,*args,**kwargs):
    df = copy.deepcopy(df)
    args = list(args)
    if(isinstance(args[0],dict)):
        kl,vl = eded.d2kvlist(args[0])
    else:
        if(isinstance(args[1],list)):
            kl =  elel.select_evens(args)
            vl =  elel.select_odds(args)
        else:
            kl,vl = elel.brkl2kvlist(args,df.index.__len__()+1)
    if(isinstance(poses[0],int)):
        pass
    else:
        colnames = poses 
        clocs_array = []
        for i in range(colnames.__len__()):
            clocs = _cn2clocs(colnames[i],**kwargs)
            clocs_array.append((clocs,i))
        if("whiches" in kwargs):
            whiches = kwargs['whiches']
            clocs_array = elel.mapv(clocs_array,lambda ele:ele[0])
            clocs = elel.batexec(lambda clocs,which:clocs[which],clocs_array,whiches)
            poses = clocs
        else:
            #by default replace all
            nkl = []
            nvl = []
            nclocs = []
            for i in range(clocs_array.__len__()):
                clocs = clocs_array[i][0]
                index = clocs_array[i][1]
                tmpkl = elel.init(clocs.__len__(),kl[i])
                tmpvl = elel.init(clocs.__len__(),vl[i])
                nkl = elel.concat(nkl,tmpkl)
                nvl = elel.concat(nvl,tmpvl)
                nclocs = elel.concat(nclocs,clocs)
            #batsort
            poses = nclocs
            kl,vl = elel.batsorted(nclocs,nkl,nvl)
    poses = elel.mapv(poses,lambda pos:pos+1)
    poses.sort()
    for i in range(0,poses.__len__()):
        pos = poses[i]
        df.insert(pos,kl[i],vl[i],kwargs['allow_duplicates'])
        pos = pos -1
        all_clocs = elel.init_range(0,df.columns.__len__(),1)
        all_clocs.remove(pos)
        df = df.iloc[:,all_clocs]
    return(df)
Пример #3
0
def dcds1(one):
    '''
        recur
    '''
    d = dcds1_norecur(one)
    ele = {'data': d, 'pl': []}
    unhandled = [ele]
    while (unhandled.__len__() > 0):
        #yield(d)
        next_unhandled = []
        for i in range(0, unhandled.__len__()):
            ele = unhandled[i]
            data = ele['data']
            ppl = ele['pl']
            cond = dcds1_is_leaf(data)
            if (cond):
                pass
            else:
                children, rchildren, plsuffix = dcds1_get_children(d, ppl)
                cpl = copy.deepcopy(ppl)
                if (plsuffix == None):
                    pass
                else:
                    cpl.append(plsuffix)
                eded._setitem_via_pathlist(d, cpl, rchildren)
                next_unhandled = elel.concat(next_unhandled, children)
        unhandled = next_unhandled
    return (d)
Пример #4
0
def _get_clocs(colnames,**kwargs):
    clocs = []
    for i in range(colnames.__len__()):
        colname = colnames[i]
        tmp = _cn2clocs(colname,**kwargs)
        clocs = elel.concat(clocs,tmp)
    clocs.sort()
    return(clocs)
Пример #5
0
def _get_rlocs(rownames,**kwargs):
    rlocs = []
    for i in range(rownames.__len__()):
        rowname = rownames[i]
        tmp = _rn2rlocs(rowname,**kwargs)
        rlocs = elel.concat(rlocs,tmp)
    rlocs.sort()
    return(rlocs)
Пример #6
0
def get_css_rule_cil(rule):
    ats = get_atkey(rule)
    head = [ats]
    pcil = get_prelude_cil(rule)
    head = elel.concat(head, pcil)
    c = dcds1(rule)
    ccil = cntnt2cil(c['content'], for_display=False)
    rslt = [head, ccil]
    return (rslt)
Пример #7
0
def _next_permutation(arr):
    pair = elel.find_fst_indexpair_fstltsnd_via_reversing(arr)
    if (pair == None):
        return (None)
    else:
        j = elel.find_fst_index_gt_via_reversing(arr[pair[0]], arr)
        arr = elel.swap(pair[0], j, arr)
        init = arr[:pair[1]]
        tail = sorted(arr[pair[1]:])
        arr = elel.concat(init, tail)
        return (arr)
Пример #8
0
def type_q_sort_by_q(darr):
    darr1 = elel.cond_select_values_all(darr,
                                        cond_func=lambda ele:
                                        (ele['q'] == None))
    darr2 = elel.cond_select_values_all(darr,
                                        cond_func=lambda ele:
                                        (ele['q'] != None))
    ndarr1 = elel.sortDictList(darr1, cond_keys=['q', 'type'], reverse=True)
    idarr2 = elel.array_map(darr2, type_q_floatize)
    ndarr2 = elel.sortDictList(darr2, cond_keys=['q', 'type'], reverse=True)
    ndarr = elel.concat(ndarr1, ndarr2)
    return (ndarr)
Пример #9
0
def gen_4e_mat(rule, **kwargs):
    if ("codec" in kwargs):
        codec = kwargs['codec']
    else:
        codec = 'utf-8'
    if ("prelude" in kwargs):
        prelude = kwargs['prelude']
    else:
        prelude = 'str'
    if ("content" in kwargs):
        content = kwargs['content']
    else:
        content = 'format'
    mat = []
    d = gen_4e(rule, prelude)
    root = {"data": d, "pl": [0]}
    unhandled = [root]
    while (unhandled.__len__() > 0):
        next_unhandled = []
        mat.append([])
        level = mat[mat.__len__() - 1]
        for i in range(0, unhandled.__len__()):
            ele = unhandled[i]
            data = ele['data']
            ppl = ele['pl']
            #####
            rules = srlz2rules(data['content'])
            ####
            cond = fe_is_leaf(rules)
            if (cond):
                ele['leaf'] = True
                if (content == 'format'):
                    data['content'] = cbs_fmt(data['content'])
                else:
                    pass
            else:
                ele['leaf'] = False
                si = next_unhandled.__len__()
                children = srlz_get_children_from_rules(
                    rules, si, ppl, prelude, codec)
                next_unhandled = elel.concat(next_unhandled, children)
            level.append(ele)
        unhandled = next_unhandled
    return (mat)
Пример #10
0
def _rmrows(df,*rownames,**kwargs):
    df = copy.deepcopy(df)
    rownames = list(rownames)
    if(isinstance(rownames[0],list)):
        rownames = rownames[0]
    else:
        pass
    rlocs_array = []
    for i in range(rownames.__len__()):
        rlocs = _rn2rlocs(rownames[i],**kwargs)
        rlocs_array.append(rlocs)
    if("whiches" in kwargs):
        whiches = kwargs['whiches']
        rlocs = elel.batexec(lambda rlocs,which:rlocs[which],rlocs_array,whiches)
    else:
        #by default remove all
        rlocs = elel.concat(*rlocs_array)
    all_rlocs = elel.init_range(0,df.index.__len__(),1)
    lefted_rlocs = elel.select_seqs_not(all_rlocs,rlocs)
    return(df.iloc[lefted_rlocs])
Пример #11
0
def _rmcols(df,*colnames,**kwargs):
    df = copy.deepcopy(df)
    colnames = list(colnames)
    if(isinstance(colnames[0],list)):
        colnames = colnames[0]
    else:
        pass
    clocs_array = []
    for i in range(colnames.__len__()):
        clocs = _cn2clocs(colnames[i],**kwargs)
        clocs_array.append(clocs)
    if("whiches" in kwargs):
        whiches = kwargs['whiches']
        clocs = elel.batexec(lambda clocs,which:clocs[which],clocs_array,whiches)
    else:
        #by default remove all
        clocs = elel.concat(*clocs_array)
    all_clocs = elel.init_range(0,df.columns.__len__(),1)
    lefted_clocs = elel.select_seqs_not(all_clocs,clocs)
    return(df.iloc[:,lefted_clocs])
Пример #12
0
def cntnt_rm_whitespace(content):
    content = cntnt_fmt_norecur(content)
    rslt = elel.init(content.__len__())
    unhandled = cntnt_init_unhandled(content)
    while (unhandled.__len__() > 0):
        next_unhandled = []
        for i in range(0, unhandled.__len__()):
            ele = unhandled[i]
            data = ele['data']
            ppl = ele['pl']
            cond = cntnt_is_leaf(data)
            if (cond):
                rslt = elel.setitem_via_pathlist(rslt, data, ppl)
            else:
                children = cntnt_get_children(data, ppl)
                rchildren = elel.array_map(children,
                                           lambda child: child['data'])
                dummy = copy.deepcopy(data)
                dummy['content'] = rchildren
                rslt = elel.setitem_via_pathlist(rslt, dummy, ppl)
                next_unhandled = elel.concat(next_unhandled, children)
        unhandled = next_unhandled
    return (rslt)
Пример #13
0
def cntnt2cil(content, for_display=False):
    ####for comments
    if (content == None):
        return ([])
    else:
        pass
    ####
    content = cntnt_fmt_norecur(content)
    rslt = elel.init(content.__len__())
    unhandled = cntnt_init_unhandled(content)
    while (unhandled.__len__() > 0):
        next_unhandled = []
        for i in range(0, unhandled.__len__()):
            ele = unhandled[i]
            data = ele['data']
            ppl = ele['pl']
            cond = cntnt_is_leaf(data)
            rpl = elel.remove_all(ppl, 'content')
            if (cond):
                s = encd_one(data)
                rslt = elel.setitem_via_pathlist(rslt, s, rpl)
            else:
                # treat square_blk as a leaf
                if (data['type'] == '[] block'):
                    s = encd_square_blk(data, for_display)
                    rslt = elel.setitem_via_pathlist(rslt, s, rpl)
                # treat paren_blk as a leaf
                elif (data['type'] == '() block'):
                    s = encd_paren_blk(data)
                    rslt = elel.setitem_via_pathlist(rslt, s, rpl)
                else:
                    children = cntnt_get_children(data, ppl)
                    dummy = elel.init(children.__len__())
                    rslt = elel.setitem_via_pathlist(rslt, dummy, rpl)
                    next_unhandled = elel.concat(next_unhandled, children)
        unhandled = next_unhandled
    return (rslt)
Пример #14
0
def dig(root, func_is_leaf, get_children, **kwargs):
    '''
    '''
    if ('func_is_leaf_args' in kwargs):
        func_is_leaf_args = kwargs['func_is_leaf_args']
    else:
        func_is_leaf_args = []
    if ('get_children_args' in kwargs):
        get_children_args = kwargs['get_children_args']
    else:
        get_children_args = []
    unhandled = [root]
    while (unhandled.__len__() > 0):
        yield (unhandled)
        next_unhandled = []
        for i in range(0, unhandled.__len__()):
            ele = unhandled[i]
            cond = func_is_leaf(ele, *func_is_leaf_args)
            if (cond):
                pass
            else:
                children = get_children(ele, *get_children_args)
                next_unhandled = elel.concat(next_unhandled, children)
        unhandled = next_unhandled
Пример #15
0
def centerlize(lines, cw, top, left):
    tb_padding = creat_tb_padding(cw, top)
    lines = padding_left(lines, left)
    return (elel.concat(tb_padding, lines, tb_padding))
Пример #16
0
def wfs_eles(wfsm):
    eles = elel.concat(*wfsm)
    return(eles)
Пример #17
0
def m2wfsel(m):
    l = elel.concat(*m)
    return(l)
Пример #18
0
def get_layer(nonleaf_unhandled):
    childrens = elel.mapv(nonleaf_unhandled, get_children)
    layer = elel.concat(*childrens)
    return (layer)
Пример #19
0
def _prev_combination(binlist):
    '''
        >>> _prev_combination([1,1,1,1,1])
        >>> _prev_combination([1,1,1,0,1])
        [1, 1, 0, 1, 1]
        >>> _prev_combination([1,1,0,1,1])
        [1, 0, 1, 1, 1]
        >>>
        >>> _prev_combination([1,0,1,1,1])
        [0, 1, 1, 1, 1]
        >>> _prev_combination([0,1,1,1,1])
        >>>
        >>>
        >>> _prev_combination([1, 0, 1, 1, 0])
        [1, 0, 1, 0, 1]
        >>>
        >>> _prev_combination([1, 0, 1,0,1])
        [1, 0, 0, 1, 1]
        >>>
        >>> _prev_combination([1, 0, 0,1,1])
        [0, 1, 0, 1, 1]
        >>> _prev_combination([0,1,0,1,1])
        [0, 0, 1, 1, 1]
        >>> _prev_combination([0,0,1,1,1])
        >>>
    '''
    lngth = len(binlist)
    last = binlist[-1]
    if (last == 1):
        '''
            [1,0,0,1,1]
               ^-------------------find this 0
            [0,1,0,1,1]
             ^ ^ ------------------swap with 1 after this 0
            [1,0,1,0,1]
                   ^-------------------find this 0
            [0,1,1,0,1]
                 ^ ^ ------------------swap with 1 after this 0             
            [1,1,1,1,1]  --------None
            [0,0,1,1,1]  --------None     0...0  1...1  
        '''
        #从后往前找,find last zero
        last_zero_index = elel.find_fst_index_eq_via_reversing(0, binlist)
        if (last_zero_index == None):
            return (None)
        else:
            pass
        #从last-zero开始,从后往前找, 找到第一个1
        prev_one_index = elel.find_fst_index_eq_via_reversing(
            1, binlist[:last_zero_index])
        if (prev_one_index == None):
            return (None)
        else:
            pass
        #这个1后面(从前往后)肯定是0,记录这个0
        prev_zero_index = prev_one_index + 1
        elel.swap(prev_zero_index, prev_one_index, binlist)
        #
        hl = binlist[:prev_zero_index + 1]
        tl = binlist[prev_zero_index + 1:]
        tl = _get_maxbinlist(tl)
        binlist = elel.concat(hl, tl)
        #
    else:
        '''
            [1,0,1,1,0]
                   ^----------------------find this 1
            [1,0,1,1,0]
                   ^ ^ --------------------swap with 1 before this 0
            [0,0,0,0,0]-----------------None
        '''
        last_one_index = elel.find_fst_index_eq_via_reversing(1, binlist)
        if (last_one_index == None):
            return (None)
        else:
            pass
        next_zero_index = last_one_index + 1
        elel.swap(next_zero_index, last_one_index, binlist)
        #
        hl = binlist[:next_zero_index + 1]
        tl = binlist[next_zero_index + 1:]
        tl = _get_maxbinlist(tl)
        binlist = elel.concat(hl, tl)
        #
    return (binlist)