Skip to content

Commit

Permalink
changed multiline params and logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hvalev committed Aug 14, 2022
1 parent abebae0 commit 071617d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
27 changes: 16 additions & 11 deletions markdownTable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class markdownTable():

def __init__(self, data):
self.data = data
self.validate()
self.row_sep = 'always'
self.padding_width = 0
self.padding_weight = 'centerleft'
Expand All @@ -46,6 +45,7 @@ def __init__(self, data):
self.float_rounding = 2
self.multiline = False
self.quote = True
self.updateMetaParams()
return

def setParams(
Expand All @@ -64,13 +64,16 @@ def setParams(
self.padding_char = padding_char
self.newline_char = newline_char
self.float_rounding = float_rounding
self.multiline = multiline
self.quote = quote
self.multiline = multiline
self.updateMetaParams()
return self

def updateMetaParams(self):
self.var_padding = self.getPadding()
if self.multiline:
self.var_padding = self.multiline
else:
self.var_padding = self.getPadding()
self.var_row_sep = self.getRowSepStr()
self.var_row_sep_last = self.getRowSepLast()

Expand All @@ -82,6 +85,13 @@ def validate(self):
for key in keys:
if key not in item:
raise Exception('Keys are not uniform')
if self.multiline:
for row in self.data:
for key, item in row.items():
multiline_data = row[key].split(" ")
multiline_max_width = max(multiline_data, key=len)
if (self.var_padding[key]) < len(multiline_max_width):
raise Exception('Contiguous string exists longer than the allocated column width')

def getPadding(self):
padding = dict()
Expand All @@ -91,14 +101,8 @@ def getPadding(self):
for key in item.keys():
if (type(item[key]) is float and self.float_rounding):
item[key] = round(item[key], self.float_rounding)
if self.multiline:
multiline_data = item[key].split(" ")
multiline_min_width = max(multiline_data, key=len)
if (padding[key]+self.padding_width) < len(multiline_min_width) + self.padding_width:
padding[key] = len(multiline_min_width)+self.padding_width
else:
if (padding[key]-self.padding_width) < len(str(item[key])):
padding[key] = len(str(item[key]))+self.padding_width
if (padding[key]-self.padding_width) < len(str(item[key])):
padding[key] = len(str(item[key]))+self.padding_width
return padding

def getRowSepStr(self):
Expand Down Expand Up @@ -226,6 +230,7 @@ def getBody(self):
return rows

def getMarkdown(self):
self.validate()
self.updateMetaParams()
data = self.getHeader()+self.getBody()
if self.quote:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py-markdown-table",
version="0.3.0",
version="0.3.1",
author="hvalev",
description="A package used to generate basic markdown tables from a list of dicts",
long_description=long_description,
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ def test_formatting_padding_char():


def test_multiline_data():
mt = markdownTable(multiline_data).setParams(padding_width=2, padding_weight="centerleft", multiline=True).getMarkdown()
res = '```\n+----------------------------------+\n| A | B | C |\n+------------+------------+--------+\n| row1_A and | row1_B | row1_C |\n| additional | | |\n| stuff | | |\n+------------+------------+--------+\n| row2_A | row2_B and | row2_C |\n| | additional | |\n| | stuff | |\n+------------+------------+--------+\n| row3_A | row3_B | row3_C |\n+----------------------------------+```'
mt = markdownTable(multiline_data).setParams(padding_width=2, padding_weight="centerleft", multiline={"A": 25, "B": 12, "C": 9}).getMarkdown()
res = '```\n+------------------------------------------------+\n| A | B | C |\n+-------------------------+------------+---------+\n| row1_A and additional | row1_B | row1_C |\n| stuff | | |\n+-------------------------+------------+---------+\n| row2_A | row2_B and | row2_C |\n| | additional | |\n| | stuff | |\n+-------------------------+------------+---------+\n| row3_A | row3_B | row3_C |\n+------------------------------------------------+```'
assert mt == res

0 comments on commit 071617d

Please sign in to comment.