<div dir="ltr"><div>"""<br>Just for Fun<br><br>A quick sketch of a Context Manager construct.<br><br>A context manager is a type instance with an<br>__enter__ and __exit__ protocol, and designed<br>to work with a suite indented under a "with statement"<br>
<br>(cc) Kirby Urner, MIT License<br></div><div>O'Reilly School of Technology<br>Sebastopol, CA<br></div><div><br>With thanks to:<br><a href="http://www.greatplay.net/uselessia/articles/e2-1000.html">http://www.greatplay.net/uselessia/articles/e2-1000.html</a><br>
"""<br><br>import decimal<br><br>class Haunted_Castle:<br> """<br> Shows the workflow of a context manager<br> """<br> def __enter__(self):<br> print("Welcome to my world...")<br>
return self<br><br> def __exit__(self, *stuff):<br> print("Sorry to see you leave. Come back again soon.")<br> return True # handled exception<br><br> def __repr__(self):<br> return "Context manager object at: {}".format(id(self))<br>
<br>print("Before the context scope...")<br>with Haunted_Castle() as ctx:<br> print("This is the suite within the scope of the context")<br> print("Available from inside:", repr(ctx))<br>
raise Exception # raise a fuss<br><br>print("After context...")<br><br># and now that we have introduced the concept, here is one way<br># to use it in practice: temporarily boost the precision of<br># Decimal type objects beyond a default of 28 decimal digits<br>
# of precision<br><br>print("Do a big decimal computation...")<br>print("Default precision is:", decimal.getcontext().prec)<br><br>with decimal.localcontext() as ctx:<br> ctx.prec = 500<br> print("Precision in suite:", decimal.getcontext().prec)<br>
n = decimal.Decimal("1"+"0"*300) # 100000....0 (300 zeros)<br> result = str((1 + 1/n) ** n) # converges to Euler's e<br><br>print("Default precision is:", decimal.getcontext().prec)<br>
<br><br># as in math.e but to more digits (1000)<br><br>euler = """\<br>2.<br>7182818284 5904523536 0287471352 6624977572<br>4709369995 9574966967 6277240766 3035354759<br>4571382178 5251664274 2746639193 2003059921<br>
8174135966 2904357290 0334295260 5956307381<br>3232862794 3490763233 8298807531 9525101901<br>1573834187 9307021540 8914993488 4167509244<br>7614606680 8226480016 8477411853 7423454424<br>3710753907 7744992069 5517027618 3860626133<br>
1384583000 7520449338 2656029760 6737113200<br>7093287091 2744374704 7230696977 2093101416<br>9283681902 5515108657 4637721112 5238978442<br>5056953696 7707854499 6996794686 4454905987<br>9316368892 3009879312 7736178215 4249992295<br>
7635148220 8269895193 6680331825 2886939849<br>6465105820 9392398294 8879332036 2509443117<br>3012381970 6841614039 7019837679 3206832823<br>7646480429 5311802328 7825098194 5581530175<br>6717361332 0698112509 9618188159 3041690351<br>
5988885193 4580727386 6738589422 8792284998<br>9208680582 5749279610 4841984443 6346324496<br>8487560233 6248270419 7862320900 2160990235<br>3043699418 4914631409 3431738143 6405462531<br>5209618369 0888707016 7683964243 7814059271<br>
4563549061 3031072085 1038375051 0115747704<br>1718986106 8739696552 1267154688 9570350354\<br>"""<br><br>#remove spaces and newlines<br>euler = euler.replace(" ", "")<br>euler = euler.replace("\n", "")<br>
<br><br># the result of the computation matches the published value to how many digits?<br>c=0<br>while True:<br> if euler[c] != result[c]:<br> print("Same to {} places".format(c))<br> break<br>
c += 1<br><br>#show significant digits<br>#print(euler[:c])<br>#print(result[:c])<br><br><br></div></div>