示例#1
0
def json_to_tabular_result(j):
    '''return an array for print_tokens'''
    jj = jsane.from_dict(j)
    results = jj.results.r(default=[])
    tabular_result = []

    for r in results:
        if 'error' in r:
            tabular_result.append((Token.Red, '[ERROR] '))
            tabular_result.append((Token, r['error']))
            tabular_result.append((Token, '\n'))
            continue

        rr = jsane.from_dict(r)
        series = rr.series[0].r(default=None)

        if series:
            series = rr.series[0]
            name = series.name.r(default=None)
            columns = series.columns.r(default=[])
            values = series.values.r(default=[])

            column_amount = len(columns)
            longest_value_len = [0] * column_amount
            _calculate_value_len(column_amount, columns, values,
                                 longest_value_len)

            if name is not None:
                tabular_result.append((Token, 'name: '))
                tabular_result.append((Token.Green, name))
                tabular_result.append((Token, '\n'))

            for index, column in enumerate(columns):
                tabular_result.append(
                    (Token.Orange, '{column: <{width}}'.format(
                        column=column, width=longest_value_len[index] + 2)))
            tabular_result.append((Token, '\n'))

            for index in range(column_amount):
                tabular_result.append(
                    (Token.Orange, '{divider: <{width}}'.format(
                        divider='---', width=longest_value_len[index] + 2)))
            tabular_result.append((Token, '\n'))

            for value in values:
                for index, value_ in enumerate(value):
                    tabular_result.append(
                        (Token, '{value: <{width}}'.format(
                            value=str(value_),
                            width=longest_value_len[index] + 2)))
                tabular_result.append((Token, '\n'))
    tabular_result.append((Token, '\n'))

    return tabular_result
示例#2
0
文件: Req.py 项目: qatool/pytest_demo
    def jsan(self):
        """
        :return: response jsane traversable object
        """

        try:
            return jsane.from_dict(self.response.json())
        except json.JSONDecodeError as e:
            logger.debug('<= err msg => \n{}'.format(e))
            logger.debug('<= response content => \n{}'.format(self.response.content))
            pytest.fail('response data format does not match the JSON format')
示例#3
0
 def test_wrapper(self):
     assert loads(dumps(self.dict1)).r() == self.dict1
     assert json.dumps(self.dict1) == dumps(self.dict1)
     assert self.dict1["foo"] == from_dict(self.dict1).foo.r()
     assert loads(dumps(self.dict1)), Traversable(self.dict1)
示例#4
0
 def test_wrapper(self):
     assert loads(dumps(self.dict1)).r() == self.dict1
     assert json.dumps(self.dict1) == dumps(self.dict1)
     assert self.dict1["foo"] == from_dict(self.dict1).foo.r()
     assert loads(dumps(self.dict1)), Traversable(self.dict1)
示例#5
0
def json_to_tabular_result(j):
    '''return an array for print_tokens'''
    jj = jsane.from_dict(j)
    results = jj.results.r(default=[])
    tabular_result = []

    for r in results:
        if 'error' in r:
            tabular_result.append(('ansibrightred', '[ERROR] '))
            tabular_result.append(('', r['error']))
            tabular_result.append(('', '\n'))
            continue

        rr = jsane.from_dict(r)
        series_list = rr.series.r(default=None)

        for series in series_list:
            name = series.get('name')
            columns = series.get('columns')
            values = series.get('values', [])

            column_amount = len(columns)
            longest_value_len = [0] * column_amount
            _calculate_value_len(
                column_amount,
                columns,
                values,
                longest_value_len
            )

            if name is not None:
                tabular_result.append(('', 'name: '))
                tabular_result.append(('ansibrightgreen', name))
                tabular_result.append(('', '\n'))

            for index, column in enumerate(columns):
                tabular_result.append((
                    'orange',
                    '{column: <{width}}'.format(
                        column=column,
                        width=longest_value_len[index]+2
                    )
                ))
            tabular_result.append(('', '\n'))

            for index in range(column_amount):
                tabular_result.append((
                    'orange',
                    '{divider: <{width}}'.format(
                        divider='---',
                        width=longest_value_len[index]+2
                    )
                ))
            tabular_result.append(('', '\n'))

            for value in values:
                for index, value_ in enumerate(value):
                    tabular_result.append((
                        '',
                        '{value: <{width}}'.format(
                            value=string(value_),
                            width=longest_value_len[index]+2
                        )
                    ))
                tabular_result.append(('', '\n'))
            tabular_result.append(('', '\n'))

    return tabular_result
示例#6
0
import jsane

if __name__ == "__main__":
    # jsane是一个json解析器
    # loads 解析一个json字符串
    j = jsane.loads('{"name": "wulj", "value": "pass"}')
    print(j.name.r())
    # from_dict 解析字典
    j2 = jsane.from_dict(
        {'key': ['v1', 'v2', ['v3', 'v4', {
            'inner': 'value'
        }]]})
    print(j2.key[2][2].inner.r())
    # 当解析找不到key时,设置默认值
    print(j2.key.new.r(default="test"))