Skip to content

Commit 1fedd4e

Browse files
committed
Merge pull request #45 from derek73/44_none_default
remove default attribute from formatted string (#44)
2 parents 0f5ef38 + 29c76a5 commit 1fedd4e

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

nameparser/parser.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def __unicode__(self):
119119
# string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
120120
_s = self.string_format.format(**self.as_dict())
121121
# remove trailing punctation from missing nicknames
122-
_s = _s.replace(" ()","").replace(" ''","").replace(' ""',"")
122+
_s = _s.replace(str(self.C.empty_attribute_default),'').replace(" ()","").replace(" ''","").replace(' ""',"")
123123
return self.collapse_whitespace(_s).strip(', ')
124124
return " ".join(self)
125125

@@ -231,8 +231,10 @@ def _set_list(self, attr, value):
231231
val = value
232232
elif isinstance(value, text_types):
233233
val = [value]
234+
elif value is None:
235+
val = []
234236
else:
235-
raise TypeError("Can only assign strings and lists to name attributes. "
237+
raise TypeError("Can only assign strings, lists or None to name attributes. "
236238
"Got {0}".format(type(value)))
237239
setattr(self, attr+"_list", self.parse_pieces(val))
238240

tests.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
class HumanNameTestBase(unittest.TestCase):
3939
def m(self, actual, expected, hn):
40-
"""assertEquals with a better message"""
40+
"""assertEquals with a better message and awareness of hn.C.empty_attribute_default"""
41+
expected = expected or hn.C.empty_attribute_default
4142
try:
4243
self.assertEqual(actual, expected, "'%s' != '%s' for '%s'\n%r" % (
4344
actual,
@@ -146,7 +147,7 @@ def test_comparison_case_insensitive(self):
146147
def test_slice(self):
147148
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
148149
self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn)
149-
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC',''], hn)
150+
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC',hn.C.empty_attribute_default], hn)
150151
self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn)
151152

152153
def test_getitem(self):
@@ -1274,6 +1275,20 @@ def test_empty_attribute_default(self):
12741275
self.m(hn.nickname, None, hn)
12751276
CONSTANTS.empty_attribute_default = _orig
12761277

1278+
def test_empty_attribute_on_instance(self):
1279+
hn = HumanName("", None)
1280+
hn.C.empty_attribute_default = None
1281+
self.m(hn.title, None, hn)
1282+
self.m(hn.first, None, hn)
1283+
self.m(hn.middle, None, hn)
1284+
self.m(hn.last, None, hn)
1285+
self.m(hn.suffix, None, hn)
1286+
self.m(hn.nickname, None, hn)
1287+
1288+
def test_none_empty_attribute_string_formatting(self):
1289+
hn = HumanName("", None)
1290+
hn.C.empty_attribute_default = None
1291+
self.assertEqual('', str(hn), hn)
12771292

12781293
class HumanNameNicknameTestCase(HumanNameTestBase):
12791294
# https://code.google.com/p/python-nameparser/issues/detail?id=33
@@ -2036,6 +2051,7 @@ def test_variations_of_TEST_NAMES(self):
20362051
hn = HumanName(name)
20372052
if len(hn.suffix_list) > 1:
20382053
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn.as_dict()).split(',')[0])
2054+
hn.C.empty_attribute_default = '' # format strings below require empty string
20392055
hn_dict = hn.as_dict()
20402056
attrs = [
20412057
'title',
@@ -2045,9 +2061,6 @@ def test_variations_of_TEST_NAMES(self):
20452061
'suffix',
20462062
'nickname',
20472063
]
2048-
for attr in attrs:
2049-
if not getattr(hn, attr):
2050-
setattr(hn,attr,'')
20512064
nocomma = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn_dict))
20522065
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix}".format(**hn_dict))
20532066
if hn.suffix:
@@ -2076,11 +2089,11 @@ def test_variations_of_TEST_NAMES(self):
20762089
hn.capitalize()
20772090
print((repr(hn)))
20782091
else:
2079-
# if log.level > 0:
2080-
# for name in TEST_NAMES:
2081-
# hn = HumanName(name)
2082-
# print((u(name)))
2083-
# print((u(hn)))
2084-
# print((repr(hn)))
2085-
# print("\n-------------------------------------------\n")
2092+
print("-"*80)
2093+
print("Running tests")
2094+
unittest.main(exit=False)
2095+
print("-"*80)
2096+
print("Running tests with empty_attribute_default = None")
2097+
from nameparser.config import CONSTANTS
2098+
CONSTANTS.empty_attribute_default = None
20862099
unittest.main()

0 commit comments

Comments
 (0)