Dekorátorok

177 days ago by Sandor_Czirbusz

%python def short_collatz(n): import math return (n == 1 and [1]) or \ ([n] + short_collatz(int(abs(math.sin(math.pi*n/2)))*(3*n+1) + int(abs(math.cos(math.pi*n/2)))*(n/2))) 
       
short_collatz(1) 
       
short_collatz(2) 
       
short_collatz(12) 
       
@interact def f(m=range(1,13), n=(1..12)): print '%s * %s = %s '%(n,m,n*m) print ('x'*m+' ')*n 
       

Click to the left again to hide and once more to show the dynamic interactive window

n = (1..12) 
       
type(n) 
       
 
       
def chatty(f): def g(*args, **kwds): print "Function called with input args=%s, kwds=%s"%(args, kwds) return f(*args, **kwds) return g 
       
@chatty def f(n, m): return n*m 
       
f(3, m=5) 
       
 
       
@cached_function def f(n, m): print "Working really hard to multiply %s by %s..."%(n,m) return n*m 
       
f(5,7) 
       
f(5,7) 
       
 
       
@cached_function @chatty def f(n, m): return n*m 
       
f(5,7) 
       
f(5,7) 
       
 
       
@parallel(2) def f(n): return factorial(n).ndigits() 
       
time f(10^6) 
       
5565709
Time: CPU 0.55 s, Wall: 0.54 s
5565709
Time: CPU 0.55 s, Wall: 0.54 s
time v = [f(10^6+i) for i in [1..4]] 
       
time w = list(f([10^6+i for i in [1..4]])) 
       
time f(10^6) 
       
 
       
@interact def _(a=5, y=(0..20)): print a + y 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a=(0,2)): show(plot(sin(x*(1+a*x)), (x,0,6))) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(n=(10,100,1)): show(factor(x^n - 1)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a=(1,4), b=(0,10)): show(plot(sin(a*x+b), (x,0,6)), figsize=3) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(t1=text_control("Factors an integer."), n="1"): print factor(Integer(n)) 
       
Factors an integer.

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact(layout=[['a','b'],['d']]) def _(a=x^2, b=(0..20), c=100, d=x+1): print a+b+c+d 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a=(0,2)): show(plot(sin(x*(1+a*x)), (x,0,6)), figsize=4) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(v = input_box((1,0,0))): show(plot(sin,color=v)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(c = color_selector((1, 0, 0))): show(plot(sin, color = c)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
xyz = 10 @interact def _(a=('xyz',5)): global xyz xyz = a 
       
xyz 

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(title=["A Plot Demo", "Something silly", "something tricky"], a=input_box(sin(x*sin(x*sin(x))), 'function'),clr = Color('red'), thickness=[1..30], zoom=(1,0.95,..,0.1), plot_points=(200..2000)): html('<h1 align=center>%s</h1>'%title) print plot_points show(plot(a, -zoom*pi,zoom*pi, color=clr, thickness=thickness, plot_points=plot_points)) 
       
title 
function 
clr 
thickness 
zoom 
plot_points 

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(color=color_selector((1,0,1), label='', widget='colorpicker', hide_box=True)): show(plot(x/(8/7+sin(x)), (x,-50,50), fill=True, fillcolor=color)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a=('first', (1,4)), b=(0,10)): show(plot(sin(a*x+sin(b*x)), (x,0,6)), figsize=3) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a = slider(1, 4, default=2, label='Multiplier'), b = slider(0, 10, default=0, label='Phase Variable')): show(plot(sin(a*x+b), (x,0,6)), figsize=4) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(b = range_slider(-20, 20, 1, default=(-19,3), label='Range')): plot(sin(x)/x, b[0], b[1]).show(xmin=b[0],xmax=b[1]) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(axes=('Show axes', True), square=False): show(plot(sin, -5,5), axes=axes, aspect_ratio = (1 if square else None)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def foo(pts = checkbox(True, "points"), n = (50,(10..100))): s = 0; v = [(0,0)] for i in range(n): s += random() - 0.5 v.append((i, s)) L = line(v, rgbcolor='#4a8de2') if pts: L += points(v, pointsize=20, rgbcolor='black') show(L) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(a=(0,1)): x,y = var('x,y') show(plot3d(sin(x*cos(y*a)), (x,0,5), (y,0,5)), figsize=4) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
pts = [(random(), random()) for _ in xrange(20)] @interact def _(n = (4..len(pts)), c=Color('purple') ): G = points(pts[:n],pointsize=60) + polygon(pts[:n], rgbcolor=c) show(G, figsize=5, xmin=0, ymin=0) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(q1=(-1,(-3,3)), q2=(-2,(-3,3))): x,y = var('x,y') f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2) C = contour_plot(f, (-2,2), (-2,2), plot_points=30, contours=15, cmap='cool') show(C, figsize=3, aspect_ratio=1) show(plot3d(f, (x,-2,2), (y,-2,2)), figsize=4) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
@interact def _(q1=(-1,(-3,3)), q2=(-2,(-3,3)), cmap=['autumn', 'bone', 'cool', 'copper', 'gray', 'hot', 'hsv', 'jet', 'pink', 'prism', 'spring', 'summer', 'winter']): x,y = var('x,y') f = q1/sqrt((x+1)^2 + y^2) + q2/sqrt((x-1)^2+(y+0.5)^2) C = contour_plot(f, (x,-2,2), (y,-2,2), plot_points=30, contours=15, cmap=cmap) show(C, figsize=3, aspect_ratio=1) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
v = [] html('<h2>Quadratic Root Etch-a-sketch</h2>') @interact def _(a=[-10..10], b=[-10..10], c=[-10..10]): f = a*x^2 + b*x + c == 0; show(f) soln = solve(a*x^2 + b*x + c == 0, x)[0].rhs() show(soln) P = tuple(CDF(soln)) v.append(P) show(line(v, rgbcolor='purple') + point(P, pointsize=200)) 
       

Click to the left again to hide and once more to show the dynamic interactive window

 
       
data = {} @interact def _(n=(500,(100,5000,1)), p=(1,(0.1,10))): n = int(n) if not data.has_key(n): data[n] = [(random(), random()) for _ in xrange(n)] show(points([(x^p,y^p) for x,y in data[n]], rgbcolor='black'), xmin=0, ymin=0, axes=False) 
       

Click to the left again to hide and once more to show the dynamic interactive window