Skip to content

Commit

Permalink
adapt pyexcel v0.2.2 and code refactoring on tabulate and json handle…
Browse files Browse the repository at this point in the history
…rs. much simpler code
  • Loading branch information
chfw committed May 11, 2016
1 parent 5dfd669 commit 4308dec
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 302 deletions.
24 changes: 10 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,21 @@ Simple
... ]
>>> sheet = pe.Sheet(content)
>>> print(sheet.simple)
Sheet Name: pyexcel
pyexcel sheet:
-------- -------- --------
Column 1 Column 2 Column 3
1 2 3
4 5 6
7 8 9
-------- -------- --------
<BLANKLINE>
>>> sheet.name_columns_by_row(0)
>>> print(sheet.simple)
Sheet Name: pyexcel
pyexcel sheet:
Column 1 Column 2 Column 3
---------- ---------- ----------
1 2 3
4 5 6
7 8 9
<BLANKLINE>
Grid
Expand All @@ -66,7 +64,7 @@ Grid
.. code-block:: python
>>> print(sheet.grid)
Sheet Name: pyexcel
pyexcel sheet:
+------------+------------+------------+
| Column 1 | Column 2 | Column 3 |
+============+============+============+
Expand All @@ -76,7 +74,7 @@ Grid
+------------+------------+------------+
| 7 | 8 | 9 |
+------------+------------+------------+
<BLANKLINE>
Mediawiki
-------------
Expand Down Expand Up @@ -107,7 +105,7 @@ Mediawiki
>>> book.save_as("myfile.mediawiki")
>>> myfile = open("myfile.mediawiki")
>>> print(myfile.read())
Sheet Name: Sheet 1
Sheet 1:
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
Expand All @@ -117,7 +115,7 @@ Mediawiki
|-
| align="right"| 7 || align="right"| 8 || align="right"| 9
|}
Sheet Name: Sheet 2
Sheet 2:
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
Expand All @@ -127,7 +125,7 @@ Mediawiki
|-
| 4.0 || 5.0 || 6.0
|}
Sheet Name: Sheet 3
Sheet 3:
{| class="wikitable" style="text-align: left;"
|+ <!-- caption -->
|-
Expand All @@ -137,7 +135,6 @@ Mediawiki
|-
| 4.0 || 3.0 || 2.0
|}
<BLANKLINE>
>>> myfile.close()
Html
Expand All @@ -148,25 +145,24 @@ Html
>>> book.save_as("myfile.html")
>>> myfile = open("myfile.html")
>>> print(myfile.read())
Sheet Name: Sheet 1
Sheet 1:
<table>
<tr><td style="text-align: right;">1</td><td style="text-align: right;">2</td><td style="text-align: right;">3</td></tr>
<tr><td style="text-align: right;">4</td><td style="text-align: right;">5</td><td style="text-align: right;">6</td></tr>
<tr><td style="text-align: right;">7</td><td style="text-align: right;">8</td><td style="text-align: right;">9</td></tr>
</table>
Sheet Name: Sheet 2
Sheet 2:
<table>
<tr><td>X </td><td>Y </td><td>Z </td></tr>
<tr><td>1.0</td><td>2.0</td><td>3.0</td></tr>
<tr><td>4.0</td><td>5.0</td><td>6.0</td></tr>
</table>
Sheet Name: Sheet 3
Sheet 3:
<table>
<tr><td>O </td><td>P </td><td>Q </td></tr>
<tr><td>3.0</td><td>2.0</td><td>1.0</td></tr>
<tr><td>4.0</td><td>3.0</td><td>2.0</td></tr>
</table>
<BLANKLINE>
.. testcode::
:hide:
Expand Down
4 changes: 0 additions & 4 deletions pyexcel_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
pyexcel.Sheet.save_as, pyexcel.Book.save_as.
"""

sources = text.sources + json.sources
file_types = text.file_types + json.file_types


def save_as(instance, filename):
"""
Legacy modular function. Now it is removed
Expand Down
116 changes: 28 additions & 88 deletions pyexcel_text/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,35 @@
"""
import json

from pyexcel.sources import params

from ._text import TextSource, WriteOnlyMemorySourceMixin


file_types = ('json',)


class JsonSheetSource(TextSource):
"""
Write a two dimensional array into json format
"""
text_file_formats = file_types

def __init__(self, file_name=None, write_title=True, **keywords):
self.file_name = file_name
self.write_title = write_title
self.keywords = keywords

def write_data(self, sheet):
data = self._transform_data(sheet)
with open(self.file_name, 'w') as jsonfile:
if self.write_title:
data = {sheet.name: data}
self._write_sheet(jsonfile, data)

def _write_sheet(self, jsonfile, data):
jsonfile.write(json.dumps(data, sort_keys=True))

def _transform_data(self, sheet):
table = sheet.to_array()
if hasattr(sheet, 'colnames'):
colnames = sheet.colnames
rownames = sheet.rownames
# In the following, row[0] is the name of each row
if colnames and rownames:
table = dict((row[0], dict(zip(colnames, row[1:])))
for row in table[1:])
elif colnames:
table = [dict(zip(colnames, row)) for row in table[1:]]
elif rownames:
table = dict((row[0], row[1:]) for row in table)
else:
table = list(table)
return table


class JsonSheetSourceInMemory(JsonSheetSource, WriteOnlyMemorySourceMixin):
fields = [params.FILE_TYPE]
def __init__(self, file_type=None, file_stream=None, write_title=True,
**keywords):
WriteOnlyMemorySourceMixin.__init__(
self,
file_type=file_type,
file_stream=file_stream,
write_title=write_title,
**keywords)

def write_data(self, sheet):
data = self._transform_data(sheet)
if self.write_title:
data = {sheet.name: data}
self._write_sheet(self.content, data)


def write_json_book(jsonfile, bookdict):
jsonfile.write(json.dumps(bookdict, sort_keys=True))


class JsonBookSourceInMemory(JsonSheetSourceInMemory):
targets = (params.BOOK,)
actions = (params.WRITE_ACTION,)

def write_data(self, book):
write_json_book(self.content, book.to_dict())


class JsonBookSource(JsonSheetSource):
"""
Write a dictionary of two dimensional arrays into json format
"""
targets = (params.BOOK,)
actions = (params.WRITE_ACTION,)

def write_data(self, book):
with open(self.file_name, 'w') as jsonfile:
write_json_book(jsonfile, book.to_dict())


sources = (JsonSheetSource, JsonBookSource,
JsonSheetSourceInMemory, JsonBookSourceInMemory)

def jsonify(sheet, file_type, write_title):
content = ""
table = sheet.to_array()
if hasattr(sheet, 'colnames'):
colnames = sheet.colnames
rownames = sheet.rownames
# In the following, row[0] is the name of each row
if colnames and rownames:
table = dict((row[0], dict(zip(colnames, row[1:])))
for row in table[1:])
elif colnames:
table = [dict(zip(colnames, row)) for row in table[1:]]
elif rownames:
table = dict((row[0], row[1:]) for row in table)
else:
table = list(table)
if write_title:
content = {sheet.name: table}
else:
content = table
return json.dumps(content, sort_keys=True)


def jsonify_book(book, file_type):
return json.dumps(book.to_dict(), sort_keys=True)


renderer = (jsonify, jsonify_book)
Loading

0 comments on commit 4308dec

Please sign in to comment.