In [12]:
from KBs import *
import itertools
# itm = henseldict.items()
henseldict = {}

for x in itertools.product(['b','s'],range(9)):
    bs = x[0]
    pref = ''.join(str(x) for x in  x)
    val = [k[-1] for k in hensellist if k.startswith(pref) ]
    henseldict[str(x[1])]=val
henseldict


Out[12]:
{'0': ['_'],
 '1': ['c', 'e'],
 '2': ['a', 'c', 'e', 'k', 'i', 'n'],
 '3': ['i', 'a', 'n', 'j', 'r', 'e', 'c', 'q', 'y', 'k'],
 '4': ['a', 'r', 'i', 'n', 'w', 'k', 'y', 'q', 't', 'j', 'z', 'e', 'c'],
 '5': ['i', 'a', 'j', 'n', 'r', 'q', 'c', 'y', 'k', 'e'],
 '6': ['a', 'c', 'k', 'n', 'e', 'i'],
 '7': ['c', 'e'],
 '8': ['_']}

In [19]:
def invert(s):
    print s
    num = s[0]
    conf = henseldict[num]
    return ''.join([x for x in conf if x not in s])
# print invert('5-kn')

In [36]:
# ?re.sub
alias = 'B1c2ae3aknq4aijqty5-kn6-n78/S01e2ackn3aekq4ijntwyz5-iq6eikn78'
o = re.search('(\d-[a-zA-Z]+)',alias)
print o.group()


('5-kn',)
('5-kn',)
5-kn

In [34]:
??re.subn

In [44]:
from KBs import *
import re
alias = 'B1c2ae3aknq4aijqty5-kn6-n78/S01e2ackn3aekq4ijntwyz5-iq6eikn78'

def invert(s):
    num = s[0]
    conf = henseldict[num]
    return ''.join([num]+[x for x in conf if x not in s])
def alias2rulestr(self,alias): 
# alias.replace('-','')
    alias = re.sub('(\d-[a-zA-Z]+)',lambda o:invert(o.group()),alias)
    alias = p_NOTnumletter.sub( '', alias).lower()
    OUT = ['0']*102
    print alias
    d = collections.OrderedDict((('b',{}),('s',{}))) ### set default
    # d.update()
    # alias.split('s')
    s = alias
    lst = [x for x  in re.split("([bs])", s) if x]
    if len(lst) % 2: #### Padding to even length
        lst += ['']
    d  = dict(ntuple(lst,2))
    idxs = []
    for k, v in d.items():
        s = v
        lst = [x for x in re.split("(\d)", s) if x]
        L  = len(lst)
        v_old = ''
        for i,v in enumerate(lst):
            if v.isdigit():
                if v_old.isdigit():
                    idx = [henselidx.get( k + v_old + c,None) for c in subconf]
                    idxs.extend(idx)
                if i + 1 == L:
                    idx = [henselidx.get( k + v + c,None) for c in subconf]
                    idxs.extend(idx)
                num = v
            else:
                idx = [henselidx[ k + num + v_i]  for v_i in v ]
                idxs.extend(idx)
            v_old = v
    idxs = [ x for x in idxs if x is not None] 
    for i in idxs:
        if not i is None:
            OUT[i] = '1'
    bitstr=''.join(OUT[::-1]);
    hexstr=hex(int(bitstr,2)).lstrip('0x').rstrip('L').zfill(26)
    return hexstr
alias2rulestr(None,alias)


b1c2ae3aknq4aijqty5iajrqcye6ackei78s01e2ackn3aekq4ijntwyz5ajnrcyke6eikn78
Out[44]:
'3fdbe9b8fd5cfaf6453019daca'

In [ ]:
111110100000111011010000000001010110010100000111011010011101000100110100010000000110011101101011001010
111110100000111011010000000001010110010100000111011010011101000100110100000000000110011101101011001010