-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_metadata.py
More file actions
40 lines (31 loc) · 1.28 KB
/
stock_metadata.py
File metadata and controls
40 lines (31 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
import pandas as pd
import numpy as np
import os
from config import DATA_ROOT_DIR
class MetadataLoader(object):
__metadata_cache = None
__ticker_dict_cache = None
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super(MetadataLoader, cls).__new__(cls, *args, **kwargs)
return cls._instance
def get_stock_metadata(self):
if not self.__metadata_cache:
self.create_metadata_cache()
return self.__metadata_cache
def create_metadata_cache(self):
company_metadata_file = os.path.join(DATA_ROOT_DIR, 'Metadata/MetaData_SP500_edited.csv')
self.__metadata_cache = pd.read_csv(company_metadata_file,
sep=';',
dtype={'id': np.int, 'Ticker': np.str,
'Company': np.str})
def create_ticker_dict(self):
metadata_df = self.get_stock_metadata()
dic = dict(zip(metadata_df.id, metadata_df.Ticker))
self.__ticker_dict_cache = dic
def get_id_to_ticker_dict(self):
if not self.__ticker_dict_cache:
self.create_ticker_dict()
return self.__ticker_dict_cache