t256.py

00001 # This file is Copyright 2009 Dean Hall.
00002 #
00003 # This file is part of the Python-on-a-Chip program.
00004 # Python-on-a-Chip is free software: you can redistribute it and/or modify
00005 # it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1.
00006 #
00007 # Python-on-a-Chip is distributed in the hope that it will be useful,
00008 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00010 # A copy of the GNU LESSER GENERAL PUBLIC LICENSE Version 2.1
00011 # is seen in the file COPYING up one directory from this.
00012 
00013 #
00014 # System Test 256
00015 # Add support for closures
00016 #
00017 
00018 
00019 callback_table = {}
00020 HOOK_1S = 1002
00021 HOOK_ADC = 1003
00022 
00023 
00024 def doNothing(f):
00025     print "in doNothing"
00026     return f
00027 
00028 
00029 def setHook(event):
00030     print "in setHook"
00031     def _sh(f):
00032         print "in _sh"
00033         callback_table[event] = f
00034         return f
00035     return _sh
00036 
00037 
00038 @doNothing
00039 @setHook(HOOK_1S)
00040 def onOneSecond():
00041     print "in onOneSecond"
00042 
00043 
00044 @setHook(HOOK_ADC)
00045 @doNothing
00046 def onAdcReady():
00047     print "in onAdcReady"
00048 
00049 
00050 print "callback_table: ", callback_table
00051 
00052 assert callback_table[1002] == onOneSecond
00053 assert callback_table[1003] == onAdcReady
00054 print "Closure test 1 passes"
00055 
00056 
00057 ####
00058 
00059 def filter(f, s):
00060     return [x for x in s if f(x)]
00061 
00062 def isEven(n):
00063     return n % 2 == 0
00064 
00065 class Employee():
00066     def __init__(self,):
00067         self.salary = 100
00068 
00069 def highPaid1(emps):
00070     threshold = 150
00071     return filter(lambda e: e.salary > threshold, emps)
00072 
00073 def paidMore(amount):
00074     return lambda x: filter(lambda e: e.salary > amount, x)
00075 highPaid2 = paidMore(150)
00076 
00077 john = Employee()
00078 john.salary = 200
00079 john.isManager = False
00080 
00081 employees = [john, Employee(), Employee()]
00082 assert highPaid1(employees) == [john,]
00083 assert highPaid2(employees) == [john,]
00084 print "Closure test 2 passes"
00085 
00086 
00087 ####
00088 
00089 def foo(a,b,c):
00090     def bar():
00091         def baz():
00092             return a+b+c
00093         return baz
00094     return bar()
00095 f = foo(1,2,3)
00096 print f
00097 assert f() == 6
00098 print "Closure test 3 passes"

Generated on Mon Oct 18 07:40:47 2010 for Python-on-a-chip by  doxygen 1.5.9