Skip to content

Commit af87f77

Browse files
hitesh-taranicodereverser
authored andcommitted
Updating logic for parsing scheme names for detailed CAS reports, handling erstwhile and non-demat suffixes
1 parent 541beba commit af87f77

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

casparser/process/cas_detailed.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def get_transaction_type(
118118
return txn_type, dividend_rate
119119

120120

121+
def get_parsed_scheme_name(scheme) -> str:
122+
scheme = re.sub(r"\((formerly|erstwhile).+?\)", "", scheme, flags=re.I | re.DOTALL).strip()
123+
scheme = re.sub(r"\((Demat|Non-Demat).*", "", scheme, flags=re.I | re.DOTALL).strip()
124+
scheme = re.sub(r"\s+", " ", scheme).strip()
125+
return re.sub(r"[^a-zA-Z0-9_)]+$", "", scheme).strip()
126+
127+
121128
def parse_transaction(line) -> Optional[ParsedTransaction]:
122129
for regex in (TRANSACTION_RE1, TRANSACTION_RE2, TRANSACTION_RE3):
123130
if m := re.search(regex, line, re.DOTALL | re.MULTILINE | re.I):
@@ -189,9 +196,7 @@ def process_detailed_text(text):
189196
elif m := re.search(SCHEME_RE, line, re.DOTALL | re.MULTILINE | re.I):
190197
if current_folio is None:
191198
raise CASParseError("Layout Error! Scheme found before folio entry.")
192-
scheme = re.sub(r"\(formerly.+?\)", "", m.group("name"), flags=re.I | re.DOTALL).strip()
193-
scheme = re.sub(r"\s+", " ", scheme).strip()
194-
scheme = re.sub(r"\W+$", "", scheme).strip()
199+
scheme = get_parsed_scheme_name(m.group("name"))
195200
if curr_scheme_data is None or curr_scheme_data.scheme != scheme:
196201
if curr_scheme_data:
197202
folios[current_folio].schemes.append(curr_scheme_data)

tests/test_process.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from casparser.exceptions import CASParseError, HeaderParseError
66
from casparser.process import process_cas_text
7-
from casparser.process.cas_detailed import parse_header, get_transaction_type
7+
from casparser.process.cas_detailed import parse_header, get_transaction_type, get_parsed_scheme_name
88
from casparser.process.cas_detailed import ParsedTransaction, parse_transaction
99
from casparser.process.cas_summary import parse_header as parse_summary_header
1010
from casparser.process.utils import isin_search
@@ -94,6 +94,23 @@ def test_dividend_transactions(self):
9494
Decimal("0.0241"),
9595
)
9696

97+
def test_parsed_scheme_name(self):
98+
assert get_parsed_scheme_name(
99+
"Axis Long Term Equity Fund - Direct Growth") == "Axis Long Term Equity Fund - Direct Growth"
100+
assert get_parsed_scheme_name(
101+
"Axis Bluechip Fund - Regular Growth ") == "Axis Bluechip Fund - Regular Growth"
102+
assert get_parsed_scheme_name(
103+
"HSBC Corporate Bond Fund - Regular Growth (Formerly known as L&T Triple Ace Bond Fund - Growth)") == \
104+
"HSBC Corporate Bond Fund - Regular Growth"
105+
assert get_parsed_scheme_name(
106+
"Bandhan ELSS Tax saver Fund-Growth-(Regular Plan)"
107+
"(erstwhile Bandhan Tax Advantage ELSS Fund-Growth-Regular Plan)") == \
108+
"Bandhan ELSS Tax saver Fund-Growth-(Regular Plan)"
109+
assert get_parsed_scheme_name(
110+
"Bandhan Liquid Fund-Growth-(Regular Plan) (erstwhile IDFC Cash Fund-Growth-Regular Plan) (Non-Demat) ") == \
111+
"Bandhan Liquid Fund-Growth-(Regular Plan)"
112+
113+
97114
def test_isin_search(self):
98115
isin, amfi, scheme_type = isin_search(
99116
"Axis Long Term Equity Fund - Direct Growth", "KFINTECH", "128TSDGG"

0 commit comments

Comments
 (0)