last day (15 days later) » 

8:44 PM
if self in online_users.values():
del online_users[value]
After I find the value I need to delete it.
 
you are using dictionaries backwards
 
now I need to get the key of that value.
so I can delete it.
 
and that del won't work
 
.remove()
I know the value of the dictionary but not the key
 
do it the other way around: store the users as keys
 
8:46 PM
I Store users as keys.
 
it really just sounds like you've designed your structure wrong
 
@eddwinpaz your data structure is broken fix it how everyone has advised you to
 
maybe go back and think about what you're really trying to accomplish
 
but when I delete I dont have the user only got the client.session.id
 
or provide a reason why you cannot
 
DSM
8:47 PM
This is like looking in a phone book for the number 867-5309. You can do it, but not efficiently.
 
when I logout of the websocket or user closes connection unexpectectly
 
@eddwinpaz we get what your problem is, you don't have to keep repeating yourself, but we're telling you that the real solution is to do something completely different
 
reverse_dict = dict([x[::-1] for x in connected_users.items()])
 
the only value I get is the websocket connection.
 
there now you can get the username by the id or whatever
 
8:48 PM
so I need to be able to remove it from the dictionary
 
have a dictionary mapping connection ids to users
 
user = reverse_dict[self]
 
then look up the user based on the id that disconnected
@eddwinpaz please stop repeating yourself
 
@eddwinpaz you need to just use an appropriate datastructure to match your needs
oh you are getting closer
 
8:49 PM
you appear to be unwilling to accept our advice that you've just designed your structure wrong, so at this point you're on your own
 
he fixed it
 
@JoranBeasley see the on_close(self):
 
he just needs to resolve his disconnect
 
I need to delete the user when disconnects based on self and not on the json user_sender
 
clients,users = connected_users.items()
del users[clients.index(self)]
oops
thats wrong dont do that
users,clients = connected_users.items()
del connected_users[users[clients.index(self)]]
I think i got that right
 
8:52 PM
let me rty
 
but really, just use a better data structure
 
but do you really need to delete it is the question
 
yes.
 
ehh he has a dict of {username:client}
 
because otherwise it will be keept as if its online
yes
 
8:53 PM
and thats what he needs everywhere except on_close
but as soon as you try to read or write you get an error and just remove it then
but meh either way
before he just had a list of connections = [...]
then he had a dict of {client:username} ... but he always need to access it by username
 
def on_close(self):

if self in online_users.values():

users,clients = online_users.items()
del online_users[users[clients.index(self)]]
like this?
 
yeah ... i dont know you need that check ... you could just put it in a try except .. and really it should always be there
 
cbg
 
I get this
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/web.py", line 1302, in _stack_context_handle_exception
raise_exc_info((type, value, traceback))
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 314, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/websocket.py", line 354, in on_connection_close
self.on_close()
File "chatroom.py", line 89, in on_close
users,clients = online_users.items()
ValueError: need more than 1 value to unpack
 
umm print online_users
users,clients = zip(*online_users.items())
I messed up sorry
now that should work
 
8:58 PM
??
dont get it?
dont get it :(
 
change that line to what i put just now
clients,users = zip(*connected_users.items() )
 
the asterisk makes the list items send seperately as arguments @eddwinpaz
 
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/tornado/web.py", line 1302, in _stack_context_handle_exception
raise_exc_info((type, value, traceback))
File "/Library/Python/2.7/site-packages/tornado/stack_context.py", line 314, in wrapped
ret = fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/tornado/websocket.py", line 354, in on_connection_close
self.on_close()
File "chatroom.py", line 90, in on_close
del online_users[users[clients.index(self)]]
NameError: global name 'clients' is not defined
Why users and clients?
I need (del online_users[index]
 
hey
 
9:01 PM
this way SOPython isnt killed with his questions ":P
 
hehehe
My issue is that I need to remove a user when socket closes.
but before I need to search him on the list and then find the index of the dictionary and then remove it.
 
do i really need to copy and paste the stuff from the other list?
 
def on_close(self):

if self in online_users.values():

uclients,users = zip(*online_users.items() )
del online_users[users[clients.index(self)]]
 
ok users and clients need to be switched there
users,clients = zip(*online_users.items())
then users is a list of usernames
 
if 3423432432 in online_users.values():
del online_users[key_of_3423432432]
 
9:03 PM
and clients is a list of ColientInstances
ClientInstances
 
34 messages moved from Python
 
@davidism good call
 
if if exsits I need to find the index of that dictionary element
and remove it by its key
 
users,clients = zip(*online_users.items())
#users == ["bob","james","nancy","..."]
#clients == [ClientConnection1,ClientConnection5,ClientConnection2,...]
 
37 messages moved from Python
 
9:06 PM
so to get the username of the current client you can get the index of the client in clients and use that index in users
my_index = clients.index(self)
my_username = users[my_index]
now you can simply delete it from your connection list
del online_users[my_username]
you can shorten it to get the username by
 
I see
Understand now
 
my_username = users[clients.index(self)]
 
let me try
 
also and perhaps easier is to attach a username to the client
online_users["bob"] = self
self.username = "bob"
 
def on_close(self):

if self in online_users.values():

clients,users = zip(*online_users.items())
user_sender = users[clients.index(self)]
del online_users[user_sender]
 
9:08 PM
then later in on disconnect
del online_users[self.username]
sure that looks right ... why not just run it?
but the better way is probably to attach the username to the ConnectionObject
 
ValueError: tuple.index(x): x not in tuple
I get
 
def on_open(self,...):
    ...
    online_users["bob"] = self
    self.username = "bob"

....


def on_close(self,...):
      del online_users[self.username]
is a better way to handle that
you understand what im saying?
as to your problem with that code ... I said many times it must be users,clients = ...
 
self exists on online_users
{u'eddwinpaz': <__main__.ChatHandler object at 0x100fe4850>}
 
NOT clients,users = ...
 
def on_close(self):

if self in online_users.values():

users,clients = zip(*online_users.items())
user_sender = users[clients.index(self)]
del online_users[self.user_sender]
 
9:14 PM
there
now that should work ... print clients right before you get user_sender
 
AttributeError: 'ChatHandler' object has no attribute 'user_sender'
 
look at your code
user_sender = users[clients.index(self)]
del online_users[self.user_sender]
 
it works now
 
should be

user_sender = users[clients.index(self)]
del online_users[user_sender]
 
just removed self from user_sender
 
9:15 PM
of coarse ...
 
let me double check
@JoranBeasley you are master :D
 
you should really do it more like in my other example
def on_open(self,...):
    ...
    online_users["bob"] = self
    self.username = "bob"

....


def on_close(self,...):
      del online_users[self.username]
 
will it run on python with many users?
 
of coarse ... self is that user ...
self only refers to a specific user each time the function is called ... it does not refer to the class
it is an instance of the class
 
username is a global variable ?
online_users = {}
username = ""
 
9:21 PM
self.username is tied to an instance of a class
you really need to read some python documentation
 
I will
promess
 
can someone pastebin what you have so far?
 
Do I just use self.username
without defininf it?
or just set it at the beggining of the class?
 
change
self.username = ''
to
username = ''
 
check this one better
 
since it isn't inside a method, there's no self @eddwinpaz
 
pastie.org/10012967 is probably how i woudl do it given no context other than this small class
 
@JoranBeasley your second example works out well also...
SHould I use the simple one?
or keep the zip() one?
 
either one should work fine ... I would use the simplest one that I could understand what was going on in
 
yeap Ill keep up with that.
are you a Tornado framework user?
 
9:28 PM
the one that i just posted makes self.username available anywhere you want though (well within the class)
nope
i think i spent about 20 minutes total with tornado
 
hehehe
Im using it because nodejs sucks
We implemented nodejs
and it crashes every 1 hour
 
meh im not sure you are limited to nodejs or tornado only .... hmmm perhaps lookinto why nodejs is crashing
that doesnt sound like standard nodejs behavior
im not familliar enough with nodejs to say for sure ... but i know several people who swear by it
 
hehehe
I think nodejs and tornado are the same. I better stand behind python and not JS
 
wthats not to say you shouldnt go with python (I love python ...) but i hope your not re-inventing the wheel
 
I like tornado.. I mostly use django.. but for websockets I dont know any django pip
 
Im using tornado hehehe
Really thank you for your time
helped me alot..
 
no worries
 

last day (15 days later) »