Hi all,

I am working on a problem that should have been very trivial but has turned into a multiple marathon without an end in sight. I have written two very small scripts, one auxiliary script as below with two classes 'Node' and 'Queue' intended for use in a linked list.

class Node:
    value=None
    nextNode=None
 
    def __init__(self,v):
        self.value=v
class Queue:
    dummy=Node(None)
    dummy.nextNode=None # Kan ifrågasättas map nödvändighet, sker redan i Node-instantieringen...
    front=None
    back=None
 
 
    def __init__(self):
        self.front=self.dummy
        self.back=self.dummy
        self.p=self.dummy
 
    def show(self):
        p=self.back
        while p!=None:
            print p.value
            p=p.nextNode
            print "\n"
        print        
 
    def isEmpty(self): 
        if self.back==None:
            return True
        else:
            return False
 
    def put(self,v):    
        x=Node(v)       
        x.nextNode=self.back 
        self.back=x     
    def get(self):
        if self.back.nextNode!=None:
            p=self.back
            while p.nextNode!=None:
                p=p.nextNode
            x=p
            self.front=p
            self.front.value=None
            self.front.nextNode=None
            return x
        else:
            print 'Cannot access an empty queue.'
            return None

The main program peruses the classes of the auxiliary script, as seen below

from Queue import *
Snake=Queue()
x = 5
while x !=4:
    print '\n'
    print '1. Add'
    print '2. Remove'
    print '3. Check if Queue is empty'
    print '4. Terminate program.'
    x = input()
    if x == 1:
        ##    print x
        str = raw_input("Enter a string ")
        Snake.put(str)
        print "Funcion put successful"
    else:
        print "No one..."
    Snake.show()

But whatever I try, and I have now tried a very great many different angles, I keep getting the error message

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python24\Lib\site-packages\pythonwin\lab4uppg2.py", line 24, in ?
Snake.show()
AttributeError: Queue instance has no attribute 'show'

D%#¤ straight Queue has an attribute 'show', it's plain to see, I put it there. And so would its instance Snake if there were any good sense. The method 'put' can be referenced, so it's just 'show' that's being bullied for no reason, not even a bad reason. The line 'from Queue import *' in the header should see that everything is included. (The file in which the classes are IS named Queue.py).

Does 'import' have a bug, or is PythonWin unstable? Rumor is PythonWin does not always take data file updates into account. If this is so, then PythonWin really is a crock without a future.

VERY grateful for any help. I am becoming a MAJOR ulcer sufferer.

I put the code in one single file and it worked (well at least it gave me no error message). Try to

reload(Queue)

. Modules are only imported once by default.

I think your trouble comes from the fact that Python itself actually has a module Queue. Rename your module MyQueue.py and it works just fine.

This small code shows you all the modules Python has installed:

help("modules")

To avoid conflicts give your own modules a prefix like "my"or "My".

I put the code in one single file and it worked (well at least it gave me no error message). Try to

reload(Queue)

. Modules are only imported once by default.

Thanks a lot, N317V! However, I didn't provide the background information (there is even more to write) that importing from another file was part of the prerequisite conditions for approval of the solution of this (academically rooted) problem. If nothing else works, I'll follow your suggestion and hide the critical class-defining code a great many carriage returns down and out of sight in the editor. :D

It's working like a beauty, Ene Uran - thanks a MILLION!! You write that you wish that I were beer, well, I can't help you there but please tell me how I can make it up to you. If you have PayPal, I'll see you on the price of a big crock of Löwenbräu.

Hi Ginner,

I just wanted to indicate, that there is no error in the code itself and I have experienced problems with PythonWin like that, where some old code was still in the memory (for example a class without a certain property, which was added later on :-) ) So I just do a reload on the console below and everything works fine. First thing I try in such cases. You might want to keep that in mind.

I had exactly that "failed-to-update-file-data-after-changes" sort of PythonWin problem in mind, but as Ene Uran showed me it actually WAS a coding error of sorts in that I failed to produce code with unique identifiers relative to the body of code already in existence. Subtle problem. How many coders can survey Python's entire body of existing code?

By the way, by

do a reload on the console below

I presume you mean the same as you wrote in your first post in this thread, with

reload(Queue)

Vielen Dank!

How many coders can survey Python's entire body of existing code?

True! Sometimes I'm being ignorant enough to just give my classes german names. They usually don't collide but are completely incomprehesible for non-german programmers. Adding "my" doesn't seem to good either, because everyone could be "me". So I started prefixing my own modules with my initials.

By the way, by I presume you mean the same as you wrote in your first post in this thread [...]
Vielen Dank!

That's exactly what I meant. Bitte schön!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.