Skip to content
Browse files

Make a declarative cube baby

  • Loading branch information...
1 parent 40f73cd commit 011325e012392d09972f25fc77fdbe52bf72b276 @paradoxxxzero paradoxxxzero committed Apr 12, 2012
Showing with 42 additions and 16 deletions.
  1. +11 −2 pypet/declarative.py
  2. +31 −14 pypet/test/test_declarative.py
View
13 pypet/declarative.py
@@ -47,7 +47,12 @@ class Measure(Declarative):
"""Declarative measure"""
def __call__(self, name, table):
- expression = column(self.args[0] if len(self.args) else name, table)
+ if len(self.args):
+ expr = self.args[0]
+ self.args = self.args[1:]
+ else:
+ expr = name
+ expression = column(expr, table)
return pypet.Measure(name, expression, *self.args, **self.kwargs)
@@ -102,7 +107,11 @@ def __new__(cls, classname, bases, classdict):
if len(default_levels):
levels = [level for _, level
in sorted(default_levels.items(), key=lambda x: x[0])]
- hierarchies.append(pypet.Hierarchy('default', levels))
+ default_hierarchy = pypet.Hierarchy('default', levels)
+ for level in levels:
+ if not hasattr(default_hierarchy, level.name):
+ setattr(default_hierarchy, level.name, level)
+ hierarchies.append(default_hierarchy)
dimension = pypet.Dimension('_unbound_', hierarchies)
dimension.definition = dimension_class
View
45 pypet/test/test_declarative.py
@@ -1,5 +1,6 @@
import pypet
from pypet.test import BaseTestCase
+from pypet import aggregates
from pypet.declarative import Level, Hierarchy, Dimension, Measure, Cube
@@ -100,33 +101,49 @@ class TimeDimension(Dimension):
assert hasattr(TimeDimension, 'default')
assert len(TimeDimension.default.l) == 5
assert TimeDimension.default.l.keys() == ['All', 'l1', 'l2', 'l3', 'l4']
+ assert hasattr(TimeDimension.default, 'l1')
+ assert hasattr(TimeDimension.default, 'l2')
class TestCube(BaseTestCase):
def test_cube(self):
-
- class TimeHierarchy(Hierarchy):
- year = Level()
- month = Level()
- day = Level()
-
- class TimeDimension(Dimension):
- time = TimeHierarchy
+ def c(col):
+ table, column = col.split('.')
+ return self.metadata.tables[table].columns[column]
+
+ class StoreDimension(Dimension):
+ region = Level(c('region.region_id'), c('region.region_name'))
+ country = Level(c('country.country_id'), c('country.country_name'))
+ store = Level(c('store.store_id'), c('store.store_name'))
+
+ class ProductDimension(Dimension):
+ category = Level(c('product_category.product_category_id'),
+ c('product_category.product_category_name'))
+ product = Level(c('product.product_id'),
+ c('product.product_name'))
+
+ # class TimeDimension(Dimension):
+ # year = Level()
+ # month = Level()
+ # day = Level()
class TestCube(Cube):
- __connection__ = 'postgresql://pypet@localhost/pypet'
+ __metadata__ = self.metadata
__fact_table__ = 'facts_table'
__fact_count_column__ = 'qty'
- time = TimeDimension
+
+ store = StoreDimension
+ product = ProductDimension
+ time = self.time_dim
price = Measure()
- quantity = Measure('qty')
+ quantity = Measure('qty', agg=aggregates.sum)
assert isinstance(TestCube, pypet.Cube)
assert isinstance(TestCube.price, pypet.Measure)
assert isinstance(TestCube.quantity, pypet.Measure)
assert isinstance(TestCube.time, pypet.Dimension)
- assert isinstance(TestCube.time.time, pypet.Hierarchy)
- assert isinstance(TestCube.time.time.day, pypet.Level)
+ # assert isinstance(TestCube.time.default, pypet.Hierarchy)
+ # assert isinstance(TestCube.time.default.day, pypet.Level)
assert isinstance(TestCube.query, pypet.Query)
- # TestCube.query.axis(TimeDimension.h1.l1)
+ TestCube.query.axis(StoreDimension.default.region).execute()

0 comments on commit 011325e

Please sign in to comment.
Something went wrong with that request. Please try again.