a useless stack dictionary python

Update: 10-6-19  Added code blocks, realized that my ascii used for describing the link between nodes [<--- value --->]  breaks my code blocks even with <pre><code>  So I'll have to go back to my linked lists and make sure I didn't break them, because I know I used this ascii in them too.

Why create a useless stack dictionary?
I've found a lot of times with endeavors in life, the better question is, why not...?
And besides.... I wanted to draw an octopus for a couple of my favorite people.
Also... spam and eggs.
Also... If I'm going to see if I can replicate how this damned thing works, why not pick something that should be pointless and nothing like the original??
It can't really do anything but horde tuples... but, like I said... useless.
Also... It was fun.
Why not.

An octopus first.....
                                     



Here's the stack dictionary:






--start code block Octopus(useless bit)--
class Octopus(object):
    def __init__(self, tentacles=8):
        self.octopus = StackList()
        for i in range(0, tentacles):
            self.octopus.push(StackList())

    def give_list(self, key):

        ###  the hash is the best way to do this.... 
        ### but a stack is not easily altered.... a stack of stacks even less so.....
        ## in dictionary this is 'hash_key'
        ## all this does is return a number between 0 - 8 
        ## hash can be anything... -9876535336363 or  0 or 786363636
        ### when modulated by 8... it will return a value between 0 and 8.
        ### since hash stays constant for the run of the program, it will 
        ## consistently give the same result for the run. 

        return hash(key) % self.octopus.count()

    def get_tentacle(self, key):

        ## in dictionary this is 'get_bucket'
        ## the stack_id it's returning is the python object id... 
        ## something like 

        stack_id = self.give_list(key)
        print(stack_id)
        print(self.octopus.get(stack_id))
        return self.octopus.get(stack_id)

    def get_sucker(self, key, default=None):

        ## in dictionary, this will get the thing that resides in a slot. or None if it's empty
        ## slot being [begin ][< something ][< something else ][< end  (/)]
        ## the tuples in the arms are the pairs ... the suckers on the arms
        tentacle = self.get_tentacle(key)
        print(tentacle)
        # if the bucket exists..... tentacle
        if tentacle:
            print(f"tentacle = {tentacle}")
            sucker = tentacle.stack
            print(f"node for while loop = {sucker}")
            while sucker:
                if key == sucker.value[0]:
                    print(sucker.value[0])
                    return tentacle, sucker 
                else:
                    sucker = sucker.next
            # if there is no match...
            print(f" tentacle = {tentacle} and None")
            ### I had return tentacle and None.... 
            ## this kept producing TypeError: 'NoneType' object not iterable
            ## error on 3 lines below.... I do love to find errors. 
            return tentacle, None
    # set looks like a protected word... Dictionary uses set here instead of ink
    def ink(self, key, value):
        # we are placing a tuple into the dictionary
        tentacle, sucker = self.get_sucker(key)
        ### in this version, identical keys are not allowed. write over them.
        ### so if sucker is returned, it is not None.. we write over it.
        if sucker:
            sucker.value = (key, value)
        else:
            ## there was nothing in the sucker, we make a new tuple value
            tentacle.push((key, value))
    def reveal(self):
        ## print out everything in the octopus
        octopus_legs = self.octopus.stack
        while octopus_legs:
            tentacles = octopus_legs.value.stack
            while tentacles:
                print(tentacles)
                tentacles = tentacles.next
            octopus_legs = octopus_legs.next
       
--end code block--

The stack:

--start code block python stack--

class StackNode(object):
    def __init__(self, value, node):
        self.value = value
        self.next = node
    def __repr__(self):
        nval = self.next and self.next.value or None
        return f"[{self.value}: {repr(nval)}]"

class StackList(object):
    def __init__(self):
        self.stack = None
    def push(self, obj):
        # add a newnode to the stack.
        #  [ value,  None]
        #  [ value,  None][ <    --,  value][<    --, value] .......
        end = self.stack
        if end == None:
            newnode = StackNode(obj, None)
            self.stack = newnode
        else:
            node = self.stack
            newnode = StackNode(obj, node)
            #[ DATA,  ( / )]
            #[ stack, ( / )][ DATA, <    --stack ]
            self.stack = newnode
            # stack = [ DATA, <    --stack ]
    def count(self):
        # get a length
        node = self.stack
        count = 0
        if node:
            while node:            
                node = node.next
                count +=1
        return count
    def graphical(self):
        # print off a representation of the stack
        node = self.stack
        length = self.count()
        if node:
            print("\n\n")
            while node:
                if length == 1:
                    print(f"[ (1){node.value}, (/) ]")
                    node = node.next
                else:    
                    print(f"[({length}){node.value}--- > ]")
                    length -= 1
                    node = node.next
        else:
            print(" EMPTY ")
    def top(self):
        node = self.stack
        if node:
           return node.value
        else:
            return None
    def pop(self):
        # take the top of the list off
        node = self.stack
        if node:
            #[DATA, (/)][DATA, <    -]
            #[DATA, (/)][DATA, <    -][DATA, <    --]
            self.stack = node.next
        else:
            print(" EMPTY ")
            return None
    def get(self, index):
        index = int(index)
        length = self.count()
        count = 0
        if length == 0:
            return None
        elif index >= (length):
            raise IndexError
        else:
            placement = length - 1
            node = self.stack
            while node:
                if placement == index:
                    print("node found")
                    return node.value 
                node = node.next
                placement -= 1
    def dump(self):
        begin = self.stack
        if begin:
            while begin:
                print(begin)
                begin = begin.next
--end code block--

    


                                                                                                                                                

Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

JavaScript and a Matrix