Order a list to get a hierarchical order
Hi, Having a list of objet with a parent_id attribute pointing to a parent, I want to order this list like this : [Parent #1, Child #1.1, Child#1.1.1, Child#1.1.2, Child#1.2, Parent #2, Child #2.1, ...] Any clue on how to do this ? Thanks, -- http://mail.python.org/mailman/listinfo/python-list
Re: Order a list to get a hierarchical order
Thanks for your help, I'll test this. 2012/6/8 Peter Otten __pete...@web.de Ivars Geidans wrote: def append_node(n, l, ls): ls.append(n) for c in [nc for nc in l if nc.parent is n]: append_node(c, l, ls) return ls def sort_nodes(l): ls = [] for r in l: if r.parent == None: append_node(r, l, ls) return ls This ensures that child nodes appear after their parent but leaves the order of nodes on the same level undefined. I think adding def sort_nodes(l): l = sorted(l, key=lambda node: node.name) #untested ... would fix that. -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Working with dates : complex problem
Hi, I've a list of python objects with dates attributes. This list is ordered by one of these date. Elements mandatory follow each other : Element #1 Element #2 Element #3 |-|--|--| Now, I want to insert an element in this timeline. This imply that I will have to resize some elements : Element #1 Element #2 Element #3 |-|--|--| New element |--| And after resize : Element #1 New element Element #2 Element #3 |-|--|-|--| |--| My question is the following : how can I know (easily) which elements my New element is over, which, in my example would have returned ['Element #1', 'Element #2']. Elements objets are simple Python objects with dates : obj.begin = datetime() obj.end = datetime() I'm looking for the more Pythonic way to handle this problem, thanks ! -- http://mail.python.org/mailman/listinfo/python-list
Sockets accept() in child processes
Hi, I'm writing a multiprocess server with Python 3.2 and the multiprocessing module. Here is my current implementation : - Main process: select() on a list of server sockets (different ips of the host, ssl or not, etc) - Children process : When they get a signal (in fact, a message in a pipe), they will accept() the server socket The problem is that after select() returns, I can accept() in the main process, but in a children process I got a EAGAIN error. The sockets are non-blocking. It seems that accept() can only be called in the same process that the one which selected(). This is a problem because I wanted to decide which process I should ask to take the connection to do some basic load-balacing based on current number of connected client in each process. Could someone help me or propose other implementations ? Thank you, -- http://mail.python.org/mailman/listinfo/python-list
Multiprocessing Logging
Hi, I'm currently writing a multiprocess applications with Python 3.2 and multiprocessing module. My subprocesses will use a QueueHandler to log messages (by sending them to the main process, which uses a QueueListener). However, if logging is already configured when I create the subprocesses, they will inherit the configuration, and opened file descriptors used for logging in the main process. However, when I tried this with a basicConfig configuration, which prints to a file, the messages are only written once to the file. I don't understand why. Normally, the mainprocess contains a logging handler to log to the file. This handler will be copied into the child processes. Child processes will then have two handlers : a QueueHandler, and a FileHandler. They should write to the file handler and then send the message to the main process QueueListener, which should write the message AGAIN to the FileHandler. But that's not the case. Any rea How can I totally unset the logging configuration in child processes and only enable the QueueHandler (foor all configured loggers, not only the root one) ? Thanks for your help, -- http://mail.python.org/mailman/listinfo/python-list