Re: [Tutor] Handling 'None' (null) values when processing sqlite cursorresults

2010-07-14 Thread Alan Gauld


Monte Milanuk memila...@gmail.com wrote

(104, None, u'Sylvester', None, u'Evans', None, u'527-9210 Proin 
Av.', u'Liberal', u'VT', u'24742', u'1-135-197-1139', 
u'vehicula.pellentes...@idmollis.edu', u'2010-07-13 22:52:50', 
u'2010-07-13 22:52:50')


At first I was having fits as str.join() was giving me a 'NoneType 
error'.  I found one way around that, by processing the results so 
the 'None' values got omitted from the list by the time they got to 
str.join().


Keep the storage and manipulation of data separate from the
display of that data. That is an important and fundamental principle
of data processing. Store the data with the nulls. Display the data 
without.


values omitted really messed with the list order which I was 
depending on i.e. list[5] could be different fields depending on how 
many 'None'


And that is why.

values had been omitted.  And if I didn't omit them, when I printed 
out the user name in the format 'first''middle''last' from the above 
record,


You need a display function that can strip out the nulls as needed.
A simple list comprehension or generator expression would work
in this case:

print ' '.join(str(field) for field in data if field is not 'None')

If you use a Class to model your data you can write a __str__() method
that does the intellifgent production of a string fit for printing. 
Then you just
print the records as normal. But for this scenario the complexity of 
building

a class may not be worth it.

The key principle is do not try to store your data in a display 
format.



HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling 'None' (null) values when processing sqlite cursorresults

2010-07-14 Thread Christian Witts

On 14/07/2010 14:32, Alan Gauld wrote:


Monte Milanuk memila...@gmail.com wrote

(104, None, u'Sylvester', None, u'Evans', None, u'527-9210 Proin 
Av.', u'Liberal', u'VT', u'24742', u'1-135-197-1139', 
u'vehicula.pellentes...@idmollis.edu', u'2010-07-13 22:52:50', 
u'2010-07-13 22:52:50')


At first I was having fits as str.join() was giving me a 'NoneType 
error'.  I found one way around that, by processing the results so 
the 'None' values got omitted from the list by the time they got to 
str.join().


Keep the storage and manipulation of data separate from the
display of that data. That is an important and fundamental principle
of data processing. Store the data with the nulls. Display the data 
without.


values omitted really messed with the list order which I was 
depending on i.e. list[5] could be different fields depending on how 
many 'None'


And that is why.

values had been omitted.  And if I didn't omit them, when I printed 
out the user name in the format 'first''middle''last' from the above 
record,


You need a display function that can strip out the nulls as needed.
A simple list comprehension or generator expression would work
in this case:

print ' '.join(str(field) for field in data if field is not 'None')




The problem with that is if you're relying on a set delimiter you are 
removing elements so you would be better served by doing `str(field) if 
field != None else '' for field in record`


--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling 'None' (null) values when processing sqlite cursorresults

2010-07-14 Thread Alan Gauld


Christian Witts cwi...@compuscan.co.za wrote


You need a display function that can strip out the nulls as needed.
A simple list comprehension or generator expression would work
in this case:
print ' '.join(str(field) for field in data if field is not 
'None')


The problem with that is if you're relying on a set delimiter you 
are removing elements so you would be better served by doing 
`str(field) if field != None else '' for field in record`


True, although in this case we do seem to have a fixed condition
since it's the database's representation of null but I originally 
didn't

notice the quotes around None. When I fixed that I should have
changed is to equals..


print ' '.join(str(field) for field in data if field != 'None')


But your modification confuses me. can you explain?
What would the print line look like?

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling 'None' (null) values when processing sqlite cursorresults

2010-07-14 Thread Monte Milanuk

On 7/14/10 5:32 AM, Alan Gauld wrote:


The key principle is do not try to store your data in a display format.


Never was my intention.  I just hadn't anticipated needing to write my 
own function to handle something as (I would think) common as a NULL 
value in a database field.


I had been working with something very simple like:

for lines in cursor.fetchall()
title = lines[1]
first = lines[2]
mid = lines[3]
last = lines[4]
...

print %s %s %s %s % (title, first, mid, last)
print %s % (street)
print %s, %s %s % (city, state, zipcode)
print %s % (phone)
print %s % (email)

etc. etc.  for one format (supposed to look like a mailing label, more 
or less), and then I was going to experiment with other formatting later.


I'll work with the stuff you all have provided as I get time... thanks a 
bunch!


Monte

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Handling 'None' (null) values when processing sqlite cursorresults

2010-07-14 Thread Christian Witts

On 14/07/2010 19:34, Alan Gauld wrote:


Christian Witts cwi...@compuscan.co.za wrote


You need a display function that can strip out the nulls as needed.
A simple list comprehension or generator expression would work
in this case:

print ' '.join(str(field) for field in data if field is not 'None')

The problem with that is if you're relying on a set delimiter you are 
removing elements so you would be better served by doing `str(field) 
if field != None else '' for field in record`


True, although in this case we do seem to have a fixed condition
since it's the database's representation of null but I originally didn't
notice the quotes around None. When I fixed that I should have
changed is to equals..


print ' '.join(str(field) for field in data if field != 'None')


But your modification confuses me. can you explain?
What would the print line look like?

Alan G.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



I prefer not to use a space delimiter, generally a pipe |, so the output 
would look a lot like
104||Sylvester||Evans||527-9210 Prion 
Av.|Liberal|VT|24742|1-135-197-1139|vehicula.pellentes...@idmollis.edu|2010-07-13 
22:52:50|2010-07-13 22:52:50


The reason I suggested that usage instead is because Monte said
 having the 'None' values omitted really messed with the list order 
which I was depending on


So if you're wanting to create a new list which removes the Nones but 
still keeps the same indexing it would be the way to do it, imho.


--
Kind Regards,
Christian Witts


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor