Skip to content

Commit 96f072b

Browse files
committed
remove default attribute from formatted string (#44)
1 parent 0f5ef38 commit 96f072b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,20 @@ def test_empty_attribute_default(self):
12741274
self.m(hn.nickname, None, hn)
12751275
CONSTANTS.empty_attribute_default = _orig
12761276

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

12781292
class HumanNameNicknameTestCase(HumanNameTestBase):
12791293
# https://code.google.com/p/python-nameparser/issues/detail?id=33

0 commit comments

Comments
 (0)