<s> """<STR_LIT>""" <EOL> import atexit <EOL> import time <EOL> import sys <EOL> try : <EOL> import curses <EOL> except ImportError : <EOL> sys . exit ( '<STR_LIT>' ) <EOL> import psutil <EOL> def tear_down ( ) : <EOL> win . keypad ( <NUM_LIT:0> ) <EOL> curses . nocbreak ( ) <EOL> curses . echo ( ) <EOL> curses . endwin ( ) <EOL> win = curses . initscr ( ) <EOL> atexit . register ( tear_down ) <EOL> curses . endwin ( ) <EOL> lineno = <NUM_LIT:0> <EOL> def print_line ( line , highlight = False ) : <EOL> """<STR_LIT>""" <EOL> global lineno <EOL> try : <EOL> if highlight : <EOL> line += "<STR_LIT:U+0020>" * ( win . getmaxyx ( ) [ <NUM_LIT:1> ] - len ( line ) ) <EOL> win . addstr ( lineno , <NUM_LIT:0> , line , curses . A_REVERSE ) <EOL> else : <EOL> win . addstr ( lineno , <NUM_LIT:0> , line , <NUM_LIT:0> ) <EOL> except curses . error : <EOL> lineno = <NUM_LIT:0> <EOL> win . refresh ( ) <EOL> raise <EOL> else : <EOL> lineno += <NUM_LIT:1> <EOL> def bytes2human ( n ) : <EOL> """<STR_LIT>""" <EOL> symbols = ( '<STR_LIT>' , '<STR_LIT:M>' , '<STR_LIT>' , '<STR_LIT:T>' , '<STR_LIT:P>' , '<STR_LIT:E>' , '<STR_LIT>' , '<STR_LIT:Y>' ) <EOL> prefix = { } <EOL> for i , s in enumerate ( symbols ) : <EOL> prefix [ s ] = <NUM_LIT:1> << ( i + <NUM_LIT:1> ) * <NUM_LIT:10> <EOL> for s in reversed ( symbols ) : <EOL> if n >= prefix [ s ] : <EOL> value = float ( n ) / prefix [ s ] <EOL> return '<STR_LIT>' % ( value , s ) <EOL> return '<STR_LIT>' % ( n ) <EOL> def poll ( interval ) : <EOL> """<STR_LIT>""" <EOL> tot_before = psutil . net_io_counters ( ) <EOL> pnic_before = psutil . net_io_counters ( pernic = True ) <EOL> time . sleep ( interval ) <EOL> tot_after = psutil . net_io_counters ( ) <EOL> pnic_after = psutil . net_io_counters ( pernic = True ) <EOL> return ( tot_before , tot_after , pnic_before , pnic_after ) <EOL> def refresh_window ( tot_before , tot_after , pnic_before , pnic_after ) : <EOL> """<STR_LIT>""" <EOL> global lineno <EOL> print_line ( "<STR_LIT>" % ( <EOL> bytes2human ( tot_after . bytes_sent ) , <EOL> bytes2human ( tot_after . bytes_recv ) ) <EOL> ) <EOL> print_line ( "<STR_LIT>" % ( <EOL> tot_after . packets_sent , tot_after . packets_recv ) ) <EOL> print_line ( "<STR_LIT>" ) <EOL> nic_names = list ( pnic_after . keys ( ) ) <EOL> nic_names . sort ( key = lambda x : sum ( pnic_after [ x ] ) , reverse = True ) <EOL> for name in nic_names : <EOL> stats_before = pnic_before [ name ] <EOL> stats_after = pnic_after [ name ] <EOL> templ = "<STR_LIT>" <EOL> print_line ( templ % ( name , "<STR_LIT>" , "<STR_LIT>" ) , highlight = True ) <EOL> print_line ( templ % ( <EOL> "<STR_LIT>" , <EOL> bytes2human ( stats_after . bytes_sent ) , <EOL> bytes2human ( <EOL> stats_after . bytes_sent - stats_before . bytes_sent ) + '<STR_LIT>' , <EOL> ) ) <EOL> print_line ( templ % ( <EOL> "<STR_LIT>" , <EOL> bytes2human ( stats_after . bytes_recv ) , <EOL> bytes2human ( <EOL> stats_after . bytes_recv - stats_before . bytes_recv ) + '<STR_LIT>' , <EOL> ) ) <EOL> print_line ( templ % ( <EOL> "<STR_LIT>" , <EOL> stats_after . packets_sent , <EOL> stats_after . packets_sent - stats_before . packets_sent , <EOL> ) ) <EOL> print_line ( templ % ( <EOL> "<STR_LIT>" , <EOL> stats_after . packets_recv , <EOL> stats_after . packets_recv - stats_before . packets_recv , <EOL> ) ) <EOL> print_line ( "<STR_LIT>" ) <EOL> win . refresh ( ) <EOL> lineno = <NUM_LIT:0> <EOL> def main ( ) : <EOL> try : <EOL> interval = <NUM_LIT:0> <EOL> while True : <EOL> args = poll ( interval ) <EOL> refresh_window ( * args ) <EOL> interval = <NUM_LIT:1> <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
