2017-12-26 14 views
0

私は、テキストファイルをアップロードしてからユーザー入力を提供し、さらにユーザー入力処理に従ってシナリオを作成しました。ユーザー入力の処理データ

サンプルファイル:

DOWN 07.09.2016 08:21:33 - 07.09.2016 08:23:33 
UP 07.11.2016 09:41:07 - 09.11.2016 09:20:33 
DOWN 09.11.2016 08:26:33 - 09.11.2016 08:46:33 
UP 09.11.2016 08:23:33 - 09.11.2016 08:25:33 
DOWN 09.11.2016 08:36:33 - 09.11.2016 08:41:33 
DOWN 10.11.2016 08:36:33 - 10.11.2016 08:39:33 

コード:コードの上

try: 
    import Tkinter as Tk 
    import tkFileDialog as fileDialog 
except ImportError: 
    import tkinter as Tk 
    import tkinter.filedialog as fileDialog 

import datetime 



def read_data(): 
    ''' 
    Read data from file and convert to list with datetime 
    which can be used to calculate time and display. 
    ''' 
    global data 

    filename = fileDialog.askopenfilename() 

    if filename: 
     # read all lines 
     with open(filename) as fileHandle: 
      lines = fileHandle.readlines() 

     # convert to `datetime` (not `timestamp`) 
     data = []   
     for line in lines: 
      #direction = line[:4].strip() 
      #dt1 = line[5:24] 
      #dt2 = line[27:46] 

      direction, d1, t1, _, d2, t2 = line.split() 
      dt1 = d1 + ' ' + t1 
      dt2 = d2 + ' ' + t2 

      t1 = datetime.datetime.strptime(dt1, "%d.%m.%Y %H:%M:%S") 
      t2 = datetime.datetime.strptime(dt2, "%d.%m.%Y %H:%M:%S") 

      seconds = (t2-t1).seconds 

      data.append([direction, t1, t2, seconds]) 

     print(data) 


def processText(lines, selected_date): 

    total = 0 
    start = None 

    print(selected_date) 
    # if there is `selected_date` then convert to `datetime` 
    if selected_date: 
     try: 
      selected_date = datetime.datetime.strptime(selected_date, "%d.%m.%Y") 
     except AttributeError as ex: 
      print("ERROR:", ex) 
      selected_date = None 

    # calculate time 
    for direction, t1, t2, seconds in lines: 

     if direction == "DOWN": 

      # if `selected_date` then filter times 
      if selected_date and t1 <= selected_date: 
       continue 

      if not start: 
       start = t1.strftime("%d.%m.%Y %H:%M:%S") 

      total += seconds 

    # convert to minutes after summing all second 
    total = total//60 

    return total, start 

def calculate(): 

    all_dates = entry.get().split(',') 
    print(all_dates) 
    all_dates = [date.strip() for date in all_dates] 

    txt = '' 

    for current_date in all_dates: 
     down, start = processText(data, current_date) 
     txt += "Total Downtime is {0} min from {1}\n".format(down, start) 

    textVar.set(txt) 

# --- main --- 

data = None # to keep data from file 

# - 

root = Tk.Tk() 

button = Tk.Button(root, text="Open", command=read_data) 
button.grid(column=1, row=1) 

textVar = Tk.StringVar(root) 

label = Tk.Label(root, textvariable=textVar) 
label.grid(column=1, row=2) 

entry = Tk.Entry(root) 
entry.grid(column=1, row=3) 

button2 = Tk.Button(root, text="Calculate", command=calculate) 
button2.grid(column=1, row=4) 

root.mainloop() 

は形式Date1.Month1.Year1、Date2.Month2.Year2で日付を選択するために私を促し...(に応じて、 。日付の数字入力)

として出力を返す:

Total Downtime is x min from date1.month1.year1 xx:xx:xx(time1) 
Total Downtime is y min from date2.month2.year2 yy:yy:yy(time2) 

ここでは、ダウンタイムの詳細を分単位で示しています。これを日付までパーセンテージで変換したいと思います。例えば ​​- >

ユーザ入力:

1.9.2016,1.11.2016,1.1.2016

出力:

Total Downtime is 30 min from 1.9.2016 08:21:33 & Availability percentage from selected date to till date : xx.xxx% 
Total Downtime is 28 min from 1.11.2016 08:26:33 & Availability percentage from selected date to till date : yy.yyy% 
Total Downtime is 30 min from 1.11.2016 08:26:33 & Availability percentage from selected date to till date : zz.zzz% 

可用性計算の背後にあるロジックは

total number of min down from date(which is retrieved)/total number of min till date * 100 
あろう

私はこの部分で立ち往生していますが、これは達成可能ですか?どんな助けも素晴らしいだろう!

+0

はい、それは達成可能 - あなたが最初にすべての分を追加する必要があります。分を追加するのに問題がありますか? BTW: 'selected_date'の代わりに' processText() 'を' None'で実行すると、「すべての分の総数」が得られます。そして、「選択した日付から合計の分」を引いて、「選択した日付までの合計の分」を取得することができます。 – furas

+0

[最小、完全、および検証可能な例](https://stackoverflow.com/help/mcve)あなたのコード全体。 – Nae

+0

はい、選択した日付までの総時間(分)を取得するにはどうすればよいか分かりません。 – vanishka

答えて

1

あなたは日付の代わりにNoneprocessText()を実行した場合、それがダウンし

total_down, start = processText(data, None) 

だったとあなたはパーセンテージを計算するために使用することができたとき、あなたは数分の合計数を取得します。

percentage = (down/total_down) * 100 

、それがその後、ダウンまたはアップしたとき、あなたは数分の合計数をしたい場合は、ドット

def calculate(): 

    all_dates = entry.get().split(',') 
    print(all_dates) 
    all_dates = [date.strip() for date in all_dates] 

    # calculate total number of minutes when it was down 
    total_down, start = processText(data, None) # <-- None 

    print('total_down:', total_down) 

    txt = '' 

    for current_date in all_dates: 
     down, start = processText(data, current_date) 

     # calculate percetage 
     percentage = (down/total_down) * 100 

     # use string formatting {:.2f} to display only two digits after dot 
     txt += "Total Downtime is {} min from {} ({:.2f}%)\n".format(down, start, percentage) 

    textVar.set(txt) 

enter image description here


後に2桁のみを表示するように{:.2f}の書式文字列を使用することができますprocessTextを変更し、新しいパラメータ(すなわち、word)を追加して、directionDOWNまたはUPまたは両方(word = None

def processText(lines, selected_date, word="DOWN"): 

    total = 0 
    start = None 

    print(selected_date) 
    # if there is `selected_date` then convert to `datetime` 
    if selected_date: 
     try: 
      selected_date = datetime.datetime.strptime(selected_date, "%d.%m.%Y") 
     except AttributeError as ex: 
      print("ERROR:", ex) 
      selected_date = None 

    # calculate time 
    for direction, t1, t2, seconds in lines: 

     if not word or word == direction: 

      # if `selected_date` then filter times 
      if selected_date and t1 <= selected_date: 
       continue 

      if not start: 
       start = t1.strftime("%d.%m.%Y %H:%M:%S") 

      total += seconds 

    # convert to minutes after summing all second 
    total = total//60 

    return total, start 

def calculate(): 

    all_dates = entry.get().split(',') 
    print(all_dates) 
    all_dates = [date.strip() for date in all_dates] 

    # calculate total number of minutes when it was down and up 
    total_down, start = processText(data, None, None) 

    print('total_down:', total_down) 

    txt = '' 

    for current_date in all_dates: 
     down, start = processText(data, current_date, "DOWN") 
     percentage = (down/total_down) * 100 
     txt += "Total Downtime is {} min from {} ({:.2f}%)\n".format(down, start, percentage) 

    textVar.set(txt) 
+0

これはO/pを提供しません。エラーはありません – vanishka

+0

コードを参照してください - 'total_down'を計算し、後でこの値を使って' percentage'を計算し、 '{:2f} 'を使用してドットの後に2桁のパーセンテージを表示します。 – furas

+0

ここで私は日付を選択しました> 12.9.2016 これはダウンタイムを2.07%としますが、計算ロジックによれば、接続した99.07% – vanishka

関連する問題