Skip to content

Commit 4e5744c

Browse files
committed
#41 add configurable default for string formatting
This changes the default way a name will be represented if it has a nickname, it will now include parenthesis around the nickname by default.
1 parent 2d267da commit 4e5744c

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

nameparser/config/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@ class Constants(object):
145145
:param regexes:
146146
:py:attr:`regexes` wrapped with :py:class:`TupleManager`.
147147
"""
148+
149+
string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
150+
"""
151+
The default string format use for all new HumanName instances.
152+
"""
153+
154+
148155
def __init__(self,
149156
prefixes=PREFIXES,
150157
suffixes=SUFFIXES,

nameparser/parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self, full_name="", constants=CONSTANTS, encoding=ENCODING,
6868
self.has_own_config = True
6969

7070
self.ENCODING = encoding
71-
self.string_format = string_format
71+
self.string_format = string_format or self.C.string_format
7272
self.full_name = full_name
7373

7474
def __iter__(self):

tests.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,12 +1758,18 @@ def test_downcasing_mc(self):
17581758

17591759
class HumanNameOutputFormatTests(HumanNameTestBase):
17601760

1761-
def test_formating(self):
1761+
def test_formatting_init_argument(self):
1762+
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)",
1763+
string_format = "TEST1")
1764+
self.assertEqual(u(hn), "TEST1")
1765+
1766+
def test_formatting_constants_attribute(self):
1767+
from nameparser.config import CONSTANTS
1768+
_orig = CONSTANTS.string_format
1769+
CONSTANTS.string_format = "TEST2"
17621770
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
1763-
hn.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
1764-
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III (Kenny)")
1765-
hn.string_format = "{last}, {title} {first} {middle}, {suffix} ({nickname})"
1766-
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth, III (Kenny)")
1771+
self.assertEqual(u(hn), "TEST2")
1772+
CONSTANTS.string_format = _orig
17671773

17681774
def test_quote_nickname_formating(self):
17691775
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
@@ -1821,6 +1827,13 @@ def test_formating_of_nicknames_with_double_quotes(self):
18211827
hn.nickname=''
18221828
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
18231829

1830+
def test_formating_of_nicknames_in_middle(self):
1831+
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
1832+
hn.string_format = "{title} {first} ({nickname}) {middle} {last} {suffix}"
1833+
self.assertEqual(u(hn), "Rev John (Kenny) A. Kenneth Doe III")
1834+
hn.nickname=''
1835+
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
1836+
18241837
TEST_NAMES = (
18251838
"John Doe",
18261839
"John Doe, Jr.",

0 commit comments

Comments
 (0)