Skip to content

Commit

Permalink
Merge pull request #262 from hiaselhans/py310
Browse files Browse the repository at this point in the history
cleanup setup.py & tests
  • Loading branch information
chfw committed Jul 6, 2022
2 parents 4c96909 + ce691a3 commit d8b9650
Show file tree
Hide file tree
Showing 28 changed files with 196 additions and 223 deletions.
4 changes: 2 additions & 2 deletions pyexcel/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '0.7.0'
__author__ = 'C.W.'
__version__ = "0.7.0"
__author__ = "C.W."
6 changes: 1 addition & 5 deletions pyexcel/docstrings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"""
from . import keywords


__GET_ARRAY__ = (
keywords.SOURCE_PARAMS_TABLE
+ """
Expand All @@ -18,10 +17,7 @@
+ keywords.SOURCE_PARAMS
)

__GET_SHEET__ = (
keywords.EXAMPLE_NOTE_PAGINATION
+ __GET_ARRAY__
)
__GET_SHEET__ = keywords.EXAMPLE_NOTE_PAGINATION + __GET_ARRAY__

__GET_BOOK__ = (
keywords.SOURCE_BOOK_PARAMS_TABLE
Expand Down
56 changes: 23 additions & 33 deletions pyexcel/internal/sheets/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""
import re
import types
from typing import Tuple
from functools import partial

from pyexcel._compact import PY2
Expand Down Expand Up @@ -71,47 +72,36 @@ def analyse_slice(aslice, upper_bound):
return my_range


def excel_column_index(index_chars):
"""translate MS excel column position to index"""
if len(index_chars) < 1:
return -1
else:
return _get_index(index_chars.upper())

def excel_cell_position(pos_chars: str) -> Tuple[int, int]:
"""
translate MS excel position to index
Return: (row: int, column: int)
"""
match = re.match("([A-Za-z]+)([0-9]+)", pos_chars)

def excel_cell_position(pos_chars):
"""translate MS excel position to index"""
if len(pos_chars) < 2:
return -1, -1
group = re.match("([A-Za-z]+)([0-9]+)", pos_chars)
if group:
return int(group.group(2)) - 1, excel_column_index(group.group(1))
if match:
return int(match.group(2)) - 1, excel_column_index(match.group(1))
else:
raise IndexError
raise KeyError(f"invalid index: {pos_chars}")


"""
In order to easily compute the actual index of 'X' or 'AX', these utility
functions were written
"""
_INDICES = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"


def _get_index(index_chars):
length = len(index_chars)
index_chars_length = len(_INDICES)
if length > 1:
index = 0
for i in range(0, length):
if i < (length - 1):
index += (_INDICES.index(index_chars[i]) + 1) * (
index_chars_length ** (length - 1 - i)
)
else:
index += _INDICES.index(index_chars[i])
return index
else:
return _INDICES.index(index_chars[0])
INDEX_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
INDEX_BASE = len(INDEX_CHARS)


def excel_column_index(index_chars: str) -> int:
index = -1
for i, char in enumerate(index_chars.upper()[::-1]):
# going from right to left, the multiplicator is:
# 26^0 = 1
# 26^1 = 26
index += (1 + INDEX_CHARS.index(char)) * INDEX_BASE**i

return index


def names_to_indices(names, series):
Expand Down
35 changes: 19 additions & 16 deletions pyexcel/internal/sheets/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
import copy
import types
from typing import Tuple, Union
from functools import partial
from itertools import chain

Expand All @@ -23,6 +24,8 @@

from . import _shared as utils

MatrixIndex = Union[Tuple[int, int], str]


class Matrix(SheetMeta):
"""The internal representation of a sheet data. Each element
Expand Down Expand Up @@ -460,29 +463,29 @@ def delete_columns(self, column_indices):
del self.__array[i][j]
self.__width = longest_row_number(self.__array)

def __setitem__(self, aset, cell_value):
def __setitem__(self, index: MatrixIndex, cell_value):
"""Override the operator to set items"""
if isinstance(aset, tuple):
return self.cell_value(aset[0], aset[1], cell_value)
elif isinstance(aset, str):
row, column = utils.excel_cell_position(aset)
if isinstance(index, tuple):
return self.cell_value(index[0], index[1], cell_value)
elif isinstance(index, str):
row, column = utils.excel_cell_position(index)
return self.cell_value(row, column, cell_value)
else:
raise IndexError

def __getitem__(self, aset):
raise IndexError

def __getitem__(self, index: MatrixIndex):
"""By default, this class recognize from top to bottom
from left to right"""
if isinstance(aset, tuple):
return self.cell_value(aset[0], aset[1])
elif isinstance(aset, str):
row, column = utils.excel_cell_position(aset)
if isinstance(index, tuple):
return self.cell_value(index[0], index[1])
elif isinstance(index, str):
row, column = utils.excel_cell_position(index)
return self.cell_value(row, column)
elif isinstance(aset, int):
elif isinstance(index, int):
print(constants.MESSAGE_DEPRECATED_ROW_COLUMN)
return self.row_at(aset)
else:
raise IndexError
return self.row_at(index)

raise IndexError

def contains(self, predicate):
"""Has something in the table"""
Expand Down
2 changes: 1 addition & 1 deletion pyexcel/internal/source_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_me_now(self, key, action=None, library=None, **keywords):
return plugin

def register_a_plugin(self, plugin_cls, plugin_info):
""" for dynamically loaded plugin """
"""for dynamically loaded plugin"""
PluginManager.register_a_plugin(self, plugin_cls, plugin_info)
self._register_a_plugin_info(plugin_info)

Expand Down
1 change: 1 addition & 0 deletions pyexcel/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class BinaryRenderer(Renderer):
"""
Renderer pyexcel data into a binary object
"""

WRITE_FLAG = "wb"

def get_io(self):
Expand Down
6 changes: 3 additions & 3 deletions pyexcel/sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def delete_named_column_at(self, name):
Matrix.delete_columns(self, [index])

def named_row_at(self, name):
"""Get a row by its name """
"""Get a row by its name"""
index = name
index = self.rownames.index(name)
row_array = self.row_at(index)
Expand Down Expand Up @@ -593,7 +593,7 @@ def __getitem__(self, aset):
column = aset[1]
return self.cell_value(row, column)
else:
return Matrix.__getitem__(self, aset)
return super().__getitem__(aset)

def __setitem__(self, aset, c):
if isinstance(aset, tuple):
Expand All @@ -608,7 +608,7 @@ def __setitem__(self, aset, c):
column = aset[1]
self.cell_value(row, column, c)
else:
Matrix.__setitem__(self, aset, c)
super().__setitem__(aset, c)

def __len__(self):
return self.number_of_rows()
Expand Down
37 changes: 7 additions & 30 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,10 @@

import os
import sys
import codecs
import locale
import platform
from shutil import rmtree

from setuptools import Command, setup, find_packages

PY2 = sys.version_info[0] == 2
PY26 = PY2 and sys.version_info[1] < 7
PY33 = sys.version_info < (3, 4)

# Work around mbcs bug in distutils.
# http://bugs.python.org/issue10945
# This work around is only if a project supports Python < 3.4

# Work around for locale not being set
try:
lc = locale.getlocale()
pf = platform.system()
if pf != "Windows" and lc == (None, None):
locale.setlocale(locale.LC_ALL, "C.UTF-8")
except (ValueError, UnicodeError, locale.Error):
locale.setlocale(locale.LC_ALL, "en_US.UTF-8")

NAME = "pyexcel"
AUTHOR = "C.W."
VERSION = "0.7.0"
Expand All @@ -41,7 +21,7 @@
)
URL = "https://github.com/pyexcel/pyexcel"
DOWNLOAD_URL = "%s/archive/0.7.0.tar.gz" % URL
FILES = ["README.rst", "CONTRIBUTORS.rst", "CHANGELOG.rst"]
README_FILES = ["README.rst", "CONTRIBUTORS.rst", "CHANGELOG.rst"]
KEYWORDS = [
"python",
'tsv',
Expand Down Expand Up @@ -155,17 +135,14 @@ def has_gease():

def read_files(*files):
"""Read files into setup"""
text = ""
for single_file in files:
content = read(single_file)
text = text + content + "\n"
return text
return "\n".join([read(filename) for filename in files])


def read(afile):
def read(filename):
"""Read a file into setup"""
the_relative_file = os.path.join(HERE, afile)
with codecs.open(the_relative_file, "r", "utf-8") as opened_file:
filename_absolute = os.path.join(HERE, filename)

with open(filename_absolute) as opened_file:
content = filter_out_test_code(opened_file)
content = "".join(list(content))
return content
Expand Down Expand Up @@ -205,7 +182,7 @@ def filter_out_test_code(file_handle):
description=DESCRIPTION,
url=URL,
download_url=DOWNLOAD_URL,
long_description=read_files(*FILES),
long_description=read_files(*README_FILES),
license=LICENSE,
keywords=KEYWORDS,
python_requires=PYTHON_REQUIRES,
Expand Down
Empty file added tests/__init__.py
Empty file.
6 changes: 3 additions & 3 deletions tests/test_bug_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

import psutil
import pyexcel as p
from _compact import StringIO, OrderedDict

from nose.tools import eq_
from ._compact import StringIO, OrderedDict


def test_bug_01():
Expand Down Expand Up @@ -166,7 +166,7 @@ def test_issue_51_normal_dict_in_records():


def test_issue_55_unicode_in_headers():
headers = [u"Äkkilähdöt", u"Matkakirjoituksia", u"Matkatoimistot"]
headers = ["Äkkilähdöt", "Matkakirjoituksia", "Matkatoimistot"]
content = [headers, [1, 2, 3]]
sheet = p.Sheet(content)
sheet.name_columns_by_row(0)
Expand Down Expand Up @@ -566,7 +566,7 @@ def test_issue_241():
+---------+---------+---+---------+"""
).lstrip()
eq_(str(book), expected)
os.unlink('issue_241.xlsx')
os.unlink("issue_241.xlsx")


def test_issue_250():
Expand Down
6 changes: 3 additions & 3 deletions tests/test_cookbook.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os

import pyexcel as pe
from base import clean_up_files

from nose.tools import eq_, raises
from .base import clean_up_files


class TestSpliting:
Expand All @@ -12,7 +12,7 @@ def setUp(self):
self.content4 = {
"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]],
"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]],
"Sheet3": [["X", "Y", "Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]],
}
pe.save_book_as(dest_file_name=self.testfile4, bookdict=self.content4)

Expand Down Expand Up @@ -87,7 +87,7 @@ def setUp(self):
self.content4 = {
"Sheet1": [[1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3]],
"Sheet2": [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]],
"Sheet3": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]],
"Sheet3": [["X", "Y", "Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]],
}
pe.save_book_as(dest_file_name=self.testfile4, bookdict=self.content4)

Expand Down
12 changes: 5 additions & 7 deletions tests/test_django_related_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pyexcel as pe
from _compact import OrderedDict

from nose.tools import eq_, raises
from ._compact import OrderedDict


class Attributable:
Expand Down Expand Up @@ -187,10 +187,10 @@ class TestBook:
def setUp(self):
self.content = OrderedDict()
self.content.update(
{"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
{"Sheet1": [["X", "Y", "Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
)
self.content.update(
{"Sheet2": [[u"A", u"B", u"C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
{"Sheet2": [["A", "B", "C"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
)
self.result1 = [
{"Y": 4, "X": 1, "Z": 7},
Expand Down Expand Up @@ -223,9 +223,7 @@ def test_book_save_to_models_with_bulk_save_false(self):

def test_model_save_to_models(self):
model = FakeDjangoModel("Sheet1")
data = {
"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
}
data = {"Sheet1": [["X", "Y", "Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
pe.save_book_as(dest_models=[model, None, None], bookdict=data)
assert model.objects.objs == self.result1

Expand All @@ -235,7 +233,7 @@ def test_load_book_from_django_model(self):
# with an exception.
model = FakeDjangoModel("Sheet1")
book = pe.Book(
{"Sheet1": [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
{"Sheet1": [["X", "Y", "Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]}
)
book.save_to_django_models([model])
assert model.objects.objs == self.result1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_them(self):
fn(path)


class TestPyexcelServer(TestCase):
class x(TestCase):
"""
This test tells how difficult to test unicode vs bytes when
dealing with csv files in relation to pyexcel. and this
Expand Down

0 comments on commit d8b9650

Please sign in to comment.