[Edu-sig] reviewing the context manager construct

kirby urner kirby.urner at gmail.com
Thu Mar 27 21:23:16 CET 2014


"""
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 at 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
> """
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20140327/f65d0c1f/attachment.html>


More information about the Edu-sig mailing list