Skip to content

Commit f05b09e

Browse files
dgunningclaude
andcommitted
Enrich FortyF API with named properties, fuzzy lookup, rich display, and 26 verification tests
Add 6 convenience properties (.risk_factors, .corporate_structure, .dividends, .capital_structure, .directors_and_officers, .legal_proceedings), make aif_text/aif_html public, add fuzzy keyword containment to __getitem__, and implement TenK-quality __rich__ display with NI 51-102 section tree. Includes input validation (TypeError for non-strings, None for empty keys) and a fix for get_structure() containment matching so variant headings like "Dividends And Distributions" correctly light up. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d9b5308 commit f05b09e

2 files changed

Lines changed: 366 additions & 17 deletions

File tree

‎edgar/company_reports/forty_f.py‎

Lines changed: 136 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
from functools import cached_property
44
from typing import List, Optional, Tuple
55

6+
from rich import box
67
from rich.console import Group, Text
8+
from rich.padding import Padding
79
from rich.panel import Panel
10+
from rich.tree import Tree
811

912
from edgar.company_reports._base import CompanyReport
13+
from edgar.display.formatting import datefmt
1014
from edgar.richtools import repr_rich
1115

1216
__all__ = ['FortyF']
@@ -154,7 +158,7 @@ def _find_aif_attachment(filing) -> Tuple[Optional[object], str]:
154158
]
155159

156160
_BUSINESS_ENDS = [
157-
r'GENERAL\s+DEVELOPMENT\s+OF\s+THE\s+BUSINESS',
161+
r'GENERAL\s+DEVELOPMENT\s+OF\s+(?:THE\s+)?(?:[\w\-][\w\-\'\u2019]*\s+)?BUSINESS',
158162
r'(?:THREE[\s-]YEAR|3[\s-]YEAR)\s+HISTORY',
159163
r'DESCRIPTION\s+OF\s+CAPITAL\s+STRUCTURE',
160164
r'MATERIAL\s+PROPERTIES',
@@ -168,7 +172,7 @@ def _find_aif_attachment(filing) -> Tuple[Optional[object], str]:
168172
# Section patterns for items detection (NI 51-102 AIF headings)
169173
_SECTION_PATTERNS = [
170174
r'CORPORATE\s+STRUCTURE',
171-
r'GENERAL\s+DEVELOPMENT\s+OF\s+THE\s+BUSINESS',
175+
r'GENERAL\s+DEVELOPMENT\s+OF\s+(?:THE\s+)?(?:[\w\-][\w\-\'\u2019]*\s+)?BUSINESS',
172176
r'(?:NARRATIVE\s+)?DESCRIPTION\s+OF\s+(?:THE\s+)?(?:\w[\w\'\u2019]*\s+)?BUSINESS(?:ES)?',
173177
r'BUSINESS\s+OF\s+(?:THE\s+)?(?:[\w][\w\'\u2019]*(?:\s+[\w][\w\'\u2019]*){0,3})',
174178
r'BUSINESS\s+OPERATIONS',
@@ -289,6 +293,7 @@ def _extract_business_section(full_text: str) -> Optional[str]:
289293
for idx, (_, name) in enumerate(positions):
290294
upper = name.upper()
291295
if ('DESCRIPTION' in upper and 'BUSINESS' in upper) or \
296+
('DEVELOPMENT' in upper and 'BUSINESS' in upper) or \
292297
upper.startswith('BUSINESS OF') or \
293298
upper.startswith('BUSINESS OVERVIEW') or \
294299
upper.startswith('BUSINESS OPERATIONS'):
@@ -349,8 +354,11 @@ def aif_attachment(self):
349354
return self._aif_result[0]
350355

351356
@cached_property
352-
def _aif_html(self) -> Optional[str]:
353-
"""Raw HTML of the AIF document (single download, shared by consumers)."""
357+
def aif_html(self) -> Optional[str]:
358+
"""Raw HTML of the AIF document.
359+
360+
Use this to render the AIF in a web UI or convert to other formats.
361+
"""
354362
att = self.aif_attachment
355363
if att is None:
356364
return None
@@ -360,7 +368,7 @@ def _aif_html(self) -> Optional[str]:
360368
@cached_property
361369
def aif_document(self):
362370
"""Parsed ``Document`` from the AIF exhibit HTML."""
363-
html = self._aif_html
371+
html = self.aif_html
364372
if not html:
365373
return None
366374
from edgar.documents import HTMLParser, ParserConfig
@@ -389,9 +397,12 @@ def document(self):
389397
# -- Business section ----------------------------------------------------
390398

391399
@cached_property
392-
def _aif_text(self) -> Optional[str]:
393-
"""Plain text of the AIF document (cached for reuse)."""
394-
html = self._aif_html
400+
def aif_text(self) -> Optional[str]:
401+
"""Full plain text of the AIF document.
402+
403+
Use this to feed the AIF into an LLM context window or text analysis pipeline.
404+
"""
405+
html = self.aif_html
395406
if not html:
396407
return None
397408
try:
@@ -406,17 +417,49 @@ def _aif_text(self) -> Optional[str]:
406417
@cached_property
407418
def business(self) -> Optional[str]:
408419
"""Business description from the AIF 'Description of the Business' section."""
409-
text = self._aif_text
420+
text = self.aif_text
410421
if text is None:
411422
return None
412423
return _extract_business_section(text)
413424

425+
# -- Named section properties (NI 51-102 high-frequency sections) --------
426+
427+
@property
428+
def risk_factors(self) -> Optional[str]:
429+
"""Risk Factors section from the AIF."""
430+
return self['Risk Factors']
431+
432+
@property
433+
def corporate_structure(self) -> Optional[str]:
434+
"""Corporate Structure section from the AIF."""
435+
return self['Corporate Structure']
436+
437+
@property
438+
def dividends(self) -> Optional[str]:
439+
"""Dividends section from the AIF."""
440+
return self['Dividends']
441+
442+
@property
443+
def capital_structure(self) -> Optional[str]:
444+
"""Description of Capital Structure section from the AIF."""
445+
return self['Description Of Capital Structure']
446+
447+
@property
448+
def directors_and_officers(self) -> Optional[str]:
449+
"""Directors and Officers section from the AIF."""
450+
return self['Directors And Officers']
451+
452+
@property
453+
def legal_proceedings(self) -> Optional[str]:
454+
"""Legal Proceedings section from the AIF."""
455+
return self['Legal Proceedings']
456+
414457
# -- Section listing / lookup --------------------------------------------
415458

416459
@cached_property
417460
def _section_positions(self) -> List[Tuple[int, str]]:
418461
"""Detected sections as (start_position, Title Case name) pairs."""
419-
text = self._aif_text
462+
text = self.aif_text
420463
if text is None:
421464
return []
422465
positions = _find_section_positions(text)
@@ -440,33 +483,109 @@ def __getitem__(self, key: str) -> Optional[str]:
440483
forty_f["Description Of The Business"]
441484
forty_f["dividends"] # case-insensitive
442485
"""
443-
text = self._aif_text
486+
if not isinstance(key, str):
487+
raise TypeError(f"Section key must be a string, got {type(key).__name__}")
488+
489+
text = self.aif_text
444490
positions = self._section_positions
445491
if not text or not positions:
446492
return None
447493

448494
key_lower = key.lower().strip()
495+
if not key_lower:
496+
return None
497+
498+
# Exact case-insensitive match
449499
for idx, (_, name) in enumerate(positions):
450500
if name.lower() == key_lower:
451501
return _extract_section_text(text, positions, idx)
452502

503+
# Keyword containment: "business" matches "Description Of The Business"
504+
for idx, (_, name) in enumerate(positions):
505+
if key_lower in name.lower():
506+
return _extract_section_text(text, positions, idx)
507+
453508
return None
454509

455510
# -- Display -------------------------------------------------------------
456511

457512
def __str__(self):
458513
return f"FortyF('{self.company}')"
459514

515+
def get_structure(self):
516+
"""Build a tree showing detected AIF sections."""
517+
tree = Tree("📄 ")
518+
# NI 51-102 expected sections (static reference)
519+
expected = [
520+
"Corporate Structure",
521+
"General Development Of The Business",
522+
"Description Of The Business",
523+
"Risk Factors",
524+
"Dividends",
525+
"Description Of Capital Structure",
526+
"Market For Securities",
527+
"Directors And Officers",
528+
"Legal Proceedings",
529+
]
530+
detected_lower = [name.lower() for name in self.items]
531+
532+
def _matches_detected(section_lower: str) -> bool:
533+
"""Check if an expected section matches any detected section (containment)."""
534+
for d in detected_lower:
535+
if section_lower == d or section_lower in d or d in section_lower:
536+
return True
537+
return False
538+
539+
def _matches_expected(name_lower: str) -> bool:
540+
"""Check if a detected section matches any expected section (containment)."""
541+
for s in expected:
542+
sl = s.lower()
543+
if name_lower == sl or sl in name_lower or name_lower in sl:
544+
return True
545+
return False
546+
547+
for section in expected:
548+
if _matches_detected(section.lower()):
549+
tree.add(Text(section, style="bold green"))
550+
else:
551+
tree.add(Text(section, style="dim"))
552+
553+
# Also show any detected sections not in the expected list
554+
for name in self.items:
555+
if not _matches_expected(name.lower()):
556+
tree.add(Text.assemble(
557+
(name, "bold green"),
558+
(" *", "dim"),
559+
))
560+
return tree
561+
460562
def __rich__(self):
461-
aif_reason = self._aif_result[1]
462-
aif_info = Text(f"AIF: {aif_reason}", style="dim")
463-
return Panel(
563+
title = Text.assemble(
564+
(f"{self.company}", "bold deep_sky_blue1"),
565+
(" ", ""),
566+
(f"{self.form}", "bold"),
567+
)
568+
periods = Text.assemble(
569+
("Period ending ", "grey70"),
570+
(f"{datefmt(self.period_of_report, '%B %d, %Y')}", "bold"),
571+
(" filed on ", "grey70"),
572+
(f"{datefmt(self.filing_date, '%B %d, %Y')}", "bold"),
573+
)
574+
aif_info = Text(f"AIF: {self._aif_result[1]}", style="dim")
575+
576+
panel = Panel(
464577
Group(
465-
self._filing.__rich__(),
578+
periods,
466579
aif_info,
467-
self.financials or Text("No financial data available"),
468-
)
580+
Padding(" ", (1, 0, 0, 0)),
581+
self.get_structure(),
582+
Padding(" ", (1, 0, 0, 0)),
583+
self.financials or Text("No financial data available", style="italic"),
584+
),
585+
title=title,
586+
box=box.ROUNDED,
469587
)
588+
return panel
470589

471590
def __repr__(self):
472591
return repr_rich(self.__rich__())

0 commit comments

Comments
 (0)