Skip to content

Commit

Permalink
🔨 code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Nov 9, 2018
1 parent e199fc1 commit 808c948
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .moban.d/tests/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'tests/base.py.jj2' %}

{%block ods_types%}
{%endblock%}
{%endblock%}
2 changes: 1 addition & 1 deletion .moban.d/tests/test_formatters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends 'tests/test_formatters.py.jj2' %}

{%block xlsx_exception%}
{%endblock%}
{%endblock%}
19 changes: 11 additions & 8 deletions pyexcel_xlsx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
"""
from pyexcel_io.plugins import IOPluginInfoChain
from pyexcel_io.io import (
get_data as read_data, isstream, store_data as write_data)
get_data as read_data,
isstream,
store_data as write_data,
)


__FILE_TYPE__ = 'xlsx'
__FILE_TYPE__ = "xlsx"
IOPluginInfoChain(__name__).add_a_reader(
relative_plugin_class_path='xlsxr.XLSXBook',
file_types=[__FILE_TYPE__, 'xlsm'],
stream_type='binary'
relative_plugin_class_path="xlsxr.XLSXBook",
file_types=[__FILE_TYPE__, "xlsm"],
stream_type="binary",
).add_a_writer(
relative_plugin_class_path='xlsxw.XLSXWriter',
file_types=[__FILE_TYPE__, 'xlsm'],
stream_type='binary'
relative_plugin_class_path="xlsxw.XLSXWriter",
file_types=[__FILE_TYPE__, "xlsm"],
stream_type="binary",
)


Expand Down
22 changes: 13 additions & 9 deletions pyexcel_xlsx/xlsxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
:license: New BSD License
"""
import openpyxl

from pyexcel_io.book import BookReader
from pyexcel_io.sheet import SheetReader
from pyexcel_io._compact import OrderedDict
Expand All @@ -18,6 +17,7 @@ class XLSXSheet(SheetReader):
"""
Iterate through rows
"""

@property
def name(self):
"""sheet name"""
Expand All @@ -44,6 +44,7 @@ class SlowSheet(XLSXSheet):
"""
This sheet will be slower because it does not use readonly sheet
"""

def row_iterator(self):
"""
skip hidden rows
Expand All @@ -66,6 +67,7 @@ class XLSXBook(BookReader):
"""
Open xlsx as read only mode
"""

def open(self, file_name, skip_hidden_sheets=True, **keywords):
BookReader.open(self, file_name, **keywords)
self.skip_hidden_sheets = skip_hidden_sheets
Expand All @@ -89,21 +91,21 @@ def read_sheet_by_index(self, sheet_index):
if sheet_index < length:
return self.read_sheet_by_name(names[sheet_index])
else:
raise IndexError("Index %d of out bound %d" % (
sheet_index,
length))
raise IndexError(
"Index %d of out bound %d" % (sheet_index, length)
)

def read_all(self):
result = OrderedDict()
for sheet in self._native_book:
if self.skip_hidden_sheets and sheet.sheet_state == 'hidden':
if self.skip_hidden_sheets and sheet.sheet_state == "hidden":
continue
data_dict = self.read_sheet(sheet)
result.update(data_dict)
return result

def read_sheet(self, native_sheet):
if self._keywords.get('skip_hidden_row_and_column', False) is True:
if self._keywords.get("skip_hidden_row_and_column", False) is True:
sheet = SlowSheet(native_sheet, **self._keywords)
else:
sheet = XLSXSheet(native_sheet, **self._keywords)
Expand All @@ -115,8 +117,10 @@ def close(self):

def _load_the_excel_file(self, file_alike_object):
read_only_flag = True
if self._keywords.get('skip_hidden_row_and_column', False) is True:
if self._keywords.get("skip_hidden_row_and_column", False) is True:
read_only_flag = False
self._native_book = openpyxl.load_workbook(
filename=file_alike_object, data_only=True,
read_only=read_only_flag)
filename=file_alike_object,
data_only=True,
read_only=read_only_flag,
)
8 changes: 5 additions & 3 deletions pyexcel_xlsx/xlsxw.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
:license: New BSD License
"""
import openpyxl

from pyexcel_io.book import BookWriter
from pyexcel_io.sheet import SheetWriter

Expand All @@ -17,6 +16,7 @@ class XLSXSheetWriter(SheetWriter):
"""
Write data into xlsx sheet
"""

def set_sheet_name(self, name):
self._native_sheet.title = name
self.current_row = 1
Expand All @@ -32,6 +32,7 @@ class XLSXWriter(BookWriter):
"""
Write data in write only mode
"""

def __init__(self):
BookWriter.__init__(self)
self.current_sheet = 0
Expand All @@ -42,8 +43,9 @@ def open(self, file_name, **keywords):
self._native_book = openpyxl.Workbook(write_only=True)

def create_sheet(self, name):
return XLSXSheetWriter(self._native_book,
self._native_book.create_sheet(), name)
return XLSXSheetWriter(
self._native_book, self._native_book.create_sheet(), name
)

def close(self):
"""
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from setuptools import Command, setup, find_packages


NAME = 'pyexcel-xlsx'
AUTHOR = 'C.W.'
VERSION = '0.6.0'
Expand Down
17 changes: 10 additions & 7 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import os # noqa
import pyexcel
import datetime # noqa
from nose.tools import raises, eq_ # noqa

import pyexcel

from nose.tools import eq_, raises # noqa


def create_sample_file1(file):
data = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 1.1, 1]
data = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", 1.1, 1]
table = []
table.append(data[:4])
table.append(data[4:8])
Expand All @@ -17,10 +19,11 @@ class PyexcelHatWriterBase:
"""
Abstract functional test for hat writers
"""

content = {
"X": [1, 2, 3, 4, 5],
"Y": [6, 7, 8, 9, 10],
"Z": [11, 12, 13, 14, 15]
"Z": [11, 12, 13, 14, 15],
}

def test_series_table(self):
Expand All @@ -36,11 +39,12 @@ class PyexcelWriterBase:
testfile and testfile2 have to be initialized before
it is used for testing
"""

content = [
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5],
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5],
]

def _create_a_file(self, file):
Expand All @@ -54,7 +58,6 @@ def test_write_array(self):


class PyexcelMultipleSheetBase:

def _write_test_file(self, filename):
pyexcel.save_book_as(bookdict=self.content, dest_file_name=filename)

Expand All @@ -80,7 +83,7 @@ def test_reading_through_sheets(self):
expected = [[4, 4, 4, 4], [5, 5, 5, 5], [6, 6, 6, 6]]
assert data == expected
data = list(b["Sheet3"].rows())
expected = [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
expected = [[u"X", u"Y", u"Z"], [1, 4, 7], [2, 5, 8], [3, 6, 9]]
assert data == expected
sheet3 = b["Sheet3"]
sheet3.name_columns_by_row(0)
Expand Down
37 changes: 22 additions & 15 deletions tests/test_bug_fixes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import sys
import datetime
from textwrap import dedent

import pyexcel as pe

from nose.tools import eq_

IN_TRAVIS = 'TRAVIS' in os.environ
IN_TRAVIS = "TRAVIS" in os.environ


PY36_ABOVE = sys.version_info[0] == 3 and sys.version_info[1] >= 6
Expand Down Expand Up @@ -38,15 +40,17 @@ def test_pyexcel_issue_8_with_physical_file():
s.save_as(tmp_file)
s2 = pe.load(tmp_file)
eq_(str(s), str(s2))
content = dedent("""
content = dedent(
"""
CNY:
+----------+----------+------+---+-------+
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
+----------+----------+------+---+-------+
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
+----------+----------+------+---+-------+
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
+----------+----------+------+---+-------+""").strip("\n")
+----------+----------+------+---+-------+"""
).strip("\n")
eq_(str(s2), content)
os.unlink(tmp_file)

Expand All @@ -58,19 +62,21 @@ def test_pyexcel_issue_8_with_memory_file():
"""
tmp_file = "issue_8_save_as.xlsx"
f = open(get_fixtures("test8.xlsx"), "rb")
s = pe.load_from_memory('xlsx', f.read())
s = pe.load_from_memory("xlsx", f.read())
s.save_as(tmp_file)
s2 = pe.load(tmp_file)
eq_(str(s), str(s2))
content = dedent("""
content = dedent(
"""
CNY:
+----------+----------+------+---+-------+
| 01/09/13 | 02/09/13 | 1000 | 5 | 13.89 |
+----------+----------+------+---+-------+
| 02/09/13 | 03/09/13 | 2000 | 6 | 33.33 |
+----------+----------+------+---+-------+
| 03/09/13 | 04/09/13 | 3000 | 7 | 58.33 |
+----------+----------+------+---+-------+""").strip("\n")
+----------+----------+------+---+-------+"""
).strip("\n")
eq_(str(s2), content)
os.unlink(tmp_file)

Expand All @@ -83,24 +89,25 @@ def test_excessive_columns():

def test_issue_8_hidden_sheet():
test_file = get_fixtures("hidden_sheets.xlsx")
book_dict = pe.get_book_dict(file_name=test_file,
library="pyexcel-xlsx")
book_dict = pe.get_book_dict(file_name=test_file, library="pyexcel-xlsx")
assert "hidden" not in book_dict
eq_(book_dict['shown'], [['A', 'B']])
eq_(book_dict["shown"], [["A", "B"]])


def test_issue_8_hidden_sheet_2():
test_file = get_fixtures("hidden_sheets.xlsx")
book_dict = pe.get_book_dict(file_name=test_file,
skip_hidden_sheets=False,
library="pyexcel-xlsx")
book_dict = pe.get_book_dict(
file_name=test_file, skip_hidden_sheets=False, library="pyexcel-xlsx"
)
assert "hidden" in book_dict
eq_(book_dict['shown'], [['A', 'B']])
eq_(book_dict['hidden'], [['a', 'b']])
eq_(book_dict["shown"], [["A", "B"]])
eq_(book_dict["hidden"], [["a", "b"]])


def test_issue_20():
pe.get_book(url="https://github.com/pyexcel/pyexcel-xlsx/raw/master/tests/fixtures/file_with_an_empty_sheet.xlsx") # noqa: E501
pe.get_book(
url="https://github.com/pyexcel/pyexcel-xlsx/raw/master/tests/fixtures/file_with_an_empty_sheet.xlsx" # noqa: E501
)


def get_fixtures(file_name):
Expand Down
Loading

0 comments on commit 808c948

Please sign in to comment.