Skip to content

Commit

Permalink
Merge b75996d into 8ee782e
Browse files Browse the repository at this point in the history
  • Loading branch information
blais committed Oct 6, 2021
2 parents 8ee782e + b75996d commit 1665fb1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
20 changes: 10 additions & 10 deletions petl/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import operator


from petl.compat import text_type, binary_type, numeric_types
from petl import compat


class Comparable(object):
Expand Down Expand Up @@ -40,17 +40,17 @@ def __lt__(self, other):
return True

# numbers < everything else (except None)
if isinstance(obj, numeric_types) \
and not isinstance(other, numeric_types):
if isinstance(obj, compat.numeric_types) \
and not isinstance(other, compat.numeric_types):
return True
if not isinstance(obj, numeric_types) \
and isinstance(other, numeric_types):
if not isinstance(obj, compat.numeric_types) \
and isinstance(other, compat.numeric_types):
return False

# binary < unicode
if isinstance(obj, text_type) and isinstance(other, binary_type):
if isinstance(obj, compat.text_type) and isinstance(other, compat.binary_type):
return False
if isinstance(obj, binary_type) and isinstance(other, text_type):
if isinstance(obj, compat.binary_type) and isinstance(other, compat.text_type):
return True

try:
Expand Down Expand Up @@ -79,7 +79,7 @@ def __str__(self):
return str(self.obj)

def __unicode__(self):
return text_type(self.obj)
return compat.text_type(self.obj)

def __repr__(self):
return 'Comparable(' + repr(self.obj) + ')'
Expand All @@ -96,9 +96,9 @@ def __getitem__(self, item):

def _typestr(x):
# attempt to preserve Python 2 name orderings
if isinstance(x, binary_type):
if isinstance(x, compat.binary_type):
return 'str'
if isinstance(x, text_type):
if isinstance(x, compat.text_type):
return 'unicode'
return type(x).__name__

Expand Down
6 changes: 4 additions & 2 deletions petl/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
if PY2:
from itertools import ifilter, ifilterfalse, imap, izip, izip_longest
from string import maketrans
from decimal import Decimal
string_types = basestring,
integer_types = int, long
numeric_types = bool, int, long, float
numeric_types = bool, int, long, float, Decimal
text_type = unicode
binary_type = str
from urllib2 import urlopen
Expand All @@ -40,13 +41,14 @@
imap = map
izip = zip
xrange = range
from decimal import Decimal
from itertools import filterfalse as ifilterfalse
from itertools import zip_longest as izip_longest
from functools import reduce
maketrans = str.maketrans
string_types = str,
integer_types = int,
numeric_types = bool, int, float
numeric_types = bool, int, float, Decimal
class_types = type,
text_type = str
binary_type = bytes
Expand Down
18 changes: 11 additions & 7 deletions petl/io/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# standard library dependencies
import io
from petl.compat import text_type, numeric_types, next, PY2, izip_longest, \
string_types, callable
from petl import compat
from petl.compat import next, PY2, izip_longest, string_types, callable


# internal dependencies
Expand All @@ -16,7 +16,7 @@


def tohtml(table, source=None, encoding=None, errors='strict', caption=None,
vrepr=text_type, lineterminator='\n', index_header=False,
vrepr=None, lineterminator='\n', index_header=False,
tr_style=None, td_styles=None, truncate=None):
"""
Write the table as HTML to a file. E.g.::
Expand Down Expand Up @@ -56,6 +56,8 @@ def tohtml(table, source=None, encoding=None, errors='strict', caption=None,
in the output HTML.
"""
if vrepr is None:
vrepr = compat.text_type

source = write_source_from_arg(source)
with source.open('wb') as buf:
Expand Down Expand Up @@ -100,13 +102,15 @@ def tohtml(table, source=None, encoding=None, errors='strict', caption=None,


def teehtml(table, source=None, encoding=None, errors='strict', caption=None,
vrepr=text_type, lineterminator='\n', index_header=False,
vrepr=None, lineterminator='\n', index_header=False,
tr_style=None, td_styles=None, truncate=None):
"""
Return a table that writes rows to a Unicode HTML file as they are
iterated over.
"""
if vrepr is None:
vrepr = compat.text_type

source = write_source_from_arg(source)
return TeeHTMLView(table, source=source, encoding=encoding, errors=errors,
Expand All @@ -121,15 +125,15 @@ def teehtml(table, source=None, encoding=None, errors='strict', caption=None,

class TeeHTMLView(Table):
def __init__(self, table, source=None, encoding=None, errors='strict',
caption=None, vrepr=text_type, lineterminator='\n',
caption=None, vrepr=None, lineterminator='\n',
index_header=False, tr_style=None, td_styles=None,
truncate=None):
self.table = table
self.source = source
self.encoding = encoding
self.errors = errors
self.caption = caption
self.vrepr = vrepr
self.vrepr = compat.text_type if vrepr is None else vrepr
self.lineterminator = lineterminator
self.index_header = index_header
self.tr_style = tr_style
Expand Down Expand Up @@ -260,7 +264,7 @@ def _get_td_css(h, v, td_styles):
raise ArgumentError('expected string, callable or dict, got %r'
% td_styles)
# fall back to default style
if isinstance(v, numeric_types) and not isinstance(v, bool):
if isinstance(v, compat.numeric_types) and not isinstance(v, bool):
return 'text-align: right'
else:
return ''
Expand Down
5 changes: 3 additions & 2 deletions petl/test/test_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


from datetime import datetime
from decimal import Decimal


from petl.test.helpers import eq_
Expand Down Expand Up @@ -29,9 +30,9 @@ def test_comparable():
eq_(e, a)

# mixed numeric
d = [3., 1, 2.5]
d = [3., 1, 2.5, Decimal('1.5')]
a = sorted(d, key=Comparable)
e = [1, 2.5, 3.]
e = [1, Decimal('1.5'), 2.5, 3.]
eq_(e, a)

# mixed numeric and bool
Expand Down
16 changes: 8 additions & 8 deletions petl/util/vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import locale
from itertools import islice
from collections import defaultdict
from petl.compat import numeric_types, text_type
from petl import compat


from petl import config
Expand Down Expand Up @@ -195,7 +195,7 @@ def _look_grid(table, vrepr, index_header, truncate, width):

# fields representation
hdr = next(it)
flds = list(map(text_type, hdr))
flds = list(map(compat.text_type, hdr))
if index_header:
fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)]
else:
Expand Down Expand Up @@ -267,7 +267,7 @@ def _look_grid(table, vrepr, index_header, truncate, width):
rowline = '|'
for i, w in enumerate(colwidths):
vr = valsrepr[i]
if i < len(vals) and isinstance(vals[i], numeric_types) \
if i < len(vals) and isinstance(vals[i], compat.numeric_types) \
and not isinstance(vals[i], bool):
# left pad numbers
rowline += ' ' * (w + 1 - len(vr)) # padding
Expand Down Expand Up @@ -295,7 +295,7 @@ def _look_simple(table, vrepr, index_header, truncate, width):

# fields representation
hdr = next(it)
flds = list(map(text_type, hdr))
flds = list(map(compat.text_type, hdr))
if index_header:
fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)]
else:
Expand Down Expand Up @@ -350,7 +350,7 @@ def _look_simple(table, vrepr, index_header, truncate, width):
rowline = ''
for i, w in enumerate(colwidths):
vr = valsrepr[i]
if i < len(vals) and isinstance(vals[i], numeric_types) \
if i < len(vals) and isinstance(vals[i], compat.numeric_types) \
and not isinstance(vals[i], bool):
# left pad numbers
rowline += vr.rjust(w)
Expand Down Expand Up @@ -378,7 +378,7 @@ def _look_minimal(table, vrepr, index_header, truncate, width):

# fields representation
hdr = next(it)
flds = list(map(text_type, hdr))
flds = list(map(compat.text_type, hdr))
if index_header:
fldsrepr = ['%s|%s' % (i, r) for (i, r) in enumerate(flds)]
else:
Expand Down Expand Up @@ -427,7 +427,7 @@ def _look_minimal(table, vrepr, index_header, truncate, width):
rowline = ''
for i, w in enumerate(colwidths):
vr = valsrepr[i]
if i < len(vals) and isinstance(vals[i], numeric_types) \
if i < len(vals) and isinstance(vals[i], compat.numeric_types) \
and not isinstance(vals[i], bool):
# left pad numbers
rowline += vr.rjust(w)
Expand Down Expand Up @@ -551,7 +551,7 @@ def _display_html(table, limit=0, vrepr=None, index_header=None, caption=None,
tohtml(table, buf, encoding=encoding, index_header=index_header,
vrepr=vrepr, caption=caption, tr_style=tr_style,
td_styles=td_styles, truncate=truncate)
output = text_type(buf.getvalue(), encoding)
output = compat.text_type(buf.getvalue(), encoding)

if epilogue:
output += '<p>%s</p>' % epilogue
Expand Down

0 comments on commit 1665fb1

Please sign in to comment.