Skip to content

Commit

Permalink
Merge pull request #130 from fls-bioinformatics-core/simple_xls-fixes…
Browse files Browse the repository at this point in the history
…-for-python3-compatibility

bcftbx/simple_xls: fixes for Python2/3 compatibility
  • Loading branch information
pjbriggs committed Sep 13, 2019
2 parents a717bfa + 865a85d commit 7ddca5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
34 changes: 22 additions & 12 deletions bcftbx/simple_xls.py
Expand Up @@ -122,6 +122,7 @@
import xlsxwriter
from . import Spreadsheet
from .utils import OrderedDictionary
from builtins import range

#######################################################################
# Constants
Expand Down Expand Up @@ -261,7 +262,7 @@ def save_as_xlsx(self,filen):
for col in ColumnRange(start.column,end.column):
# Maximum column width
max_width = default_min_col_width
for row in xrange(start.row,end.row+1):
for row in range(start.row,end.row+1):
# Get the value
value = worksheet.render_cell(cell(col,row),
eval_formulae=False,
Expand Down Expand Up @@ -407,7 +408,7 @@ def __setitem__(self,idx,value):
self.data[idx.idx] = value
if idx.column not in self.columns:
self.columns.append(idx.column)
self.columns.sort(cmp=cmp_column_indices)
self.columns = sorted(self.columns,key=lambda x: x[::-1])
if idx.row not in self.rows:
self.rows.append(idx.row)
self.rows.sort()
Expand Down Expand Up @@ -660,7 +661,7 @@ def write_column(self,col,data=None,text=None,fill=None,from_row=None,style=None
self.set_style(style,cell(col,row))
row += 1
# Sort the column and row indices
self.columns.sort(cmp=cmp_column_indices)
self.columns = sorted(self.columns,key=lambda x: x[::-1])
self.rows.sort()

def insert_column_data(self,col,data,start=None,style=None):
Expand Down Expand Up @@ -846,7 +847,7 @@ def write_row(self,row,data=None,text=None,fill=None,from_column=None,style=None
self.set_style(style,cell(col,row))
col = incr_col(col)
# Sort the column and row indices
self.columns.sort(cmp=cmp_column_indices)
self.columns = sorted(self.columns,key=lambda x: x[::-1])
self.rows.sort()

def insert_row_data(self,row,data,start=None,style=None):
Expand Down Expand Up @@ -955,7 +956,7 @@ def fill_column(self,column,item,start=None,end=None,style=None):
j = self.last_row
else:
j = int(end)
for row in xrange(i,j+1):
for row in range(i,j+1):
self[cell(column,row)] = item
if style is not None:
self.set_style(style,cell(column,row))
Expand Down Expand Up @@ -986,7 +987,7 @@ def set_style(self,cell_style,start,end=None):
start_cell = CellIndex(start)
end_cell = CellIndex(end)
for col in ColumnRange(start_cell.column,end_cell.column):
for row in xrange(start_cell.row,end_cell.row+1):
for row in range(start_cell.row,end_cell.row+1):
self.styles[cell(col,row)] = cell_style

def get_style(self,idx):
Expand Down Expand Up @@ -1109,7 +1110,7 @@ def render_as_text(self,include_columns_and_rows=False,
for col in ColumnRange(start.column,end.column):
line.append(col)
text.append('\t'.join(line))
for row in xrange(start.row,end.row+1):
for row in range(start.row,end.row+1):
line = []
if include_columns_and_rows:
line.append(u'%s' % row)
Expand Down Expand Up @@ -1170,6 +1171,9 @@ def __init__(self,bold=False,color=None,bgcolor=None,wrap=False,
self.shrink_to_fit = shrink_to_fit

def __nonzero__(self):
return self.__bool__()

def __bool__(self):
return \
(self.bold) or \
(self.color is not None) or \
Expand Down Expand Up @@ -1313,7 +1317,13 @@ def __init__(self,i,j=None,include_end=True,reverse=False):
self.end += self.incr

def next(self):
"""Implements Iterator subclass 'next' method
"""Implements Iterator subclass 'next' method (Python 2 only)
"""
return self.__next__()

def __next__(self):
"""Implements Iterator subclass '__next__' method
"""
self.column = self.column + self.incr
Expand Down Expand Up @@ -1438,7 +1448,7 @@ def cmp_column_indices(x,y):
"""
# Do string comparision on reverse of column indices
return cmp(x[::-1],y[::-1])
return (x[::-1] > y[::-1]) - (x[::-1] < y[::-1])

def cell(col,row):
"""Return XLS cell index for column and row
Expand Down Expand Up @@ -1489,7 +1499,7 @@ def column_integer_to_index(idx):
col = ''
while idx >= 0:
col += chr((idx%26)+65)
idx = idx/26-1
idx = idx//26-1
return col[::-1]

def eval_formula(item,worksheet):
Expand Down Expand Up @@ -1616,7 +1626,7 @@ def format_value(value,number_format=None):
value = []
while i >= 1000:
value.append("%03d" % (i%1000))
i = i/1000
i = i//1000
value.append(str(i))
value = value[::-1]
return ','.join(value)
Expand Down Expand Up @@ -1689,7 +1699,7 @@ def format_value(value,number_format=None):
style=XLSStyle(color='white',
bgcolor='green',
bold=True))
for i in xrange(100):
for i in range(100):
ws.append_row(data=(i,i*2,'=A?+B?'))
ws.freeze_panes = 'A2'
# Save out to XLS(X) files
Expand Down
49 changes: 26 additions & 23 deletions bcftbx/test/test_simple_xls.py
Expand Up @@ -3,9 +3,14 @@
#######################################################################
from bcftbx.simple_xls import *
import unittest
import itertools
import os
import tempfile
try:
# Python 2
from itertools import izip as zip
except ImportError:
pass
from builtins import range

class TestXLSWorkBook(unittest.TestCase):
"""
Expand Down Expand Up @@ -204,13 +209,13 @@ def test_write_column_with_list(self):
ws.write_column('B',data=col_data)
self.assertEqual(ws.last_column,'B')
self.assertEqual(ws.last_row,3)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
exp_cell = ['C3','C4','C5']
ws.write_column('C',data=col_data,from_row=3)
self.assertEqual(ws.last_column,'C')
self.assertEqual(ws.last_row,5)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
def test_write_column_with_text(self):
ws = self.ws
Expand All @@ -219,14 +224,14 @@ def test_write_column_with_text(self):
ws.write_column('A',text="hello\ngoodbye\nwhatev")
self.assertEqual(ws.last_column,'A')
self.assertEqual(ws.last_row,3)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
exp_cell = ['M7','M8','M9']
ws.write_column('M',text="hello\ngoodbye\nwhatev",
from_row=7)
self.assertEqual(ws.last_column,'M')
self.assertEqual(ws.last_row,9)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
def test_write_column_with_fill(self):
ws = self.ws
Expand All @@ -248,11 +253,11 @@ def test_insert_column_data(self):
col_data = ['hello','goodbye','whatev']
exp_cell = ['B1','B2','B3']
ws.insert_column_data('B',col_data)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
exp_cell = ['C3','C4','C5']
ws.insert_column_data('C',col_data,start=3)
for i in xrange(3):
for i in range(3):
self.assertEqual(ws[exp_cell[i]],col_data[i])
def test_insert_row(self):
ws = self.ws
Expand Down Expand Up @@ -302,13 +307,13 @@ def test_write_row_with_list(self):
ws.write_row(4,data=row_data)
self.assertEqual(ws.last_column,'D')
self.assertEqual(ws.last_row,4)
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
exp_cell = ['E5','F5','G5','H5']
ws.write_row(5,data=row_data,from_column='E')
self.assertEqual(ws.last_column,'H')
self.assertEqual(ws.last_row,5)
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
def test_write_row_with_text(self):
ws = self.ws
Expand All @@ -318,24 +323,24 @@ def test_write_row_with_text(self):
ws.write_row(4,text=row_text)
self.assertEqual(ws.last_column,'D')
self.assertEqual(ws.last_row,4)
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
exp_cell = ['E5','F5','G5','H5']
ws.write_row(5,text=row_text,from_column='E')
self.assertEqual(ws.last_column,'H')
self.assertEqual(ws.last_row,5)
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
def test_insert_row_data(self):
ws = self.ws
row_data = ['Dozy','Beaky','Mick','Titch']
exp_cell = ['A4','B4','C4','D4']
ws.insert_row_data(4,row_data)
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
exp_cell = ['E5','F5','G5','H5']
ws.insert_row_data(5,row_data,start='E')
for i in xrange(4):
for i in range(4):
self.assertEqual(ws[exp_cell[i]],row_data[i])
def test_insert_block_data(self):
ws = self.ws
Expand Down Expand Up @@ -475,22 +480,20 @@ class TestColumnRange(unittest.TestCase):
"""
"""
def test_column_range(self):
for expected,actual in itertools.izip(['A','B','C'],
ColumnRange('A','C')):
for expected,actual in zip(['A','B','C'],
ColumnRange('A','C')):
self.assertEqual(expected,actual)
def test_column_range_implicit_start(self):
for expected,actual in itertools.izip(['A','B','C'],
ColumnRange('C')):
for expected,actual in zip(['A','B','C'],
ColumnRange('C')):
self.assertEqual(expected,actual)
def test_column_range_dont_include_end(self):
for expected,actual in itertools.izip(['A','B','C'],
ColumnRange('A','D',
include_end=False)):
for expected,actual in zip(['A','B','C'],
ColumnRange('A','D',include_end=False)):
self.assertEqual(expected,actual)
def test_column_range_reverse(self):
for expected,actual in itertools.izip(['C','B','A'],
ColumnRange('A','C',
reverse=True)):
for expected,actual in zip(['C','B','A'],
ColumnRange('A','C',reverse=True)):
self.assertEqual(expected,actual)

class TestXLSStyle(unittest.TestCase):
Expand Down

0 comments on commit 7ddca5d

Please sign in to comment.