Skip to content

Commit 96c349a

Browse files
committed
make HumanName instances pickleable
1 parent 9f11f41 commit 96c349a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

dev-requirements.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
ipdb==0.8
2-
nose==1.3.6
1+
ipdb==0.8.1
2+
nose==1.3.7
33
Sphinx==1.3.1
44
coverage==3.7.1
5-
ipython==3.1.0
5+
ipython==4.0.0
66
Pygments==2.0.2
7+
dill==0.2.4

nameparser/config/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ def __getattr__(self, attr):
108108
__setattr__= dict.__setitem__
109109
__delattr__= dict.__delitem__
110110

111+
def __getstate__(self):
112+
return self
113+
114+
def __setstate__(self, state):
115+
self = dict(state)
116+
111117

112118
class Constants(object):
113119
"""
@@ -153,6 +159,13 @@ def suffixes_prefixes_titles(self):
153159

154160
def __repr__(self):
155161
return "<Constants() instance>"
162+
163+
def __setstate__(self, state):
164+
self.__init__(state)
165+
166+
def __getstate__(self):
167+
attrs = [x for x in dir(self) if not x.startswith('_')]
168+
return dict([(a,getattr(self, a)) for a in attrs])
156169

157170
#: A module-level instance of the :py:class:`Constants()` class.
158171
#: Provides a common instance for the module to share

tests.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import logging
22+
import dill
2223

2324
from nameparser import HumanName
2425
from nameparser.util import u
@@ -72,6 +73,14 @@ def test_len(self):
7273
hn = HumanName("John Doe")
7374
self.m(len(hn), 2, hn)
7475

76+
def test_config_pickle(self):
77+
C = Constants()
78+
self.assertTrue(dill.pickles(C))
79+
80+
def test_name_instance_pickle(self):
81+
hn = HumanName("First Last")
82+
self.assertTrue(dill.pickles(hn))
83+
7584
def test_comparison(self):
7685
hn1 = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
7786
hn2 = HumanName("Dr. John P. Doe-Ray, CLU, CFP, LUTC")

0 commit comments

Comments
 (0)