Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions nameparser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __unicode__(self):
# string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
_s = self.string_format.format(**self.as_dict())
# remove trailing punctation from missing nicknames
_s = _s.replace(" ()","").replace(" ''","").replace(' ""',"")
_s = _s.replace(str(self.C.empty_attribute_default),'').replace(" ()","").replace(" ''","").replace(' ""',"")
return self.collapse_whitespace(_s).strip(', ')
return " ".join(self)

Expand Down Expand Up @@ -231,8 +231,10 @@ def _set_list(self, attr, value):
val = value
elif isinstance(value, text_types):
val = [value]
elif value is None:
val = []
else:
raise TypeError("Can only assign strings and lists to name attributes. "
raise TypeError("Can only assign strings, lists or None to name attributes. "
"Got {0}".format(type(value)))
setattr(self, attr+"_list", self.parse_pieces(val))

Expand Down
37 changes: 25 additions & 12 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@

class HumanNameTestBase(unittest.TestCase):
def m(self, actual, expected, hn):
"""assertEquals with a better message"""
"""assertEquals with a better message and awareness of hn.C.empty_attribute_default"""
expected = expected or hn.C.empty_attribute_default
try:
self.assertEqual(actual, expected, "'%s' != '%s' for '%s'\n%r" % (
actual,
Expand Down Expand Up @@ -146,7 +147,7 @@ def test_comparison_case_insensitive(self):
def test_slice(self):
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn)
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC',''], hn)
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC',hn.C.empty_attribute_default], hn)
self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn)

def test_getitem(self):
Expand Down Expand Up @@ -1274,6 +1275,20 @@ def test_empty_attribute_default(self):
self.m(hn.nickname, None, hn)
CONSTANTS.empty_attribute_default = _orig

def test_empty_attribute_on_instance(self):
hn = HumanName("", None)
hn.C.empty_attribute_default = None
self.m(hn.title, None, hn)
self.m(hn.first, None, hn)
self.m(hn.middle, None, hn)
self.m(hn.last, None, hn)
self.m(hn.suffix, None, hn)
self.m(hn.nickname, None, hn)

def test_none_empty_attribute_string_formatting(self):
hn = HumanName("", None)
hn.C.empty_attribute_default = None
self.assertEqual('', str(hn), hn)

class HumanNameNicknameTestCase(HumanNameTestBase):
# https://code.google.com/p/python-nameparser/issues/detail?id=33
Expand Down Expand Up @@ -2036,6 +2051,7 @@ def test_variations_of_TEST_NAMES(self):
hn = HumanName(name)
if len(hn.suffix_list) > 1:
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn.as_dict()).split(',')[0])
hn.C.empty_attribute_default = '' # format strings below require empty string
hn_dict = hn.as_dict()
attrs = [
'title',
Expand All @@ -2045,9 +2061,6 @@ def test_variations_of_TEST_NAMES(self):
'suffix',
'nickname',
]
for attr in attrs:
if not getattr(hn, attr):
setattr(hn,attr,'')
nocomma = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn_dict))
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix}".format(**hn_dict))
if hn.suffix:
Expand Down Expand Up @@ -2076,11 +2089,11 @@ def test_variations_of_TEST_NAMES(self):
hn.capitalize()
print((repr(hn)))
else:
# if log.level > 0:
# for name in TEST_NAMES:
# hn = HumanName(name)
# print((u(name)))
# print((u(hn)))
# print((repr(hn)))
# print("\n-------------------------------------------\n")
print("-"*80)
print("Running tests")
unittest.main(exit=False)
print("-"*80)
print("Running tests with empty_attribute_default = None")
from nameparser.config import CONSTANTS
CONSTANTS.empty_attribute_default = None
unittest.main()