Demo entry 6355122

temp

   

Submitted by anonymous on Apr 12, 2017 at 14:25
Language: Python 3. Code size: 4.6 kB.

from textwrap import dedent, indent
from scipy import misc

def cleancode(str_in, indent_level=0, strip=True, do_dedent=True, ln_end=True):
    str_out = str_in
    if do_dedent:
        str_out = dedent(str_in)
    if strip:
        str_out = '\n'.join(i for i in str_out.split('\n') if i.strip()!='') 
    str_out = indent(str_out, "    "*indent_level)
    if ln_end:
        str_out += '\n';
       
    return str_out


unique_vars_precompiled = {}
def unique_vars(list_of_iters):
    """
    #An example of the code template generated for len(list_of_iters)=3
    indices = []
    for x0 in list_of_iters[0]:
        if not x0 in []:
            for x1 in list_of_iters[1]:
                if not x1 in [x0]:
                    for x2 in list_of_iters[2]:
                        if not x2 in [x0,x1]:
                            indices.append([x0,x1,x2])
    """
    
    n = len(list_of_iters)
    if not n in unique_vars_precompiled:
        varnames = ["x%d"%i for i in range(n)]
        varnamestacks = [','.join(varnames[:i]) for i in range(n+1)]

        indent_level = 0
        str_code = cleancode(      'indices = []'                  )
        for i in range(n):         #>
            str_code += cleancode( 'for %s in list_of_iters[%d]:'% (varnames[i],i), indent_level)
            indent_level+=1        #->
            str_code += cleancode( 'if not %s in [%s]:'%           (varnames[i],varnamestacks[i]), indent_level)
            indent_level+=1        #-->
        str_code += cleancode(     'indices.append([%s])'%         (varnamestacks[i+1]), indent_level)

        execstr = compile(str_code, '', 'exec')
        unique_vars_precompiled[n] = execstr
   
    print(str_code)
    d = locals()
    exec(unique_vars_precompiled[n], d, d)
   
    return d['indices']


no_pair_repeats_precompiled = {}
def no_pair_repeats(domains_left, domains_right):
    """
    #An example of the code template generated for K=3
    table=[]
    for a0 in domains_left[0]:
        if not a0 in []:
            for a1 in domains_left[1]:
                if not a1 in [a0]:
                    for a2 in domains_left[2]:
                        if not a2 in [a0,a1]:
                            for b0 in domains_right[0]:
                                if not b0 in []:
                                    for b1 in domains_right[1]:
                                        if not b1 in [b0]:
                                            for b2 in domains_right[2]:
                                                if not b2 in [b0,b1]:
                                                    if not(a0==b0 and a1==b1 and a2==b2):
                                                        table.append([a0,a1,a2,b0,b1,b2])
    """
    K = len(domains_left)
   
    if not K in unique_vars_precompiled:
        # Make sure there are no pair-repeat for any layer
        indent_level=0             #
        str_code = cleancode(      'table=[]')
        for i in range(K):         #>
            str_code += cleancode( 'for a%d in domains_left[%d]:'%   (i,i), indent_level)
            indent_level+=1        #->
            str_code += cleancode( 'if not a%d in [%s]:'%            (i, ','.join('a%d'%j for j in range(i))),
                                                                                  indent_level)
            indent_level+=1
        for i in range(K):         #-->
            str_code += cleancode( 'for b%d in domains_right[%d]:'%  (i,i), indent_level)
            indent_level+=1        #--->
            str_code += cleancode( 'if not b%d in [%s]:'%            (i, ','.join('b%d'%j for j in range(i))),
                                                                                  indent_level)
            indent_level+=1        #---->
        str_code += cleancode(     'if not(%s):'%                    (' and '.join(['a%d==b%d'%(i,i) for i in range(K)])),
                                                                                   indent_level)
        indent_level+=1            #----->
        str_code += cleancode(     'table.append([%s])'%             (','.join(['a%d'%i for i in range(K)]+
                                                                               ['b%d'%i for i in range(K)])),
                                                                               indent_level)
        execstr = compile(str_code, '', 'exec')
        no_pair_repeats_precompiled[K] = execstr
       
    d = locals()
    exec(no_pair_repeats_precompiled[K], d, d)
   
    return d['table']

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).