将标题从 Cucumber 的数据 Table 顶部移动到侧面 - Python
Move the headings from top of Cucumber's Data Table to side - Python
我正在寻找将 Cucumber 的数据 Table 的标题更改到一边的方法。所以它将使特征文件可读。
普通方式:
| Name | Email | Phone No. | ......... |
| John | i@g.net | 098765644 | ......... |
它可能是一个非常宽的数据table,我将不得不来回滚动。
所需方式:
| Name | John |
| Email | i@g.net |
| Phone No. | 098765444 |
.
.
.
Java和Ruby中有少量示例。但我正在与 Python 合作。
我尝试了很多不同的东西,比如 numpy.transpose(),将它们转换为列表。但它不会工作,因为数据 Table 的格式是:
[<Row['Name','John'],...]
这看起来与 numpy 无关。
旋转列表列表通常使用 zip(*the_list)
这将 return 旋转行为 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')
你可以很简单地自己实现这个行为,这是我的版本:
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
我正在寻找将 Cucumber 的数据 Table 的标题更改到一边的方法。所以它将使特征文件可读。
普通方式:
| Name | Email | Phone No. | ......... |
| John | i@g.net | 098765644 | ......... |
它可能是一个非常宽的数据table,我将不得不来回滚动。
所需方式:
| Name | John |
| Email | i@g.net |
| Phone No. | 098765444 |
.
.
.
Java和Ruby中有少量示例。但我正在与 Python 合作。
我尝试了很多不同的东西,比如 numpy.transpose(),将它们转换为列表。但它不会工作,因为数据 Table 的格式是:
[<Row['Name','John'],...]
这看起来与 numpy 无关。
旋转列表列表通常使用 zip(*the_list)
这将 return 旋转行为 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')
你可以很简单地自己实现这个行为,这是我的版本:
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