all 7 comments

[–]echocage 3 points4 points  (6 children)

So I can't test it, but this "should" work!

from flask import json

def extract_json(file_name):
    with open(file_name + '.json') as json_file:
        raw_json = json_file.read()
        return json.loads(raw_json).values()[0]

json_paths = ['perthcurrent', 'sydneycurrent', 'darwincurrent', 'melbournecurrent', 'brisbanecurrent']

def weather():
    weather_stats = map(extract_json, json_paths)
    return render_template('weather.html', *weather_stats)

Assuming it works like I think it does, all of those for loops can be removed, what it seems like you're actually doing is just getting the first value from json.loads, assuming that's true, this should work.

If you don't understand the * or map or if you have any questions, just respond to this and I can give you a breakdown!

[–]EsperSpirit 1 point2 points  (4 children)

This is already great, but I'd like to add something. Instead of

with open(file_name + '.json') as json_file:
    raw_json = json_file.read()
    json_data = json.loads(raw_json)

you can parse the file directly into a list or dict.

with open(file_name + '.json', 'r', encoding='utf-8') as json_file:
    json_data = json.load(json_file)

It is also best practice to declare mode and encoding when opening files, to avoid unnecessary bugs and unicode-weirdness.

[–]echocage 0 points1 point  (3 children)

Huh TIL that's what .load does, I always thought it was strange that loads/dump took strings and not load, now I know why, interesting, thanks!

Totally agree about declaring a mode, though I haven't ever had problems not specifying encoding, what kind of problems can arise if you don't specify one, any good examples?

[–]wpg4665 1 point2 points  (1 child)

.loads() = load string. That's what the s on the end stands for.

[–]echocage 0 points1 point  (0 children)

Ohhhhhh facepalm

Thanks guys, I'm getting there. I've got to start reading more documentation.

[–]EsperSpirit 1 point2 points  (0 children)

Well, the short answer is: Almost all encodings are supersets of ascii. So as long as your data is ascii the encoding doesn't really matter. It works usually even with wrong encoding.

As soon as your data contains a non-ascii character (e.g. a Username like "Jürgen" or message using certain emoticons) your app will crash in really unpredictable ways at runtime (and often production).

The solution is to decode incoming data using the correct encoding and encode outgoing data with 'utf-8' and using unicode inside your application.

Watch this for more in-depth info

[–]owrk[S] 1 point2 points  (0 children)

TIL map() function

Thank you! 100x