TypeError:必须使用csv_util实例作为第一个参数调用unbound方法download_csv_file_web()(改为使用str实例)

时间:2016-12-20 12:02:01

标签: python pandas yahoo-finance

我正在使用Python从Yahoo下载股票数据。下载代码放在 csv_util.py 中。
调用脚本是 mapper.py 。导入csv_util脚本后,出现以下错误:

Traceback (most recent call last):
('Lines:', [])
  File "C:/Users/laurel.ts/Desktop/predictstock/mapper.py", line 56, in <module>
    download_file_ticker(ticker,ref_ticker)
  File "C:/Users/laurel.ts/Desktop/predictstock/mapper.py", line 53, in download_file_ticker
    csv_util.download_csv_file_web(ticker,ref_ticker)
TypeError: unbound method download_csv_file_web() must be called with csv_util instance as first argument (got str instance instead)

以下是代码:

mapper.py

###### Mapper.py ######
import time
import sys

# New imports
import stock_predict_main_app
import predict_stock.csv_util
from predict_stock.csv_util import csv_util


predict_date = '03112016'

ticker = 'ARVIND.NS'
ref_ticker = 'MSFT'

input_default_values = {'numdays2predict': 2,
                        'simulations': 10,
                        'historicaldatalen': 0,
                        'tickersymbol': 'ARVIND.NS',
                        # 'tickersymbol': 'MSFT',
                        'stockdate2predict': predict_date,
                        'downloadstock': 1,
                        'plotshow': 0,
                        'industrytype': 'itindustry'}



# Pass ticker symbol and date
def pass_ticker_date(predict_date, input_default_values):
    with open('tickerList.txt') as f:
        lines = f.read().splitlines()

    print(type(input_default_values))

    tickersymbol = input_default_values["tickersymbol"]

    print("Lines:", lines)
    for tickersymbol in lines:
        print("tickersymbol:", tickersymbol)
        stock_predict_main_app.test_predict(tickersymbol)


# Download the file based on the ticker symbol
def download_file_ticker(ticker,ref_ticker):
    # ticker= input_default_values["tickersymbol"]
    # ref_ticker = input_default_values["tickersymbol"]

    csv_util.download_csv_file_web(ticker,ref_ticker)

pass_ticker_date(predict_date, input_default_values)
download_file_ticker(ticker,ref_ticker)


csv_util.py

import logging
import csv
import urllib
import datetime
import numpy as np
import pandas as pd
import sys
import os
from collections import defaultdict

###custom local modules###
from datetime_util import datetime_util
from loggingpy import loggingpy

global stock_input_file,stock_input_ref_file
global loggingpy_obj,datetime_util_obj
global actual_stock_price, output_stock_price

class csv_util:

    def __init__(self,actual_stock_price,output_stock_price,download_flag):
        print("Class anme: __init__",self.__class__.__name__)
        self.stock_input_file=""
        self.stock_input_ref_file = ""
        self.actual_stock_price = actual_stock_price
        self.output_stock_price = output_stock_price
        self.download_flag=download_flag
        self.datetime_util_obj = datetime_util()
        self.loggingpy_obj = loggingpy()
        #datetime_util_obj = self.datetime_util_obj
        #loggingpy_obj=self.loggingpy_obj


    '''
     METHOD: prepare_actual_data2writecsv
    '''
    def prepare_actual_data2writecsv(self, predict_date_wise_data_wd, predict_datewise_data_wod):
        logging.info("<<prepare_actual_data2writecsv")
        temp_date_ary = []
        temp_date_ary = self.actual_stock_price['date']
        temp_closeprice_ary = self.actual_stock_price['closeprice']
        temp_dailyreturn_ary = self.actual_stock_price['dailyreturn']

        # predicted_date_array = sorted(temp_predicted_values_wd.keys(),reverse=True)
        # remove last element of array or appenda dummy 0 to daily returns
        temp_date_ary.pop()
        temp_closeprice_ary.pop()
        # temp_dailyreturn_ary.append(0)

        self.loggingpy_obj.log_func({'temp_date_ary': temp_date_ary, 'temp_closeprice_ary': temp_closeprice_ary,
                                     'temp_dailyreturn_ary': temp_dailyreturn_ary})

        np_column_ary = np.column_stack((temp_date_ary, temp_closeprice_ary, temp_dailyreturn_ary))
        num_rows, num_columns = np_column_ary.shape

        logging.info("np_column_ary:%s,Rowsdata %s,ColumnData %s", np_column_ary.size, np_column_ary[:][0],
                     np_column_ary[:, 0])
        logging.info("NumRows:%d,Num Columns:%s", num_rows, num_columns)

        counter = 0
        for i in range(0, num_rows):
            counter += 1
            temp_temp_row_data = []
            temp_row_data = np_column_ary[:][i]
            temp_temp_row_data = list(temp_row_data)
            temp_rowdate = temp_row_data[0]
            logging.debug("[%d],Length:[%d],type:[%s],Date:%s,Rowsdata:%s", i, len(temp_row_data), type(temp_row_data),
                          temp_rowdate, temp_row_data)

            predict_date_wise_data_wd[temp_rowdate] = (map(str, temp_temp_row_data))
            predict_datewise_data_wod[temp_rowdate] = (map(str, temp_temp_row_data))
        logging.info(">>prepare_actual_data2writecsv")

    '''
     METHOD: prepare_data2writecsv
    '''
    def prep_predicted_data2writecsv(self, predict_date_wise_data_wd, predict_datewise_data_wod):
        logging.info("<<prep_predicted_data2writecsv")
        temp_predicted_values_wd = self.actual_stock_price['predicted_vals_with_drift']
        temp_predicted_values_wod = self.actual_stock_price['predicted_vals_without_drift']

        self.actual_stock_price['meanwithdrift'] = []
        self.actual_stock_price['meanwithoutdrift'] = []

        temp_var = temp_predicted_values_wd.keys()
        predicted_date_array = self.datetime_util_obj.sort_datettime_list(temp_var, False)

        for eack_key in predicted_date_array:
            logging.debug("WD:eack key:%s", eack_key)
            temp_string_val_wd = []
            temp_string_val_wod = []

            temp_string_val_wd = temp_predicted_values_wd[eack_key]
            temp_string_val_wod = temp_predicted_values_wod[eack_key]

            mean_wd = np.mean(temp_string_val_wd)
            mean_wod = np.mean(temp_string_val_wod)

            # Store mean in global variable
            self.actual_stock_price['meanwithdrift'].append(mean_wd)
            self.actual_stock_price['meanwithoutdrift'].append(mean_wod)

            logging.debug("meanwithdrift:%s,meanwithoutdrift:%s", mean_wd, mean_wod)
            logging.debug("temp_string_val_wd:len:%d,type:%s", len(temp_string_val_wd), type(temp_string_val_wd))
            logging.debug("temp_string_val_wd:len:%d,type:%s", len(temp_string_val_wod), type(temp_string_val_wod))

            temp_string_wd = []
            temp_string_wod = []
            if not predict_datewise_data_wod.has_key(eack_key):
                predict_datewise_data_wod[eack_key] = []
                predict_date_wise_data_wd[eack_key] = []
                temp_string_wd = [eack_key, "", ""]
                temp_string_wod = [eack_key, "", ""]

            temp_string_wd.append(mean_wd)
            temp_string_wod.append(mean_wod)

            temp_string_wd.extend(temp_string_val_wd)

            temp_string_wod.extend(temp_string_val_wod)

            logging.debug("temp_string_wd:len:%d,type:%s,Data:%s", len(temp_string_wd), type(temp_string_wd),
                          temp_string_wd)
            logging.debug("temp_string_wod:len:%d,type:%s,Data:%s", len(temp_string_wod), type(temp_string_wod),
                          temp_string_wod)

            predict_date_wise_data_wd[eack_key].extend(temp_string_wd)
            predict_datewise_data_wod[eack_key].extend(temp_string_wod)

            #self.loggingpy_obj.log_func({"temp_string_wd": temp_string_wd, "temp_string_wod": temp_string_wod})
        logging.info(">>prepare_data2writecsv")

    '''
     METHOD: write2csvfile
     Writes given data to the given csv absolute path filename
     Input arguments: filename to be written, data to be written
    '''
    def write2csvfile(self,file_name,local_rows_data):
        logging.info("<<:write2csvfile")
        #output_data_path=self.loggingpy_obj.output_data_path
        #os.chdir(output_data_path)
        with open(file_name, 'w') as csv_fw:
            out_csv_writer = csv.writer(csv_fw, lineterminator='\n')
            out_csv_writer.writerows(local_rows_data)
        logging.info("csv file[%s]writing :Sucess",file_name)
        logging.info(">>:write2csvfile")

    '''
    # Prepare header list of columns to write to csv file
    # Write predicted values to csv file
    predicted_data_wod_date_val_ary
    '''
    #@classmethod
    #def write2csv_file(ticker_symbol):
    def write2csv_file(self,ticker_symbol):
        logging.info("<<:write2csv_file")
        datetime_stamp=datetime.datetime.now().strftime("%Y%m%d_%H%M")
        file_name="output_prediction_with_drift"+ticker_symbol+"_"+datetime_stamp+".csv"
        file_name_wod = "output_prediction_without_drift" + ticker_symbol + "_" + datetime_stamp + ".csv"

        file_name=self.loggingpy_obj.output_data_path + file_name
        file_name_wod = self.loggingpy_obj.output_data_path + file_name_wod

        column_headers=self.output_stock_price['column_headers']

        #Prepare header list of columns to write to csv file;column_headers is a global variable
        column_headers.insert(0,'Date')
        column_headers.insert(1, 'Actual Prices')
        column_headers.insert(2, 'Daily Return')
        column_headers.insert(3, 'Mean')

        logging.info("column_headers,len:%s,type:%s,data:%s", len(column_headers), type(column_headers), column_headers)
        logging.info("self:column_headers,len:%s", len(self.output_stock_price['column_headers']))

        predict_date_wise_data_wd = {}
        predict_datewise_data_wod = {}
        self.prepare_actual_data2writecsv(predict_date_wise_data_wd, predict_datewise_data_wod)
        self.loggingpy_obj.log_func(
            {"Before:predict data_wd": predict_date_wise_data_wd, "predict data_wod": predict_datewise_data_wod})
        self.prep_predicted_data2writecsv(predict_date_wise_data_wd,predict_datewise_data_wod)
        self.loggingpy_obj.log_func({"After:pred data_wd": predict_date_wise_data_wd, "pred data_wod": predict_datewise_data_wod})

        temp_new_data_ary=predict_date_wise_data_wd.keys()
        sorted_temp_new_data_ary = self.datetime_util_obj.sort_datettime_list(temp_new_data_ary,True)

        self.loggingpy_obj.log_func({"sorted_temp_new_data_ary":sorted_temp_new_data_ary})

        data2write2csv_wd = [column_headers]
        data2write2csv_wod = [column_headers]

        counter=1
        # add headers
        for each_key in sorted_temp_new_data_ary:
            counter+=1
            data2write2csv_wd.insert(counter, predict_date_wise_data_wd[each_key])
            data2write2csv_wod.insert(counter,predict_datewise_data_wod[each_key])

        self.write2csvfile(file_name, data2write2csv_wd)
        self.write2csvfile(file_name_wod, data2write2csv_wod)

        logging.debug("data2write2csv_wd:%s", repr(data2write2csv_wd))
        logging.info("<<:write2csv_file")

        #sys.exit()
    # ######################### END OF METHOD write2csv_file  ################################

    '''
     METHOD: read_csv_file
    '''

    #@classmethod
    def read_csv_file(self,file_name,ref_data_flag):
        logging.debug("<<:read_csv_file")
        logging.info("file_name,%s,",file_name)
        if not os.path.exists(file_name):
            logging.critical("File not found:Check!"+file_name)
            sys.exit(2)

        #actual_stock_price=self.actual_stock_price
        logging.info("file_name,%s,", file_name)
        data = pd.read_csv(file_name,parse_dates=False, infer_datetime_format=False,date_parser=None)

        logging.info("self:::::::::::::%s",repr(self.datetime_util_obj));

        logging.info("data columns,len:%d,type:%s,data:%s,",len(data.columns),type(data.columns),data.columns)
        logging.info(",data.columns:%s",data.columns.values)

        datetime_list_dmy = self.datetime_util_obj.convert2datettime_list(data['Date'].tolist())

        #actual_stock_price=defaultdict();
        actual_stock_price = self.actual_stock_price

        if ref_data_flag == 1:
            actual_stock_price['ref_data']={}
            actual_stock_price['ref_data']['date'] = datetime_list_dmy
            actual_stock_price['ref_data']['closeprice'] = data['Close'].tolist()
        else:
            actual_stock_price['date'] = datetime_list_dmy
            actual_stock_price['closeprice'] = data['Close'].tolist()
            self.loggingpy_obj.log_func({"datetime_list_dmy": datetime_list_dmy})

        #logging.debug("repr self asp:%s",repr(self.actual_stock_price))

        del data
        logging.debug(">>:read_csv_file")


    '''
     METHOD: download_csv_file_web
     Download stock data from web yahoofinance
    '''
    def download_csv_file_web(self,ticker_symbol,ref_ticker_symbol):
        logging.debug("<<:download_csv_file_web")

        input_data_path=self.loggingpy_obj.input_data_path

        logging.info("input_data_path:%s:",input_data_path)


        yahoo_url="http://real-chart.finance.yahoo.com/table.csv?s="
        base_url=yahoo_url + ticker_symbol
        base_url_ref = yahoo_url + ref_ticker_symbol

        #datetime_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M")
        datetime_stamp = datetime.datetime.now().strftime("%Y%m%d")
        global stock_input_file,stock_input_ref_file

        #print("File found1:", stock_input_file)

        if self.download_flag == 1:
            file_name = "stock_input_" + ticker_symbol + "_" + datetime_stamp + ".csv"
            ref_file_name = "stock_ref_input_" + ref_ticker_symbol + "_" + datetime_stamp + ".csv"

            stock_input_file = input_data_path + "\\" + file_name
            stock_input_ref_file = input_data_path + "\\" + ref_file_name

            self.download_file(stock_input_file,base_url)
            self.download_file(stock_input_ref_file, base_url_ref)
        else:
            file_name = "stock_input_" + ticker_symbol + ".csv"
            ref_file_name = "stock_ref_input_" + ref_ticker_symbol + ".csv"

            stock_input_file = input_data_path + "\\" + file_name
            stock_input_ref_file = input_data_path + "\\" + ref_file_name

            if os.path.isfile(stock_input_file) and os.path.isfile(stock_input_ref_file):
                logging.info("File found 3 :[%s],[%s]",stock_input_file,stock_input_ref_file)
                print("File found3:",stock_input_file,stock_input_ref_file)
            else:
                print("File not found4:", stock_input_file,stock_input_ref_file)
                logging.critical("File not found4![%s] or [%s]",stock_input_file,stock_input_ref_file)
                sys.exit(2)
        print("File found5:", stock_input_file,stock_input_ref_file)
        logging.info("stock_input_file 5 :%s,base_url:%s,ref file name:[%s]", stock_input_file, base_url,stock_input_ref_file)

        self.stock_input_file=stock_input_file
        self.stock_input_ref_file=stock_input_ref_file

        #sys.exit(2)
        logging.debug(">>:download_csv_file_web")


    '''
     METHOD: download_file
     Downlaod stock data from web yahoofinance
    '''
    def download_file(self,file_name,base_url):
        logging.debug("<<:download_file")
        try:
            logging.info("Try Reading [:%s]",base_url)
            status = urllib.urlretrieve(base_url, file_name)
            logging.info("Status:%s", status)
            urllib.urlcleanup()
            if os.path.exists(file_name):
                logging.info ("File exists, File download success!File"+file_name)
            else:
                logging.critical("File downloaded DOE NOT EXIST, exit.."+file_name)
                sys.exit(2)
        except urllib.ContentTooShortError as ctse:
            print("File download: Failed, found some error")
            logging.critical("File donwlaod failed from url:%s",base_url)
            sys.exit(2)
            #logfile_handle.write(ctse.content)
        logging.debug(">>:download_file")


    def download_read_csv_file(self,ticker_symbol, industry_type_ts):
        logging.debug("<<:download_read_csv_file")
        ref_data_flag=0
        self.download_csv_file_web(ticker_symbol, industry_type_ts)

        stock_input_file = self.stock_input_file
        stock_input_ref_file = self.stock_input_ref_file

        # download_csv_file_web("GLD")
        ref_data_flag = 0
        self.read_csv_file(stock_input_file, 0)
        self.read_csv_file(stock_input_ref_file, 1)

        #self.loggingpy_obj.log_func({"csv_util:actual_stock_price":self.actual_stock_price})

        logging.debug(">>:download_read_csv_file")



loggingpy.py

import logging
import datetime
import os
import sys

global current_dir_path, input_data_path, output_data_path

class loggingpy:

    def __init__(self):
        loglevel=logging.INFO
        self.loglevel=loglevel
        self.log_config()
        print("loggingpy - log __init__")


    def log_config(self):
        global current_dir_path, input_data_path, output_data_path
        current_dir_path = os.getcwd()
        input_data_path = current_dir_path + "\..\stock_data"
        output_data_path = current_dir_path + "\..\stock_data"

        if not os.path.exists(output_data_path):
            os.mkdir(output_data_path)

        print("current_dir_path:" + current_dir_path + ",input_data_path:" + input_data_path,
              ",output_data_path:" + output_data_path)

        main_date_time_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M")
        logfile = output_data_path + "\log_" + main_date_time_stamp + ".log"
        #logging.basicConfig(stream=sys.stdout,level=logging.DEBUG, format='%(levelname)s:%(message)s')
        logging.basicConfig(stream=sys.stdout, level=logging.INFO)
        #logging.basicConfig(stream=sys.stdout, level=self.loglevel)
        #logging.basicConfig(filename=logfile, level=logging.INFO, format='%(levelname)s:%(message)s')
        #console=logging.StreamHandler().setLevel(logging.DEBUG)
        #logging.getLogger('abcd').addHandler(console)

        self.input_data_path = input_data_path
        self.output_data_path = output_data_path
        self.current_dir_path = current_dir_path

        logging.info("Logging test %s","HELLO TEST")
        logging.info("current_dir_path:%s,input_data_path:%s,output_data_path:%s", current_dir_path,input_data_path,output_data_path)


    def log_fn(self,temp_list):
        for i in range(0, len(temp_list)):
            log_level_set=logging.getLogger().getEffectiveLevel()
            #print("log_level_set",log_level_set,logging.INFO,logging.DEBUG)
            if log_level_set==logging.INFO:
                logging.info(":len:%d,Type:%s", len(temp_list[i]), type(temp_list[i]))
            if log_level_set == logging.DEBUG:
                logging.debug("len:%d,Type:%sData:%s", len(temp_list[i]), type(temp_list[i]),temp_list[i])

    def log_info(self,msg):
        logging.info(msg)

    def log_func(self,templog):
        log_level_set = logging.getLogger().getEffectiveLevel()
        #log_string_info = "Name:%s,len:%d,type:%s"
        #log_string_debug = "Name:%s,len:%d,type:%s,Data:%s"
        log_string_info = "Name:{0},len:{1},type:{2}"
        log_string_debug = "Name:{0},len:{1},type:{2},Data:%{3}"
        for var_name,var_value in templog.iteritems():
            if log_level_set == logging.INFO:
                #logging.info(""+log_string_info,var_name, len(var_value),type(var_value))
                logging.info(log_string_info.format(var_name, len(var_value), type(var_value)))
            if log_level_set == logging.DEBUG:
                #logging.debug(""+log_string_debug,var_name, len(var_value), type(var_name),var_value)
                logging.debug(log_string_debug.format(var_name, len(var_value), type(var_name), var_value))

错误的原因是什么?

0 个答案:

没有答案