0

I am working on a client/server to make a simple chat. Client is in VB and server in python.

I would like to make my server stock my message, and i though the smartest was to build a linked list (I am a novice in python but much more advanced in C#).

What i try to store is composed of :

  • the recipient (it is called dest here)
  • the message (called msg)

I dont know how many, and i will overwrite if I have a new message for someone who already have a message stored

I tried that as class

class tabmessage:
    def __init__(self, dest=None,msg=None, next=None): 
        self.dest = dest
        self.msg = msg
        self.next = None

and that as call

#I create the top of the chain on the beginning
messages = tabmessage(dest='Control',msg='Message Integrity')

... Then in a function later

#Setting the top of the chain
(d,m,s) = (messages.dest,messages.msg,messages.next)
#Looking for a similar dest in chain and getting at the end at the same time
while True:
        if (d == tempdest):
            m = (tempmsg+".")[:-1]
            print("Overwrite of msg for" + d + " : " + m);
            return
        if (s is None):
            break
        (d,m,s)=(s.dest,s.msg,s.next)
#If I did not found it i try to add it to the chain
s = tabmessage(dest=(tempdest+".")[:-1],msg=(tempmsg+".")[:-1])
print("Trying to add : " + s.dest + " : " + s. msg)

The last print looks ok :

Trying to add : User : This is my message

but if i do :

print("Trying to add : " + messages.next.dest + " : " + messages.next. msg)

An error occured (NoneType don't have a dest element ...), so the top is still alone.

Or maybe if there is smarter way to do it in python ?

Foxhunt
  • 764
  • 1
  • 9
  • 23
  • Why use a linked list? It isn't clear to me what you are trying to accomplish, and why it requires rolling your own linked list, but `m = tempmsg` won't affect anything. It simply assigns `tempmsg` to the local variable `m` and then the function returns. – juanpa.arrivillaga Feb 07 '19 at 23:50
  • I need to stock a list of message with their recipient (it is dest). I dont know how many then. In C or C# I would have a use linked list. And I am a newbie in Python so i think the same. – Foxhunt Feb 08 '19 at 00:04
  • I modified m = tempmsg into m = (tempmsg + ".")[:-1] to create a new tring here – Foxhunt Feb 08 '19 at 00:17
  • Why not just use a built in `list`? – juanpa.arrivillaga Feb 08 '19 at 00:58
  • Again, you are just assigning to a local variable `m` that doesn't change anything – juanpa.arrivillaga Feb 08 '19 at 00:59
  • I read here that is it a new string no ? : https://stackoverflow.com/questions/24804453/how-can-i-copy-a-python-string – Foxhunt Feb 08 '19 at 10:43
  • Yes, strings are immutable and all string methods return new strings. Not sure why that's relevant. As I've tried to explain, the code snippets you provide show *no changes to anything except local variables* which won't affect anything after the function terminates. – juanpa.arrivillaga Feb 08 '19 at 16:54

2 Answers2

0

If I understand your function correctly, you are finding the node you want with

if (d == tempdest):

Then you are writing your message and returning. Because you return, the statement at the bottom where you assign s to a new node never runs. I think you want to break there instead. I'm not sure that this is your only issue, but I think that's the reason your list is not getting additional nodes.

Schematic
  • 134
  • 6
  • Actually, I reach this part when no recipient "d" exist (and only there i want to add a node), so when i debug, with zero message, i reached it, but the node was not added to the chain – Foxhunt Feb 08 '19 at 10:12
0

I tried to manipulate on the higher node, and it works. Now i have :

#Setting the top of the chain
cunode = messages
#Looking for a similar dest in chain and getting at the end at the same time
while True:
    if (cunode.dest == tempdest):
        cunode.msg = (tempmsg+".")[:-1]
        print("Overwrite of msg for" + cunode.dest + " : " + cunode.msg);
        return
    if (cunode.next is None):
        break
    cunode = cunode.next

#If I did not found it i try to add it to the chain
cunode.next = tabmessage(dest=(tempdest+".")[:-1],msg=(tempmsg+".")[:-1])

Looks quite the same for me ...

Foxhunt
  • 764
  • 1
  • 9
  • 23