Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, November 13, 2009

Python didn’t have it, anyway…

Now, however, it does. Kinda.

def Python_to_XML_String(inst, name):
   builder = "<" + name + ">"
   builder += Python_to_XML_String_Recursion(inst)
   builder += "</" + name + ">"
   return minidom.parseString(builder)
   
def Python_to_XML_String_Recursion(inst):
   # Put entire object inside an elem w/ same name as the class.
   builder = ""
   for attr in inst:
      value = inst[attr]
      if type(value) == types.DictionaryType:
         # Recursively process subobjects
         builder += '<' + attr + '>'
         builder += Python_to_XML_String_Recursion(value)
         builder += '</' + attr + '>'
      elif type(value) == types.ListType:
         builder += '<' + attr + '>'
         for item in value:
            if type(item) == types.DictionaryType:
               builder += '<Item>'
               builder += Python_to_XML_String_Recursion(item)
               builder += '</Item>'
            else:
               builder += '<Item>'
               builder += str(item).replace("&","&amp;").replace("<","&lt;")
               builder += '</Item>'
         builder += '</' + attr + '>'
      else:
         # Convert anything else to string, put it in an element
         builder += '<' + attr + '>'
         builder += str(value).replace("&","&amp;").replace("<","&lt;")
         builder += '</' + attr + '>'
   return builder

(Note: I need a better source colouring plugin for WLW. Anyone know of one?)

This code is pretty limiting:

  • It’ll pretty much only work with generic type objects ({}, [] and primitives), but that’s all I need.
  • It also relies on the str() function to convert everything into a string, with no typing. This means that booleans (True, False) stay capitalised, which could cause some problems if your code is expecting lower-case values, and dates are unformatted.
  • It returns a minidom object (Which, obviously, has to be imported). You can output this with a .toxml() call.
  • It relies on the first object being passed in being a dictionary object, and that there will never be a list inside a list. This could be coded around, but it’ll take more work (and more helper functions, to fill in the “missing” information.
  • It takes an initial “name” argument to define the outer object.

To use it, call it like this:

Python_to_XML_String({'Thing': 'Value', 'Object', ['Thing 1', 'Thing 2']},'MyObject')

And it’ll return (for that example) an XML object looking like this:

<MyObject>
   <Thing>Value</Thing>
   <Object>
      <Item>Thing 1</Item>
      <Item>Thing 2</Item>
   </Object>
</MyObject>

I hope this is helpful to someone else out there…

Thursday, November 12, 2009

Wow. Python doesn’t have that? (Updated!)

This is going to be a rant, just so you are forewarned.

I have an object in Python that I’ve built that looks something like this:

Object = {
   'User': {
      'IsBanned': False, 
      'IsModerator': True, 
      'UserID': 'test@example.com', 
      'IsAdmin': True, 
      'LogoutURL': '/_ah/login?continue=http%3A//localhost%3A8080/CURRENT_URL&action=Logout', 
      'Location': u'Here.', 
      'Key': 'agVmb3J1bXIKCxIEVXNlchgCDA', 
      'UserLevel': 9L, 
      'NickName': u'Testing this works'
   }, 
   'Forum': [
      {
         'SubForums': [
            {
               'Threads': 0, 
               'Description': u'Test Three', 
               'Key': 'agVmb3J1bXILCxIFRm9ydW0YBQw', 
               'Title': u'Sub Forum'
            }
         ], 
         'Threads': 0, 
         'Description': u'Testing', 
         'Key': 'agVmb3J1bXILCxIFRm9ydW0YAww', 
         'Title': u'First Test'
      }, 
      {
         'Threads': 0, 
         'Description': u'Testing Two', 
         'Key': 'agVmb3J1bXILCxIFRm9ydW0YBAw', 
         'Title': u'Second Test'
      }, 
      {
         'SubForums': [
            {
               'SubForums': 
                  [
                     {'SubForums': 
                        [
                           {
                              'Threads': 0, 
                              'Description': u'Testing yet again', 
                              'Key': 'agVmb3J1bXILCxIFRm9ydW0YCQw', 
                              'Title': u'Another Forum'
                           }
                        ], 
                        'Threads': 0, 
                        'Description': u'Testing even more', 
                        'Key': 'agVmb3J1bXILCxIFRm9ydW0YCAw', 
                        'Title': u'Test Four'
                     }
                  ], 
                  'Threads': 0, 
                  'Description': u'Another test from JSON...', 
                  'Key': 'agVmb3J1bXILCxIFRm9ydW0YBww', 
                  'Title': u'Test Three'
               }
            ], 
            'Threads': 0, 
            'Description': u'Testing that JSON submitting a forum works...', 
            'Key': 'agVmb3J1bXILCxIFRm9ydW0YBgw', 
            'Title': u'test'
         }
      ]
   }

Taking advantage of the SimpleJSON library, I can serialise this into JSON with a single call. But I can’t find anything to serialise it into XML.

Well, technically speaking, I can. But all the libraries I have found want to do a LOT of massaging of the data structure, making it complex and unwieldy. What I want is a simple XML representation, like this:

<Object>
   <User>
      <IsBanned>False</IsBanned>
      <IsModerator>True</IsModerator>
      <UserID>test@example.com</UserID>
      <IsAdmin>True</IsAdmin>
      <LogoutURL>/_ah/login?continue=http%3A//localhost%3A8080/CURRENT_URL&action=Logout</LogoutURL>
      <Location>Here.</Location>
      <Key>agVmb3J1bXIKCxIEVXNlchgCDA</Key>
      <UserLevel>9</UserLevel>
      <NickName>Testing this works</NickName>
   </User>
   <Forum>
      <Item>
         <SubForums>
            <Item>
               <Threads>0</Thread>
               <Description>Test Three</Description>
               <Key>agVmb3J1bXILCxIFRm9ydW0YBQw</Key>
               <Title>Sub Forum</Title>
            </Item>
         </SubForums>
         <Threads>0</Threads>
         <Description>Testing</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YAww</Key>
         <Title>First Test</Title>
      </Item>
      <Item>
         <Threads>0</Threads>
         <Description>Testing Two</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YBAw</Key>
         <Title>Second Test</Title>
      </Item>
      <Item>
         <SubForums>
            <Item>
               <SubForums>
                  <Item>
                     <SubForums>
                        <Item>
                           <Threads>0</Threads>
                           <Description>Testing yet again</Description>
                           <Key>agVmb3J1bXILCxIFRm9ydW0YCQw</Key>
                           <Title>Another Forum</Title>
                        </Item>
                     </SubForums>
                     <Threads>0</Threads>
                     <Description>Testing even more</Description>
                     <Key>agVmb3J1bXILCxIFRm9ydW0YCAw</Key>
                     <Title>Test Four</Title>
                  </Item>
               </SubForums>
               <Threads>0</Threads>
               <Description>Another test from JSON...</Description>
               <Key>agVmb3J1bXILCxIFRm9ydW0YBww</Key>
               <Title>Test Three</Title>
            </Item>
         </SubForums>
         <Threads>0</Threads>
         <Description>Testing that JSON submitting a forum works...</Description>
         <Key>agVmb3J1bXILCxIFRm9ydW0YBgw</Key>
         <Title>test</Title>
      </Item>
   </Forum>
</Object>

It seems, however, that there is no Python library that can do that for me.

So why, I hear you asking, do I need such a thing?

Well, as you can probably tell from the listing(s) above, I’m writing an AJAX forum system. I’m doing it in Python so I can host it on Google App Engine (for free!), and can release it as open-source, but I’ve run into a pretty major snag.

I was using JSON to send data back and forth, which was working well. But I’ve found out that when you try and post using JSON to a different server (As the App Engine server will be, this is intended to be a “Widget” to use with Google Sites, among other things), an OPTIONS request is sent first to see if the cross-site request is allowed. I’ve grabbed the OPTIONS request, and replied with an “approved” message, but (even more to my consternation), the subsequent post from App Engine is empty. Oh, and cookies set before (on the GET) are not being sent with the POST, either. Thus I have decided to try switching to XML, and have run across the above roadblock.

I’m sure there are ways to do it, but searching across the ‘net has returned nothing. So I’m going to have to throw this open to the blogosphere, and hope something comes up. And soon.

[Update] As I found no-one had invented this particular wheel for me, I decided to roll my own, if you’ll excuse the pun(s).