From: Josh English on
I have a script that generates a report from a bunch of data I've been
collecting for the past year. I ran the script, successfully, for
several weeks on test runs and creating more detailed reports.

Today (back from vacation) and the script doesn't work. It's giving me
a name error.

I'm running python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.
1310 32 bit (Intel)]. (Upgrading is not a possibility right now.
Eventually we'll be upgraded top Windows 7.)

Here is the reduced code:

fws_first_col = 6

for student in range(32):

if True:
idx = fws_first_col

for _month in range(12):
idx += 2
fws_last_col = idx


print fws_last_col

I get:

NameError: name 'fws_last_col' is not defined

This snippet of code works on it's own, but not in the main script. I
know it looks funny, but this should preserve the appropriate nesting
of things.

Where else could I look to find this problem?
From: Matt McCredie on
Josh English <joshua.r.english <at> gmail.com> writes:

>
> I have a script that generates a report from a bunch of data I've been
> collecting for the past year. I ran the script, successfully, for
> several weeks on test runs and creating more detailed reports.
>
> Today (back from vacation) and the script doesn't work. It's giving me
> a name error.
>
> I'm running python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.
> 1310 32 bit (Intel)]. (Upgrading is not a possibility right now.
> Eventually we'll be upgraded top Windows 7.)
>
> Here is the reduced code:
>
> fws_first_col = 6
>
> for student in range(32):
>
> if True:
> idx = fws_first_col
>
> for _month in range(12):
> idx += 2
> fws_last_col = idx
>
> print fws_last_col
>
> I get:
>
> NameError: name 'fws_last_col' is not defined
>
> This snippet of code works on it's own, but not in the main script.

That doesn't give me enough information to help you with the issue. In general
you need to provide enough code to reproduce the failure, not some modified
version that doesn't fail. My guess is that the "if True" is actually something
else, and it isn't being interpreted as "True". As such, "fws_last_col" never
gets assigned, and thus never gets created. You can fix that by assigning
fws_last_col to an appropriate default value before the for loop. But what do I
know, that is just a guess.

Matt



From: Josh English on
On Jul 1, 2:50 pm, Matt McCredie <mccre...(a)gmail.com> wrote:
>
> That doesn't give me enough information to help you with the issue. In general
> you need to provide enough code to reproduce the failure, not some modified
> version that doesn't fail. My guess is that the "if True" is actually something
> else, and it isn't being interpreted as "True". As such, "fws_last_col" never
> gets assigned, and thus never gets created. You can fix that by assigning
> fws_last_col to an appropriate default value before the for loop. But what do I
> know, that is just a guess.
>
> Matt

This is the code snippet with more context, but it isn't runnable
without creating a bunch of dummy objects:
# data is a dictionary. The keys are student names, the values are
Tracker object.
# The tracker class defines the method hasFWS() which returns a
boolean.
# _monthnumbers is a list: [7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6]
I just cut out the code that doesn't have anything to do the variable
fws_last_col

fws_first_col = 6

for student in sorted(data.keys() ):
#~ print student

tracker = data[student]

if tracker.hasFWS():

idx = fws_first_col


for _month in iter(_monthnumbers):
idx += 2
fws_last_col = idx

fws_month_count_col = idx + 4
fwsrow += 1

print fws_last_col
From: Mark Lawrence on
On 01/07/2010 22:30, Josh English wrote:
> I have a script that generates a report from a bunch of data I've been
> collecting for the past year. I ran the script, successfully, for
> several weeks on test runs and creating more detailed reports.
>
> Today (back from vacation) and the script doesn't work. It's giving me
> a name error.
>
> I'm running python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.
> 1310 32 bit (Intel)]. (Upgrading is not a possibility right now.
> Eventually we'll be upgraded top Windows 7.)
>
> Here is the reduced code:
>
> fws_first_col = 6

So fws_first_col is set to 6.

>
> for student in range(32):

student isn't used in the snippet below.

>
> if True:

Isn't this always going to run?

> idx = fws_first_col

Effectively set idx to 6 each time around the for loop.

>
> for _month in range(12):
> idx += 2

Add 24 to idx each time around the for loop. 24 + 6 == 30.

> fws_last_col = idx

set fws_last_col to 30 each time around the for loop.

>
>
> print fws_last_col

prints 30 only once.

>
> I get:
>
> NameError: name 'fws_last_col' is not defined
>
> This snippet of code works on it's own, but not in the main script. I
> know it looks funny, but this should preserve the appropriate nesting
> of things.

Certainly doesn't look at all correct to me.

>
> Where else could I look to find this problem?

In a mirror? :)

Kindest regards.

Mark Lawrence.

From: Rhodri James on
On Thu, 01 Jul 2010 23:07:53 +0100, Josh English
<joshua.r.english(a)gmail.com> wrote:

> On Jul 1, 2:50 pm, Matt McCredie <mccre...(a)gmail.com> wrote:
>>
>> My guess is that the "if True" is actually something
>> else, and it isn't being interpreted as "True". As such, "fws_last_col"
>> never
>> gets assigned, and thus never gets created. You can fix that by
>> assigning
>> fws_last_col to an appropriate default value before the for loop.
>
[snip]
> fws_first_col = 6
>
> for student in sorted(data.keys() ):
> #~ print student
>
> tracker = data[student]
>
> if tracker.hasFWS():
>
> idx = fws_first_col
>
>
> for _month in iter(_monthnumbers):
> idx += 2
> fws_last_col = idx
>
> fws_month_count_col = idx + 4
> fwsrow += 1
>
> print fws_last_col

[I snipped the explanations for brevity]

If this is a version of your code that actually fails when you run it
(rather than being another artistic interpretation of a photograph of your
code :-), then I'd go with Matt's analysis. This will give you a
NameError for fws_last_col if tracker.hasFWS() happens to return False for
all students.

--
Rhodri James *-* Wildebeeste Herder to the Masses
 |  Next  |  Last
Pages: 1 2
Prev: Packaging question
Next: Why defaultdict?