Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

python XML vs JSON

Name: Anonymous 2013-01-07 19:36

Does /prog/ use python? And if so, are XML or JSON formats preferred?

Name: Anonymous 2013-01-07 19:41

1. One word, FIOC, etc.
2. http://homepages.inf.ed.ac.uk/wadler/papers/xml-essence/xml-essence-slides.pdf, which basically means XML is shit.

Conclusion: Lisp and S-expressions.

Name: Anonymous 2013-01-07 19:50

I asked because JSON does not seem to be easily queryable in python, where XML is a lot easier.

Name: Anonymous 2013-01-07 19:51

JSON does not seem to be easily queryable in python
import json

Name: Anonymous 2013-01-07 19:52

>>4

No shit, it's easily loaded, but not queried. How can I get a list of certain embedded elements? Of parent nodes that may not even exist?

Name: Anonymous 2013-01-07 19:56

http://docs.python.org/2/library/json.html
This is as easy as it gets. It returns a fucking dictionary with shit on it. If you can't even handle that, please go back to /g/ and don't come back til you've read SICP.

Name: Anonymous 2013-01-07 20:00

Sure, it's easy, but is it Abelson easy?

Name: Anonymous 2013-01-07 20:00

Maybe I'm not making myself clear. Maybe you're a faggot. Considering the following JSON:


{
    "query": {
        "pages": {
            "27697087": {
                "revisions": [
                    {
                        "user": "Graham87",
                        "timestamp": "2010-06-13T08:41:17Z",
                    }
                ]
            },
            "15580374": {
                "revisions": [
                    {
                        "user": "Edokter",
                        "timestamp": "2012-11-05T22:00:18Z",
                    }
                ]
            }
        }
    }
}


How would I select for users? With XML/lxml it'd be easy as fuck. With a dict, it seems labored.

Name: Anonymous 2013-01-07 20:00

Name: Anonymous 2013-01-07 20:05

>>8
Not sure if this may help you:


list = []
crap = data["query"]["pages"]
for number in crap:
    list.append(crap[number]["revisions"][0]["user"])
print list


I've never used XML so I don't know if it's easier that way, but this wasn't that hard. Hell, I don't even know if my solution works.

>>10
YOU (probably?) HELPED HIM!

Name: Anonymous 2013-01-07 20:10

My first thoughts were along those same lines. I guess my example was poor. Imagine user nodes were distributed at various depths in the tree.

Or is JSON just a shit choice with data that's not uniform in structure?

Name: Anonymous 2013-01-07 20:12

>>11
JSON is always a shit choice, but it's way better than XML.

The real problem here is that JSON libraries for Python are shit. No, Python is shit.

Name: Anonymous 2013-01-07 20:13

>>12
Those are basically the conclusions I was attempting to confirm. Thank you.

Name: Hartley Hare 2013-01-07 20:14

>>8
Who's Graham and why was he born in 1987?

Name: Anonymous 2013-01-07 20:14

>>11
Data that's not uniform in structure is just a shit choice.
If the data isn't properly structured when you get it, fix that first, before you try to query it.

Name: Anonymous 2013-01-07 20:23

>>15
My concern was that programming around the specific structure of the data was dangerous.

Name: Anonymous 2013-01-07 22:52

>>16
If you want your code to be able to adapt to changes in the JSON structure, you can write wrapper types for each structure with getter functions. It might seem shitty and it is shitty but xpath can be dangerous in other ways. Also you could use xpath with json if you wanted to, although there would need to be a way to handle json's arrays.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List