Skip to content

Commit

Permalink
Merge pull request #233 from hiaselhans/resize_table
Browse files Browse the repository at this point in the history
dynamically resize the table matrix on set_value
  • Loading branch information
chfw committed Nov 6, 2020
2 parents 82be52b + e1c24a7 commit 9b54557
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
6 changes: 4 additions & 2 deletions README.rst
Expand Up @@ -5,7 +5,7 @@ pyexcel - Let you focus on data, instead of file formats
.. image:: https://raw.githubusercontent.com/pyexcel/pyexcel.github.io/master/images/patreon.png
:target: https://www.patreon.com/chfw

.. image:: https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg
.. image:: https://raw.githubusercontent.com/pyexcel/pyexcel-mobans/master/images/awesome-badge.svg
:target: https://awesome-python.com/#specific-formats-processing

.. image:: https://travis-ci.org/pyexcel/pyexcel.svg?branch=master
Expand All @@ -21,7 +21,7 @@ pyexcel - Let you focus on data, instead of file formats
:target: https://anaconda.org/conda-forge/pyexcel

.. image:: https://pepy.tech/badge/pyexcel/month
:target: https://pepy.tech/project/pyexcel/month
:target: https://pepy.tech/project/pyexcel

.. image:: https://anaconda.org/conda-forge/pyexcel/badges/downloads.svg
:target: https://anaconda.org/conda-forge/pyexcel
Expand Down Expand Up @@ -60,6 +60,8 @@ Known constraints

Fonts, colors and charts are not supported.

Nor to read password protected xls, xlsx and ods files.

Introduction
================================================================================

Expand Down
30 changes: 19 additions & 11 deletions pyexcel/internal/sheets/matrix.py
Expand Up @@ -82,18 +82,18 @@ def cell_value(self, row, column, new_value=None):
:param int column: column index which starts from 0
:param any new_value: new value if this is to set the value
"""
if row < self.number_of_rows() and column < self.number_of_columns():
if new_value is None:
# get
fit = row < self.number_of_rows() and column < self.number_of_columns()
if new_value is None:
if fit:
return self.__array[row][column]
else:
# set
self.__array[row][column] = new_value
else:
if new_value is None:
raise IndexError("Index out of range")
else:
self.paste((row, column), [[new_value]])
else:
if not fit:
width, array = uniform(self.__array, row+1, column+1)
self.__width, self.__array = width, array

self.__array[row][column] = new_value

def row_at(self, index):
"""
Expand Down Expand Up @@ -797,12 +797,17 @@ def longest_row_number(array):
return 0


def uniform(array):
def uniform(array, min_rows=0, min_columns=0):
"""Fill-in empty strings to empty cells to make it MxN
:param list in_array: a list of arrays
:param int row_no: desired minimum row count
:param int column_no: desired minimum column length
"""
width = longest_row_number(array)
width = max(min_columns, longest_row_number(array))
array_length = len(array)
height = max(array_length, min_rows)

if width == 0:
return 0, array
else:
Expand All @@ -813,6 +818,9 @@ def uniform(array):
row[index] = constants.DEFAULT_NA
if row_length < width:
row += [constants.DEFAULT_NA] * (width - row_length)
for _ in range(array_length, height):
row = [constants.DEFAULT_NA] * width
array.append(row)
return width, array


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -88,7 +88,7 @@
PUBLISH_COMMAND = "{0} setup.py sdist bdist_wheel upload -r pypi".format(sys.executable)
HERE = os.path.abspath(os.path.dirname(__file__))

GS_COMMAND = ("gs pyexcel v0.6.6 " +
GS_COMMAND = ("gease pyexcel v0.6.6 " +
"Find 0.6.6 in changelog for more details")
NO_GS_MESSAGE = ("Automatic github release is disabled. " +
"Please install gease to enable it.")
Expand Down
11 changes: 11 additions & 0 deletions tests/base.py
Expand Up @@ -3,6 +3,7 @@

import pyexcel as pe

import unittest
from nose.tools import eq_, raises


Expand Down Expand Up @@ -63,6 +64,16 @@ def create_sample_file2_in_memory(file_type):
return io


class PyexcelSheetBase(unittest.TestCase):
filename = "fixtures/non-uniform-rows.csv"

def setUp(self):
self.sheet = pe.Sheet([
[f"{row_no}_{col_no}" for col_no in range(10)]
for row_no in range(10)
])


class PyexcelBase:
def _write_test_file(self, filename):
"""
Expand Down
39 changes: 39 additions & 0 deletions tests/test_sheet_access.py
@@ -0,0 +1,39 @@
import random

from base import PyexcelSheetBase


class TestSheetAccess(PyexcelSheetBase):
@staticmethod
def get_random_char():
i = random.randint(97, 122)
return chr(i)

def test_out_of_bounds_write(self):
value = self.get_random_char()
column = self.get_random_char() + self.get_random_char()
cell = column + str(random.randint(1, 30))
self.sheet[cell] = value

self.assertEqual(value, self.sheet[cell])

def test_out_of_bounds_read(self):
with self.assertRaises(IndexError):
self.sheet[0, 20]

with self.assertRaises(IndexError):
self.sheet[20, 0]

def test_column_edge_case(self):
column = self.sheet.number_of_columns()-1

self.assertEqual(self.sheet[0, column], f"0_{column}")
with self.assertRaises(IndexError):
self.sheet[0, column+1]

def test_row_edge_case(self):
row = self.sheet.number_of_rows()-1

self.assertEqual(self.sheet[row, 0], f"{row}_0")
with self.assertRaises(IndexError):
self.sheet[row+1, 0]

0 comments on commit 9b54557

Please sign in to comment.