471,052 Members | 1,183 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,052 software developers and data experts.

TypeNone field detection

What is the best way to detect a TypeNone field in a tuple, or in a list?

I am accessing a MySQL database using the MySQLdb Python interface... this
interface returns a tuple object type in response to SQL SELECT
statements. My understanding of the MySQLdb interface is that NULL
database values are returned as a Python 'None' object.

Because I need to create some work fields based on the contents of the
database I am doing so by copying the tuple to a list object and then
calculating these work fields as needed. My problem is that I need to be
able to detect the Python 'None' objects and convert them to an integer
zero value field to enable my calculations to work.

I'm new to Python so I'm sure I'm overlooking something simple – but what
is the easiest way to do a logical test for the existence of a TypeNone
field?
Jun 27 '08 #1
3 1107
Joe Blow wrote:
What is the best way to detect a TypeNone field in a tuple, or in a list?

I am accessing a MySQL database using the MySQLdb Python interface... this
interface returns a tuple object type in response to SQL SELECT
statements. My understanding of the MySQLdb interface is that NULL
database values are returned as a Python 'None' object.

Because I need to create some work fields based on the contents of the
database I am doing so by copying the tuple to a list object and then
calculating these work fields as needed. My problem is that I need to be
able to detect the Python 'None' objects and convert them to an integer
zero value field to enable my calculations to work.

I'm new to Python so I'm sure I'm overlooking something simple – but what
is the easiest way to do a logical test for the existence of a TypeNone
field?
Since there is only one instance of TypeNone (the value we reference as
None) the easiest test is

if x is None:

There is no need to create a list first: you can create a list as you
iterate over the tuple:

a = (1, 2, None, "a", "b")
a = [0 if x is None else x for x in a]

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Jun 27 '08 #2
On Apr 16, 7:24*pm, Joe Blow <joeb...@nowhere.nohowwrote:
What is the best way to detect a TypeNone field in a tuple, or in a list?

I am accessing a MySQL database using the MySQLdb Python interface... this
interface returns a tuple object type in response to SQL SELECT
statements. *My understanding of the MySQLdb interface is that NULL
database values are returned as a Python 'None' object.

Because I need to create some work fields based on the contents of the
database I am doing so by copying the tuple to a list object and then
calculating these work fields as needed. *My problem is that I need to be
able to detect the Python 'None' objects and convert them to an integer
zero value field to enable my calculations to work.

I'm new to Python so I'm sure I'm overlooking something simple but what
is the easiest way to do a logical test for the existence of a TypeNone
field?
>>a = 0
a==None
False
>>b = None
b==None
True
>>type(a)
<type 'int'>
>>type(b)
<type 'NoneType'>
Jun 27 '08 #3
On Wed, 16 Apr 2008 21:13:18 -0400, Steve Holden wrote:
>
Since there is only one instance of TypeNone (the value we reference as
None) the easiest test is

if x is None:
Thanks... the "if x is None:" statement is exactly what I was looking for.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

60 posts views Thread by Fotios | last post: by
6 posts views Thread by Gustav Medler | last post: by
18 posts views Thread by Mickey Segal | last post: by
8 posts views Thread by R. Smits | last post: by
12 posts views Thread by Adam Lipscombe | last post: by
7 posts views Thread by mosaic | last post: by
reply views Thread by origami.takarana | last post: by
10 posts views Thread by Conrad Lender | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.