reviewing the context manager construct
""" Just for Fun A quick sketch of a Context Manager construct. A context manager is a type instance with an __enter__ and __exit__ protocol, and designed to work with a suite indented under a "with statement" (cc) Kirby Urner, MIT License O'Reilly School of Technology Sebastopol, CA With thanks to: http://www.greatplay.net/uselessia/articles/e2-1000.html """ import decimal class Haunted_Castle: """ Shows the workflow of a context manager """ def __enter__(self): print("Welcome to my world...") return self def __exit__(self, *stuff): print("Sorry to see you leave. Come back again soon.") return True # handled exception def __repr__(self): return "Context manager object at: {}".format(id(self)) print("Before the context scope...") with Haunted_Castle() as ctx: print("This is the suite within the scope of the context") print("Available from inside:", repr(ctx)) raise Exception # raise a fuss print("After context...") # and now that we have introduced the concept, here is one way # to use it in practice: temporarily boost the precision of # Decimal type objects beyond a default of 28 decimal digits # of precision print("Do a big decimal computation...") print("Default precision is:", decimal.getcontext().prec) with decimal.localcontext() as ctx: ctx.prec = 500 print("Precision in suite:", decimal.getcontext().prec) n = decimal.Decimal("1"+"0"*300) # 100000....0 (300 zeros) result = str((1 + 1/n) ** n) # converges to Euler's e print("Default precision is:", decimal.getcontext().prec) # as in math.e but to more digits (1000) euler = """\ 2. 7182818284 5904523536 0287471352 6624977572 4709369995 9574966967 6277240766 3035354759 4571382178 5251664274 2746639193 2003059921 8174135966 2904357290 0334295260 5956307381 3232862794 3490763233 8298807531 9525101901 1573834187 9307021540 8914993488 4167509244 7614606680 8226480016 8477411853 7423454424 3710753907 7744992069 5517027618 3860626133 1384583000 7520449338 2656029760 6737113200 7093287091 2744374704 7230696977 2093101416 9283681902 5515108657 4637721112 5238978442 5056953696 7707854499 6996794686 4454905987 9316368892 3009879312 7736178215 4249992295 7635148220 8269895193 6680331825 2886939849 6465105820 9392398294 8879332036 2509443117 3012381970 6841614039 7019837679 3206832823 7646480429 5311802328 7825098194 5581530175 6717361332 0698112509 9618188159 3041690351 5988885193 4580727386 6738589422 8792284998 9208680582 5749279610 4841984443 6346324496 8487560233 6248270419 7862320900 2160990235 3043699418 4914631409 3431738143 6405462531 5209618369 0888707016 7683964243 7814059271 4563549061 3031072085 1038375051 0115747704 1718986106 8739696552 1267154688 9570350354\ """ #remove spaces and newlines euler = euler.replace(" ", "") euler = euler.replace("\n", "") # the result of the computation matches the published value to how many digits? c=0 while True: if euler[c] != result[c]: print("Same to {} places".format(c)) break c += 1 #show significant digits #print(euler[:c]) #print(result[:c])
""" Just for the Fun of It. [~tm] More in a similar vein.... These exercises may seem pretty pointless, but I remember coming from FoxPro to Python how excited I was by such long ints, then still a separate type from ints proper. The Decimal object had yet to join us back then. I feel programs below are like ritual welcoming ceremonies in that they show off the kinds of abilities our new circus animal brings to our circus. Apologies in advance for any unwanted wrapping. These give the idea but if you want to cut and paste / run, some tweaking may be in order to achieve runnability. A couple identities involving Phi: http://www.flickr.com/photos/kirbyurner/13432252025/ You might just want to show something these computers can do that a mere calculator cannot easily duplicate. These are "monster truck" decimals versus the abbreviated ones in most textbooks. """ digits = """1.61803398874989484820458683436563811772030917980576286213544862270526046281890 244970720720418939113748475408807538689175212663386222353693179318006076672635 443338908659593958290563832266131992829026788067520876689250171169620703222104 321626954862629631361443814975870122034080588795445474924618569536486444924104 432077134494704956584678850987433944221254487706647809158846074998871240076521 705751797883416625624940758906970400028121042762177111777805315317141011704666 599146697987317613560067087480710131795236894275219484353056783002287856997829 778347845878228911097625003026961561700250464338243776486102838312683303724292""".replace("\n","") import decimal with decimal.localcontext() as context: context.prec = len(digits)-1 phi = decimal.Decimal(digits) print(phi**2 + phi**-2) # Identity 1 (== 3) print(8 * phi**2 - phi**6) # Identity 2 (== 3) # and so they equal each other, see Flickr comments for more # http://www.flickr.com/photos/kirbyurner/13432252025/ (DK = David Koski) OUTPUT: 2.9999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 999999999999999999999999999999999999999999999999999999999999999999999999999999 8 3.0000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000002 On Tue, Mar 4, 2014 at 7:23 PM, Kirby Urner <kurner@oreillyschool.com>wrote:
""" Just for Fun
A quick sketch of a Context Manager construct.
A context manager is a type instance with an __enter__ and __exit__ protocol, and designed to work with a suite indented under a "with statement"
(cc) Kirby Urner, MIT License O'Reilly School of Technology Sebastopol, CA
With thanks to: http://www.greatplay.net/uselessia/articles/e2-1000.html """
participants (2)
-
kirby urner -
Kirby Urner