Skip to content

Commit

Permalink
Add a second example of parens and Context Managers.
Browse files Browse the repository at this point in the history
  - uses two copies of `localcontext`
  • Loading branch information
brocla authored and BethanyG committed Mar 22, 2024
1 parent 0365f28 commit a094c4a
Show file tree
Hide file tree
Showing 4 changed files with 1,186 additions and 750 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
"""Examples of Parenthesized Context Managers. New feature in Python 3.10"""



# This example shows parens around two `localcontext` context mangers.
"""Calculate the fixed interest rate."""

from decimal import ROUND_DOWN, ROUND_UP, Decimal, localcontext
import pandas as pd


def calc_fixed_rate(spot_price: pd.Series, position_duration: Decimal) -> pd.Series:
"""Calculates the fixed rate given trade data.
Arguments
---------
spot_price: pd.Series
The spot price.
position_duration: Decimal
The position duration in seconds.
Returns
-------
pd.Series
The fixed interest rate.
"""
# Position duration (in seconds) in terms of fraction of year
# This div should round up
# This replicates div up in fixed point
with (localcontext() as ctx_time,
localcontext() as ctx_rate):
ctx_time.prec = 18
ctx_time.rounding = ROUND_UP

ctx_rate.prec = 12
ctx_rate.rounding = ROUND_DOWN

annualized_time = position_duration / Decimal(60 * 60 * 24 * 365)
fixed_rate = (1 - spot_price) / (spot_price * annualized_time) # type: ignore
return fixed_rate





# This example shows a CLI that opens two files with context managers that are grouped by parens.
#!/usr/bin/python3
"""convert json to jsonl"""

Expand Down Expand Up @@ -28,45 +71,4 @@ def parse_args():
return args

if __name__ == "__main__":
main()





# Examples from python docs
# https://docs.python.org/3/whatsnew/3.10.html#parenthesized-context-managers

def CtxManager(): pass
def CtxManager1(): pass
def CtxManager2(): pass
def CtxManager3(): pass

with (CtxManager() as example):
...

with (
CtxManager1(),
CtxManager2()
):
...

with (CtxManager1() as example,
CtxManager2()):
...

with (CtxManager1(),
CtxManager2() as example):
...

with (
CtxManager1() as example1,
CtxManager2() as example2
):
...
with (
CtxManager1() as example1,
CtxManager2() as example2,
CtxManager3() as example3,
):
...
main()
28 changes: 19 additions & 9 deletions test/example-parencontext-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"placeholder_0": "main",
"placeholder_1": "args",
"placeholder_2": "in_file",
"placeholder_3": "mode",
"placeholder_4": "out_file",
"placeholder_5": "records",
"placeholder_6": "record",
"placeholder_7": "parse_args",
"placeholder_8": "parser"
"placeholder_0": "pd",
"placeholder_1": "calc_fixed_rate",
"placeholder_2": "spot_price",
"placeholder_3": "position_duration",
"placeholder_4": "ctx_time",
"placeholder_5": "ctx_rate",
"placeholder_6": "prec",
"placeholder_7": "rounding",
"placeholder_8": "annualized_time",
"placeholder_9": "fixed_rate",
"placeholder_10": "main",
"placeholder_11": "args",
"placeholder_12": "in_file",
"placeholder_13": "mode",
"placeholder_14": "out_file",
"placeholder_15": "records",
"placeholder_16": "record",
"placeholder_17": "parse_args",
"placeholder_18": "parser"
}
Loading

0 comments on commit a094c4a

Please sign in to comment.