Exemplo n.º 1
0
def parsepat(cat):
    '''
    Open file with the name cat, extract RLE,
    and convert it into glife.pattern.
    '''
    with open(os.path.join(ptbcatdir, cat)) as catfile:
        for line in catfile:
            # Get the first non-comment and parse its RLE string
            if not iscomment(line):
                return glife.pattern(line)
Exemplo n.º 2
0
def parsefreq(freq):
    '''Get the frequency and convert it to glife.pattern.'''
    # Format string and convert it to cell list.
    pat = list(make_text('{:.3f}'.format(freq), font='mono'))
    # Convert one-state cell list to multi-state cell list
    pat = g.join(pat, [0])
    # Set cell state to state 4
    for idx in xrange(2, len(pat), 3):
        pat[idx] = 4
    return glife.pattern(pat)
Exemplo n.º 3
0
def parsename(name):
    '''Get the catalyst name and convert it to glife.pattern.'''
    # Convert the string to cell list.
    pat = list(make_text(name, font='mono'))
    # Convert one-state cell list to multi-state cell list
    pat = g.join(pat, [0])
    # Set cell state to state 4
    for idx in xrange(2, len(pat), 3):
        pat[idx] = 4
    return glife.pattern(pat)
Exemplo n.º 4
0
def color_text(string, extrastate):
    t = make_text(string, "mono")
    bbox = getminbox(t)
    # convert two-state pattern to multi-state and set state to extrastate
    mlist = []
    tlist = list(t)
    for i in xrange(0, len(tlist), 2):
        mlist.append(tlist[i])
        mlist.append(tlist[i + 1])
        mlist.append(extrastate)
    if len(mlist) % 2 == 0: mlist.append(0)
    p = pattern(mlist)
    return p, bbox.wd, bbox.ht
Exemplo n.º 5
0
def color_text(string, extrastate):
    t = make_text(string, "mono")
    bbox = getminbox(t)
    # convert two-state pattern to multi-state and set state to extrastate
    mlist = []
    tlist = list(t)
    for i in xrange(0, len(tlist), 2):
        mlist.append(tlist[i])
        mlist.append(tlist[i+1])
        mlist.append(extrastate)
    if len(mlist) % 2 == 0: mlist.append(0)
    p = pattern(mlist)
    return p, bbox.wd, bbox.ht
Exemplo n.º 6
0
def iscomment(line):
    return line.strip() == "" or line.lstrip().startswith("#")


g.setrule("LifeHistory")

try:
    currcats = []

    with open(catfilename) as catfile:
        for line in catfile:
            if iscomment(line):
                # Organize all the previous cats.
                if currcats:
                    currcat = reduce(operator.add, (cat.translate(0, idx * size) for idx, cat in enumerate(currcats)))
                    cats.append(currcat)
                # Prepare new cat container
                currcats = []
            else:
                currcats.append(glife.pattern(line))
        currcat = reduce(operator.add, (cat.translate(0, idx * size) for idx, cat in enumerate(currcats)))
        cats.append(currcat)
except:
    if catfilename == "":
        g.exit()
    else:
        raise

totalpat = reduce(operator.add, (currcat.translate(idx * size, 0) for idx, currcat in enumerate(cats)))
totalpat.put()