Skip to content

Commit 0033f39

Browse files
committed
add support for name parts in the constructor, fix #140
1 parent 3147993 commit 0033f39

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

docs/release_log.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
Release Log
22
===========
3+
* 1.1.2 - November 13, 2022
4+
- Add support for attributes in constructor (#140)
35
* 1.1.1 - January 28, 2022
46
- Fix bug in is_suffix handling of lists (#129)
57
* 1.1.0 - January 3, 2022

nameparser/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = (1, 1, 1)
1+
VERSION = (1, 1, 2)
22
__version__ = '.'.join(map(str, VERSION))
33
__author__ = "Derek Gulbranson"
44
__author_email__ = '[email protected]'

nameparser/parser.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ class HumanName(object):
3636
3737
Instantiation assigns to ``full_name``, and assignment to
3838
:py:attr:`full_name` triggers :py:func:`parse_full_name`. After parsing the
39-
name, these instance attributes are available.
39+
name, these instance attributes are available. Alternatively, you can pass
40+
any of the instance attributes to the constructor method and skip the parsing
41+
process. If any of the the instance attributes are passed to the constructor
42+
as keywords, :py:func:`parse_full_name` will not be performed.
4043
4144
**HumanName Instance Attributes**
4245
@@ -56,6 +59,12 @@ class HumanName(object):
5659
:param str string_format: python string formatting
5760
:param str initials_format: python initials string formatting
5861
:param str initials_delimter: string delimiter for initials
62+
:param str first: first name
63+
:param str middle: middle name
64+
:param str last: last name
65+
:param str title: The title or prenominal
66+
:param str suffix: The suffix or postnominal
67+
:param str nickname: Nicknames
5968
"""
6069

6170
C = CONSTANTS
@@ -77,7 +86,9 @@ class HumanName(object):
7786
_full_name = ''
7887

7988
def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING,
80-
string_format=None, initials_format=None, initials_delimiter=None):
89+
string_format=None, initials_format=None, initials_delimiter=None,
90+
first=None, middle=None, last=None, title=None, suffix=None,
91+
nickname=None):
8192
self.C = constants
8293
if type(self.C) is not type(CONSTANTS):
8394
self.C = Constants()
@@ -86,8 +97,17 @@ def __init__(self, full_name="", constants=CONSTANTS, encoding=DEFAULT_ENCODING,
8697
self.string_format = string_format or self.C.string_format
8798
self.initials_format = initials_format or self.C.initials_format
8899
self.initials_delimiter = initials_delimiter or self.C.initials_delimiter
89-
# full_name setter triggers the parse
90-
self.full_name = full_name
100+
if (first or middle or last or title or suffix or nickname):
101+
self.first = first
102+
self.middle = middle
103+
self.last = last
104+
self.title = title
105+
self.suffix = suffix
106+
self.nickname = nickname
107+
self.unparsable = False
108+
else:
109+
# full_name setter triggers the parse
110+
self.full_name = full_name
91111

92112
def __iter__(self):
93113
return self

tests.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2343,7 +2343,44 @@ def test_initials_with_prefix_firstname(self):
23432343
def test_initials_with_prefix(self):
23442344
hn = HumanName("Alex van Johnson")
23452345
self.m(hn.initials_list(), ["A", "J"], hn)
2346-
2346+
2347+
def test_constructor_first(self):
2348+
hn = HumanName(first="TheName")
2349+
self.assertFalse(hn.unparsable)
2350+
self.m(hn.first, "TheName", hn)
2351+
2352+
def test_constructor_middle(self):
2353+
hn = HumanName(middle="TheName")
2354+
self.assertFalse(hn.unparsable)
2355+
self.m(hn.middle, "TheName", hn)
2356+
2357+
def test_constructor_last(self):
2358+
hn = HumanName(last="TheName")
2359+
self.assertFalse(hn.unparsable)
2360+
self.m(hn.last, "TheName", hn)
2361+
2362+
def test_constructor_title(self):
2363+
hn = HumanName(title="TheName")
2364+
self.assertFalse(hn.unparsable)
2365+
self.m(hn.title, "TheName", hn)
2366+
2367+
def test_constructor_suffix(self):
2368+
hn = HumanName(suffix="TheName")
2369+
self.assertFalse(hn.unparsable)
2370+
self.m(hn.suffix, "TheName", hn)
2371+
2372+
def test_constructor_nickname(self):
2373+
hn = HumanName(nickname="TheName")
2374+
self.assertFalse(hn.unparsable)
2375+
self.m(hn.nickname, "TheName", hn)
2376+
2377+
def test_constructor_multiple(self):
2378+
hn = HumanName(first="TheName", last="lastname", title="mytitle", full_name="donotparse")
2379+
self.assertFalse(hn.unparsable)
2380+
self.m(hn.first, "TheName", hn)
2381+
self.m(hn.last, "lastname", hn)
2382+
self.m(hn.title, "mytitle", hn)
2383+
23472384

23482385
TEST_NAMES = (
23492386
"John Doe",

0 commit comments

Comments
 (0)