本文整理汇总了Python中outputty.Table.to_list_of_dicts方法的典型用法代码示例。如果您正苦于以下问题:Python Table.to_list_of_dicts方法的具体用法?Python Table.to_list_of_dicts怎么用?Python Table.to_list_of_dicts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类outputty.Table
的用法示例。
在下文中一共展示了Table.to_list_of_dicts方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_to_dict_should_return_unicode_data_when_encoding_is_None
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_list_of_dicts [as 别名]
def test_to_dict_should_return_unicode_data_when_encoding_is_None(self):
my_table = Table(headers=["ham", "spam", "eggs"])
my_table.append({"ham": "á", "spam": "ç", "eggs": "à"})
my_dict = my_table.to_list_of_dicts(encoding=None)
self.assertEquals(len(my_dict), 1)
self.assertTrue(isinstance(my_dict[0]["ham"], unicode))
self.assertTrue(isinstance(my_dict[0]["spam"], unicode))
self.assertTrue(isinstance(my_dict[0]["eggs"], unicode))
self.assertEquals(my_dict[0]["ham"], u"á")
self.assertEquals(my_dict[0]["spam"], u"ç")
self.assertEquals(my_dict[0]["eggs"], u"à")
my_table = Table(headers=["ham", "spam", "eggs"], output_encoding="utf16")
my_table.append({"ham": "á", "spam": "ç", "eggs": "à"})
my_dict = my_table.to_list_of_dicts(encoding="iso-8859-1")
self.assertEquals(len(my_dict), 1)
self.assertEquals(my_dict[0]["ham"], u"á".encode("iso-8859-1"))
self.assertEquals(my_dict[0]["spam"], u"ç".encode("iso-8859-1"))
self.assertEquals(my_dict[0]["eggs"], u"à".encode("iso-8859-1"))
示例2: test_list_of_dicts_should_handle_output_encoding_correctly
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_list_of_dicts [as 别名]
def test_list_of_dicts_should_handle_output_encoding_correctly(self):
my_table = Table(headers=["ham", "spam", "eggs"], output_encoding="utf16")
my_table.append((123, 456, 789))
my_table.append({"ham": "abc", "spam": "def", "eggs": "ghi"})
my_table.append((987, 654, 321))
my_dicts = my_table.to_list_of_dicts()
self.assertEquals(len(my_dicts), 3)
self.assertEquals(my_dicts[1][u"ham".encode("utf16")], u"abc".encode("utf16"))
self.assertEquals(my_dicts[1][u"spam".encode("utf16")], u"def".encode("utf16"))
self.assertEquals(my_dicts[1][u"eggs".encode("utf16")], u"ghi".encode("utf16"))
self.assertEquals(my_dicts[2][u"ham".encode("utf16")], 987)
self.assertEquals(my_dicts[2][u"spam".encode("utf16")], 654)
self.assertEquals(my_dicts[2][u"eggs".encode("utf16")], 321)
示例3: test_to_dict_should_return_a_list_of_dict_with_headers_as_keys
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_list_of_dicts [as 别名]
def test_to_dict_should_return_a_list_of_dict_with_headers_as_keys(self):
my_table = Table(headers=["ham", "spam", "eggs"])
my_table.append((123, 456, 789))
my_table.append({"ham": "abc", "spam": "def", "eggs": "ghi"})
my_table.append((987, 654, 321))
my_dict = my_table.to_list_of_dicts()
self.assertEquals(len(my_dict), 3)
self.assertEquals(my_dict[0]["ham"], 123)
self.assertEquals(my_dict[0]["spam"], 456)
self.assertEquals(my_dict[0]["eggs"], 789)
self.assertEquals(my_dict[1]["ham"], "abc")
self.assertEquals(my_dict[1]["spam"], "def")
self.assertEquals(my_dict[1]["eggs"], "ghi")
self.assertEquals(my_dict[2]["ham"], 987)
self.assertEquals(my_dict[2]["spam"], 654)
self.assertEquals(my_dict[2]["eggs"], 321)
示例4: Plotter
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_list_of_dicts [as 别名]
#.........这里部分代码省略.........
if count is None:
bars_titles = [legends[header] \
for header in bars_titles]
xticklabels = self.data[x_column]
else:
bars_titles = [legends[count]]
subplot.legend(bars, bars_titles)
else:
xticklabels = self.data[x_column]
subplot.set_xticklabels(xticklabels, rotation=x_rotation)
if y_label is not None:
subplot.set_ylabel(y_label)
if y_lim is not None:
subplot.set_ylim(y_lim)
def stacked_bar(self, x_column, y_column, y_labels=None, title='',
grid=True, bar_width=0.5, x_rotation=0, legends=True,
legend_location='upper left', legend_box=(-0.4, 1),
colors=None, colormap=matplotlib.cm.gist_heat):
subplot = self._get_new_subplot()
subplot.set_title(title)
subplot.grid(grid)
x_offset = (1.0 - bar_width) / 2
x_values_unique = list(set(self.data[x_column]))
x_values = array(range(len(set(self.data[x_column]))))
subplot.set_xticks(x_values + x_offset)
subplot.set_xticklabels(x_values_unique, rotation=x_rotation)
y_labels_values = list(set(self.data[y_labels]))
y_labels_values.sort()
data = {y: Counter() for y in y_labels_values}
if colors is None:
color_range = linspace(0, 0.9, len(data.keys()))
colors = [colormap(i) for i in color_range]
for row in self.data.to_list_of_dicts(encoding=None):
data[row[y_labels]][row[x_column]] += row[y_column]
bottom = zeros(len(x_values))
for y in y_labels_values:
values = [data[y][x] for x in x_values_unique]
subplot.bar(x_values, values, width=bar_width, label=unicode(y),
color=colors.pop(0), bottom=bottom)
bottom = [bottom[index] + value \
for index, value in enumerate(values)]
if legends:
subplot.legend(loc=legend_location, bbox_to_anchor=legend_box)
self.fig.subplots_adjust(bottom=0.1, left=0.25)
def radar(self, axis_labels, values, legends_column, title='',
x_grid=False, y_grid=True, fill_alpha=0.5, colors=None,
colormap=matplotlib.cm.gist_heat,
legend_location='upper left', legend_box=(-0.4, 1),
legends=False):
subplot = self._get_new_subplot(projection='polar')
subplot.set_title(title)
subplot.xaxis.grid(x_grid)
subplot.yaxis.grid(y_grid)
axis_labels_values = list(set(self.data[axis_labels]))
axis_labels_values.sort()
number_of_axis = len(axis_labels_values)
axis_angles = 2 * pi * linspace(0, 1 - 1.0 / number_of_axis,
number_of_axis)
subplot.set_thetagrids(axis_angles * 180 / pi, axis_labels_values)
legends_values = list(set(self.data[legends_column]))
legends_values.sort()
if colors is None:
len_legends = len(legends_values)
color_range = linspace(0, 1 - 1.0 / len_legends, len_legends)
示例5: Table
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import to_list_of_dicts [as 别名]
#!/usr/bin/env python
# coding: utf-8
#A `Table` made with `dict`-like, `list`-like and `tuple`-like objects. For
#example, this code:
from outputty import Table
my_table = Table(headers=['First Name', 'Last Name', 'Main Language'])
my_table.append({'First Name': 'Álvaro', 'Last Name': 'Justen',
'Main Language': 'Python'})
my_table.append(('Flávio', 'Amieiro', 'Python'))
rows = my_table.to_list_of_dicts()
print rows[1]['First Name']
table_dict = my_table.to_dict()
print table_dict
table_dict_filtered = my_table.to_dict(only=['First Name', 'Last Name'])
print table_dict_filtered
other_table = Table(headers=['date', 'measure'])
other_table.append(('2011-12-01', 21))
other_table.append(('2011-12-02', 42))
other_table.append(('2011-12-03', 3.14))
other_table.append(('2011-12-04', 2.71))
values_as_dict = other_table.to_dict(key='date', value='measure')
print values_as_dict