Skip to content

Commit

Permalink
#1 Added utils function trim_name
Browse files Browse the repository at this point in the history
  • Loading branch information
zembrodt committed Nov 25, 2019
1 parent 0d37a00 commit cb147fc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/modules/utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ split_by_br
-----------
.. autofunction:: split_by_br

trim_name
---------
.. autofunction:: trim_name

trim_year
---------
.. autofunction:: trim_year
Expand Down
17 changes: 17 additions & 0 deletions pymdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ def get_episode_info(node):
return episode_count, episode_year_start, episode_year_end


def trim_name(name):
"""Used to trim roman numerals from names.
IMDb differentiates people's names that are the same with the format:
`<name> (<Roman numeral>)`. This function removes the roman numerals
and returns only the name.
Args:
name (:obj:`str`): The name and roman numeral combination.
Returns:
:obj:`str`: The name with roman numerals removed, or `None` if name was `None`.
"""

return re.sub(r'\s*\(\w+\)', '', name) if name is not None else None


def trim_year(year):
"""Used to trim roman numerals from year values.
Expand Down
18 changes: 18 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,24 @@ def test_get_category(self):
self.assertEqual(get_ref_marker(node), correct_result)


class TestTrimName(unittest.TestCase):
def test_trim_name_none(self):
self.assertIsNone(trim_name(None))

def test_trim_name_no_roman_numerals(self):
name = 'Rob Schneider'
correct_result = 'Rob Schneider'
self.assertEqual(trim_name(name), correct_result)

def test_trim_name_roman_numerals(self):
name1 = 'Rob Schneider (I)'
name2 = 'Rob Schneider (IV)'
name3 = 'Rob Schneider (VII)'
correct_result = 'Rob Schneider'
self.assertEqual(trim_name(name1), correct_result)
self.assertEqual(trim_name(name2), correct_result)
self.assertEqual(trim_name(name3), correct_result)

class TestTrimYear(unittest.TestCase):
def test_trim_year_None(self):
self.assertIsNone(trim_year(None))
Expand Down

0 comments on commit cb147fc

Please sign in to comment.