1

here i have added a code to generate xml_tags, This is what my output is :

md5 value = "sdfhsdkjgfjw35378563"
repOperation value = "push"
size value = "toolarge"
images value = "/home/rakesh/from_t_jack/imag1.ipds"
status value = "completed"
replication value = "mode"
sdlist value = "f:"

I have used dictionary. I am unable to get correct sort of my dictionary. I want the dictionary to be sorted and printed as the order i declared. one other problem is , if the output is observed in 6th line, the output is kinda reversed , i was expecting :mode value = "replication". but the output is reversed.i do not know why it is behaving in this manner. I was wondering how to fix these 2 issues . Here is my Code.

  def generate_tag(name,val):

     full_tag = ''+name+' value = "'+val+'"'
     return full_tag



  def auto_xml(sdList,repOperation,images,status,md5):


            tags =          {'repOperationTag'  :{'repOperation_tag_name':'repOperation' ,
                                                  'repOperation_val'     : repOperation },

                            'modeTag'             :{'mode_tag_name':'mode' ,
                                                    'mode_val'     : 'replication' },


                             'imagesTag'        :{'images_tag_name': 'images',
                                                  'images_val'     : images  },

                             'statusTag'        :{'status_tag_name':'status' ,
                                                  'status_val'     : status},

                            'sizeTag'          :{'size_tag_name':'size' ,
                                                 'size_val'     : 'toolarge'},

                             'md5Tag'           :{'md5_tag_name'    : 'md5' ,
                                                  'md5_val'         : md5} ,

                             'sdListTag'        :{'sdList_tag_name': 'sdlist' , 
                                                  'sdList_val'     : sdList} }

            count = 0
            tag_len = len(tags)

            while count < tag_len :

                inner = tags[tags.keys()[count]]

                tag_name = inner[inner.keys()[0]]
                tag_val = inner[inner.keys()[1]]

                full_tag = generate_tag(tag_name,tag_val)

                print full_tag
                count = count + 1



  def myordered_dict(): 

      from collections import OrderedDict


      tags =         {'A_repOperationTag'  :{'tag_name':'repOperation' ,
                                      'tag_val'     : repOperation },

                'B_modeTag'             :{'tag_name':'mode' ,
                                        'tag_val'     : 'replication' },


                'C_imagesTag'        :{'tag_name': 'images',
                                      'tag_val'     : images  },

                'D_statusTag'        :{'tag_name':'status' ,
                                      'tag_val'     : status},

                'E_sizeTag'          :{'tag_name':'size' ,
                                     'tag_val'     : 'toolarge'},

                'F_md5Tag'           :{'tag_name'    : 'md5' ,
                                      'tag_val'         : md5} ,

                'G_sdListTag'        :{'tag_name': 'sdlist' , 
                                      'tag_val'     : sdList},

                'H_machinename'        :{'tag_name': 'hostname' , 
                                      'tag_val'     : 'xp_vm'} }

      tags = OrderedDict(sorted(tags.items(), key=lambda t: t[0]))
rgm
  • 1,241
  • 2
  • 16
  • 33
  • 2
    Dictionaries are not ever ordered. But there are a variant called [ordered dictionaries](http://docs.python.org/2/library/collections.html#collections.OrderedDict), which probably do what you want. – Waleed Khan Mar 12 '13 at 12:15
  • Is my Output issue also related to Ordering in dictionary – rgm Mar 12 '13 at 12:22
  • 1
    @rakesh -- Yes. When you do `inner.keys()[0]`, you're assuming that `inner` keeps it's insertion order when in reality it doesn't. – mgilson Mar 12 '13 at 12:23
  • Thanks Guys, ALL i had to do was USe `Ordereddict`. I will post my code for required output . – rgm Mar 13 '13 at 06:38

1 Answers1

8

dict objects are not ordered -- More correctly, the order of a dict object is implementation, version and insertion order dependent. This is not to say that the order you insert the keys is the order that you get them back out. It only implies that if you insert the same keys into two different dictionaries, but in a different order, the order that you will get them back out is not guaranteed to be the same. For example:

>>> print {1:1, 9:2}
{1: 1, 9: 2}
>>> print {9:1, 1:2}
{9: 1, 1: 2}

This isn't to imply that the keys maintain their insertion order though. Notice what happens if I insert a key 2 into the dict at the end:

>>> print {9:1, 1:2, 2:4}
{9: 1, 2: 4, 1: 2}
>>> print {1:1, 9:2, 2:4}
{1: 1, 2: 4, 9: 2}

Here's another answer that I gave a while back which talks about how (C)python determines the order of your dictionary if you're interested.

If you need a dict to maintain order, you should use a collections.OrderedDict.

Community
  • 1
  • 1
mgilson
  • 300,191
  • 65
  • 633
  • 696