Python checking for None/Null values

F

Fuzzydave

Okay, I have been handed a python project and working through it I have

had to add a report. I am returning 10 variables the results of an SQL
Query
and as usual the number of results vary from 1 result to 10 results so
I
implemented a check to see if the array item was empty or not. The code

is below based upon the code already in the python project i was
handed.

history8=historyRep[8]
if history8!=None:
history8=cmi.format_history(historyRep[8])
else:
history8=''

and

if historyRep[8]!=None:
history8=cmi.format_history(historyRep[8])
else:
history8=''

but regardless i am getting the error below and i can't seen to resolve
this, what am i
doing wrong?

Traceback (most recent call last): File
"/home/phillipsd/work/medusa-new/htdocs/pricingrep.cgi", line 326, in ?
if historyRep[8]==None: IndexError: list index out of range


David P
 
M

Marc 'BlackJack' Rintsch

Fuzzydave said:
but regardless i am getting the error below and i can't seen to resolve
this, what am i
doing wrong?

Traceback (most recent call last): File
"/home/phillipsd/work/medusa-new/htdocs/pricingrep.cgi", line 326, in ?
if historyRep[8]==None: IndexError: list index out of range

`historyRep` seems to be shorter than you think it is. Try printing it
too see what it actually contains.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Sybren Stuvel

Fuzzydave enlightened us with:
if history8!=None:

Check with "if history8 is not None". Won't help your problem, but it
is a bit more pythonic code ;-)

Sybren
 
F

Fuzzydave

`historyRep` seems to be shorter than you think it is. Try printing it
too see what it actually contains.

Ciao,
Marc 'BlackJack' Rintsch

HistoryRep is an array value so historyRep[0] to [7] all have values
in them but historyRep[8] and [9] do not as the query does not always
return a full 10 values. I am trying to check all of the historyRep
items
to check if they are empty/null/None (whatever the term is in python)
and make it return an empty value or a none to the screen. I did print
historyRep[8] out and it falls over, I am assuming if its an array and
if the SQL query only returns 8 records instead of 10 then the last
two array values i am checking for litterly don't exist instead of
being
null but i can't find a if exists style function either?

David P
 
F

Fuzzydave

Check with "if history8 is not None". Won't help your problem, but it
is a bit more pythonic code ;-)

Sybren

Actually i tried that as well when i was fooling around, atm i am less
concenred
with pythonic code and making it work in the first place. The entire
program to
be fair is a bit messy but i do agree on the basis that the easier the
code is to
read the easier it is to work with.

David P
 
B

bearophileHUGS

Fuzzydave:
I am trying to check all of the historyRep items
to check if they are empty/null/None (whatever the term is in python)

An item can't be empty in Python,and null doesn't exist, it can be the
object None. But probly that's not your case.

I did print
historyRep[8] out and it falls over, I am assuming if its an array and
if the SQL query only returns 8 records instead of 10 then the last
two array values i am checking for litterly don't exist instead of
being null but i can't find a if exists style function either?

A way to solve your problem is to see how many elements the list
contains with
len(sequence)
then act accordingly with the elements that exist.
Even better is to work on the elements that exist, with something like:
for element in sequence:
do_stuff

Note: sometimes having a clean and readable program is better than
having a running program that you can't read, because you can fix the
the first one, and it can teach you something.

Bye,
bearophile
 
F

Fuzzydave

Note: sometimes having a clean and readable program is better than
having a running program that you can't read, because you can fix the
the first one, and it can teach you something.

Bye,
bearophile

Thanks for your help and suggestions i'll give them a shot.

Unfortunatly when working with 6 years of other peoples legacy code
(esspecially *this* code) it sometimes looks like it was specifiyed in
the
project spec *not* to be readable :)

But I will endevour to change that while working with it myself ;)

David P
 
P

Peter Otten

Fuzzydave said:
Okay, I have been handed a python project and working through it I have
had to add a report. I am returning 10 variables the results of an SQL
Query and as usual the number of results vary from 1 result to 10 results
so I implemented a check to see if the array item was empty or not. The
code is below based upon the code already in the python project i was
handed.

In Python list items do not magically spring into existence if you ask for
them. Therefore items[index] raises an IndexError if index is >=
len(items). A possible resolution is to check the length of historyRep
first:

if len(historyRep) > 8 and historyRep[8] is not None:
history8 = cmi.format_history(historyRep[8])
else:
history8 = ""

Note that names like history8 are a strong indication that you should use a
list rather than individual variables, e. g:

history = []
for item in historyRep:
if item is None:
s = ""
else:
s = cmi.format_history(item)
history.append(s)

or maybe even

history = [cmi.format_history(item) for item in historyRep]

if historyRep doesn't contain any None values.

Peter
 
J

John Machin

Fuzzydave said:
HistoryRep is an array value so historyRep[0] to [7] all have values
in them but historyRep[8] and [9] do not as the query does not always
return a full 10 values. I am trying to check all of the historyRep
items
to check if they are empty/null/None (whatever the term is in python)
and make it return an empty value or a none to the screen. I did print
historyRep[8] out and it falls over, I am assuming if its an array and
if the SQL query only returns 8 records instead of 10 then the last
two array values i am checking for litterly don't exist instead of
being
null but i can't find a if exists style function either?

Just paste this in immediately after getting the result back from the
query; it's not necessarily the fastest way but it's effin' obvious
what it's doing :)

while len(historyRep) < 10:
historyRep.append(None)

Cheers,
John
 
F

Fuzzydave

A way to solve your problem is to see how many elements the list
contains with
len(sequence)


cheers after your post went of to try it and it worked first time
thanks
for being helpful and plesant :)

Fuzzy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top