Wrapping paper, anyone ?

S

simon

#!/usr/bin/env python

from math import *

from random import *

import cairo
from cairo import Context

Black = (0, 0, 0)
White = (1, 1, 1)

def rand():
return random()*2 - 1

def rotate(theta, x, y):
x, y = x*cos(theta)-y*sin(theta), x*sin(theta)+y*cos(theta)
return x, y

def star(ctx, n=5, r0=0.4):

for c in range(2):
ctx.set_source_rgba(*(White, Black)[c])
x, y = 0, 1
ctx.move_to(x, y)
theta = 2*pi / n
for i in range(n):
x1, y1 = rotate(theta/2, x, y)
ctx.line_to(r0*x1, r0*y1)
x, y = rotate(theta/2, x1, y1)
ctx.line_to(x, y)
ctx.close_path()
(ctx.fill, ctx.stroke)[c]()


class ScribeCall(object):
def __init__(self, scribe, name):
self.scribe = scribe
self.name = name

def __call__(self, *args, **kw):
self.args = args
self.kw = kw
self.scribe.calls.append(self)


class Scribe(object):
def __init__(self):
self.calls = []

__getattr__ = ScribeCall
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x
in call.args))
getattr(ctx, call.name)(*call.args, **call.kw)


def orbit(dx, dy, w, h):
x = -2*w
while x < 2*w:
y = -2*h
while y < 2*h:
yield x, y
y += dy
x += dx


def main_stars(w=800, h=1000):

seed(0)

surface = cairo.PSSurface('stars.ps', w, h)
ctx = Context(surface)

ctx.set_source_rgba(*White)
ctx.rectangle(0, 0, w, h)
ctx.fill()

ctx.set_line_width(1./30)

scribe = Scribe()

for i in range(10):
scribe.save()

scribe.translate(w*random(), h*random())
scribe.scale(20, 20)
scribe.rotate(random() * 2 * pi)

r = exp(random())
scribe.scale(r, r)
star(scribe, 5)
scribe.scale(1./r, 1./r)

scribe.translate(rand(), rand())
scribe.restore()

for x, y in orbit(120, 240, w, h):
ctx.save()
ctx.translate(x, y)
scribe.run(ctx)
ctx.restore()


if __name__=="__main__":
main_stars()
 
P

Peter Otten

simon wrote:

Nice :)

--- stars.py 2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py 2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []

- __getattr__ = ScribeCall
+ def __getattr__(self, name):
+ return ScribeCall(self, name)
+
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Peter
 
R

r0g

Peter said:
simon wrote:

Nice :)

--- stars.py 2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py 2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []

- __getattr__ = ScribeCall
+ def __getattr__(self, name):
+ return ScribeCall(self, name)
+
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Peter


Nice :) Now all I need is an A0 printer, maybe Santa will bring me one!

Roger.
 
S

simon

simon wrote:

Nice :)

--- stars.py    2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py      2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
     def __init__(self):
         self.calls = []

-    __getattr__ = ScribeCall
+    def __getattr__(self, name):
+        return ScribeCall(self, name)
+
     def run(self, ctx):
         for call in self.calls:
             #print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Peter

Oh.. I'm on py2.5.. does this not work for you ?
 
S

simon

Peter said:
simon wrote:
--- stars.py    2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py      2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
     def __init__(self):
         self.calls = []
-    __getattr__ = ScribeCall
+    def __getattr__(self, name):
+        return ScribeCall(self, name)
+
     def run(self, ctx):
         for call in self.calls:
             #print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Nice :) Now all I need is an A0 printer, maybe Santa will bring me one!

Roger.

I found I could wrap a CD in A4 paper... for bigger things, i might
stick some A4 paper together.

Wow. Who is getting the A0 gift ?
 
P

Peter Otten

simon said:
simon wrote:

Nice :)

--- stars.py 2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py 2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []

- __getattr__ = ScribeCall
+ def __getattr__(self, name):
+ return ScribeCall(self, name)
+
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))

Peter

Oh.. I'm on py2.5.. does this not work for you ?

You mean 2.4? Here's a little demo:

$ cat scribecall.py
class ScribeCall(object):
def __init__(self, scribe, name):
print "init", scribe, name
def __call__(self, *args, **kw):
print "call", args, kw

class Scribe(object):
__getattr__ = ScribeCall

if __name__ == "__main__":
scribe = Scribe()
scribe.yadda(42)

$ python2.4 scribecall.py
init <__main__.Scribe object at 0x7fc87b9a1450> yadda
call (42,) {}

$ python2.5 scribecall.py
Traceback (most recent call last):
File "scribecall.py", line 12, in <module>
scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

$ python2.5 -V
Python 2.5.4

$ python2.6 scribecall.py
Traceback (most recent call last):
File "scribecall.py", line 12, in <module>
scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

Peter
 
R

r0g

simon said:
Peter said:
simon wrote:
Nice :)
--- stars.py 2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py 2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []
- __getattr__ = ScribeCall
+ def __getattr__(self, name):
+ return ScribeCall(self, name)
+
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))
Peter
Nice :) Now all I need is an A0 printer, maybe Santa will bring me one!

Roger.

I found I could wrap a CD in A4 paper... for bigger things, i might
stick some A4 paper together.

Wow. Who is getting the A0 gift ?



I was thinking of selling it, this is BSD licensed right! ;D

R.
 
S

simon

simon said:
simon wrote:
Nice :)
--- stars.py    2009-12-16 10:52:49.553505036 +0100
+++ stars_fixed.py      2009-12-16 10:53:32.545786454 +0100
@@ -48,7 +48,9 @@
def __init__(self):
self.calls = []
-    __getattr__ = ScribeCall
+    def __getattr__(self, name):
+        return ScribeCall(self, name)
+
def run(self, ctx):
for call in self.calls:
#print "ctx.%s(%s)" % (call.name, ', '.join(str(x) for x in
call.args))
Peter
Oh.. I'm on py2.5.. does this not work for you ?

You mean 2.4? Here's a little demo:

$ cat scribecall.py
class ScribeCall(object):
    def __init__(self, scribe, name):
        print "init", scribe, name
    def __call__(self, *args, **kw):
        print "call", args, kw

class Scribe(object):
    __getattr__ = ScribeCall

if __name__ == "__main__":
    scribe = Scribe()
    scribe.yadda(42)

$ python2.4 scribecall.py
init <__main__.Scribe object at 0x7fc87b9a1450> yadda
call (42,) {}

$ python2.5 scribecall.py
Traceback (most recent call last):
  File "scribecall.py", line 12, in <module>
    scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

$ python2.5 -V
Python 2.5.4

$ python2.6 scribecall.py
Traceback (most recent call last):
  File "scribecall.py", line 12, in <module>
    scribe.yadda(42)
TypeError: __init__() takes exactly 3 arguments (2 given)

Peter

Oh wow, it works on python 2.5.2 !

Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top