Categories
Python Answers

How to get distance between two points based on latitude and longitude with Python?

Sometimes, we want to get distance between two points based on latitude and longitude with Python.

In this article, we’ll look at how to get distance between two points based on latitude and longitude with Python.

How to get distance between two points based on latitude and longitude with Python?

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.

To install it, we run

pip install geopy

Then we use it by writing

import geopy.distance

coords_1 = (52.2296756, 21.0122287)
coords_2 = (52.406374, 16.9251681)

print(geopy.distance.geodesic(coords_1, coords_2).km)

to get the distance between coords_1 and coords_2 with the geodesic method.

And we get the distance in kilometers with the km property.

Conclusion

To get distance between two points based on latitude and longitude with Python, we can use the geopy.distance module.

Categories
Python Answers

How to make HTTP requests and parse JSON response in Python?

Sometimes, we want to make HTTP requests and parse JSON response in Python.

In this article, we’ll look at how to make HTTP requests and parse JSON response in Python.

How to make HTTP requests and parse JSON response in Python?

To make HTTP requests and parse JSON response in Python, we can call the json method.

For instance, we write

import requests

r = requests.get('https://github.com/timeline.json')
d = r.json()

to call requests.get to make a GET request to a URL that returns a JSON response.

Then we call json on the response object to return a dict with the JSON response body.

Conclusion

To make HTTP requests and parse JSON response in Python, we can call the json method.

Categories
Python Answers

How to get list of parameter names inside Python function?

Sometimes, we want to get list of parameter names inside Python function.

In this article, we’ll look at how to get list of parameter names inside Python function.

How to get list of parameter names inside Python function?

To get list of parameter names inside Python function, we can use the func.__code__.co_varnames property on the function func.

For instance, we write

vars = func.__code__.co_varnames

to get a tuple of parameter names as strings of the function func.

Conclusion

To get list of parameter names inside Python function, we can use the func.__code__.co_varnames property on the function func.

Categories
Python Answers

How to do exponential and logarithmic curve fitting in Python?

Sometimes, we want to do exponential and logarithmic curve fitting in Python.

In this article, we’ll look at how to do exponential and logarithmic curve fitting in Python.

How to do exponential and logarithmic curve fitting in Python?

To do exponential and logarithmic curve fitting in Python, we fit y against log x.

For instance, we write

x = numpy.array([1, 7, 20, 50, 79])
y = numpy.array([10, 19, 30, 35, 51])
numpy.polyfit(numpy.log(x), y, 1)

to call numpy.array to create NumPy arrays of x and y axis values.

Then we call numpy.polyfit with numpy.log(x), which is an array with the log of each value in x, y, and 1 to do logarithmic curve fitting.

Conclusion

To do exponential and logarithmic curve fitting in Python, we fit y against log x.

Categories
Python Answers

How to get list of methods in a Python class?

Sometimes, we want to get list of methods in a Python class.

In this article, we’ll look at how to get list of methods in a Python class.

How to get list of methods in a Python class?

To get list of methods in a Python class, we can use list comprehension with the dir and callable functions.

For instance, we write

method_list = [func for func in dir(Foo) if callable(getattr(Foo, func))]

to call dir with the Foo class to get a list of members of Foo.

And then we use if callable(getattr(Foo, func) to check if the func member in the Foo class is a function.

If it is, then we include it in the list. Otherwise, they’re filtered out.

Then a list of methods in Foo is returned.

Conclusion

To get list of methods in a Python class, we can use list comprehension with the dir and callable functions.