Skip to content

Commit 73e6934

Browse files
committed
document as_dict() method
1 parent 5b205d0 commit 73e6934

4 files changed

Lines changed: 23 additions & 33 deletions

File tree

docs/customize.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ Parser Customization Examples
4242
+++++++++++++++++++++++++++++
4343

4444
"Hon" is a common abbreviation for "Honorable", a title used when
45-
addressing judges, and is included in the default tiles constants which
46-
means it will never be considered a first name.
45+
addressing judges, and is included in the default tiles constants. This
46+
means it will never be considered a first name, because titles are the
47+
pieces before first names.
4748

4849
But "Hon" is also sometimes a first name. If your dataset contains more
4950
"Hon"s than "Honorable"s, you may wish to remove it from the titles

docs/usage.rst

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Example
55
-------
66

77
.. doctest::
8-
:options: -ELLIPSIS, +NORMALIZE_WHITESPACE
8+
:options: +NORMALIZE_WHITESPACE
99

1010
>>> from nameparser import HumanName
1111
>>> name = HumanName("Dr. Juan Q. Xavier de la Vega III")
@@ -19,30 +19,16 @@ Example
1919
u'de la Vega'
2020
>>> name.suffix
2121
u'III'
22-
>>> name.full_name = 'Doe-Ray, Col. Jonathan "John" A. Harris III'
23-
>>> name.title
24-
u'Col.'
25-
>>> name.first
26-
u'Jonathan'
27-
>>> name.middle
28-
u'A. Harris'
29-
>>> name.last
30-
u'Doe-Ray'
31-
>>> name.suffix
32-
u'III'
33-
>>> name.nickname
34-
u'John'
3522
>>> name.full_name = "Juan Q. Xavier Velasquez y Garcia, Jr."
36-
>>> name.title
37-
u''
38-
>>> name.first
39-
u'Juan'
40-
>>> name.middle
41-
u'Q. Xavier'
42-
>>> name.last
43-
u'Velasquez y Garcia'
44-
>>> name.suffix
45-
u'Jr.'
23+
>>> name
24+
<HumanName : [
25+
title: ''
26+
first: 'Juan'
27+
middle: 'Q. Xavier'
28+
last: 'Velasquez y Garcia'
29+
suffix: 'Jr.'
30+
nickname: ''
31+
]>
4632
>>> name.middle = "Jason Alexander"
4733
>>> name.middle
4834
u'Jason Alexander'
@@ -55,6 +41,9 @@ Example
5541
suffix: 'Jr.'
5642
nickname: ''
5743
]>
44+
>>> name.full_name = 'Doe-Ray, Col. Jonathan "John" A. Harris III'
45+
>>> name.as_dict()
46+
{u'last': u'Doe-Ray', u'suffix': u'III', u'title': u'Col.', u'middle': u'A. Harris', u'nickname': u'John', u'first': u'Jonathan'}
5847
>>> name = HumanName("Dr. Juan Q. Xavier de la Vega III")
5948
>>> name2 = HumanName("de la vega, dr. juan Q. xavier III")
6049
>>> name == name2

nameparser/parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __next__(self):
115115
def __unicode__(self):
116116
if self.string_format:
117117
# string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
118-
return self.string_format.format(**self._dict)
118+
return self.string_format.format(**self.as_dict())
119119
return " ".join(self)
120120

121121
def __str__(self):
@@ -134,13 +134,13 @@ def __repr__(self):
134134
'nickname': self.nickname,
135135
}
136136

137-
@property
138-
def _dict(self):
137+
def as_dict(self):
138+
"""Return the parsed name as a dictionary of its attributes."""
139139
d = {}
140140
for m in self._members:
141141
d[m] = getattr(self, m)
142142
return d
143-
143+
144144
### attributes
145145

146146
@property
@@ -310,7 +310,7 @@ def handle_firstnames(self):
310310
and len(self) == 2 \
311311
and not lc(self.title) in self.C.first_name_titles:
312312
self.last, self.first = self.first, self.last
313-
313+
314314
def parse_full_name(self):
315315
"""
316316
The main parse method for the parser. This method is run upon assignment to the

tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,8 +1705,8 @@ def test_variations_of_TEST_NAMES(self):
17051705
for name in self.TEST_NAMES:
17061706
hn = HumanName(name)
17071707
if len(hn.suffix_list) > 1:
1708-
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn._dict).split(',')[0])
1709-
hn_dict = hn._dict.copy()
1708+
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn.as_dict()).split(',')[0])
1709+
hn_dict = hn.as_dict()
17101710
attrs = [
17111711
'title',
17121712
'first',

0 commit comments

Comments
 (0)