Skip to content

Commit

Permalink
v0.2.20
Browse files Browse the repository at this point in the history
  • Loading branch information
yh202109 committed Jul 13, 2024
1 parent 2f8df86 commit da50f49
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
7 changes: 1 addition & 6 deletions docs/example_ectd2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,6 @@
"To include the upper CTOC sections that include search results:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -207,7 +202,7 @@
},
"outputs": [],
"source": [
"print(\"\\n\".join(ctoc.find_section_given_words([\"REMS\",\"isE\"], outfmt='tree')))"
"print(\"\\n\".join(ctoc.find_section_given_words(words=[\"REMS\",\"isE\"], outfmt='tree')))"
]
},
{
Expand Down
11 changes: 8 additions & 3 deletions mtbp3/health/ectd.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ def show_ctoc_tree(self, module=None, to_right=False):
return tree.list_tree(to_right=to_right)

def find_section_given_words(self, words, outfmt='simple', include='up', to_right=False):
if not isinstance(words, str) or not words:
raise ValueError("str must be a nonempty string")
if isinstance(words, str) and words:
words = [words]
elif isinstance(words, list) and len(words)>0:
assert all(isinstance(word, str) for word in words), "Elements in the list must be strings"
else:
raise ValueError("words must be a string or a list")

if include not in ['up', 'both']:
raise ValueError("Invalid value for include. Supported values are 'up' and 'both'.")

out = [row for row in self.ctoc if words.lower() in row.lower()]
out = [row for row in self.ctoc if any(word.lower() in row.lower() for word in words)]
if outfmt == 'simple':
return out
elif outfmt == 'tree':
Expand Down
32 changes: 32 additions & 0 deletions tests/test_health_ectd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import unittest
from mtbp3.health.ectd import ctoc_by_fda

class TestCtocByFDA(unittest.TestCase):

def setUp(self):
self.ctoc = ctoc_by_fda(ectd_version="3.2.2", ctoc_version="2.3.3")

def test_load_list(self):
self.assertIsInstance(self.ctoc.ctoc, list)
self.assertGreater(len(self.ctoc.ctoc), 0)

def test_show_ctoc_tree(self):
tree = self.ctoc.show_ctoc_tree(module=1, to_right=False)
self.assertIsInstance(tree[0], str)
self.assertGreater(len(tree), 0)

def test_find_section_given_words(self):
result = self.ctoc.find_section_given_words("REMS", outfmt='simple', include='up', to_right=False)
self.assertIsInstance(result, list)
self.assertGreater(len(result), 0)

def test_find_section_given_words_invalid_include(self):
with self.assertRaises(ValueError):
self.ctoc.find_section_given_words("REMS", outfmt='simple', include='invalid', to_right=False)

def test_find_section_given_words_invalid_outfmt(self):
with self.assertRaises(ValueError):
self.ctoc.find_section_given_words("REMS", outfmt='invalid', include='up', to_right=False)

if __name__ == "__main__":
unittest.main()
File renamed without changes.

0 comments on commit da50f49

Please sign in to comment.