Skip to content

Add optional precision kwarg for formatting floating point data #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 26, 2019

Conversation

brentru
Copy link
Member

@brentru brentru commented Feb 26, 2019

Added precision kwarg to send_data to truncate floating point values to a specified precision decimal places before the payload is created and sent to Adafruit IO.

Addresses #12

Tested with PyPortal

@brentru brentru added the enhancement New feature or request label Feb 26, 2019
@brentru brentru requested review from jerryneedell and a team February 26, 2019 18:26
@brentru
Copy link
Member Author

brentru commented Feb 26, 2019

@jerryneedell I think this is OK for now as far as formatting, but would be open to other ideas.

@jerryneedell
Copy link

just setting up a test

@jerryneedell
Copy link

jerryneedell commented Feb 26, 2019

I modified the "data" simpletest to this

# Send random integer values to the feed
random_value = randint(0, 50)
print('Sending {0} to temperature feed...'.format(random_value))
io.send_data(temperature_feed['key'], random_value, metadata=None, precision=1)
print('Data sent!')

and I get this error on my ItsyBitsy_M4

Press any key to enter the REPL. Use CTRL-D to reload.
Adafruit CircuitPython 4.0.0-beta.2-141-g2c9fbb5d4 on 2019-02-24; Adafruit ItsyBitsy M4 Express with samd51g19
>>> 
>>> import ibm4_aio_data
Failed to connect, retrying
 ('No such ssid', b'Needell Airport')
Sending 30 to temperature feed...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ibm4_aio_data.py", line 67, in <module>
  File "adafruit_io/adafruit_io.py", line 146, in send_data
NotImplementedError: 
>>> 

@jerryneedell
Copy link

fixed -- the data value cannot be an "int" must be float. It does not make sense to "round" an int anyway but will this cause any unexpected errors?

@brentru
Copy link
Member Author

brentru commented Feb 26, 2019

Thanks for the catch for ints - I'll add handling for if a non-floating point value is passed in

@brentru
Copy link
Member Author

brentru commented Feb 26, 2019

Added a check for incorrect data types, throwing an error.

Copy link

@jerryneedell jerryneedell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be some corner cases where using precision catches a user sending an integer value but I think the warning will make it clear what happened,

@brentru brentru merged commit 52f8b9c into adafruit:master Feb 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants