Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85440 > unrolled thread

Re: Varable parsing error with python

Started byOmPs <torque.india@gmail.com>
First post2015-02-10 14:00 +0530
Last post2015-02-11 10:55 +1100
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Varable parsing error with python OmPs <torque.india@gmail.com> - 2015-02-10 14:00 +0530
    Re: Varable parsing error with python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-11 10:55 +1100

#85440 — Re: Varable parsing error with python

FromOmPs <torque.india@gmail.com>
Date2015-02-10 14:00 +0530
SubjectRe: Varable parsing error with python
Message-ID<mailman.18605.1423557491.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

On 10 Feb 2015 13:59, "OmPs" <torque.india@gmail.com> wrote:
>
>
> On 10 Feb 2015 13:12, "Chris Angelico" <rosuav@gmail.com> wrote:
> >
> > On Tue, Feb 10, 2015 at 6:30 PM, OmPs <torque.india@gmail.com> wrote:
> > >     def _getPackgeVersion(xmlfile, p):
> > >         package = str(p)
> > >         if isinstance(fpmdict["application"]["package"], list):
> > >             for i in fpmdict["application"]["package"]:
> > >                 if i["@name"] == p:
> > >                     _pkgVersion = i["version"]
> > >         else:
> > >             _pkgversion = fpmdict["application"]["package"]["version"]
> > >             return _pkgVersion
> >
> > One of your branches doesn't have a return statement in it, so Python
> > just returns None. You may want to unindent that return statement one
> > level.
> >
> Tried that as well getting the same error.

I feel its something to do with variable substitution.

> > ChrisA
> > --
> > https://mail.python.org/mailman/listinfo/python-list

[toc] | [next] | [standalone]


#85481

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2015-02-11 10:55 +1100
Message-ID<54da9a68$0$13013$c3e8da3$5496439d@news.astraweb.com>
In reply to#85440
OmPs wrote:

> On 10 Feb 2015 13:59, "OmPs" <torque.india@gmail.com> wrote:

>> Tried that as well getting the same error.
> 
> I feel its something to do with variable substitution.

Python doesn't do variable substitution. At least not the way I think of it.
What do you mean by "variable substitution"?

Instead of "feeling" what the problem is, let's do some old-fashioned
debugging. Start by reading through the code, and in your head (or on
paper) trace through the possible lines that may be reached. The
_getPackgeVersion function has only 11 lines, it isn't hard. Here is your
function again:


def _getPackgeVersion(xmlfile, p):
    package = str(p)
    if isinstance(fpmdict["application"]["package"], list):
        for i in fpmdict["application"]["package"]:
            if i["@name"] == p:
                _pkgVersion = i["version"]
    else:
        _pkgversion = fpmdict["application"]["package"]["version"]
        return _pkgVersion


I can immediately see four problems:

(1) One branch of the "if isinstance" fails to return. So if the function
takes that branch, it will finish the loop, then exit the if...else clause,
then hit the end of the function and return None.

(2) Sometimes that branch may fail to set the _pkgVersion variable at all,
if none of the "@name" keys equal p.

(3) The other branch fails to set the _pkgVersion variable. Python is
case-sensitive, so _pkgversion and _pkgVersion are different variables.

(4) Your package variable is never used.

I don't know how to fix problem #2 since I don't know what behaviour would
be useful to you. So I'll just leave that for you. I can fix problems #1 #3
and #4:

def _getPackgeVersion(xmlfile, p):
    if isinstance(fpmdict["application"]["package"], list):
        for i in fpmdict["application"]["package"]:
            if i["@name"] == p:
                _pkgVersion = i["version"]
    else:
        _pkgVersion = fpmdict["application"]["package"]["version"]
    return _pkgVersion


Now we can see that if _getPackgeVersion still returns None, the only way it
can do so is if the "version" key is set to have None as a value.

Are you sure that there is a "version" key set?



I can see another problem, earlier in your code:


for i in doc.get("application").get("instance", None):
    ...


That does not do what you expect. If there is no "instance" key, the second
get will return None, which is not iterable:

py> for i in None:
...     pass
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable




-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web