Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matrix "writer" - to produce native py matrix #9

Open
dz0 opened this issue Mar 18, 2021 · 2 comments
Open

matrix "writer" - to produce native py matrix #9

dz0 opened this issue Mar 18, 2021 · 2 comments

Comments

@dz0
Copy link

dz0 commented Mar 18, 2021

that would allow incorporating tool in variuos contexts -- for example I have openpyxl workflow, where I'd like to use it

also could be proxy for #6

@dz0
Copy link
Author

dz0 commented Mar 18, 2021

I made it easily from csv Writer

class NativeTableWriter(bWriter):
    EMPTY = ''

    def __init__(self, empty=EMPTY):
        self.table = []
        self.header_depth = 0
        self.EMPTY = empty
        

    def reset(self):
        self.table = []

    def write_header(self, header):
        self.write_row(header, None)
        self.header_depth += 1

    def write_row(self, row, data):
        out = []
        for h in row:
            v = h.value
            if v is None:
                v = ''
            out.append(v)
            if h.columns > 1:
                for _ in range(h.columns - 1):
                    out.append(self.EMPTY)
        self.table.append(out)

@dz0
Copy link
Author

dz0 commented Mar 19, 2021

a test , based on

def test_csv_converter():

def test_native_table_converter():
    data = [
        {
            'a': [
                {
                    'b': '1'
                },
                {
                    'c': '2'
                }
            ],
            'd': 'abc'
        }
    ]
    data += data

    conv = Converter()
    w = TableWriter()
    conv.convert(data, w)
    assert w.header_depth == 2
    assert w.table == (
        [['a', '', 'a', '', 'd'],
         ['b', 'c', 'b', 'c', ''],
         ['1', '', '', '2', 'abc'],
         ['1', '', '', '2', 'abc']]
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant