Skip to content

Commit

Permalink
expose rounding type
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowbean committed Jul 4, 2023
1 parent 48662f3 commit 917b1c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
18 changes: 13 additions & 5 deletions absbox/local/component.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from absbox.local.util import mkTag, DC, mkTs, guess_locale, readTagStr, subMap, subMap2, renameKs, ensure100, mapListValBy, uplift_m_list, mapValsBy, allList, getValWithKs
from absbox.local.util import mkTag, DC, mkTs, guess_locale, readTagStr, subMap, subMap2, renameKs, ensure100, mapListValBy, uplift_m_list, mapValsBy, allList, getValWithKs, applyFnToKey
from absbox.local.base import *
from enum import Enum
import itertools
Expand Down Expand Up @@ -678,19 +678,28 @@ def mkWaterfall(r, x):
r[_w_tag] = [mkAction(_a) for _a in _v]
return mkWaterfall(r, x)

def mkRoundingType(x):
match x:
case ["floor",r]:
return mkTag(("RoundFloor",r))
case ["ceiling",r]:
return mkTag(("RoundCeil",r))
case _:
raise RuntimeError(f"Failed to match {x}:mkRoundingType")

def mkAssetRate(x):
match x:
case ["固定", r] | ["fix", r]:
return mkTag(("Fix", r))
case ["浮动", r, {"基准": idx, "利差": spd, "重置频率": p}]:
return mkTag(("Floater", [idx, spd, r, freqMap[p], None]))
case ["floater", r, {"index": idx, "spread": spd, "reset": p}]:
return mkTag(("Floater2", [idx, spd, r, mkDatePattern(p)]))
case ["floater", r, {"index": idx, "spread": spd, "reset": p} as m]:
_m = subMap(m,[("cap",None),("floor",None),("rounding",None)])
_m = applyFnToKey(_m, mkRoundingType, 'rounding')
return mkTag(("Floater2", [idx, spd, r, mkDatePattern(p), _m['floor'], _m['cap'],_m['rounding']]))
case _:
raise RuntimeError(f"Failed to match {x}:mkAssetRate")


def mkAmortPlan(x) -> dict:
match x:
case "等额本息" | "Level" | "level":
Expand Down Expand Up @@ -727,7 +736,6 @@ def mkAssetStatus(x):
raise RuntimeError(f"Failed to match asset statuts {x}:mkAssetStatus")



def mkAsset(x):
match x:
case ["AdjustRateMortgage", {"originBalance": originBalance, "originRate": originRate, "originTerm": originTerm, "freq": freq, "type": _type, "originDate": startDate, "arm": arm}
Expand Down
15 changes: 14 additions & 1 deletion absbox/local/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def renameKs(m:dict,mapping,opt_key=False):
return m

def subMap(m:dict,ks:list):
fieldNames = [ fName for (fName,fDefaultValue) in ks]
''' get a map subset by keys,if keys not found, supplied with default value '''
return {k:m.get(k,defaultVal) for (k,defaultVal) in ks}

def subMap2(m:dict,ks:list):
Expand All @@ -224,6 +224,18 @@ def mapListValBy(m:dict, f):
assert isinstance(m, dict),"M is not a map"
return {k: [f(_v) for _v in v] for k,v in m.items()}

def applyFnToKey(m:dict, f, k, applyNone=False):
assert isinstance(m, dict),f"{m} is not a map"
assert k in m, f"{k} is not in map {m}"
match (m[k],applyNone):
case (None,True):
m[k] = f(m[k])
case (None,False):
pass
case (_, _):
m[k] = f(m[k])
return m

def renameKs2(m:dict,kmapping):
assert isinstance(m, dict),"M is not a map"
assert isinstance(kmapping, dict),f"Mapping is not a map: {kmapping}"
Expand Down Expand Up @@ -257,6 +269,7 @@ def uplift_m_list(l:list):
for k,v in m.items()}

def getValWithKs(m:dict,ks:list):
''' Get first available key/value in m'''
if isinstance(m, dict):
for k in ks:
if k in m:
Expand Down

0 comments on commit 917b1c9

Please sign in to comment.