<s> import random <EOL> import sys <EOL> from pyglet . gl import * <EOL> from pyglet import clock <EOL> from pyglet import font <EOL> from pyglet import graphics <EOL> from pyglet import window <EOL> BARS = <NUM_LIT:100> <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> BARS = int ( sys . argv [ <NUM_LIT:1> ] ) <EOL> MIN_BAR_LENGTH = <NUM_LIT:4> <EOL> MAX_BAR_LENGTH = <NUM_LIT:100> <EOL> BAR_SEGMENT_HEIGHT = <NUM_LIT:10> <EOL> UPDATE_PERIOD = <NUM_LIT> <EOL> win = window . Window ( vsync = False ) <EOL> batch = graphics . Batch ( ) <EOL> bars = [ ] <EOL> colors = [ <EOL> [ <NUM_LIT> , <NUM_LIT:0> , <NUM_LIT:0> ] , <EOL> [ <NUM_LIT:0> , <NUM_LIT:255> , <NUM_LIT:100> ] , <EOL> [ <NUM_LIT> , <NUM_LIT:100> , <NUM_LIT:255> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:200> , <NUM_LIT:255> , <NUM_LIT:100> ] , <EOL> [ <NUM_LIT:255> , <NUM_LIT> , <NUM_LIT:200> ] , <EOL> ] <EOL> def create_bars ( ) : <EOL> width = win . width / float ( BARS ) <EOL> for i in range ( BARS ) : <EOL> position = [ i * width , <NUM_LIT:0> , <EOL> i * width , <NUM_LIT:0> , <EOL> ( i + <NUM_LIT:1> ) * width , <NUM_LIT:0> , <EOL> ( i + <NUM_LIT:1> ) * width , <NUM_LIT:0> <EOL> ] <EOL> color = colors [ i % len ( colors ) ] * <NUM_LIT:4> <EOL> bar = batch . add ( <NUM_LIT:4> , GL_TRIANGLE_STRIP , None , <EOL> ( '<STR_LIT>' , position ) , <EOL> ( '<STR_LIT>' , color ) ) <EOL> bars . append ( bar ) <EOL> def update_bars ( ) : <EOL> for bar in bars : <EOL> old_length = bar . count <EOL> length = random . randint ( MIN_BAR_LENGTH , MAX_BAR_LENGTH ) <EOL> bar . resize ( length ) <EOL> vertices = bar . vertices <EOL> for i in range ( ( old_length - <NUM_LIT:1> ) * <NUM_LIT:2> , length * <NUM_LIT:2> ) : <EOL> if i & <NUM_LIT:1> : <EOL> vertices [ i ] = BAR_SEGMENT_HEIGHT * ( i // <NUM_LIT:4> ) <EOL> else : <EOL> vertices [ i ] = vertices [ i - <NUM_LIT:4> ] <EOL> vertices [ - <NUM_LIT:2> : ] = vertices [ - <NUM_LIT:4> : - <NUM_LIT:2> ] <EOL> if length > old_length : <EOL> bar . colors [ old_length * <NUM_LIT:3> : length * <NUM_LIT:3> ] = bar . colors [ : <NUM_LIT:3> ] * ( length - old_length ) <EOL> stats_text = font . Text ( font . load ( '<STR_LIT>' , <NUM_LIT:12> , bold = True ) , '<STR_LIT>' , <EOL> x = win . width , y = <NUM_LIT:0> , <EOL> halign = '<STR_LIT:right>' ) <EOL> def update_stats ( dt ) : <EOL> np = len ( bars ) <EOL> usage = bars [ <NUM_LIT:0> ] . domain . allocator . get_usage ( ) <EOL> fragmentation = bars [ <NUM_LIT:0> ] . domain . allocator . get_fragmentation ( ) <EOL> blocks = len ( bars [ <NUM_LIT:0> ] . domain . allocator . starts ) <EOL> stats_text . text = '<STR_LIT>' % ( np , blocks , usage * <NUM_LIT:100> , fragmentation * <NUM_LIT:100> ) <EOL> clock . schedule_interval ( update_stats , <NUM_LIT:1> ) <EOL> fps_text = clock . ClockDisplay ( color = ( <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> create_bars ( ) <EOL> update_time = <NUM_LIT:0.> <EOL> while not win . has_exit : <EOL> win . dispatch_events ( ) <EOL> dt = clock . tick ( ) <EOL> dt = min ( dt , <NUM_LIT> ) <EOL> update_time += dt <EOL> if update_time > UPDATE_PERIOD : <EOL> update_bars ( ) <EOL> update_time -= UPDATE_PERIOD <EOL> win . clear ( ) <EOL> batch . draw ( ) <EOL> stats_text . draw ( ) <EOL> fps_text . draw ( ) <EOL> win . flip ( ) </s>
