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=histor yRep[8]
if history8!=None:
history8=cmi.fo rmat_history(hi storyRep[8])
else:
history8=''
and
if historyRep[8]!=None:
history8=cmi.fo rmat_history(hi storyRep[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 8 4998
In <11************ *********@m73g2 000cwd.googlegr oups.com>, Fuzzydave
wrote:
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
`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
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
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
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
Fuzzydave wrote:
>
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.appe nd(None)
Cheers,
John
Fuzzydave wrote:
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_hist ory(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_hist ory(item)
history.append( s)
or maybe even
history = [cmi.format_hist ory(item) for item in historyRep]
if historyRep doesn't contain any None values.
Peter be************@ lycos.com wrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Andrew Dalke |
last post by:
Is there an author index for the new version of the
Python cookbook? As a contributor I got my comp version
delivered today and my ego wanted some gratification.
I couldn't find my entries.
Andrew
dalke@dalkescientific.com
|
by: Python_it |
last post by:
Python 2.4
MySQL-python.exe-1.2.0.win32-py2.4.zip
How can I insert a NULL value in a table (MySQL-database).
I can't set a var to NULL? Or is there a other possibility?
My var must be variable string or NULL.
Becaus i have a if statement:
if ....
cursor.execute(".................insert NULL ..............")
if ....
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c */
#include <time.h>
|
by: Dmitry Anikin |
last post by:
I mean, it's very convenient when default parameters
can be in any position, like
def a_func(x = 2, y = 1, z):
...
(that defaults must go last is really a C++ quirk which
is needed for overload resolution, isn't it?)
and when calling, just omit parameter when you want to
use defaults:
a_func(, , 3)
|
by: Kurt B. Kaiser |
last post by:
Patch / Bug Summary
___________________
Patches : 393 open (+15) / 3315 closed (+17) / 3708 total (+32)
Bugs : 908 open (+22) / 5975 closed (+49) / 6883 total (+71)
RFE : 223 open ( -1) / 229 closed ( +2) / 452 total ( +1)
New / Reopened Patches
______________________
| |
by: p.lavarre |
last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes
how?
A: We are of course supposed to write something like:
def c_not_null(pv):
return (ctypes.cast(pv, ctypes.c_void_p).value != None)
Yes?
|
by: Justin Johnson |
last post by:
Hello,
I'm trying to build Python 2.5.0 on AIX 5.3 using IBM's compiler
(VisualAge C++ Professional / C for AIX Compiler, Version 6). I run
configure and make, but makes fails with undefined symbols. See the
output from configure and make below.
svnadm /svn/build/python-2.5.0>env CC=cc CXX=xlC ./configure
--prefix=$base_dir \
checking MACHDEP... aix5
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |