2016-03-26 87 views
-1

我對python相當陌生,需要一點指導。我試圖通過從控制檯一些變量並獲得和錯誤消息:從控制檯錯誤解析變量

AuctionStrategy_2.0.py: error: argument -s/--sectorStocks: invalid int value: 'tep3'

當我運行控制檯命令:

run AuctionStrategy_2.0.py -in10 -out5 -rolls15 -step3 -t.001 -s5 -m100 -v50 -e'01/01/2016' 

可能有人讓我怎麼解決這個問題嗎?此刻我的代碼除了嘗試從控制檯傳遞變量外什麼也不做。請參閱以下代碼:

import argparse 
import os 
import fnmatch 
import pandas as pd 
from pandas.tseries.offsets import BDay 
import lzma 
import numpy as np 
import math 
import datetime 

def main(): 
    print('Processing args....') 

    insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate = get_args() 
    print(insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate) 
    rawDataPath = 'C:/Users/simon/Documents/data/close_unadjusted/close_unadjusted/' 

def get_args(): 

    '''This function parses and return arguments passed in''' 

    insampleLength = 0 
    outsampleLength = 0 
    rolls = 0 
    step = 0 
    endDate ='' 
    minStocksPerSector = 0 
    threshold = 0 


    parser = argparse.ArgumentParser(
     description='Args to run VWAP Auction simulation') 

    ''' Command line arguments''' 

    parser.add_argument('-in', '--inSampleDataLength', type=int, help='Number of historic epochs insample', required=True) 
    parser.add_argument('-out', '--outSampleDataLength', type=int, help='Number of historic epochs outsample', required=True) 
    parser.add_argument('-rolls', '--numberRolls', type=int, help='Number of rolls', required=True) 
    parser.add_argument('-step', '--rollStep', type=int, help='Number of historic epochs', required=True) 

    parser.add_argument('-t','--threshold', type=float, help='starting value', required=True)  
    parser.add_argument('-s','--sectorStocks', type=int, help='minimum number', required=True) 
    parser.add_argument('-m','--marketCapCutOff', type=int,help='market capitalisation', required=True) 
    parser.add_argument('-v','--volumeCutOff', type=int, help='daily volume', required = True) 
    parser.add_argument('-e', '--endDate', type=str,help='last day of testing',required = True) 


    args = parser.parse_args() 

    ''' Assign args to variables''' 

    insampleLength = args.inSampleDataLength 
    outsampleLength = args.outSampleDataLength 
    rolls = args.numberRolls 
    step = args.rollStep 
    threshold = args.threshold 
    minStocksPerSector = args.sectorStocks 
    minMarketCap = args.marketCapCutOff 
    minVolume = args.volumeCutOff 
    endDate = datetime.datetime.strptime(args.endDate, "%d-%b-%Y") 


    return insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate 


if __name__ == "__main__": 
    print ("AuctionStategy_1.0...25/03/16") 

    try: 
     main()    
    except KeyboardInterrupt: 
     print ("Ctrl+C pressed. Stopping...") 

回答

1

單個破折號總是標識單字符參數。但是你試圖定義-step;這被解釋爲-s,稍後由實際的-s參數重新定義。

您應該爲「step」選擇不同的標識符,或者始終使用雙破折號版本--rollStep

0

自變量-s需要一個整數,你給了一個字符串,這會導致你得到的錯誤。

順便說一句,我認爲這是最好的參數的名稱之間加空格,它的值,例如:

run AuctionStrategy_2.0.py -in 10 -out 5 -rolls 15 -step 3 -t .001 -s 5 -m 100 -v 50 -e '01/01/2016' 

希望這有助於