Re: How/where to store calibration values - written by program A, read by program B

2023-12-09 Thread Peter J. Holzer via Python-list
On 2023-12-06 07:23:51 -0500, Thomas Passin via Python-list wrote:
> On 12/6/2023 6:35 AM, Barry Scott via Python-list wrote:
> > Personally I would not use .ini style these days as the format does not 
> > include type of the data.
> 
> Neither does JSON.

Well, it distinguishes between some primitive types (string, number,
boolean, null) and provides two container types (dict/object,
list/array). As long as those types are sufficient, JSON includes them.
If you need anything else, you're on your own.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


A problem with str VS int.

2023-12-09 Thread Steve GS via Python-list
 If I enter a one-digit input or a three-digit number, the code works but if I 
enter a two digit number, the if statement fails and the else condition 
prevails.

   tsReading = input("   Enter the " + Brand + " test strip reading: ")
if tsReading == "": tsReading = "0"
print(tsReading)
if ((tsReading < "400") and (tsReading >= "0")):
tsDose = GetDose(sReading)
print(tsReading + "-" + tsDose)
ValueFailed = False
else:
print("Enter valid sensor test strip Reading.")

I converted the variable to int along with the if statement comparison and it 
works as expected.
See if it fails for you...

Steve

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


RE: A problem with str VS int.

2023-12-09 Thread AVI GROSS via Python-list
Steve,

I would say converting to a number, as you eventually did, is the way to go.

When you compare character strings, it will not be in numeric order. Compare
"80" with "400" and since 8 is greater than 4, the comparison is over and
"80" is greater then "40" even though 80 is way less than 400.

The only way to get the kind of comparison you want would be to pad with
zeroes so all your "numbers" are the same length. In that case, "080" would
indeed test as less than "400" but rarely does that seem a good idea. If the
user can enter any text, they might enter ".01" or "hello" or al kinds of
nonsense.

If you converted to numbers and tested whether it failed, ...

-Original Message-
From: Python-list  On
Behalf Of Steve GS via Python-list
Sent: Saturday, December 9, 2023 9:42 PM
To: python-list@python.org
Subject: A problem with str VS int.

 If I enter a one-digit input or a three-digit number, the code works but if
I enter a two digit number, the if statement fails and the else condition
prevails.

   tsReading = input("   Enter the " + Brand + " test strip reading: ")
if tsReading == "": tsReading = "0"
print(tsReading)
if ((tsReading < "400") and (tsReading >= "0")):
tsDose = GetDose(sReading)
print(tsReading + "-" + tsDose)
ValueFailed = False
else:
print("Enter valid sensor test strip Reading.")

I converted the variable to int along with the if statement comparison and
it works as expected.
See if it fails for you...

Steve

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

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


Re: A problem with str VS int.

2023-12-09 Thread Thomas Passin via Python-list

On 12/9/2023 9:42 PM, Steve GS via Python-list wrote:

  If I enter a one-digit input or a three-digit number, the code works but if I 
enter a two digit number, the if statement fails and the else condition 
prevails.

tsReading = input("   Enter the " + Brand + " test strip reading: ")
 if tsReading == "": tsReading = "0"
 print(tsReading)
 if ((tsReading < "400") and (tsReading >= "0")):
 tsDose = GetDose(sReading)
 print(tsReading + "-" + tsDose)
 ValueFailed = False
 else:
 print("Enter valid sensor test strip Reading.")

I converted the variable to int along with the if statement comparison and it 
works as expected.
See if it fails for you...


I don't have to try it to know it will fail.  You think you are 
comparing numbers but you are comparing strings instead, which won't 
work as you expect.


You would do better to convert the inputs and limits to numbers, as well 
as the GetDose() function.  In a more realistic version, you would also 
have to make sure the user input is legal, either an int or a float, 
whichever you want to work with.


And along those lines (a more realistic version), it would be preferable 
to change the limits to be named constants, which will make the code 
easier to understand and change when it's revisited later.  Something 
like this:


UPPER = 400
LOWER = 0
# ...
if LOWER < value < UPPER:
# .

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


Re: A problem with str VS int.

2023-12-09 Thread dn via Python-list

On 10/12/23 15:42, Steve GS via Python-list wrote:

  If I enter a one-digit input or a three-digit number, the code works but if I 
enter a two digit number, the if statement fails and the else condition 
prevails.

tsReading = input("   Enter the " + Brand + " test strip reading: ")
 if tsReading == "": tsReading = "0"
 print(tsReading)
 if ((tsReading < "400") and (tsReading >= "0")):
 tsDose = GetDose(sReading)
 print(tsReading + "-" + tsDose)
 ValueFailed = False
 else:
 print("Enter valid sensor test strip Reading.")

I converted the variable to int along with the if statement comparison and it 
works as expected.
See if it fails for you...


It works as expected (by Python)! This is how strings are compared - 
which is not the same as the apparently-equivalent numeric comparison.


Think about what you expect from the code below, and then take it for a 
spin (of mental agility):


values = [ 333, 33, 3, 222, 22, 2, 111, 11, 1, ]
print( sorted( values ) )
strings = [ "333", "33", "3", "222", "22", "2", "111", "11", "1", ]
print( sorted( strings ) )


The application's data appears numeric (GetDose() decides!). 
Accordingly, treat it so by wrapping int() or float() within a 
try-except (and adjusting thereafter...).



"But wait, there's more!"
(assuming implement as-above):

if 0 <= ts_reading < 400:

1 consistent 'direction' of the comparisons = readability
2 able to "chain" the comparisons = convenience
3 identifier is PEP-008-compliant = quality and style

--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list