0

So a few days ago, SOCommunity helped me with a parser. ie this:

def MyList(self):
    class _MyList(list):
        def __init__(self, parent):
            self.parent = parent
            list.__init__(self)
    top = current = _MyList(None)
    ntab_old = 0
    for line in self.raw_data:
        ntab = line.count('\t')
        if(ntab_old > ntab):
            for _ in range(ntab_old-ntab):
                current = current.parent
        elif(ntab_old<ntab):
            current.append(_MyList(current))
            current = current[-1]
        current.append(line.strip('\t').strip('|'))
        ntab_old =ntab
    return top

Parsing through data like this:

 |[nothing detected] www.neopets.com/
        |status: (referer=http://www.google.com)saved 55189 bytes /fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799
        |file: fetch_d4cd213a56276ca726ddd437a1e75f1024ab7799: 55189 bytes
        |file: decoding_367af53b4963986ecdeb9c09ce1a405b5b1ecd91: 68 bytes
        |[nothing detected] (script) images.neopets.com/js/common.js?v=6
            |status: (referer=http://www.google.com)saved 1523 bytes /fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7failure: [Errno 13] Permission denied: '/tmp/tmpsha1_8d7fb3ff1ef087c7ea2bf044dee294735a76ed4b.js'
            |file: fetch_8eeadcc08d4cb48c02dedf18648510b75190d1d7: 1523 bytes

This is supposed to return nested lists containing each sub section. However, out of nowhere, I'm getting these errors:

      current.append(line.strip('\t').strip('|'))
AttributeError: 'NoneType' object has no attribute 'append'

Any help would be appreciated

2
  • The data you posted above has an irregular mixture of tabs and spaces creating the indention. Is that the way your data really is? Commented Oct 25, 2012 at 12:59
  • Can you post the repr of your data snippet? That would show us exactly where the tabs are located. Commented Oct 25, 2012 at 13:06

1 Answer 1

2
current.append(...)
AttributeError: 'NoneType' object has no attribute 'append'

is saying that at some point current is None.

So how is current being set to None? My guess is it probably happens here:

        for _ in range(ntab_old-ntab):
            current = current.parent

If current is set to its parent often enough, it will eventually get set to None since at the top level, _MyList is instantiated with None as the parent:

top = current = _MyList(None)

This code might also be problematic:

    elif(ntab_old<ntab):
        current.append(_MyList(current))
        current = current[-1]

If ntab is say 3, and ntab_old is 1, then might need to move in two levels of "indention", but the code only creates one nested _MyList.

You might think that your levels of indention never jump by two. But that might be hard to tell with your eyes especially if the data is long. The data may not be as regular as we think.

Also, if there is a stray '\t' on the right-hand side of the |, then it would affect the ntab count even though it probably should not affect the indention level.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I see what you mean. But then, how would I prevent this from happening? :S I figured the only way you become a parent is if you have '0' tabs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.