Sunday, March 20, 2011

Use a list of strings to dynamically add properties to a class

Got #1 now.  The idea is that we need to add the properties as attributes of the class.  (Although it seems like one could also add the properties as attributes of an object.)  The function looks something like this:




class Test(object):

stat_list = ["x", "y", "z"]

def __init__(self):

for stat in self.stat_list:

name = '_'+stat.lower()

setattr(self.__class__, name, 0)

setattr(self.__class__,

stat.lower(),

property(lambda self: getattr(self, name),

lambda self, value: setattr(self, name, value)))


Update:
Apparently, I need to be creating getter/setters for the object not the class. The odd thing is that I cannot now access the properties through ._stat

stat_list = ["x", "y", "z"]
for stat in self.stat_list:
name = '_'+stat.lower()
#self.__setattr__(name, 0)
self.__setattr__(stat.lower(),
property(lambda self: getattr(self, name),
lambda self, value: setattr(self, name, value)))
print "Added: " + stat.lower() + " " + str(eval("self."+stat.lower()))

Programming Questions

Two questions:
First, is there a built-in way to iterate over properties?  I have a class that contains a number of properties.  I'd like to also include a function that iterates over those properties.  Is there any easy way to do this?

Second, the above question is the kind of question I'd like to hack away at.  One good way to hack at this problem is to load the class into an interpreter and see what kind of functions I can develop.  Now, I use Gedit as my editor, and it comes with a built-in python interpreter.  How can I load the classes in my current file into the interpreter? 

Thursday, March 17, 2011

SG Universe

How, in one show, can you directly refer to the scientists of previous SG series and not know that solar flares and gate-travel equals time travel?  It seems like they drew in knowledge from previous SG series.  Anyways, SG Universe is still awesome.  The tension man vs. universe is great.  Aliens are fun, but the loneliness of space creates constant anxiety. 

Monday, March 07, 2011

I spent the morning toying with the python perlin noise library. It turns out that the perlin noise function doesn't produce random noise unless you instantiate a the SimplexNoise() class and call randomize.


Originally, I was using this:

def pn(m, octaves=1):
    from noise import pnoise2
    freq = 16.0 * octaves
    for y in range(len(m)):
        for x in range(len(m[0])):
            m[y][x] = int(pnoise2(x/freq, y/freq, octaves) * 127.0)
which constantly give us:

Now, I import the SimplexNoise class and randomize the permutation table

def pn3(m, octaves=1):
    from noise.perlin import SimplexNoise
    sn = SimplexNoise()
    sn.randomize()
    freq = 16.0 * octaves
    for y in range(len(m)):
        for x in range(len(m[0])):
            m[y][x] = int(sn.noise2(x/freq, y/freq) * 127.0)

and now I get random perlin noise:

I hope that helps anyone else struggling with the library.

Monday, February 21, 2011

Testing Code

Will this format the code?
def diamond_square(m, x1, y1, x2, y2, r):

    """ Using standard python array.  No numpy

        Now x and y are backwards, though.

        m = any empty 2d array

        x1, y1, x2, y2 = range on m to apply calculations

        r = value range

    """

    i = ((m[x1][y1] + m[x1][y2] + m[x2][y1] + m[x2][y2]) // 4) + random.randint(-r,r)

    m[(x1+x2)//2]   [(y1+y2)//2] = i                         # Center

    m[x1]           [(y1+y2)//2] = i + random.randint(-r,r)  # Up

    m[x2]           [(y1+y2)//2] = i + random.randint(-r,r)  # Left

    m[(x1+x2)//2]   [y1]         = i + random.randint(-r,r)  # Right

    m[(x1+x2)//2]   [y2]         = i + random.randint(-r,r)  # Down

    if x2-x1 > 1 and y2-y1 > 1:

        diamond_square(m, x1,           y1,         (x1+x2)//2,     (y1+y2)//2, r//2)

        diamond_square(m, (x1+x2)//2,   y1,         x2,             (y1+y2)//2, r//2)

        diamond_square(m, x1,           (y1+y2)//2, (x1+x2)//2,     y2,         r//2)

        diamond_square(m, (x1+x2)//2,   (y1+y2)//2, x2,             y2,         r//2)