You could use the with..as construct for opening the url. This way you don't have to remember to close it:
def data_fetch(full_api_url):
with urllib.request.urlopen(full_api_url) as url:
return json.loads(url.read().decode('utf-8'))
Defining temporary variables makes the following more readable imho:
def data_organizer(raw_data):
main = raw_data.get('main')
sys = raw_data.get('sys')
data = dict(
city=raw_data.get('name'),
country=sys.get('country'),
temp=main.get('temp'),
temp_max=main.get('temp_max'),
temp_min=main.get('temp_min'),
humidity=main.get('humidity'),
pressure=main.get('pressure'),
sky=raw_data['weather'][0]['main'],
sunrise=time_converter(sys.get('sunrise')),
sunset=time_converter(sys.get('sunset')),
wind=raw_data.get('wind').get('speed'),
wind_deg=raw_data.get('deg'),
dt=time_converter(raw_data.get('dt')),
cloudiness=raw_data.get('clouds').get('all')
)
return data
Also note that raw_api_dict.get('main').get('temp_max')
will fail if 'main' not in raw_api_dict
, same as above code. Depending on the API you might want to wrap it in a try..except or use assert(main is not None)
.
You could go one step further from what @Sisoma Munden proposed, by giving format the data dictionary and using named placeholders:
def data_output(data):
data['m_symbol'] = '\xb0' + 'C'
s = '''---------------------------------------
Current weather in: {city}, {country}:
{temp}{m_symbol} {sky}
Max: {temp_max}, Min: {temp_min}
Wind Speed: {wind}, Degree: {wind_deg}
Humidity: {humidity}
Cloud: {cloudiness}
Pressure: {pressure}
Sunrise at: {sunrise}
Sunset at: {sunset}
Last update from the server: {dt}
---------------------------------------'''
print(s.format(**data))
This permanently adds the key 'm_symbol' to data, but this should be of no consequence, since it is not used elsewhere. If you want to play it safer, copy the dict first and modify the copy.