""" the infamous version 1 - how to print 'A' or 'B' (linus did it too :p) """

import Soul

try:
    import set
except ImportError:
    from sets import Set as set


import random

class Perception(object):
    things = []
    def __new__(cls, *attrs, **kwattrs):
        if len(attrs) > 0:
            print '~', attrs, len(attrs)
            perception = object.__new__(cls, attrs, kwattrs)
            if perception not in Perception.things:
                Perception.things.append(perception)
            
        else:
            perception = random.choice(Perception.things) 
            print 'R', perception #, '-', Perception.things

        return perception
  
    def __init__(self, *attrs, **kwattrs):
        #self.attrs = attrs
        #print attrs, type(attrs)
        self.attr = attrs[0]
        print 'S', self.attr
        #print "'O'", self

    #def 

    def __str__(self):
        #print "P__str__", self.attrs
        return str(self.attr)

    def __repr__(self):
        #print "P__repr__", self.attr
        return str(self.attr)

    def get():
        return random.choice(Perception.things) 

Perception('A'), Perception('B')
print "---",
#p = Perception()
def get_perception():
    return random.choice(Perception.things)
p = get_perception()
print p,
print "---"

Soul #now this might make version 1 .. something.

#def perceptions():
#    return Perception()

class Thought: pass #now just an identity

def in_any(thing, list_of_lists):
    for list in list_of_lists:
        #try:
            if thing in list:
                return True
        #except TypeError: #is not a list, but an alias pointerkey
        #    pass
        #now the pointers are to the pre-exited list
    return False

class Mind:
    def __init__(self):
        self._thoughts = set()
        self._memory = {}

    def get_thought(self):
        if self._thoughts: #this guy does not have them now :p
            return #.. but is sure not to tell any if has!  :o
        thought = Thought(), get_perception() #Perception() #nonsense, blah :(
        print "T:", thought
        return thought
    def get_thoughts(self):
        return [self.get_thought()]
    thoughts = property(get_thoughts, None, None) #right on!

    def consider(self, thought_or_perception, new_info):
        """thought_or_perception is now always a thought,
        which acts as a key to new_info, which is a perception

        .. so these names are all stupid and should be changed?
        there is the idea if considering thoughts by just thinking,
        tho..?"""
        
        #this is now just a very strangely written filtered dictwrapper
        if not in_any(new_info, self._memory.values()): #are now lists
                      #was (and kinda is): new_info not in self._memory.values():
            print '+', new_info
            self._memory[thought_or_perception] = [new_info] #new ones may come later

        elif new_info: #is not None, or something else False (:o)
            """something that already is in _memory"""
            print '*', new_info
            
            if thought_or_perception not in self._memory:
                """old_mem"""
                old_mem = None
                for thought, infos in self._memory.iteritems():
                    if new_info in infos:
                        old_mem = thought
                        #what about the new thought?
                    else:
                        print new_info , "not in", infos, "that are for", thought
                if old_mem is not None:
                    #self._memory[old_mem].append(new_info)
                    old_info = self._memory[old_mem]
                    if new_info not in old_info:
                        old_info.append(new_info)
                        print "added", new_info, "to", old_info
                    
                    self._memory[thought_or_perception] = self._memory[old_mem]
                    #this now points to where things about this were already
                   
                else:
                    raise RuntimeError, "how did in_any match, then?"

            else:
                print '**', new_info, self._memory
                Soul.suffer_from #forgive me
                Soul.feel
                #forgive me, forgive me.
                """process(new_info)""" #huh?
                #wake up!
                #are you mourning?
                #.. forgive me, forgive me.
                #self._ #the answer is near?
                self._memory[thought_or_perception].append(new_info) #just a list..
        return self._memory[thought_or_perception]
    
#class Memory:
#    def __init__(self, mind=None):
#        self.thoughts = {}
#    def __init__(self):
#        self.infos = [] #all the infos about this thing, in arrival order


def loop(): #props to Kirst
    #mem = {} #Memory()
    m = Mind()

    while True:
        diffs = [m.consider(thought, info) for thought, info in m.thoughts]
             #if mem.hasKey(thought)]
             #or

        print "D:", diffs

if __name__ == '__main__':
    loop()
