Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for Parenthesized Context Mgr #52

Merged
merged 5 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions test/example-parencontext-normalization/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"authors": [
"brocla"
],
"files": {
"solution": [
"example_parencontext_normalization.py"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Examples of Parenthesized Context Managers. New feature in Python 3.10"""



# This example shows parens around a `localcontext` context mangers.
# Uses two styles of parens.
"""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):
ctx.prec = 18
ctx.rounding = ROUND_UP
annualized_time = position_duration / Decimal(60 * 60 * 24 * 365)

# Pandas is smart enough to be able to broadcast with internal Decimal types at runtime
# We keep things in 18 precision here
with (
localcontext() as ctx
):
ctx.prec = 18
ctx.rounding = ROUND_DOWN
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"""

from argparse import ArgumentParser
import json

def main():
args = parse_args()

# Context Mgrs
with (
open(args.in_filename) as in_file,
open(args.out_filename, mode="wt") as out_file,
):
records = json.load(in_file)
for record in records:
out_file.write(json.dumps(record) + "\n")


def parse_args():
parser = ArgumentParser()
parser.add_argument("in_filename")
parser.add_argument("out_filename")
args = parser.parse_args()
return args

if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions test/example-parencontext-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"placeholder_0": "pd",
"placeholder_1": "calc_fixed_rate",
"placeholder_2": "spot_price",
"placeholder_3": "position_duration",
"placeholder_4": "ctx",
"placeholder_5": "prec",
"placeholder_6": "rounding",
"placeholder_7": "annualized_time",
"placeholder_8": "fixed_rate",
"placeholder_9": "main",
"placeholder_10": "args",
"placeholder_11": "in_file",
"placeholder_12": "mode",
"placeholder_13": "out_file",
"placeholder_14": "records",
"placeholder_15": "record",
"placeholder_16": "parse_args",
"placeholder_17": "parser"
}
3 changes: 3 additions & 0 deletions test/example-parencontext-normalization/representation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 2
}
Loading
Loading