def get_stored_historic_score(origin,destination,startTime,endTime,sinceDate,beforeDate,alpha): score = None scores = None calculatedScore = -1 today = datetime.now() today = datetime(2015,05,07,15,00) # If the route already exists, get from database, else save new instance. route = Route.get_or_none(origin=origin,destination=destination) if route is None: route = Route(origin=origin,destination=destination) route.save() # If both extremes of the date range are not none, then the scores of # the tweets from that time range are retrived. if sinceDate is not None and beforeDate is not None: scores = HistoricScore.get_scores_between_dates(route_id=route.id, lower_timestamp=sinceDate,higher_timestamp=beforeDate) else: if sinceDate is None: scores = HistoricScore.get_scores_until_date(route_id=route.id,timestamp=beforeDate) if beforeDate is None: scores = HistoricScore.get_scores_from_date(route_id=route.id,timestamp=sinceDate) #If previous scores don't exist, then a score is calculated and saved to the database. if scores is None or not scores: # Verifies if a score was already calculated for the route with today's date. score = HistoricScore.get_or_none(route_id=route.id,timestamp=today) if score is None: calculatedScore = get_score(related_tweets_time(origin, destination, startTime, endTime, sinceDate, beforeDate)) score = HistoricScore(route_id=route.id,timestamp=today,score=calculatedScore) score.save() else: calculatedScore = score.score else: calculatedScore = exponential_smoothing(scores,alpha) return calculatedScore