Skip to content

Instantly share code, notes, and snippets.

@jakubczaplicki
Created September 27, 2021 13:55
Show Gist options
  • Save jakubczaplicki/369f85c34117e5b7e7a8ec40f41fb1d8 to your computer and use it in GitHub Desktop.
Save jakubczaplicki/369f85c34117e5b7e7a8ec40f41fb1d8 to your computer and use it in GitHub Desktop.
Python singledispatch
from datetime import datetime
from functools import singledispatch
from utils.data.data_point import DataPoint
@singledispatch
def extract_data(item, **kwargs):
return None # unknown type
@extract_data.register(dict)
def _(item):
return {
"lat": float(item["latitude"]),
"lng": float(item["longitude"]),
}
@extract_data.register(DataPoint)
def _(item: DataPoint):
return {
"lat": item.latitude,
"lng": item.longitude,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment