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)



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

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

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)
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()
    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)

Tuesday, June 08, 2010

logic and ontology

This article helps to summarize a number of the positions debated last night.
Logic and Ontology
Some of the highlights:

We might wonder why we should think that quantifiers are of great importance for making ontological commitments explicit. After all, if I accept the apparently trivial mathematical fact that there is a number between 6 and 8, does this already commit me to an answer to the ontological question whether there are numbers out there, as part of reality? The above strategy tries to make explicit that and why it in fact does commit me to such an answer. This is so since natural language quantifiers are fully captured by their formal analogues in canonical notation, and the latter make ontological commitments obvious because of their semantics. Such formal quantifiers are given what is called an ‘objectual semantics’. This is to say that a particular quantified statement ‘∃xFx’ is true just in case there is an object in the domain of quantification that, when assigned as the value of the variable ‘x’, satisfies the open formula ‘Fx’. This makes obvious that the truth of a quantified statement is ontologically relevant, and in fact ideally suited to make ontological commitment explicit, since we need entities to assign as the values of the variables. Thus (L1) is tied to (O1). The philosopher most closely associated with this way of determining ontological commitment, and with the meta-ontological view on which it is based, is Quine, in particular his (Quine 1948).

Logically valid inferences are those that are guaranteed to be valid by their form. And above we spelled this out as follows: an inference is valid by its form if as long as we fix the meaning of certain special expressions, the logical constants, we can ignore the meaning of the other expressions in the statements involved in the inference, and we are always guaranteed the the inference is valid, no matter what the meaning of the other expressions is, as long as the whole is meaningful. A logical truth can be understood as a statement whose truth is guaranteed as long as the meanings of the logical constants are fixed, no matter what the meaning of the other expressions is. Alternatively, a logical truth is one that is a logical consequence from no assumptions, i.e. an empty set of premises.

Whatever one says about the possibility of proving the existence of an object purely with conceptual truths, many philosophers have maintained that at least logic has to be neutral about what there is. One of the reasons for this insistence is the idea that logic is topic neutral, or purely general. The logical truths are the ones that hold no matter what the representations are about, and thus they hold in any domain. In particular, they hold in an empty domain, one where there is nothing at all. And if that is true then logical truths can't imply that anything exists. But that argument might be turned around by a believer in logical objects, objects whose existence is implied by logic alone. If it is granted that logical truths have to hold in any domain, then any domain has to contain the logical objects. Thus for a believer in logical objects there can be no empty domain.

What the philosophers aim to ask, according to Carnap, is not a question internal to the framework, but external to it. They aim to ask whether the framework correctly corresponds to reality, whether or not there really are numbers. However, the words used in the question ‘Are there numbers?’ only have meaning within the framework of talk about numbers, and thus if they are meaningful at all they form an internal question, with a trivial answer. The external questions that the metaphysician tries to ask are meaningless. Ontology, the philosophical discipline that tries to answer hard questions about what there really is is based on a mistake. The question it tries to answer are meaningless questions, and this enterprise should be abandoned. The words ‘Are there numbers?’ thus can be used in two ways: as an internal question, in which case the answer is trivially ‘yes’, but this has nothing to do with metaphysics or ontology, or as an external question, which is the one the philosophers are trying to ask, but which is meaningless.

If there is an explanation of this similarity [the similarity between the subject-predicate structure of though, and the object-property structure of reality] to be given it seems it could go in one of two ways: either the structure of thought explains the structure of reality, or the other way round.

Monday, May 17, 2010

Sim City and "sick ambitions"

The Totalitarian Buddhist Who Beat Sim City

But it wouldn’t be the same as doing it in the game, for the reason that I wanted to magnify the unbelievably sick ambitions of egotistical political dictators, ruling elites and downright insane architects, urban planners and social engineers.

I took an American Anthropology course when i was an undergraduate at Towson, and one of our assignments was to construct a city in SimCity 2000. I'd been playing these games for years, so it wasn't even a challenge. But what you quickly learn is how a game can walk the line between very realistic and totally arbitrary. And, perhaps most interestingly, how we respond (as moral agents) to these distinctions.

Wednesday, April 28, 2010

python gtk and Glade

I spent a few hours yesterday figuring out this problem.

Given a simple glade3 (gtk.Builder) window, setting the on_window1_destroy signal to gtk.quit didn't seem to work. The window itself would disappear, but the process would continue to run in the background.

After fiddling with it, I finally identified the problem.  It turns out that you have to set on_window1_destroy in the .glade file as well.  The tutorials were unclear, but I guess that kind of makes sense.  I had assumed that the "on_OBJECT_command" format was being parsed so as to hook directly to the OBJECT.  That is, it knew which object to attach the signal to by parsing the signal name. But, instead, you need to set the signal in the glade file so that it is attached to the GtkWindow.

Seems like it will be a pain to set the signals for every object in the glade file.  Hopefully there is an easier way and I'm just missing it.