PyPortal Truncates Floats in JSON

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
macewmi
 
Posts: 69
Joined: Sat Feb 08, 2020 10:26 am

PyPortal Truncates Floats in JSON

Post by macewmi »

I have the following code:

Code: Select all

DATA_SOURCE = "https://exchange-rates.abstractapi.com/v1/live/?api_key=" + secrets['abstract'] + "&base=USD&target=CAD"

# {"base":"USD","last_updated":1679404500,"exchange_rates":{"EUR":0.927214}}
last_updated_LOCATION = ['last_updated']
EUR_LOCATION = ['exchange_rates', 'CAD']

cwd = ("/"+__file__).rsplit('/', 1)[0]
pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=(EUR_LOCATION, last_updated_LOCATION),
                    text_font = cwd+"/fonts/6x10.bdf",
                    text_position=((5,25), (190, 25)),
                    text_color=(0xffff00, 0xffff00),
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/cta_black_splash.bmp"
                    )

pyportal.preload_font()

while True:
    try:
        value = pyportal.fetch()
    except RuntimeError as e:
        print("Some error occured, retrying! -", e)
    print(value)
    time.sleep(60)
    
In the while loop, value is [1.37061, 1679404500]. This is expected. But on the display, the 1.37061 displays as 1 and the 1679404500 displays as 1,679,404,500. The PyPortal seems to be formatting the data the way it wants to.

Can I easily prevent that from happening?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: PyPortal Truncates Floats in JSON

Post by adafruit_support_mike »

[moved to the CircuitPython forum]

User avatar
neradoc
 
Posts: 542
Joined: Wed Apr 27, 2016 2:38 pm

Re: PyPortal Truncates Floats in JSON

Post by neradoc »

Hi, numbers are formatted as ints by default in the pyportal library.
You can add a "text_transform" argument to PyPortal().
Here is an example:

Code: Select all

import time
def _format_datetime(datetime):
    return "{}/{:02}/{:02} {:02}:{:02}:{:02}".format(
        datetime.tm_year,
        datetime.tm_mon,
        datetime.tm_mday,
        datetime.tm_hour,
        datetime.tm_min,
        datetime.tm_sec,
    )

def transformer_rate(convert):
    return f"{convert:.3f}"

def transformer_location(convert):
    tt = time.localtime(convert)
    return _format_datetime(tt)

pyportal = PyPortal(url=DATA_SOURCE,
                    json_path=(EUR_LOCATION, last_updated_LOCATION),
                    text_font = cwd+"/fonts/6x10.bdf",
                    text_position=((5,25), (190, 25)),
                    text_color=(0xffff00, 0xffff00),
                    status_neopixel=board.NEOPIXEL,
                    default_bg=cwd+"/cta_black_splash.bmp",
                    text_transform=(transformer_rate, transformer_location),
                    )

User avatar
macewmi
 
Posts: 69
Joined: Sat Feb 08, 2020 10:26 am

Re: PyPortal Truncates Floats in JSON

Post by macewmi »

@neradoc, excellent! It's exactly what I needed to know. Thank you.

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”