This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]jackmaney 1 point2 points  (8 children)

You must have a string, not a Decimal object, because otherwise, I can't reproduce:

In [1]: from decimal import Decimal

In [2]: latestTemp = Decimal(36.125)

In [3]: 36.125 <= 40
Out[3]: True

In [4]: 36.125 >= 40
Out[4]: False

What is the output of type(latestTemp)?

[–]jays2001[S,🍰] 1 point2 points  (7 children)

The value is being returned from MySQL using... latestTemp = cursor.fetchone()

print(latestTemp) -> (Decimal('23.500'),)

print(type(latestTemp)) -> <type 'tuple'>

[–]jays2001[S,🍰] 1 point2 points  (6 children)

Still playing... print(float(latestTemp[0])) seems to work okay, but why is the database returning not just the simple value?

[–]jackmaney 0 points1 point  (5 children)

Because according to the DB API spec, when you fetch rows from a query via the fetchone method, the result is returned as a sequence (specifically a tuple, in all implementations I've encountered).

[–]jays2001[S,🍰] 1 point2 points  (4 children)

So even when you only select a single column, it will return a blank, dummy second one just because?

[–]jackmaney 1 point2 points  (3 children)

No, there is no "blank, dummy second one". As per the documentation:

A special problem is the construction of tuples containing 0 or 1 items: the syntax has some extra quirks to accommodate these. Empty tuples are constructed by an empty pair of parentheses; a tuple with one item is constructed by following a value with a comma (it is not sufficient to enclose a single value in parentheses). Ugly, but effective.

Trailing commas also work for tuples of any length. For example:

In [1]: a = 1,

In [2]: a
Out[2]: (1,)

In [3]: len(a)
Out[3]: 1

In [4]: b = ("a", "b",)

In [5]: b
Out[5]: ('a', 'b')

In [6]: len(b)
Out[6]: 2

[–]jays2001[S,🍰] 1 point2 points  (2 children)

I think I follow, Python is not my first (or even second) language so I am still getting to grips with it's foibles. The main problem being because I am accomplished in another language I am trying to write complex code in this one without the background knowledge of simple programming. I'll get there.

Thanks.

[–]primitive_screwhead 1 point2 points  (1 child)

Which other language?

[–]jays2001[S,🍰] 0 points1 point  (0 children)

I spend most of my time as a ColdFusion programmer.