Esempio n. 1
0
        def figure(frame):
            fig = PyFig(size=(550,400), frame_num=frame, y=25, name_right=230, val_gap=75)

            n_nums = fig.auto_name("nums")
            l_nums = fig.list(texts=["1", "2", "3"], pos=fig.val_for_name(n_nums), set=8, fade=1)
            fig.reference(n_nums, l_nums[0])

            the_frame = fig.frame(pos=fig.next_frame(), size=(230,200), text="augment_twice_bad", rise=2, set=7, fade=1)
            n_a_list = fig.auto_name("a_list", rise=2, set=5)
            fig.reference(n_a_list, l_nums[0])
            n_val = fig.auto_name("val", rise=2, set=6, fade=1)
            i_val = fig.int(pos=fig.val_for_name(n_val), text="7", rise=2, set=6, fade=1)
            fig.reference(n_val, i_val)

            n_a_list = fig.name(center=n_a_list.center, size=n_a_list.size, text="a_list", rise=5, set=6, fade=1)
            l_a_list = fig.list(texts=["1", "2", "3", "7", "7"], pos=fig.val_for_name(n_a_list), rise=4)
            fig.reference(n_a_list, l_a_list[0])

            n_return = fig.name(center=(the_frame.right, n_a_list.cy), size=(25,25), text="", rise=6, set=8)
            fig.reference(n_return, l_a_list[0])

            n_nums = fig.name(center=n_nums.center, size=n_nums.size, text="nums", rise=8)
            fig.reference(n_nums, l_a_list[0])

            cog.outl(fig.tostring())
Esempio n. 2
0
        def figure(frame):
            fig = PyFig(size=(500,400), frame_num=frame, y=25, name_right=200, val_gap=75)

            n_num = fig.auto_name("num")
            i_17 = fig.int(pos=fig.val_for_name(n_num), text="17")
            fig.reference(n_num, i_17)

            fig.frame(pos=fig.next_frame(), size=(200, 125), text="func", rise=2, set=5, fade=1)
            n_x = fig.auto_name("x", rise=2, set=5, fade=1)
            fig.reference(n_x, i_17)

            cog.outl(fig.tostring())
Esempio n. 3
0
        def figure(frame):
            fig = PyFig(size=(500,400), frame_num=frame, y=25, name_right=200, val_gap=75)

            n_nums = fig.auto_name("nums")
            l_nums = fig.list(texts=["1", "2", "3"], pos=fig.val_for_name(n_nums))
            fig.reference(n_nums, l_nums[0])

            fig.frame(pos=fig.next_frame(), size=(200,200), text="augment_twice", rise=2, set=6, fade=1)
            n_a_list = fig.auto_name("a_list", rise=2, set=6, fade=1)
            fig.reference(n_a_list, l_nums[0])
            n_val = fig.auto_name("val", rise=2, set=6, fade=1)
            i_val = fig.int(pos=fig.val_for_name(n_val), text="7", rise=2, set=7, fade=1)
            fig.reference(n_val, i_val)

            l_nums = fig.list(texts=["1", "2", "3", "7"], center=l_nums[0].center, rise=4)
            fig.highlight(box=l_nums[-1], rise=4, set=5)
            l_nums = fig.list(texts=["1", "2", "3", "7", "7"], center=l_nums[0].center, rise=5)
            fig.highlight(box=l_nums[-1], rise=5, set=6)
            cog.outl(fig.tostring())
Esempio n. 4
0
    def test_names_have_no_type_values_have_no_scope(self):
        # Random figure!

        class MyRandom(object):
            """A custom implementation of a few methods from random.Random.

            For this test to succeed, the random choices must always be the same.
            But Python 3 implements these methods differently, so the same seed
            doesn't produce the same choices.  The .random() method is the same,
            so as long as we only depend on that, this code will work the same
            on both Python 2 and Python 3.  For other Python implementations,
            we'll have to use a static list of random numbers.

            """
            def __init__(self, seed):
                self.r = random.Random(seed)
            def randint(self, a, b):
                return int(a+int(self.r.random()*(b-a+1)))
            def choice(self, seq):
                return seq[int(self.r.random() * len(seq))]

        fig = PyFig(size=(400,600), scale=0.55, y=25, name_right=200, val_gap=175)

        r = MyRandom(14)   # seeded to make it pretty good.

        def rand_name():
            return "".join(r.choice(string.ascii_lowercase) for i in range(r.randint(3,7)))

        def lightly_shuffle(seq):
            """Reorder a list randomly, but don't move things too far."""
            numbered = list(enumerate(seq))
            def jitter(p):
                return p[0]+r.randint(-3,3)
            shuffled = [x for i,x in sorted(numbered, key=jitter)]
            return shuffled

        names = []
        for i in range(r.randint(2,3)):
            names.append(fig.auto_name(rand_name()))

        for f in range(3):
            num_vars = r.randint(2,3)
            fig.frame(pos=fig.next_frame(), size=(200,50+75*num_vars), text="func_"+rand_name())
            for i in range(num_vars):
                names.append(fig.auto_name(rand_name()))
            fig.end_frame()

        values = []
        for name in names:
            pos = fig.val_for_name(name)
            type = r.choice(['int']*2 + ['string']*4 + ['list']*8)
            if type == 'int':
                val = fig.int(pos=pos, text=str(r.randint(5,20)))
            elif type == 'string':
                s = rand_name()
                val = fig.string(pos=pos, text=repr(s), size=(70+10*len(s), 50))
            else:
                assert type == 'list'
                els = r.randint(4, 10)
                val = fig.list(pos=pos, texts=[str(r.randint(5,20)) for i in range(els)])
                val = val[0]
            values.append(val)

        # Mix things up
        shuffled = lightly_shuffle(names)
        for name, val in zip(shuffled, values):
            fig.reference(name, val)

        cog.outl(fig.tostring())

        self.assert_cog_output()