#!/usr/bin/env python
        
import weak

# A trivial item for our map.
#
# Announcing deletion is good for convincing ourselves the
# weak proxy maps work, because a new foo can quite often be
# allocated at the spot where the last one was deleted!
class foo:
    def __del__(self):
        print 'deleting', self
    
# A trivial map to wrap.  It just creates a new foo on every getitem.
# You can imagine it's doing something much more expensive.
class foos:
    def __getitem__(self, k):
        return foo()

# Create the weak proxy.
wfoos = weak.proxymap(foos())

# Example behavior.  Only the first and last subscripting of wfoos
# indexes into foos, so this prints one foo instance three times and
# then a new one once. (See the above note about deletion however;
# whether the reported *address* is different can vary from run to
# run.)

l = wfoos['lala']
l1 = wfoos['lala']
print l
print l1
del l
print l1
del l1
# Try to help make the next foo looked up be at a different address!
l2 = foo()                              
l3 = foo()                              
print wfoos['lala']

# Now we check out setting and deletion.  wfoos1 should behave exactly
# like a regular dictionary here printing one foo instance 3 times and
# another one once.
foos1 = {}

# Create the weak proxy.
wfoos1 = weak.proxymap(foos1)

wfoos1['ghee'] = foo()
l = wfoos1['ghee']
l1 = wfoos1['ghee']
print l
print l1
wfoos1['ghee'] = foo()
del l
print l1
del l1
print wfoos1['ghee']
