Skip to content
Snippets Groups Projects
block_processor.py 1.78 KiB
Newer Older
  • Learn to ignore specific revisions
  • tuhe's avatar
    tuhe committed
    from snipper.fix_s import get_s #!s
    from snipper.block_parsing import block_split
    import textwrap
    import numpy as np
    
    # Implement a sieve here.
    def primes_sieve(limit):
        limitn = limit+1 #!b
        primes = range(2, limitn)
    
        for i in primes:
            factors = list(range(i, limitn, i))
            for f in factors[1:]:
                if f in primes:
                    primes.remove(f)
        return primes #!b
    #!s
    
    def obscure(blk, fun):
        blok = block_split(blk, "#!b")
        lines2 = blok['first'] + fun(blok['block']) + blok['last']
        s = '\n'.join(lines2)
        print(s)
        return s
    
    # driver program
    if __name__ == '__main__':
        with open(__file__, 'r') as f:
            s = f.read().splitlines()
        blk = get_s(s)['']
    
        def cmnt(lines):
            whitespace = " " * (len(lines[0]) - len(lines[0].lstrip()))
            lines = textwrap.dedent("\n".join(lines)).splitlines()
            lines = ["# " + l for l in lines]
            return lines, whitespace
    
        def f1(lines):
            lines, whitespace = cmnt(lines)
            lines = [lines[i] for i in np.random.permutation(len(lines))]
            lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
            return lines
    
        obscure(blk, f1)
    
        def f2(lines):
            lines, whitespace = cmnt(lines)
            kp = """#'"[](){},.+-012345679:="""
            l2 = []
            for line in lines:
                line2 = []
                for w in line.split(' '):
                    if w in ['', 'return', 'if', 'else' '=', '#', "for", "in"]:
                        line2.append(w)
                    else:
                        w2 = "".join( [ (t if t in kp else '?') for t in w] )
                        line2.append(w2)
                l2.append(" ".join(line2))
            lines = l2
            lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
            return lines
    
        obscure(blk, f2)