Skip to content

Commit

Permalink
BibAuthorID: fix Python-2.4 compatibility
Browse files Browse the repository at this point in the history
  * Added string_partition() to replace String.partition()
    which is not present in python 2.4
    (closes #462)
  • Loading branch information
WohthaN authored and tiborsimko committed Jan 28, 2011
1 parent 2e43f1f commit 106ba5f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
12 changes: 8 additions & 4 deletions modules/bibauthorid/lib/bibauthorid_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def test_split_name_parts(self):
baidu.split_name_parts('Strange+)*{ (=]&-$Char, Name'))

class TestCreateUnifiedNames(unittest.TestCase):
"""Test for the functionality of creation of unified names strings"""

def test_create_unified_name(self):
"""bibauthorid - test create unified name"""
"""bibauthorid - test creation of unified name strings"""

self.assertEqual('this, I. F. ',
baidu.create_unified_name('this, isa fullname'))
Expand All @@ -62,9 +63,10 @@ def test_create_unified_name(self):


class TestCreateNormalizedName(unittest.TestCase):
"""Test for the functionality of creation of normalized names strings"""

def test_create_normalized_name(self):
"""bibauthorid - test create normalized name"""
"""bibauthorid - test creation of normalized name strings"""

self.assertEqual('this, Isa Fullname',
baidu.create_normalized_name(
Expand All @@ -83,9 +85,10 @@ def test_create_normalized_name(self):
baidu.split_name_parts('')))

class TestCleanNameString(unittest.TestCase):
"""Test for the functionality of creation of cleaned names strings"""

def test_clean_name_string(self):
"""bibauthorid - test crean name string"""
"""bibauthorid - test cleaning of name strings"""

self.assertEqual('this is a full name',
baidu.clean_name_string('this is a full name'))
Expand All @@ -97,9 +100,10 @@ def test_clean_name_string(self):
baidu.clean_name_string(''))

class TestCompareNames(unittest.TestCase):
"""Test for the functionality of comparison of names strings"""

def test_compare_names(self):
"""bibauthorid - test compare names"""
"""bibauthorid - test names comparison funcions"""

self.assertEqual(0.94999999999999996,
bau.compare_names('Ellis, j.', 'Ellis, j.'))
Expand Down
20 changes: 15 additions & 5 deletions modules/bibauthorid/lib/bibauthorid_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
import bibauthorid_structs as dat


def string_partition(s, sep, dir='l'):
if dir=='r':
i = s.rfind(sep)
else:
i = s.find(sep)
if i < 0:
return (s,'','')
else:
return (s[0:i],s[i:i+1],s[i+1:])

def split_name_parts(name_string, delete_name_additions=True,
override_surname_sep=''):
'''
Expand Down Expand Up @@ -67,13 +77,13 @@ def split_name_parts(name_string, delete_name_additions=True,
for sep in surname_separators:
if name_string.count(sep) >= 1:
found_sep = sep
surname, rest_of_name = name_string.partition(sep)[0::2]
surname, rest_of_name = string_partition(name_string,sep)[0::2]
break
if not found_sep:
rest_of_name, surname = name_string.rpartition(' ')[0::2]
rest_of_name, surname = string_partition(name_string,' ',dir='r')[0::2]

if rest_of_name.count(","):
rest_of_name = rest_of_name.partition(",")[0]
rest_of_name = string_partition(rest_of_name,",")[0]

substitution_regexp = re.compile('[%s]' % (name_separators))
initials_names_list = substitution_regexp.sub(' ', rest_of_name).split()
Expand Down Expand Up @@ -120,10 +130,10 @@ def split_name_parts_old(name_string, delete_name_additions=True):
for name_addition in name_additions:
name_string = name_string.replace(name_addition, '')

surname, rest_of_name = name_string.partition(',')[0::2]
surname, rest_of_name = string_partition(name_string,',')[0::2]

if rest_of_name.count(","):
rest_of_name = rest_of_name.partition(",")[0]
rest_of_name = string_partition(rest_of_name,",")[0]

substitution_regexp = re.compile('[%s]' % (name_separators))
initials_names_list = substitution_regexp.sub(' ', rest_of_name).split()
Expand Down

0 comments on commit 106ba5f

Please sign in to comment.