from analysis.market_snapshot import Market, available_date_ranges from db_layer.capturas_interface import capturas_interface from db_layer.indices_interface import indices_interface class IndexMM: def __init__(self): self.name = 'indexmm' self.market = None self.date = None self.data = None self.value = None def calculate(self, market): self.market = market self.date = self.market.end_date self.data = self.market.get_market_data() 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()} 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'])) def get_data(self): return {'name': self.name, 'date': self.date, 'value': self.value.item()} 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') index = IndexMM() index.calculate(market) indices_interface.write_index(index.get_data())