2016-01-29 151 views
1

我在大熊貓系列上做的比較沉重。有沒有什麼辦法可以找回一些打印反饋信息,說明在每次調用函數時,在函數內部打印多少內容?大理大熊貓適用

+1

我不這麼認爲,不。 – BrenBarn

+0

這是不幸的,謝謝 –

回答

3

你可以用跟蹤包裝你的功能。下面有兩個例子,一個基於完成的迭代次數,另一個基於總工作的百分比。

from pandas import Series 

def progress_coroutine(print_on = 10000): 
    print "Starting progress monitor" 

    iterations = 0 
    while True: 
     yield 
     iterations += 1 
     if (iterations % print_on == 0): 
      print "{} iterations done".format(iterations) 

def percentage_coroutine(to_process, print_on_percent = 0.10): 
    print "Starting progress percentage monitor" 

    processed = 0 
    count = 0 
    print_count = to_process*print_on_percent 
    while True: 
     yield 
     processed += 1 
     count += 1 
     if (count >= print_count): 
      count = 0 
      pct = (float(processed)/float(to_process))*100 

      print "{}% finished".format(pct) 

def trace_progress(func, progress = None): 
    def callf(*args, **kwargs): 
     if (progress is not None): 
      progress.send(None) 

     return func(*args, **kwargs) 

    return callf 

def my_func(i): 
    return i * 2 

data_series = Series(xrange(100000)) 
co1 = progress_coroutine() 
co1.next() 
co2 = percentage_coroutine(len(data_series)) 
co2.next() 
data_series.apply(trace_progress(my_func, progress = co1)) 
data_series.apply(trace_progress(my_func, progress = co2)) 

Starting progress monitor 
Starting progress percentage monitor 
10000 iterations done 
20000 iterations done 
30000 iterations done 
40000 iterations done 
50000 iterations done 
60000 iterations done 
70000 iterations done 
80000 iterations done 
90000 iterations done 
100000 iterations done 
10.0% finished 
20.0% finished 
30.0% finished 
40.0% finished 
50.0% finished 
60.0% finished 
70.0% finished 
80.0% finished 
90.0% finished 
100.0% finished 
+0

完美,謝謝:) –

+0

不客氣。很高興我能幫上忙。 – Clockw3rk

0

我對上述Python 3的代碼進行了更改,以防萬一任何人想使用。謝謝@ Clockw3rk爲上述答案

from pandas import Series 

def progress_coroutine(print_on = 10): 
    print ("Starting progress monitor") 

    iterations = 0 
    while True: 
     yield 
     iterations += 1 
     if (iterations % print_on == 0): 
      print ("{} iterations done".format(iterations)) 

def percentage_coroutine(to_process, print_on_percent = 0.10): 
    print ("Starting progress percentage monitor") 

    processed = 0 
    count = 0 
    print_count = to_process*print_on_percent 
    while True: 
     yield 
     processed += 1 
     count += 1 
     if (count >= print_count): 
      count = 0 
      pct = (float(processed)/float(to_process))*100 

      print ("{}% finished".format(pct)) 

def trace_progress(func, progress = None): 
    def callf(*args, **kwargs): 
     if (progress is not None): 
      progress.send(None) 

     return func(*args, **kwargs) 

    return callf 

def my_func(i): 
    return i ** 2 

data_series = Series(list(range(100))) 
co1 = progress_coroutine() 
co2 = percentage_coroutine(len(data_series)) 
data_series.apply(trace_progress(my_func, progress = co1)) 
data_series.apply(trace_progress(my_func, progress = co2)) 

Starting progress monitor 
10 iterations done 
20 iterations done 
30 iterations done 
40 iterations done 
50 iterations done 
60 iterations done 
70 iterations done 
80 iterations done 
90 iterations done 
Starting progress percentage monitor 
10.0% finished 
20.0% finished 
30.0% finished 
40.0% finished 
50.0% finished 
60.0% finished 
70.0% finished 
80.0% finished 
90.0% finished 
Out[12]: 
0  0 
1  1 
2  4 
3  9 
4  16 
5  25 
6  36 
7  49 
8  64 
9  81 
10  100 
11  121 
12  144 
13  169 
14  196 
15  225 
16  256 
17  289 
18  324 
19  361 
20  400 
21  441 
22  484 
23  529 
24  576 
25  625 
26  676 
27  729 
28  784 
29  841 
     ... 
70 4900 
71 5041 
72 5184 
73 5329 
74 5476 
75 5625 
76 5776 
77 5929 
78 6084 
79 6241 
80 6400 
81 6561 
82 6724 
83 6889 
84 7056 
85 7225 
86 7396 
87 7569 
88 7744 
89 7921 
90 8100 
91 8281 
92 8464 
93 8649 
94 8836 
95 9025 
96 9216 
97 9409 
98 9604 
99 9801 
Length: 100, dtype: int64