From: Tim Chase on
On 08/05/10 13:52, Νίκος wrote:
> dataset = cursor.fetchall()
>
> for row in dataset:
> print ( '''<tr> ''' )
>
> date = row[2].strftime( '%d %b, %H:%M' )
>
> print ( '''<td> %s</td> <td> %s</td> <td> %s</td> ''' %
> ( row[0], row[1], date ) )
>
> Unfortunately had to ditch the 'for entry in row' line because
> couldn't iterate over the items of the row.
>
> Could you please shoe me how could i do the same thing with
> iteration?!

Well, depending on whether "row" is a tuple or a list, you can do
either

row[2] = row[2].strftime(...) # if it's a list
# the assignment will fail if it's a tuple

or you can just iterate over a predefined list/tuple:

for row in dataset:
print ("<tr>")
for item in (row[0], row[1], row[2].strftime(...)):
print ("<td>%s</td" % item)
print ("</tr>")

Though I think I'd make it a bit clearer by naming the fields:

for host, hits, dt in dataset:
print ("<tr>")
for item in (host, hits, dt.strftime(...)):
print ("<td>%s</td>" % item)
print ("</tr>")

Or perhaps even just

print ("".join("<td>%s</td>" % item
for item in (host, hits, dt.strftime(...))
)

Whichever you prefer. I think I'm partial to the 2nd-from-last
version, especially as the list of fields may grow.

-tkc



From: Tim Chase on
On 08/05/10 16:01, ����� wrote:
>> On 5 ���, 22:09, Tim Chase<python.l...(a)tim.thechases.com> wrote:
>>> dataset = cursor.fetchall()
>>
>>> for row in dataset:
>>> print ( '''<tr> ''' )
>
> So, 'dataset' in here is a 'list of tuples' right? and 'row'
> in here is a tuple form the above list of tuples right?
>
> Am i understanding this correctly?!
>
> It was a tuple. But it migth as well be a list too?!?!
>
> Could 'dataset' be a 'list of lists' as well?

Pretty much...it's either a list-of-tuples or a list-of-lists
(I'm not sure if is part of the DB-API spec to mandate one or the
other). For the most part, you can treat them as the same thing.
However, tuples are immutable, so you can't say

my_tuple[3] = some_value

but with a list you can:

my_list[3] = some_value

> How one would know in which way the returned mysql data is saved in?

Well, you can ask:

print type(row)

(I *suspect* it's a tuple) or you can just tell it what to be:

for row in dataset:
row = list(row)
row[3] = row[3].strftime(...)
for item in row:
...

I don't usually have cause to write a value back into the data
I'm reading from the DB, so it's never really mattered to me
whether the DB gives me tuples or lists.

>> Though I think I'd make it a bit clearer by naming the fields:
>>
>> for host, hits, dt in dataset:
>> print ("<tr>")
>> for item in (host, hits, dt.strftime(...)):
>> print ("<td>%s</td>" % item)
>> print ("</tr>")
>
> Cool! I myself like this solution best over the all working other!
> very nice approach thank you very much! Is what i anted and couldn't
> write myself!
>
> But please tell me if in my example 'row' was a tuple, what kind of
> objects is 'host', 'hits', 'dt' here and how do they sore the data?

Python supports "tuple assignment" which means that the following
are about[*] the same:

# variant A
for row in dataset:
host = row[0]
hits = row[1]
dt = row[2]
# rest of your code here

# variant B
for row in dataset:
host, hits, dt = row
# rest of your code here

# variant C
for host, hits, dt in row:
# rest of your code here

The data-type of the individual values would be whatever comes
back from the database as translated into Python (string,
float/Decimal, boolean, datetime, etc). In your example, it's
likely a string+integer+datetime as the 3 values. You can see
why I prefer the elegance of just performing the assignment in
the for-loop (variant C).

Hope this helps,

-tkc


[*] "about" the same because in #1 and #2, you also have access
to the whole row; whereas in #3, you don't have something called
"row", but you could reassemble it if you needed:

row = (host, hits, dt)




From: Νικόλαος Κούρας on
���� 6/8/2010 2:46 ��, �/� Tim Chase ������:
> # variant B
> for row in dataset:
> host, hits, dt = row
> # rest of your code here
So, row is a tuple comprising of 3 fields,
and host, hist, dt are variables assigned each one of row's tuple values
by breaking it to it's elements.

But what kind of objects is host, hits, dt that containes the row's
tuple data themselves? tuples or lists and why?

> # variant C
> for host, hits, dt in row:
> # rest of your code here
>
host, hits, data each and every one of them hold a piece of the row's
tuple values.

But what happens in here?

'for host, hits, dt in dataset:'

Here we don't have the row tuple. So what tthose variabels store, and in
what datatype they strore info in and what is the difference between this
and

'for host, hits, dt in row:'

What datatypes are these vars here and what data each one hold?

> The data-type of the individual values would be whatever comes back
> from the database as translated into Python (string, float/Decimal,
> boolean, datetime, etc). In your example, it's likely a
> string+integer+datetime as the 3 values. You can see why I prefer the
> elegance of just performing the assignment in the for-loop (variant C).
>

If the fieds datatypes returned form the database are for exmaple page
varchar(50) , hits inteeger[11], date datetime then
the when python grabs those results fields it would translate them to
'page as string' , (hits as int) , 'date as string' respectively?
Whcih emans it translated those fileds returned to the most
appropriate-most close to prototype stored in database' datatypes
automatically?

>
> [*] "about" the same because in #1 and #2, you also have access to the
> whole row; whereas in #3, you don't have something called "row", but
> you could reassemble it if you needed:
>
> row = (host, hits, dt

Would that be a row or a tuple when joined?

Again, thanks for ALL your preciosu help you provide me!

From: Tim Chase on
On 08/07/10 01:45, �������� ������ wrote:
>> # variant B
>> for row in dataset:
>> host, hits, dt = row
>> # rest of your code here
>
> So, row is a tuple comprising of 3 fields, and host, hist, dt
> are variables assigned each one of row's tuple values by
> breaking it to it's elements.
>
> But what kind of objects is host, hits, dt that containes the
> row's tuple data themselves? tuples or lists and why?

They contain the data of each respective element. E.g.:

>>> dataset = [
.... (1, 'a', True),
.... (2, 'b', False),
.... ]
>>>
>>> for one, two, three in dataset:
.... print 'one%s = %r' % (type(one), one)
.... print 'two%s = %r' % (type(two), two)
.... print 'three%s = %r' % (type(three), three)
.... print '-' * 10
....
one<type 'int'> = 1
two<type 'str'> = 'a'
three<type 'bool'> = True
----------
one<type 'int'> = 2
two<type 'str'> = 'b'
three<type 'bool'> = False
----------

So likely in your case, "host" is a string, "hits" is an int, and
"dt" is a datetime.datetime object. The three of them together
are the row as represented as a tuple:

>>> type( (host, hits, dt) )
<type 'tuple'>

which you can see in your own code by changing it temporarily to:

for row in dataset:
print type(row), len(row)

>> # variant C
>> for host, hits, dt in row:
>> # rest of your code here
>>
> host, hits, data each and every one of them hold a piece of the row's
> tuple values.
>
> But what happens in here?

The same as Variant B, only it doesn't use the intermediate tuple
"row".

> 'for host, hits, dt in dataset:'
>
> Here we don't have the row tuple. So what tthose variabels store, and in
> what datatype they strore info in and what is the difference between this
> and
>
> 'for host, hits, dt in row:'

The second one will fail because it would be the same as

for tpl in row:
host, hits, dt = tpl

The 1st time through the loop, tpl=host; the 2nd time through the
loop, tpl=hits; and the 3rd time through the loop, tpl=dt

Attempting to do a tuple assignment (that 2nd line) will attempt
to do something like

host, hits, dt = "example.com" # 1st pass through the loop
host, hits, dt = 42 # 2nd pass through the loop
host, hits, dt = datetime(2010,7,5)# 3rd pass through the loop

In most cases, it will fail on the first pass through the loop
(except in the freak case your string value happens to have 3
characters:

>>> host, hits, dt = "abc" #exactly 3 letters
>>> host
'a'
)

> If the fieds datatypes returned form the database are for exmaple page
> varchar(50) , hits inteeger[11], date datetime then
> the when python grabs those results fields it would translate them to
> 'page as string' , (hits as int) , 'date as string' respectively?
> Whcih emans it translated those fileds returned to the most
> appropriate-most close to prototype stored in database' datatypes
> automatically?

Yes, except the internals (of the DB module...in this case mysql)
are smart enough to translate the date into a datetime.datetime
object, instead of a string.

>> row = (host, hits, dt
>
> Would that be a row or a tuple when joined?

A "row" is a conceptual thing -- one row of data from your query.
It can be represented as either a tuple or a list (or any
iteratable that represents "things in this row"). In this case
(and I believe each row returned by a cursor.fetch*() call), it
was tuple.

I hope that helps...it would behoove you to experiment with
tuple-assignments such as the example code above so that you
understand what it's doing in each case.

-tkc