ch07
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
# context/decimal.prec.py
|
||||
from decimal import Context, Decimal, getcontext, setcontext
|
||||
|
||||
one = Decimal("1")
|
||||
three = Decimal("3")
|
||||
|
||||
orig_ctx = getcontext()
|
||||
ctx = Context(prec=5)
|
||||
setcontext(ctx)
|
||||
print(ctx)
|
||||
print(one / three)
|
||||
setcontext(orig_ctx)
|
||||
print(one / three)
|
||||
|
||||
|
||||
"""
|
||||
$ python context/decimal.prec.py
|
||||
Context(prec=5, rounding=ROUND_HALF_EVEN, Emin=-999999,
|
||||
Emax=999999, capitals=1, clamp=0, flags=[],
|
||||
traps=[InvalidOperation, DivisionByZero, Overflow])
|
||||
0.33333
|
||||
0.3333333333333333333333333333
|
||||
"""
|
||||
|
||||
|
||||
orig_ctx = getcontext()
|
||||
ctx = Context(prec=5)
|
||||
setcontext(ctx)
|
||||
try:
|
||||
print(ctx)
|
||||
print(one / three)
|
||||
finally:
|
||||
setcontext(orig_ctx)
|
||||
print(one / three)
|
||||
|
||||
|
||||
from decimal import localcontext
|
||||
|
||||
with localcontext(Context(prec=5)) as ctx:
|
||||
print(ctx)
|
||||
print(one / three)
|
||||
print(one / three)
|
||||
|
||||
|
||||
with localcontext(Context(prec=5)), open("out.txt", "w") as out_f:
|
||||
out_f.write(f"{one} / {three} = {one / three}\n")
|
||||
@@ -0,0 +1,38 @@
|
||||
# context/generator.py
|
||||
from contextlib import contextmanager
|
||||
|
||||
@contextmanager
|
||||
def my_context_manager():
|
||||
print("Entering 'with' context")
|
||||
val = object()
|
||||
print(id(val))
|
||||
try:
|
||||
yield val
|
||||
except Exception as e:
|
||||
print(f"{type(e)=} {e=} {e.__traceback__=}")
|
||||
finally:
|
||||
print("Exiting 'with' context")
|
||||
|
||||
|
||||
print("About to enter 'with' context")
|
||||
with my_context_manager() as val:
|
||||
print("Inside 'with' context")
|
||||
print(id(val))
|
||||
raise Exception("Exception inside 'with' context")
|
||||
print("This line will never be reached")
|
||||
|
||||
print("After 'with' context")
|
||||
|
||||
|
||||
"""
|
||||
$ python context/generator.py
|
||||
About to enter 'with' context
|
||||
Entering 'with' context
|
||||
139768531985040
|
||||
Inside 'with' context
|
||||
139768531985040
|
||||
type(e)=<class 'Exception'> e=Exception("Exception inside 'with'
|
||||
context") e.__traceback__=<traceback object at 0x7f1e65a42800>
|
||||
Exiting 'with' context
|
||||
After 'with' context
|
||||
"""
|
||||
@@ -0,0 +1,40 @@
|
||||
# context/manager.class.py
|
||||
class MyContextManager:
|
||||
def __init__(self):
|
||||
print("MyContextManager init", id(self))
|
||||
|
||||
def __enter__(self):
|
||||
print("Entering 'with' context")
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
print(f"{exc_type=} {exc_val=} {exc_tb=}")
|
||||
print("Exiting 'with' context")
|
||||
return True
|
||||
|
||||
|
||||
ctx_mgr = MyContextManager()
|
||||
|
||||
print("About to enter 'with' context")
|
||||
|
||||
with ctx_mgr as mgr:
|
||||
print("Inside 'with' context")
|
||||
print(id(mgr))
|
||||
raise Exception("Exception inside 'with' context")
|
||||
print("This line will never be reached")
|
||||
|
||||
print("After 'with' context")
|
||||
|
||||
|
||||
"""
|
||||
$ python context/manager.class.py
|
||||
MyContextManager init 140340228792272
|
||||
About to enter 'with' context
|
||||
Entering 'with' context
|
||||
Inside 'with' context
|
||||
140340228792272
|
||||
exc_type=<class 'Exception'> exc_val=Exception("Exception inside
|
||||
'with' context") exc_tb=<traceback object at 0x7fa3817c5340>
|
||||
Exiting 'with' context
|
||||
After 'with' context
|
||||
"""
|
||||
Reference in New Issue
Block a user