Easy Extend
|
|
![]() |
|
Author: Kay Schluehr LastModified: 17.10.2006 Version: 0.5 - requires Python 2.4 and EasyExtend v 2.0 |
if
E1: assert E2 == (E1 and E2) else: assert E1 == (E1 and E2) if E1: assert E1 == (E1 or E2) else: assert E2 == (E1 or E2) |
if
E1: if E2: assert E2 == E1 and E2 or E3 else: assert E3 == E1 and E2 or E3 else: assert E3 == E1 and E2 or E3 |
if
E1: assert E2 == (E1 and (E2,True) or (E3,True))[0] else: assert E3 == (E1 and (E2,True) or (E3,True))[0] |
E1 if E2 else E3 |
==>
|
(E2 and (E1,True) or (E3,True))[0] |
FINALLY_BLOCK |
==> |
try: FINALLY_BLOCK except Exception: # uncaught exception in FINALLY_BLOCK raise FinallyException(sys.exc_info()) |
try: TRY_BLOCK except SomeException: EXCEPT_BLOCK finally: FINALLY_BLOCK |
==> |
try: try: TRY_BLOCK FINALLY_BLOCK except SomeException: EXCEPT_BLOCK FINALLY_BLOCK except Exception: exc_cls, exc_value, exc_tb = sys.exc_info() # stems from Exception clause of FINALLY_BLOCK if exc_cls == FinallyException: fin_exc_cls, fin_exc_value, fin_exc_tb = exc_value raise fin_exc_cls, fin_exc_value, fin_exc_tb else: FINALLY_BLOCK raise exc_cls, exc_instance, exc_tb |
for i in range(10): try: break finally: print "Hello" |
==> |
for i in range(10): try: FINALLY_BLOCK break except Exception: ... |
def gen(): a = 0 for i in range(10): a = yield i+a |
==> |
def
gen(**kwd):
# - modified signature if kwds are not already declared gvar = kwd["gvar"] # - read gvar from kwd a = 0 for i in range(10): yield i+a # - break down yield expression into a yield statement and a = gvar["value"] # a variable assignment |
|
class
EnhancedGenerator(object): def __init__(self, gen): self.gen_func = gen def __call__(self, *args, **kwd): self.gvar = {"value":None, "throw":None, "close":None} kwd["gvar"] = self.gvar self.gen = self.gen_func(*args, **kwd) return self def next(self): return self.gen.next() def send(self, value): self.gvar["value"] = value return self.next() def enhance_generator(func): eg = EnhancedGenerator(func) fn = lambda *args, **kwd: eg(*args, **kwd) fn.__name__ = func.__name__ return fn |
def gen(): a = 0 for i in range(10): a = yield i+a |
==> |
@enhanced_generator def gen(**kwd): # - modified signature if kwds are not already declared gvar = kwd["gvar"] # - read gvar from kwd a = 0 for i in range(10): yield i+a # - break down yield expression into a yield statement and a = gvar["value"] # a variable assignment |
with <EXPR> as <VAR>: <BLOCK> |
===> |
mgr = (<EXPR>) exit = mgr.__exit__ # Not calling it yet value = mgr.__enter__() exc = True try: try: <VAR> = value # Only if "as VAR" is present <BLOCK> except: # The exceptional case is handled here exc = False if not exit(*sys.exc_info()): raise # The exception is swallowed if exit() returns true finally: # The normal and non-local-goto cases are handled here if exc: exit(None, None, None) SUITE1 else: SUITE2 |
d
= {2:5, 3:9} x = 0 repeat: on m = d.get(x): x = m break x+=1 until: x == 3 assert x == 5 |