Skip to content

Commit 8928e1e

Browse files
committed
Use a mixin to define __str__ in terms of __unicode__
1 parent 4185440 commit 8928e1e

7 files changed

Lines changed: 27 additions & 44 deletions

File tree

python/buildmetadatafromxml.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
# Pull in the data structure definitions
4646
from phonenumbers.phonemetadata import NumberFormat, PhoneNumberDesc, PhoneMetadata
47+
from phonenumbers.util import UnicodeMixin
4748

4849
# Convention: variables beginning with 'x' are XML objects
4950

@@ -131,7 +132,7 @@ def _expand_formatting_rule(rule, national_prefix):
131132
return rule
132133

133134

134-
class XNumberFormat(object):
135+
class XNumberFormat(UnicodeMixin):
135136
"""Parsed NumberFormat objects from XML element"""
136137
def __init__(self, owning_xterr, xtag, national_prefix, national_prefix_formatting_rule, carrier_code_formatting_rule):
137138
if xtag is None:
@@ -200,14 +201,11 @@ def __init__(self, owning_xterr, xtag, national_prefix, national_prefix_formatti
200201
# Add this international NumberFormat object into the owning metadata
201202
owning_xterr.o.intl_number_format.append(self.io)
202203

203-
def __str__(self):
204-
return unicode(self).encode('utf-8')
205-
206204
def __unicode__(self):
207205
return unicode(self.o)
208206

209207

210-
class XPhoneNumberDesc(object):
208+
class XPhoneNumberDesc(UnicodeMixin):
211209
"""Parse PhoneNumberDesc object from XML element"""
212210
def __init__(self, xtag,
213211
template=None, fill_na=True):
@@ -237,14 +235,11 @@ def __init__(self, xtag,
237235
if example_number is not None:
238236
self.o.example_number = example_number
239237

240-
def __str__(self):
241-
return unicode(self).encode('utf-8')
242-
243238
def __unicode__(self):
244239
return unicode(self.o)
245240

246241

247-
class XTerritory(object):
242+
class XTerritory(UnicodeMixin):
248243
"""Parse PhoneMetadata from XML element (territory)"""
249244
def __init__(self, xterritory):
250245
# Retrieve the REQUIRED attributes
@@ -342,14 +337,11 @@ def __init__(self, xterritory):
342337
# national formats.
343338
self.o.intl_number_format = []
344339

345-
def __str__(self):
346-
return unicode(self).encode('utf-8')
347-
348340
def __unicode__(self):
349341
return unicode(self.o)
350342

351343

352-
class XPhoneNumberMetadata(object):
344+
class XPhoneNumberMetadata(UnicodeMixin):
353345
"""Entire collection of phone number metadata retrieved from XML"""
354346
def __init__(self, filename):
355347
# Load the XML data from the given filename
@@ -369,9 +361,6 @@ def __init__(self, filename):
369361
else:
370362
raise Exception("Unexpected element %s found" % xterritory.tag)
371363

372-
def __str__(self):
373-
return unicode(self).encode('utf-8')
374-
375364
def __unicode__(self):
376365
return u'\n'.join([u"%s: %s" % (country_id, territory) for country_id, territory in self.territory.items()])
377366

python/phonenumbers/phonemetadata.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
19+
from util import UnicodeMixin
1920

2021

21-
class NumberFormat(object):
22+
class NumberFormat(UnicodeMixin):
2223
"""Representation of way that a phone number can be formatted for output"""
2324
def __init__(self,
2425
pattern=None,
@@ -107,9 +108,6 @@ def __ne__(self, other):
107108
def __repr__(self):
108109
return str(self)
109110

110-
def __str__(self):
111-
return unicode(self).encode('utf-8')
112-
113111
def __unicode__(self):
114112
# Generate a string that is valid Python input for the constructor.
115113
# Note that we use %r, which generates its own quotes.
@@ -125,7 +123,7 @@ def __unicode__(self):
125123
return result
126124

127125

128-
class PhoneNumberDesc(object):
126+
class PhoneNumberDesc(UnicodeMixin):
129127
"""Class representing the description of a set of phone numbers."""
130128

131129
def __init__(self,
@@ -170,9 +168,6 @@ def __ne__(self, other):
170168
def __repr__(self):
171169
return str(self)
172170

173-
def __str__(self):
174-
return unicode(self).encode('utf-8')
175-
176171
def __unicode__(self):
177172
# Generate a string that is valid Python input for constructor
178173
result = u"PhoneNumberDesc("
@@ -190,7 +185,7 @@ def __unicode__(self):
190185
return result
191186

192187

193-
class PhoneMetadata(object):
188+
class PhoneMetadata(UnicodeMixin):
194189
"""Class representing metadata for international telephone numbers for a region.
195190
196191
This class is hand created based on phonemetadata.proto. Please refer to that file
@@ -382,9 +377,6 @@ def __ne__(self, other):
382377
def __repr__(self):
383378
return str(self)
384379

385-
def __str__(self):
386-
return unicode(self).encode('utf-8')
387-
388380
def __unicode__(self):
389381
# Generate a string that is valid Python input for the constructor
390382
country_code = self.country_code

python/phonenumbers/phonenumber.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1717
# See the License for the specific language governing permissions and
1818
# limitations under the License.
19+
from util import UnicodeMixin
1920

2021

2122
class CountryCodeSource(object):
@@ -44,7 +45,7 @@ class CountryCodeSource(object):
4445
FROM_DEFAULT_COUNTRY = 20
4546

4647

47-
class PhoneNumber(object):
48+
class PhoneNumber(UnicodeMixin):
4849
"""Class representing international telephone numbers.
4950
5051
This class is hand-created based on phonenumber.proto. Please refer
@@ -188,9 +189,6 @@ def __repr__(self):
188189
self.country_code_source,
189190
self.preferred_domestic_carrier_code))
190191

191-
def __str__(self):
192-
return unicode(self).encode('utf-8')
193-
194192
def __unicode__(self):
195193
result = ("Country Code: %s National Number: %s" %
196194
(self.country_code, self.national_number))

python/phonenumbers/phonenumbermatcher.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
# Extra regexp function; see README
2323
from re_util import fullmatch
24+
from util import UnicodeMixin
2425
import unicode_util
2526
import phonenumberutil
2627

@@ -546,7 +547,7 @@ def _parse_and_verify(self, candidate, offset):
546547
return None
547548

548549

549-
class PhoneNumberMatch(object):
550+
class PhoneNumberMatch(UnicodeMixin):
550551
"""The immutable match of a phone number within a piece of text.
551552
552553
Matches may be found using the find() method of PhoneNumberMatcher.
@@ -604,8 +605,5 @@ def __repr__(self):
604605
self.raw_string,
605606
self.number))
606607

607-
def __str__(self):
608-
return unicode(self).encode('utf-8')
609-
610608
def __unicode__(self):
611609
return u"PhoneNumberMatch [%s,%s) %s" % (self.start, self.end, self.raw_string)

python/phonenumbers/phonenumberutil.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import re
3232

3333
from re_util import fullmatch # Extra regexp function; see README
34+
from util import UnicodeMixin
3435
import unicode_util
3536

3637
# Data class definitions
@@ -2331,7 +2332,7 @@ def _can_be_internationally_dialled(numobj):
23312332
return not _is_number_matching_desc(nsn, metadata.no_international_dialling)
23322333

23332334

2334-
class NumberParseException(Exception):
2335+
class NumberParseException(UnicodeMixin, Exception):
23352336
"""Exception when attempting to parse a putative phone number"""
23362337
# Invalid country code specified
23372338
INVALID_COUNTRY_CODE = 0
@@ -2358,8 +2359,5 @@ def __init__(self, error_type, msg):
23582359
self.error_type = error_type
23592360
self._msg = msg
23602361

2361-
def __str__(self):
2362-
return unicode(self).encode('utf-8')
2363-
23642362
def __unicode__(self):
23652363
return u"(%s) %s" % (self.error_type, self._msg)

python/phonenumbers/unicode_util.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
import bisect
6666
import unicodedata # Python 2.5 onward
6767

68+
from util import UnicodeMixin
69+
6870

6971
class Category(object):
7072
"""General category of a Unicode character.
@@ -126,7 +128,7 @@ def is_letter(unichr):
126128
category == Category.OTHER_LETTER)
127129

128130

129-
class _BlockRange(object):
131+
class _BlockRange(UnicodeMixin):
130132
"""Describe the range of characters encompassed by a Unicode block"""
131133
def __init__(self, start, end, regdict=None):
132134
self.start = start
@@ -143,9 +145,6 @@ def __ne__(self, other):
143145
def __hash__(self):
144146
return hash((self.start, self.end))
145147

146-
def __str__(self):
147-
return unicode(self).encode('utf-8')
148-
149148
def __unicode__(self):
150149
return u"Block[%04x, %04x]" % (self.start, self.end)
151150

python/phonenumbers/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import sys
2+
3+
4+
class UnicodeMixin(object): # pragma no cover
5+
"""Define __str__ operator in terms of __unicode__ for Python 2/3"""
6+
if sys.version_info > (3, 0):
7+
__str__ = lambda x: x.__unicode__()
8+
else:
9+
__str__ = lambda x: unicode(x).encode('utf-8')

0 commit comments

Comments
 (0)