Python checking for None/Null values

2006-08-11 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python checking for None/Null values

2006-08-11 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python checking for None/Null values

2006-08-11 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python checking for None/Null values

2006-08-11 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python checking for None/Null values

2006-08-11 Thread Fuzzydave
> [EMAIL PROTECTED] 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

-- 
http://mail.python.org/mailman/listinfo/python-list


round not rounding to 0 places

2006-08-16 Thread Fuzzydave
I have been using a round command in a few places to round
a value to zero decimal places using the following format,

round('+value+', 0)

but this consistantly returns the rounded result of the value
to one decimal place with a zero

EG:

4.97 is returned as 5.0 when i want it returned as 5, does
anyone know why this is and if i can get the round to make
the value 5?

David P

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: round not rounding to 0 places

2006-08-16 Thread Fuzzydave
> Sybren Stuvel wrote:
> round returns a float. Use
> int(round('+value+', 0))
> to get an integer.
> Sybren

ahh of course it does, slaps own forehead sorted 
thanks :)

David P

-- 
http://mail.python.org/mailman/listinfo/python-list


Nested Looping SQL Querys

2006-09-20 Thread Fuzzydave
I am back developing futher our Python/CGI based web application run by
a Postgres DB
and as per usual I am having some issues. It Involves a lot of Legacy
code. All the actual
SQL Querys are stored in the .py files and run in the .cgi files. I
have the problem that I
need to construct a row from two seprate SQL Querys, I have tried
combining the two
Querys but all that does is create a Query that returns nothing after a
long period running.

the first query results are delimited with [] and the second with {} I
want my result to
return [ val1 ] [ val2 ] [ val3 ] [ val4 ] { valA } { valB }
unfortunatly when i put my second
query in anywhere on the page its crashes and returns a Internal Server
Error.

the functions from the cmi file are below.



def creationSQL(pubID, productCode, description, suppNo1, all):

validQuery=0

if all:
all=int(all[0])
all = cromwell.toString(all)


sql='SELECT S.product_code, S.description, S.suppno1,
P.discount_factor, S.status, S.list_price, S.offer_price, P.page_no,
int8(P.oid), S.stock_qty '
sql=sql+'FROM (medusa.cmi_stockrec AS S INNER JOIN
medusa.cmi_auxstockrec AS A ON S.product_code=A.product_code) LEFT JOIN
medusa.cmi_pricing AS P ON S.product_code=P.product_code AND
P.pub_id='+pubID+' '
sql=sql+'WHERE '


if productCode!='':
sql=sql+cromwell.orSQL('S.product_code', productCode, 'ILIKE 
\'',
'%\'', 1)+' AND '
print 'Product Code: 
'+productCode+''
validQuery=1
if description!='':
sql=sql+' (upper(S.description) LIKE 
upper(\'%'+description+'%\'))
AND '
print 'Description: 
'+description+''
validQuery=1
if suppNo1!='':
sql=sql+' (upper(S.suppno1) LIKE upper(\'%'+suppNo1+'%\')) AND '
print 'Part No: '+suppNo1+''
validQuery=1
if all!=pubID:
sql=sql+' (P.product_code IS NULL) AND '

sql=sql[:-4]
sql=sql+' ORDER BY S.product_code'

print ''

if validQuery==1:
return sql
else:
return ''

def creationPubSQL(pubID, productCode, description, suppNo1, all,
pubList, pubPageNo):

validQuery=0

if all:
all=int(all[0])
all = cromwell.toString(all)


sql='SELECT Pl.product_code, S.description, S.suppno1,
P.discount_factor, S.status, Pl.list_price, Pl.offer_price, P.page_no,
int8(P.oid), Pl.page_no, S.stock_qty '
sql=sql+'FROM ((medusa.cmi_pricing AS Pl INNER JOIN
medusa.cmi_stockrec AS S ON S.product_code=Pl.product_code) INNER JOIN
medusa.cmi_auxstockrec AS A ON S.product_code=A.product_code) LEFT JOIN
medusa.cmi_pricing AS P ON S.product_code=P.product_code AND
P.pub_id='+pubID+' '
sql=sql+'WHERE Pl.pub_id='+pubList+' AND '

if productCode!='':
sql=sql+cromwell.orSQL('Pl.product_code', productCode, 'ILIKE 
\'',
'%\'', 1)+' AND '
print 'Product Code: 
'+productCode+''
validQuery=1
if description!='':
sql=sql+' (upper(S.description) LIKE 
upper(\'%'+description+'%\'))
AND '
print 'Description: 
'+description+''
validQuery=1
if suppNo1!='':
sql=sql+' (upper(S.suppno1) LIKE upper(\'%'+suppNo1+'%\')) AND '
print 'Part No: '+suppNo1+''
validQuery=1
if pubPageNo!='':
sql=sql+cromwell.orSQL('Pl.page_no', pubPageNo, '=\'', '\'', 
1)+' AND
'
print 'Publication Page No:
'+pubPageNo+''
validQuery=1
if all!=pubID:
sql=sql+' (P.product_code IS NULL) AND '

sql=sql[:-4]
sql=sql+' ORDER BY Pl.product_code'

print ''

if validQuery==1:
return sql
else:
return ''


def stockdetailsSQL(productCode):

validQuery=0

sql="SELECT (stkphys - stkalloc) as free_stock, stk_qty_wk, stkalloc,
stkordq, r.fd_deliverydue "
sql=sql+'FROM charisma.sk_stklfl LEFT JOIN
progress.report_firstdelivery as r ON stkl_stockno = r.fd_sordstk '
sql=sql+'WHERE stkl_stockno = \''+productCode+'\' AND stkl_location =
\'081\' ORDER BY stkl_stockno'
validQuery=1

sql=sql[:-4]

print ''


if validQuery==1:
return sql
else:
return ''


The page code for the CGI file that genereates the tables

#!/usr/bin/python

# Creation Screen
# MeDuSa - Marketing Data System

# $Id: creation.cgi 54 2006-02-16 11:32:12Z
[EMAIL PROTECTED] $


print 'Content-Type: text/html\n\n'


import sys
sys.stderr = sys.stdout

from pyPgSQL import libpq
import cgi
import string
import os
import cmi
import cromwell

import hermes
conn = hermes.db()


# This will allow us to retrieve submitted form fields.
cgiForm=cgi.FieldStorage()



# Start as

Calling a Postgres Function using CGI written in Python

2006-05-03 Thread Fuzzydave
I need to call a function stored in Postgres which does a lot of the db
and calculation work
all the SQL queries are hardcoded in a file called cmi.py. What i need
to do is too to call
my function from postgres passing in my product_code variable into it
and returning the
value from the query into a variable i can print to screen as part of a
HTML page.

I am sure its very easy but have never done anything in python as have
none of the guys
i work with making this a bit of a problem.

Cheers Guys
David

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling a Postgres Function using CGI written in Python

2006-05-03 Thread Fuzzydave
I did do a google search i have looking through the one python book i
have
and there are plenty of references for how to write a function in
Python but
not as many on how to call a function stored in postgres using python I
have
tried the conn.execute(cmi_grn_cost(productCode)) and also the
conn.callproc(cmi_grn_cost(productCode)) commands where conn is the
connection object and cmi_grn_cost() is the the function getting called
from 
Postgres and productCode is the variable to passed.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calling a Postgres Function using CGI written in Python

2006-05-04 Thread Fuzzydave
cheers :)

thats what i wanted to know :)

David

-- 
http://mail.python.org/mailman/listinfo/python-list