Move the headings from top of Cucumber's Data Table to side - Python

风流意气都作罢 提交于 2020-01-07 04:07:40

问题


I am looking for the ways to change the headings of Cucumber's Data Table to the side. So it will make the feature file readable.

Ordinary way:

| Name | Email   | Phone No. | ......... |
| John | i@g.net | 098765644 | ......... |

It can be a very wide data table and I would have to scroll back and forth.

Desired way:

| Name      | John      |
| Email     | i@g.net   |
| Phone No. | 098765444 |
.
.
.

There are a small number of examples in Java and Ruby. But I am working with Python.

I had tried many different things like numpy.transpose(), converting them to list. But it won't work because the Data Table's format is:

[<Row['Name','John'],...]

回答1:


This doesn't look like it's related to numpy.

pivoting a list of list is often done with zip(*the_list)

This will return a pivoted behave table

from behave.model import Table


class TurnTable(unittest.TestCase):
    """ 
    """

    def test_transpose(self):
        table = Table(
            ['Name', 'John', 'Mary'],
            rows=[
                ['Email', "john@example.com", "mary@example.com"],
                ['Phone', "0123456789", "9876543210"],
            ])

        aggregate = [table.headings[:]]
        aggregate.extend(table.rows)
        pivoted = list(zip(*aggregate))
        self.assertListEqual(pivoted,
                             [('Name', 'Email', 'Phone'),
                              ('John', 'john@example.com', '0123456789'),
                              ('Mary', 'mary@example.com', '9876543210')])

        pivoted_table = Table(
            pivoted[0],
            rows=pivoted[1:])
        mary = pivoted_table.rows[1]
        self.assertEqual(mary['Name'], 'Mary')
        self.assertEqual(mary['Phone'], '9876543210')

you can also have a look at https://pypi.python.org/pypi/pivottable




回答2:


You can implement this behaviour quite simply yourself, here is my version:

def tabledict(table, defaults, aliases = {}):
"""
    Converts a behave context.table to a dictionary.
    Throws NotImplementedError if the table references an unknown key.

    defaults should contain a dictionary with the (surprise) default values.
    aliases makes it possible to map alternative names to the keys of the defaults.
    All keys of the table will be converted to lowercase, you sould make sure that
    you defaults and aliases dictionaries also use lowercase.

    Example:
        Given the book
          | Property                             | Value              |
          | Title                                | The Tragedy of Man |
          | Author                               | Madach, Imre       |
          | International Standard Book Number   | 9631527395         |

    defaults = { "title": "Untitled", "author": "Anonymous", "isbn": None, "publisher": None }
    aliases = { "International Standard Book Number" : "isbn" }
    givenBook = tabledict(context.table, defaults, aliases)

    will give you:
    givenBook == {
        "title": "The Tragedy of Man",
        "author": "Madach, Imre",
        "isbn": 9631527395,
        "publisher": None
        }
"""
initParams = defaults.copy()
validKeys = aliases.keys()[:] + defaults.keys()[:]
for row in table:
    name, value = row[0].lower(), row[1]
    if not name in validKeys:
        raise NotImplementedError(u'%s property is not supported.'%name)

    if name in aliases: name = aliases[name]
    initParams[name] = value
return initParams


来源:https://stackoverflow.com/questions/43756434/move-the-headings-from-top-of-cucumbers-data-table-to-side-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!