Skip to content
Browse files

refactoring: many occurences of "date" and "time" wrt field names. We…

… just use "dt". Add global variable IN_CAPS for price/dt column names to avoid confusion.
  • Loading branch information
stnbu committed Sep 7, 2018
1 parent c84a906 commit 6cf2a47b5535eb0a13f0de5fbfcf3598433915eb
Showing with 29 additions and 24 deletions.
  1. +3 −2 coincharts/
  2. +10 −10 coincharts/
  3. +13 −9 coincharts/
  4. +3 −3 coincharts/
@@ -18,6 +18,7 @@
import daemon.pidfile

from coincharts import config, db
from coincharts.models import THE_DATETIME_FIELD, THE_PRICE_FIELD

# We're replacing the module with a dict. Importing the file shouldn't result in reading from disk, etc. That's why.
config = config.get_config()
@@ -114,7 +115,7 @@ def fetch(self, symbol):
data = response.json()
# validate the FIRST date from the data returned. Not perfect, but will prevent future heartache.
self.validate_datetime_object(data[0]['time_period_start']) # TODO. how to track that we want *this* time field
return data

def get_last_date_from_store(self, symbol):
@@ -123,7 +124,7 @@ def get_last_date_from_store(self, symbol):
except db.Prices.DoesNotExist:'No `time_period_end` value found for {}'.format(symbol))
return None
dt = getattr(obj, 'time')
dt = getattr(obj, 'dt')
return parse_dt(dt)

def insert(self, symbol, data):
@@ -25,9 +25,9 @@ def normalized_price_history(self):

def date_range(self):
start = self.history[0].time
end = self.history[len(self.history)-1].time
def dt_range(self):
start = self.history[0].dt
end = self.history[len(self.history)-1].dt
return start, end

@@ -59,20 +59,20 @@ def __init__(self, *args, **kwargs):

def start_date_indexes(self):
def start_dt_indexes(self):
indexes = {}
for symbol, data in self.items():
indexes[symbol] = [s.time for s in data.history].index(self.earliest_common_time)
indexes[symbol] = [s.dt for s in data.history].index(self.earliest_common_dt)
except ValueError:
raise ValueError('Could not find date {} in history of {}'.format(
self.earliest_common_time, symbol))
raise ValueError('Could not find datetime {} in history of {}'.format(
self.earliest_common_dt, symbol))
return indexes

def earliest_common_time(self):
return sorted([symbol.history[0].time for symbol in self.values()])[0]
def earliest_common_dt(self):
return sorted([symbol.history[0].dt for symbol in self.values()])[0]

def normalized_price_history_averages(self):
normalized_price_history_generators = []
@@ -102,7 +102,7 @@ def normalized_price_history_averages(self):
# print(name,
# info.min,
# info.max, '\t',
# info.date_range, sep='\t')
# info.dt_range, sep='\t')

comparison = SymbolComparison(symbol_info)

@@ -1,6 +1,10 @@

from django.db import models

# There are many datetime/price fields. We are interested almost exclusively in these two, and they are used in many places.
# Less headache if we use these ugly global names to avoid confusion
THE_DATETIME_FIELD = 'time_period_end'
THE_PRICE_FIELD = 'price_close'

class Prices(models.Model):

@@ -21,25 +25,25 @@ class Prices(models.Model):

def price(self):
return self.price_close
return getattr(self, THE_PRICE_FIELD)

def price(self, value):
self.price_close = value
setattr(self, THE_PRICE_FIELD, value)

def time(self):
return self.time_period_end
def dt(self):
return getattr(self, THE_DATETIME_FIELD)

def time(self, value):
self.time_period_start = value
def dt(self, value):
setattr(self, THE_DATETIME_FIELD, value)

def __str__(self):
class_name = self.__class__.__name__
return '{}@{}'.format(

class Meta:
@@ -18,13 +18,13 @@ def string_to_epoch(string):
def prices_gen(prices):
x = []
for p in prices:
x.append((p.time, int(p.price)))
x.append((p.dt, int(p.price)))
return x

title = '{} from {} to {}'.format(
prices[len(prices)-1].time, # "negative indexing is not supported"
prices[len(prices)-1].dt, # "negative indexing is not supported"

graph = svg_graph.LineGraph(

0 comments on commit 6cf2a47

Please sign in to comment.