Commit c38ae421 authored by Alexander Lercher's avatar Alexander Lercher

Added db entities

parent fe2835b5
from db.entities.location import Location
from db.entities.popular_location import PopularLocation
from db.entities.user_cluster import UserCluster
\ No newline at end of file
import json
from datetime import datetime
class Location:
def __init__(self, location_info=None):
super().__init__()
if location_info is not None:
self.latitude = float(location_info['latitude'])
self.longitude = float(location_info['longitude'])
self.timestamp = datetime.fromtimestamp(location_info['timestamp'])
self.timestamp_raw = location_info['timestamp']
self.user = location_info['user']
self.id = f'{self.user}-{self.timestamp_raw}'
def to_serializable_dict(self):
return {
"id": self.id,
"latitude": self.latitude,
"longitude": self.longitude,
"timestamp": self.timestamp_raw,
"user": self.user
}
def __repr__(self):
return json.dumps(self.to_serializable_dict())
def __str__(self):
return f"Location({self.__repr__()})"
import json
class PopularLocation:
def __init__(self, date, top_locations: list):
super().__init__()
self.date = date
self.top_locations = top_locations
def to_serializable_dict(self, for_db=False):
return {
"date": str(self.date),
"top-locations": json.dumps(self.top_locations) if for_db else self.top_locations
}
def __repr__(self):
return json.dumps(self.to_serializable_dict())
def __str__(self):
return f"PopularLocation({self.__repr__()})"
import json
class UserCluster:
def __init__(self, date, hour, clusters):
super().__init__()
self.date = date
self.hour = hour
self.clusters = clusters
self.id = f'{self.date}-{self.hour}'
def to_serializable_dict(self, for_db=False):
return {
"id": self.id,
"date": str(self.date),
"hour": self.hour,
"clusters": json.dumps(self.clusters) if for_db else self.clusters
}
def __repr__(self):
return json.dumps(self.to_serializable_dict())
def __str__(self):
return f"UserCluster({self.__repr__()})"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment