A question on lists and sorting, to prepare for a template
I have a views code that produces a dictionary like the one below, and I want to get them in order in accordance to the keys within the dicts; how can one do that? Example: {'Breakfast':[{'day':'1', 'main_course':'pancakes', 'beverage':'milk'}, {'day':'3', 'main_course':'cereal', 'beverage':'orange_juice'}, {'day':'2', 'main_course':'waffles', 'beverage':'milk'}], 'Supper': [{'day':'1', 'main_course':'spaghetti', 'beverage':'water'}, {'day':'2', 'main_course':'pork_chop', 'beverage':'water'}, {'day':'3', 'main_course':'grilled_cheese', 'beverage':'water'}]} In this dictionary, I am not worried if breakfast and supper are not in order, but I do care if the days are not ordered, as I am putting them into a script that "branches out" information, like a treeview. Is it possible to do what I want to do? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Need a little more help with for loops
When I use the below code, the first item is "treed" like it is supposed to be. However, when I try to put in additional branches at the same "level", the information from the previous loops stays, thus, when a new branch is looped, it will contain information of all branches before it. Here's a truncated version of the dictionary: ## switch_info = {'Router': {'1': {'av_ports': [], 'serial': 'XXX', 'model': 'WS-SVC-FWM-1', 'ports': '6', 'card': 'Firewall Module'}, '9': {'av_ports': [{'status': 'disabled', 'description': ' ', 'duplex': 'full', 'type': 'No Connector', 'VLAN': '1', 'speed': '10G', 'port': 'Te9/1'}, {'status': 'disabled', 'description': ' ', 'duplex': 'full', 'type': 'No Connector', 'VLAN': '1', 'speed': '10G', 'port': 'Te9/2'}], 'serial': 'XXX', 'model': 'WS-X6708-10GE', 'ports': '8', 'card': 'CEF720 8 port 10GE with DFC'}}, 'Switch': {'1': {'av_ports': [{'status': 'notconnect', 'description': ' ', 'duplex': 'full', 'type': 'No X2', 'VLAN': '1', 'speed': '10G', 'port': 'Te1/1'}, {'status': 'notconnect', 'description': ' ', 'duplex': 'full', 'type': 'No X2', 'VLAN': '1', 'speed': '10G', 'port': 'Te1/2'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/3'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/4'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/5'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/6'}], 'serial': 'XXY', 'model': 'WS-X4516-10GE', 'ports': '6', 'card': 'Sup V-10GE 10GE (X2), 1000BaseX (SFP)'}}} # and here is the template code: # And, again, the code for the javascript tree can be found here: http://developer.yahoo.com/yui/treeview/ link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/ 2.2.2/build/treeview/assets/tree.css"> http://yui.yahooapis.com/2.2.2/build/yahoo/yahoo- min.js" > http://yui.yahooapis.com/2.2.2/build/event/event- min.js" > http://yui.yahooapis.com/2.2.2/build/treeview/ treeview-min.js" > var tree; function treeInit() { tree = new YAHOO.widget.TreeView("treeDiv1"); var root = tree.getRoot(); {% for switches in switch_info %} var tmpNode = new YAHOO.widget.TextNode("{{switches}}", root, false); {% for key, value in switch_info.Router.items %} var tmpNode1 = new YAHOO.widget.TextNode(" {{value.card}}", tmpNode, false); {% for item in value.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}} {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} {% endfor %} {% for key2, value2 in switch_info.Switch.items %} var tmpNode1 = new YAHOO.widget.TextNode("{{value2.card}}", tmpNode, false); {% for item in value2.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}} {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} tree.draw(); YAHOO.util.Event.addListener(window, "load", treeInit); } {% block content %} I had to modify it so that the first loop was isolated from the other loops, but I'm still not sure why these loops do not do the same thing as the code in the original post. Anyway, is it possible to make a clean slate for every "branch", or is there a hiccup in the for loops that can be fixed? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Multiple for loops, alternating between key and value?
Thanks, Russ! Your solution worked fine, however, the format that I will ultimately be given is different than the one I gave you, with it being mostly dictionaries, rather than lists. Thankfully, I was able to figure out the loops enough to get the structure nearly correct, except for one thing. When I use the below code, the first item is "treed" like it is supposed to be. However, when I try to put in additional branches at the same "level", the information from the previous loops stays, thus, when a new branch is looped, it will contain information of all branches before it. Here's a truncated version of the dictionary: ## switch_info = {'Router': {'1': {'av_ports': [], 'serial': 'XXX', 'model': 'WS-SVC-FWM-1', 'ports': '6', 'card': 'Firewall Module'}, '9': {'av_ports': [{'status': 'disabled', 'description': ' ', 'duplex': 'full', 'type': 'No Connector', 'VLAN': '1', 'speed': '10G', 'port': 'Te9/1'}, {'status': 'disabled', 'description': ' ', 'duplex': 'full', 'type': 'No Connector', 'VLAN': '1', 'speed': '10G', 'port': 'Te9/2'}], 'serial': 'SAD110502P7', 'model': 'WS-X6708-10GE', 'ports': '8', 'card': 'CEF720 8 port 10GE with DFC'}}, 'Switch': {'1': {'av_ports': [{'status': 'notconnect', 'description': ' ', 'duplex': 'full', 'type': 'No X2', 'VLAN': '1', 'speed': '10G', 'port': 'Te1/1'}, {'status': 'notconnect', 'description': ' ', 'duplex': 'full', 'type': 'No X2', 'VLAN': '1', 'speed': '10G', 'port': 'Te1/2'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/3'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/4'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/5'}, {'status': 'inactive', 'description': ' ', 'duplex': 'full', 'type': 'No Gbic', 'VLAN': '1', 'speed': '1000', 'port': 'Gi1/6'}], 'serial': 'XXY', 'model': 'WS-X4516-10GE', 'ports': '6', 'card': 'Sup V-10GE 10GE (X2), 1000BaseX (SFP)'}}} # and here is the template code: # And, again, the code for the javascript tree can be found here: http://developer.yahoo.com/yui/treeview/ var tree; function treeInit() { tree = new YAHOO.widget.TreeView("treeDiv1"); var root = tree.getRoot(); {% for switches in switch_info %} var tmpNode = new YAHOO.widget.TextNode("{{switches}}", root, false); {% for key, value in switch_info.Router.items %} var tmpNode1 = new YAHOO.widget.TextNode(" {{value.card}}", tmpNode, false); {% for item in value.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}} {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} {% endfor %} {% for key2, value2 in switch_info.Switch.items %} var tmpNode1 = new YAHOO.widget.TextNode("{{value2.card}}", tmpNode, false); {% for item in value2.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}} {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} I had to modify it so that the first loop was isolated from the other loops, but I'm still not sure why these loops do not do the same thing as the code in the original post. Anyway, is it possible to make a clean slate for every "branch", or is there a hiccup in the for loops that can be fixed? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Multiple for loops, alternating between key and value?
Hi all, first time poster, beginner Django user. RIght now, I'm trying to build a page that telnets into two or more switches, gathers the data from each switch, and puts the information into a palatable form. I managed to get the information into a format that I think is acceptable; a list of dictionaries that have dictionaries with lists in them. Here is a very slimmed down version of it, as the original would fill too much space: switch_info = [{'Router':[{'module':'1', 'type':'RJ45', 'av_ports': [{'port':'fa1/1', 'duplex':'auto'}, {'port':'fa1/2', 'duplex':'auto'}]}, {'module':'2', 'type':'RJ45', 'av_ports': [{'port':'fa2/1', 'duplex':'auto'}, {'port':'fa2/2', 'duplex':'auto'}]}]}, {'Switch':[{'module':'1', 'type':'RJ45', 'av_ports':[{'port':'fa1/1', 'duplex':'full'}, {'port':'fa1/2', 'duplex':'auto'}]}, {'module':'2', 'type':'RJ45', 'av_ports': [{'port':'fa2/1', 'duplex':'auto'}, {'port':'fa2/2', 'duplex':'auto'}]}]}] If you were to organize that better, you'd see that there are two dictionaries within a list, Router and Switch. Each of those dictionaries have a list of two dictionaries, comprised of module information. And THOSE module dictionaries have a key that has a list of information for each port. Rather complex, as you can see. However, I want to use a treeview script that yahoo provides, and in order to get all that information into the script, I will have to use for loops. I've gotten far enough to the point that the port information and module information branch out correctly, but the two roots of the tree are a problem. The roots are supposed to be the keys, Router and Switch. However, all I've gotten so far is the actual dictionaries. So, I want it to look like this: ### -Router || |-1, RJ45 | || | |-fa1/1, auto | || | |-fa1/2, auto | | | +2, RJ45 | +Switch Instead, it looks like this: -{'Router': [{'av_ports': [{'duplex': 'auto', 'port': 'fa1/1'}, {'duplex': 'auto', 'port': 'fa1/2'}], 'type': 'RJ45', 'module': '1'}, {'av_ports': [{'duplex': 'auto', 'port': 'fa2/1'}, {'duplex': 'auto', 'port': 'fa2/2'}], 'type': 'RJ45', 'module': '2'}]} | | | 1, RJ45 || | || | || fa1/1, auto || | || | || fa1/2, auto || |2, RJ45 | +{'Switch': [{'av_ports': [{'duplex': 'full', 'port': 'fa1/1'}, {'duplex': 'auto', 'port': 'fa1/2'}], 'type': 'RJ45', 'module': '1'}, {'av_ports': [{'duplex': 'auto', 'port': 'fa2/1'}, {'duplex': 'auto', 'port': 'fa2/2'}], 'type': 'RJ45', 'module': '2'}]} ## And here's the code within the template: # The code for the javascript tree can be found here: http://developer.yahoo.com/yui/treeview/ var tree; function treeInit() { tree = new YAHOO.widget.TreeView("treeDiv1"); var root = tree.getRoot(); {% for switches in switch_info %} var tmpNode = new YAHOO.widget.TextNode("{{switches|}}", root, false); {% for row in switches.Router %} var tmpNode1 = new YAHOO.widget.TextNode("{{row.module}}, {{row.type}}", tmpNode, false); {% for item in row.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}}, {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} {% for row in switches.Switch %} var tmpNode1 = new YAHOO.widget.TextNode("{{row.module}}, {{row.type}}", tmpNode, false); {% for item in row.av_ports %} var tmpNode2 = new YAHOO.widget.TextNode("{{item.port}}, {{item.duplex}}", tmpNode1, false); {% endfor %} {% endfor %} {% endfor %} tree.draw(); ### Is there any possible way to have the first loop just display the key? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---