drogon/analysis/index_batch.py

50 lines
2 KiB
Python
Raw Permalink Normal View History

2018-12-21 19:17:39 +01:00
from analysis.market_snapshot import Market, available_date_ranges
2018-12-23 18:30:11 +01:00
from db_layer.capturas_interface import capturas_interface
from db_layer.indices_interface import indices_interface
2018-12-21 19:17:39 +01:00
class IndexMM:
def __init__(self):
self.name = 'indexmm'
2018-12-23 18:30:11 +01:00
self.market = None
2018-12-21 19:17:39 +01:00
self.date = None
self.data = None
2018-12-23 18:30:11 +01:00
self.value = None
2018-12-21 19:17:39 +01:00
def calculate(self, market):
self.market = market
self.date = self.market.end_date
2018-12-23 18:30:11 +01:00
self.data = self.market.get_market_data()
2018-12-21 19:17:39 +01:00
2018-12-23 18:30:11 +01:00
data_coche_pequeno = {'count': self.data[self.data['tamano_categorico'] == 'coche pequeño'].shape[0],
'mean': self.data[self.data['tamano_categorico'] == 'coche pequeño']['precio'].mean()}
data_coche_grande = {'count': self.data[self.data['tamano_categorico'] == 'coche grande'].shape[0],
'mean': self.data[self.data['tamano_categorico'] == 'coche grande']['precio'].mean()}
data_coche_moto = {'count': self.data[self.data['tamano_categorico'] == 'coche y moto'].shape[0],
'mean': self.data[self.data['tamano_categorico'] == 'coche y moto']['precio'].mean()}
2018-12-21 19:17:39 +01:00
self.value = (((data_coche_grande['count'] * data_coche_grande['mean']) + (data_coche_moto['count'] * data_coche_moto['mean'])
+ (data_coche_pequeno['count'] * data_coche_pequeno['mean']))
/ (data_coche_grande['count'] + data_coche_moto['count'] + data_coche_pequeno['count']))
2018-12-23 18:30:11 +01:00
def get_data(self):
return {'name': self.name,
'date': self.date,
'value': self.value.item()}
2018-12-21 19:17:39 +01:00
2018-12-23 18:30:11 +01:00
if __name__ == '__main__':
for date_range in available_date_ranges:
market = Market()
market.load_market(capturas_interface.get_market_snapshot(date_range['start'], date_range['end']),
date_range=date_range)
market.clean_market('index')
2018-12-21 19:17:39 +01:00
2018-12-23 18:30:11 +01:00
index = IndexMM()
index.calculate(market)
2018-12-21 19:17:39 +01:00
2018-12-23 18:30:11 +01:00
indices_interface.write_index(index.get_data())
2018-12-21 19:17:39 +01:00