- . (. ). Django, doctext. , 1) Store, 2) type_detail 3) type_detail. (store, type) = > .
class Cube(object):
"""Iterable sparse cube. Iterating gives an item for every dimension member.
>>> pythons = ['eric', 'john', 'terry']
>>> cheeses = ['limburg', 'feta', 'parmigiano']
>>> cheese_consumption = {
('eric', 'limburg'): 2,
('eric', 'parmigiano'): 4,
('john', 'feta'): 5
}
>>> cheese_cube = Cube((pythons, cheeses), cheese_consumption)
>>> for python, python_cheeses in cheese_cube:
for cheese, consumption in python_cheeses:
print python, cheese, consumption or 0
eric limburg 2
eric feta 0
eric parmigiano 4
john limburg 0
john feta 5
john parmigiano 0
terry limburg 0
terry feta 0
terry parmigiano 0
"""
def __init__(self, dimensions, facts, slice=None):
self.dimensions = dimensions
self.data_dict = facts
self.slice = slice or ()
def __iter__(self):
if len(self.slice) + 1 < len(self.dimensions):
for item in self.dimensions[len(self.slice)]:
yield item, Cube(self.dimensions, self.data_dict, self.slice + (item,))
else:
for item in self.dimensions[len(self.slice)]:
yield item, self.data_dict.get(self.slice + (item,), None)