Permalink
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/daemonize.py
  2. +10 −10 coincharts/data.py
  3. +13 −9 coincharts/models.py
  4. +3 −3 coincharts/views.py
@@ -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):
response.headers['X-RateLimit-Remaining']))
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
self.validate_datetime_object(data[0][THE_DATETIME_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:
logging.info('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):
@property
@memoize
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
@property
@@ -59,20 +59,20 @@ def __init__(self, *args, **kwargs):
@property
@memoize
def start_date_indexes(self):
def start_dt_indexes(self):
indexes = {}
for symbol, data in self.items():
try:
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
@property
@memoize
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):
@property
def price(self):
return self.price_close
return getattr(self, THE_PRICE_FIELD)
@price.setter
def price(self, value):
self.price_close = value
setattr(self, THE_PRICE_FIELD, value)
@property
def time(self):
return self.time_period_end
def dt(self):
return getattr(self, THE_DATETIME_FIELD)
@time.setter
def time(self, value):
self.time_period_start = value
@dt.setter
def dt(self, value):
setattr(self, THE_DATETIME_FIELD, value)
def __str__(self):
class_name = self.__class__.__name__
return '{}@{}'.format(
self.time_period_end,
self.price_close
self.dt,
self.price
)
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(
symbol,
prices[0].time,
prices[len(prices)-1].time, # "negative indexing is not supported"
prices[0].dt,
prices[len(prices)-1].dt, # "negative indexing is not supported"
)
graph = svg_graph.LineGraph(

0 comments on commit 6cf2a47

Please sign in to comment.