printing out dictionnaires

I have a rather specific question:

I want to print out characters at a specific place using the \033[ syntax. This is what the code below should do: (the dict cells has the same keys as coords but with either '*' or '-' as value.)

coords = {'x'+str(x)+'y'+str(y) : (x,y) for x,y, in itertools.product(range(60), range(20))}

for key, value in coords.items():  

    char = cells[key]  
    x,y = value
    HORIZ=str(x)
    VERT=str(y)
    char = str(char)
    print('\033['+VERT+';'+HORIZ+'f'+char)

However, I noticed that if I put this into a infinite while loop, it does not always prints the same characters at the same position. There are only slight changes, but it deletes some and puts them back in after some loops. I already tried it with lists, and there it seems to behave just fine, so I tend to think it has something todo with the dict, but I can not figure out what it could be. You can see the Problem in a console here: SharedConsole.I am happy for every tip on this matter.

On a related topic: After the printing, some changes should be made at the values of the cells dict, but for reason unknown to me, the only the first two rules are executed and the rest is ignored. The rules should test how many neighbours (which is in population) are around the cell and apply the according rule. In my implemention of this I have some kind of weird tumor growth (which should not happen, as if there more than three around they should the cell should die) (see FreakingTumor):

if cells_copy [coord] == '-':
    if population == 3:
        cells [coord] = '*'
if cells_copy [coord] == '*':
    if population > 3:
        cells [coord] = '-'
    elif population <= 1:
        cells [coord] = '-'
    elif population == 2 or 3:
        cells [coord] = '*'

I checked the population variable several times, so I am quite sure that is not the matter. I am sorry for the slow consoles.

Thanks in advance! Kyril

Answers


Your syntax

    elif population == 2 or 3:

evaluates as

    elif (population == 2) or (3 != 0):

and 3 is never 0, so this clause is always True. What you meant to write was

    elif population == 2 or population == 3:

or maybe

    elif population in (2,3):

This logic could be reduce to

EMPTY = '-'
FULL = '*'
if cells_copy[coord] == EMPTY:
    if population == 3:
        cells[coord] = FULL
else:
    if population not in set([2,3]):
        cells[coord] = EMPTY

I also suggest coalescing your writes (writing a full line at a time):

# Make sure everything in `cells` is a string
ESC = chr(27)

for y in xrange(20):
    outp = (cells['x{}y{}'.format(x,y)] for x in xrange(60))
    print('{}[{};{}f{}'.format(ESC, y, x, ''.join(outp)), end='')

You would probably also benefit from making cells a list instead of a dict

WIDTH = 60
HEIGHT = 20
cells = [[EMPTY]*WIDTH for y in xrange(HEIGHT)]

Need Your Help

UpdatePanel not working in IE or Chrome

asp.net ajax asp.net-ajax updatepanel

I have an updatepanel on my masterpage. Within the contentplace holder I have my update progress control. When a user clicks on the button I load some data into a gridview. This works perfectly in

How to configure JMX to bind to localhost only?

java jmx

I run Tomcat8 using JDK8 on Centos6.