Commit 4e038901 authored by Jim Fulton's avatar Jim Fulton

untabified.

parent 5a85f09e
#!/bin/env python
############################################################################## ##############################################################################
# #
# Copyright # Copyright
...@@ -77,8 +76,8 @@ called automatically, if it has one. ...@@ -77,8 +76,8 @@ called automatically, if it has one.
This module provides a simple computed attribute implementation and This module provides a simple computed attribute implementation and
an example of its usage. an example of its usage.
$Id: ComputedAttribute.py,v 1.1 1998/04/07 16:43:21 jim Exp $''' $Id: ComputedAttribute.py,v 1.2 1998/12/04 20:57:46 jim Exp $'''
__version__='$Revision: 1.1 $'[11:-2] __version__='$Revision: 1.2 $'[11:-2]
import ExtensionClass import ExtensionClass
...@@ -98,18 +97,9 @@ if __name__ == "__main__": ...@@ -98,18 +97,9 @@ if __name__ == "__main__":
class point(ExtensionClass.Base): class point(ExtensionClass.Base):
def __init__(self, x, y): self.x, self.y = x, y def __init__(self, x, y): self.x, self.y = x, y
radius=ComputedAttribute(lambda self: sqrt(self.x**2+self.y**2)) radius=ComputedAttribute(lambda self: sqrt(self.x**2+self.y**2))
p=point(2,2) p=point(2,2)
print p.radius print p.radius
##############################################################################
#
# $Log: ComputedAttribute.py,v $
# Revision 1.1 1998/04/07 16:43:21 jim
# *** empty log message ***
#
#
...@@ -8,28 +8,28 @@ from time import sleep ...@@ -8,28 +8,28 @@ from time import sleep
class P(Synchronized): class P(Synchronized):
def __init__(self,*args,**kw): def __init__(self,*args,**kw):
self.count=0 self.count=0
def inc(self): def inc(self):
c=self.count c=self.count
sleep(rand()/327680.0) sleep(rand()/327680.0)
self.count=self.count+1 self.count=self.count+1
return c,self.count return c,self.count
def incn(self,n): def incn(self,n):
c=self.count c=self.count
for i in range(n): self.inc() for i in range(n): self.inc()
return c,self.count return c,self.count
p=P(1,2,spam=3) p=P(1,2,spam=3)
def test(): def test():
for i in range(10): for i in range(10):
n=3 n=3
old,new=p.incn(n) old,new=p.incn(n)
print old,new print old,new
if old+n != new: print 'oops' if old+n != new: print 'oops'
for i in range(10): thread.start_new_thread(test,()) for i in range(10): thread.start_new_thread(test,())
......
...@@ -10,44 +10,44 @@ with_lock=1 ...@@ -10,44 +10,44 @@ with_lock=1
class P(Base): class P(Base):
def __oldcall_method__(self,f,a,k={}): def __oldcall_method__(self,f,a,k={}):
if with_lock: if with_lock:
try: lock=self.lock try: lock=self.lock
except AttributeError: return apply(f,a,k) except AttributeError: return apply(f,a,k)
else: return apply(f,a,k) else: return apply(f,a,k)
try: try:
lock.acquire() lock.acquire()
return apply(f,a,k) return apply(f,a,k)
finally: finally:
lock.release() lock.release()
__call_method__=apply __call_method__=apply
def __init__(self,*args,**kw): def __init__(self,*args,**kw):
self.count=0 self.count=0
if with_lock: if with_lock:
self.lock=lock=ThreadLock.allocate_lock() self.lock=lock=ThreadLock.allocate_lock()
self.__call_method__=lock.guarded_apply self.__call_method__=lock.guarded_apply
def inc(self): def inc(self):
c=self.count c=self.count
sleep(rand()/32768.0) sleep(rand()/32768.0)
self.count=self.count+1 self.count=self.count+1
return c,self.count return c,self.count
def incn(self,n): def incn(self,n):
c=self.count c=self.count
for i in range(n): self.inc() for i in range(n): self.inc()
return c,self.count return c,self.count
p=P(1,2,spam=3) p=P(1,2,spam=3)
def test(): def test():
for i in range(10): for i in range(10):
n=3 n=3
old,new=p.incn(n) old,new=p.incn(n)
print old,new print old,new
if old+n != new: print 'oops' if old+n != new: print 'oops'
for i in range(10): thread.start_new_thread(test,()) for i in range(10): thread.start_new_thread(test,())
......
...@@ -4,8 +4,8 @@ import ExtensionClass ...@@ -4,8 +4,8 @@ import ExtensionClass
class C(ExtensionClass.Base): class C(ExtensionClass.Base):
def __call_method__(self,meth,args,kw={}): def __call_method__(self,meth,args,kw={}):
print 'give us a hook, hook, hook...' print 'give us a hook, hook, hook...'
apply(meth,args,kw) apply(meth,args,kw)
def hi(self,*args,**kw): print self, args, kw def hi(self,*args,**kw): print self, args, kw
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment