Skip to content

Commit

Permalink
Django jump column (#103)
Browse files Browse the repository at this point in the history
* ✨ support skipping columns when importing a spreadsheet. fix #102

* This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst

* 📚 update typo in changelog.yml

* This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst

Co-authored-by: chfw <chfw@users.noreply.github.com>
  • Loading branch information
chfw and chfw committed Oct 29, 2020
1 parent 4a0b013 commit cd081dd
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 23 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Change log
================================================================================

0.6.4 - tbd
--------------------------------------------------------------------------------

**updated**

#. `#102 <https://github.com/pyexcel/pyexcel-io/issues/102>`_: skip columns from
imported excel sheet.

0.6.3 - 12.10.2020
--------------------------------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pyexcel-io - Let you focus on data, instead of file formats
:target: https://anaconda.org/conda-forge/pyexcel-io

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

.. image:: https://anaconda.org/conda-forge/pyexcel-io/badges/downloads.svg
:target: https://anaconda.org/conda-forge/pyexcel-io
Expand Down
6 changes: 6 additions & 0 deletions changelog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: pyexcel-io
organisation: pyexcel
releases:
- changes:
- action: updated
details:
- "`#102`: skip columns from imported excel sheet."
version: 0.6.4
date: tbd
- changes:
- action: fixed
details:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# The short X.Y version
version = '0.6.3'
# The full version, including alpha/beta/rc tags
release = '0.6.3'
release = '0.6.4'

# -- General configuration ---------------------------------------------------

Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:Source code: http://github.com/pyexcel/pyexcel-io.git
:Issues: http://github.com/pyexcel/pyexcel-io/issues
:License: New BSD License
:Development: |release|
:Released: |version|
:Generated: |today|

Expand Down
4 changes: 2 additions & 2 deletions pyexcel-io.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ project: "pyexcel-io"
name: pyexcel-io
nick_name: io
version: 0.6.3
current_version: 0.6.3
release: 0.6.3
current_version: 0.6.4
release: 0.6.4
copyright_year: 2015-2020
moban_command: false
is_on_conda: true
Expand Down
14 changes: 9 additions & 5 deletions pyexcel_io/database/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,15 @@ def _process_parameters(self):
self.__column_name_mapping_dict.output = None
elif isinstance(self.__column_name_mapping_dict.input, dict):
if self.__column_names.input:
self.__column_names.output = [
self.__column_name_mapping_dict.input[name]
for name in self.__column_names.input
]
self.__column_name_mapping_dict.output = None
self.__column_names.output = []
indices = []
for index, name in enumerate(self.__column_names.input):
if name in self.__column_name_mapping_dict.input:
self.__column_names.output.append(
self.__column_name_mapping_dict.input[name]
)
indices.append(index)
self.__column_name_mapping_dict.output = indices
if self.__column_names.output is None:
self.__column_names.output = self.__column_names.input

Expand Down
9 changes: 8 additions & 1 deletion pyexcel_io/database/importers/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ def write_row(self, array):
print(constants.MESSAGE_EMPTY_ARRAY)
else:
new_array = swap_empty_string_for_none(array)
if self.__mapdict:
another_new_array = []
for index, element in enumerate(new_array):
if index in self.__mapdict:
another_new_array.append(element)
new_array = another_new_array
model_to_be_created = new_array
if self.__initializer is not None:
model_to_be_created = self.__initializer(new_array)
if model_to_be_created:
row = dict(zip(self.__column_names, model_to_be_created))
self.__objs.append(
self.__model(
**dict(zip(self.__column_names, model_to_be_created))
**row
)
)

Expand Down
15 changes: 9 additions & 6 deletions pyexcel_io/database/importers/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ def write_row(self, array):
print(new_array)

def _write_row(self, array):
row = dict(zip(self.adapter.column_names, array))
new_array = array
if self.adapter.column_name_mapping_dict:
another_new_array = []
for index, element in enumerate(new_array):
if index in self.adapter.column_name_mapping_dict:
another_new_array.append(element)
new_array = another_new_array
row = dict(zip(self.adapter.column_names, new_array))
obj = None
if self.adapter.row_initializer:
# allow initinalizer to return None
Expand All @@ -54,11 +61,7 @@ def _write_row(self, array):
if obj is None:
obj = self.adapter.table()
for name in self.adapter.column_names:
if self.adapter.column_name_mapping_dict is not None:
key = self.adapter.column_name_mapping_dict[name]
else:
key = name
setattr(obj, key, row[name])
setattr(obj, name, row[name])
self.importer.session.add(obj)
if self.__auto_commit and self.__bulk_size != float("inf"):
self.__count += 1
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@

NAME = "pyexcel-io"
AUTHOR = "C.W."
VERSION = "0.6.3"
VERSION = "0.6.4"
EMAIL = "info@pyexcel.org"
LICENSE = "New BSD"
DESCRIPTION = (
"A python library to read and write structured data in csv, zipped csv" +
"format and to/from databases"
)
URL = "https://github.com/pyexcel/pyexcel-io"
DOWNLOAD_URL = "%s/archive/0.6.3.tar.gz" % URL
DOWNLOAD_URL = "%s/archive/0.6.4.tar.gz" % URL
FILES = ["README.rst", "CHANGELOG.rst"]
KEYWORDS = [
"python",
Expand Down Expand Up @@ -87,8 +87,8 @@
PUBLISH_COMMAND = "{0} setup.py sdist bdist_wheel upload -r pypi".format(sys.executable)
HERE = os.path.abspath(os.path.dirname(__file__))

GS_COMMAND = ("gease pyexcel-io v0.6.3 " +
"Find 0.6.3 in changelog for more details")
GS_COMMAND = ("gease pyexcel-io v0.6.4 " +
"Find 0.6.4 in changelog for more details")
NO_GS_MESSAGE = ("Automatic github release is disabled. " +
"Please install gease to enable it.")
UPLOAD_FAILED_MSG = (
Expand Down
18 changes: 15 additions & 3 deletions tests/test_django_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ def wrapper(row):
writer = DjangoModelWriter(None, adapter)
writer.write_array(self.data[1:])
writer.close()
assert model.objects.objs == [
eq_(model.objects.objs, [
{"Y": 2, "X": 2, "Z": 3},
{"Y": 5, "X": 5, "Z": 6},
]
])

def test_sheet_save_to_django_model_skip_me(self):
model = FakeDjangoModel()
Expand All @@ -178,7 +178,7 @@ def wrapper(row):
writer = DjangoModelWriter(None, adapter)
writer.write_array(self.data[1:])
writer.close()
assert model.objects.objs == [{"Y": 2, "X": 1, "Z": 3}]
eq_(model.objects.objs, [{"Y": 2, "X": 1, "Z": 3}])

def test_load_sheet_from_django_model(self):
model = FakeDjangoModel()
Expand Down Expand Up @@ -241,6 +241,18 @@ def test_mapping_dict(self):
writer.close()
eq_(model.objects.objs, self.result)

def test_jumping_columns(self):
data2 = [["D", "A", "B", "C"], [1, 1, 2, 3], [10, 4, 5, 6]]
mapdict = {"C": "Z", "A": "X", "B": "Y"}
model = FakeDjangoModel()
adapter = DjangoModelImportAdapter(model)
adapter.column_names = data2[0]
adapter.column_name_mapping_dict = mapdict
writer = DjangoModelWriter(None, adapter)
writer.write_array(data2[1:])
writer.close()
eq_(model.objects.objs, self.result)

def test_empty_model(self):
model = FakeDjangoModel()
reader = DjangoModelReader(model)
Expand Down

0 comments on commit cd081dd

Please sign in to comment.