Diary of an IT Guy #AnakBinus

Build Bridges, not Walls. Collaboration Forever!

Posts Tagged ‘python

Integrasi REST API dalam Aplikasi Python

leave a comment »

Berikut adalah contoh coding sederhana untuk mengkonsumi REST API dari MAPQuest dalam sebuah aplikasi Python sederhana. Di sini akan dilakukan input antara dua kota dengan output menampilkan jumlah kilometer dan perkiraan konsumi bahan bakar dalam liter.

import urllib.parse
import requests

main_api = "https://www.mapquestapi.com/directions/v2/route?"
key = "AGhVxXq1gAn9cNLB1XfYYA6FeEQ9SG4U"

while True:
    orig = input("Starting Location: ")
    if orig == "quit" or orig == "q":
        break
    dest = input("Destination: ")
    if dest == "quit" or dest == "q":
        break
    
    url = main_api + urllib.parse.urlencode({"key":key, "from":orig, "to":dest})
    print("URL: " + (url))
    json_data = requests.get(url).json()
    json_status = json_data["info"]["statuscode"]
    if json_status == 0:
        print("API Status: " + str(json_status) + " = A successful route call.\n")
        print("=============================================")
        print("Directions from " + (orig) + " to " + (dest))
        print("Trip Duration: " + (json_data["route"]["formattedTime"]))
        #print("Miles: " + str(json_data["route"]["distance"]))
        #print("Fuel Used (Gal): " + str(json_data["route"]["fuelUsed"]))
        print("Kilometers: " + str((json_data["route"]["distance"])*1.61))
        print("Fuel Used (Ltr): " + str((json_data["route"]["fuelUsed"])*3.78))
        print("=============================================")

Hasil outputnya:

Kita upgrade app di atas menjadi lebih lengkap lagi dengan dilengkapi error input error handling sbb:

import urllib.parse
import requests

main_api = "https://www.mapquestapi.com/directions/v2/route?"
key = "AGhVxXq1gAn9cNLB1XfYYA6FeEQ9SG4U"

while True:
    orig = input("Starting Location: ")
    if orig == "quit" or orig == "q":
        break
    dest = input("Destination: ")
    if dest == "quit" or dest == "q":
        break
    
    url = main_api + urllib.parse.urlencode({"key":key, "from":orig, "to":dest})
    print("URL: " + (url))
    json_data = requests.get(url).json()
    json_status = json_data["info"]["statuscode"]
    if json_status == 0:
        print("API Status: " + str(json_status) + " = A successful route call.\n")
        print("=============================================")
        print("Directions from " + (orig) + " to " + (dest))
        print("Trip Duration: " + (json_data["route"]["formattedTime"]))
        #print("Miles: " + str(json_data["route"]["distance"]))
        #print("Fuel Used (Gal): " + str(json_data["route"]["fuelUsed"]))
        print("Kilometers: " + str((json_data["route"]["distance"])*1.61))
        print("Fuel Used (Ltr): " + str("{:.2f}".format((json_data["route"]["fuelUsed"])*3.78)))
        print("=============================================")

        for each in json_data["route"]["legs"][0]["maneuvers"]:
            print((each["narrative"]) + " (" + str("{:.2f}".format((each["distance"])*1.61) + " km)"))
        print("=============================================\n")
    
    elif json_status == 402:
        print("**********************************************")
        print("Status Code: " + str(json_status) + "; Invalid user inputs for one or both locations.")
        print("**********************************************\n")
    elif json_status == 611:
        print("**********************************************")
        print("Status Code: " + str(json_status) + "; Missing an entry for one or both locations.")
        print("**********************************************\n")
    else:
        print("************************************************************************")
        print("For Staus Code: " + str(json_status) + "; Refer to:")
        print("https://developer.mapquest.com/documentation/directions-api/status-codes")
        print("************************************************************************\n")

Hasilnya adalah sbb:

Written by isal

14 September 2021 at 09:29

Ditulis dalam Web Programming

Tagged with , ,

Python: Line Plot Basic (1)

leave a comment »

With matplotlib, you can create a bunch of different plots in Python. The most basic plot is the line plot. A general recipe is given here.

import matplotlib.pyplot as plt
plt.plot(x,y)
plt.show()

The world bank has estimates of the world population for the years 1950 up to 2100. The years are loaded in your workspace as a list called year, and the corresponding populations as a list called pop.


# Print the last item from year and pop
print (year[-1])
print (pop[-1])

# Import matplotlib.pyplot as plt
import matplotlib.pyplot as plt

# Make a line plot: year on the x-axis, pop on the y-axis
plt.plot (year,pop)

# Display the plot with plt.show()
plt.show()

RESULT:

2019-01-20_001154

Written by isal

20 Januari 2019 at 00:13

Ditulis dalam Data Science, Uncategorized

Tagged with ,

Latihan Numpy Array di Python (2019-01-19)

leave a comment »

In the last few exercises you’ve learned everything there is to know about heights and weights of baseball players. Now it’s time to dive into another sport: soccer.

You’ve contacted FIFA for some data and they handed you two lists. The lists are the following:

positions = ['GK', 'M', 'A', 'D', ...]
heights = [191, 184, 185, 180, ...]

Each element in the lists corresponds to a player. The first list, positions, contains strings representing each player’s position. The possible positions are: 'GK' (goalkeeper), 'M' (midfield), 'A' (attack) and 'D' (defense). The second list, heights, contains integers representing the height of the player in cm. The first player in the lists is a goalkeeper and is pretty tall (191 cm).

You’re fairly confident that the median height of goalkeepers is higher than that of other players on the soccer field. Some of your friends don’t believe you, so you are determined to show them using the data you received from FIFA and your newly acquired Python skills.


# heights and positions are available as lists

# Import numpy
import numpy as np

# Convert positions and heights to numpy arrays: np_positions, np_heights
np_positions = np.array(positions)
np_heights = np.array(heights)

# Heights of the goalkeepers: gk_heights
gk_heights = np.array(np_heights[np_positions=='GK'])
#print (gk_heights)

# Heights of the other players: other_heights
other_heights = np.array(np_heights[np_positions !='GK'])
#print (other_heights)

# Print out the median height of goalkeepers. Replace 'None'
print("Median height of goalkeepers: " + str(np.median(gk_heights)))

# Print out the median height of other players. Replace 'None'
print("Median height of other players: " + str(np.median(other_heights)))

RESULT:

Median height of goalkeepers: 188.0
Median height of other players: 181.0

Written by isal

19 Januari 2019 at 23:54

Ditulis dalam Data Science

Tagged with , ,

Top Five Data Science Domain

leave a comment »

Hello world Data Science!

Data Science Domains.pdf

Written by isal

18 Januari 2019 at 19:15

%d blogger menyukai ini: