示例#1
0
文件: model.py 项目: ande7590/plato
 def __setup_data_context(self):
     
     dc = self.data_context
     dc_navigator = DataContextNavigator(dc)        
     for col_name, col in self.columns.iteritems():
         metadata = Column.get_metadata(col)            
         if metadata.uses_data_context:
             
             dc_prop = col_name if metadata.data_context == True else\
                 metadata.data_context                
             try: column_context = dc_navigator.navigate(dc_prop)
             except (AttributeError, KeyError):
                 raise Exception(type(self).__name__ +                                    
                                 ', missing data_context value for column '
                                  '%(col_name)s: %(dc_prop)s'
                                  '\n\t data_context = %(dc)s' % locals())
             
             # allow binding primitives to single argument functions
             if not isinstance(column_context, dict):
                 args = inspect.getargspec(metadata.inner_func).args
                 if args == ['self']:
                     column_context = {tuple():column_context}    
                 else:
                     raise Exception(type(self).__name__ +                                        
                                     ', data_context error for column '
                                     '%s(col_name), unexpected data type. '
                                     'Must be dictionary, since the function'
                                     ' takes multiple arguments '
                                     '(not just "self"\ndata_context='
                                     '%(column_context)' % column_context)                   
                 
                                                 
             for args, val in column_context.iteritems():
                 col.set_value(args, val)        
示例#2
0
    def __setup(parent_model, submodel):

        parent_data_ctx = parent_model.data_context
        factory_fn = SubModel.get_factory_fn(submodel)
        data_context_prop = submodel.data_context_property
        child_data_ctx = DataContextNavigator(parent_data_ctx)\
            .navigate(data_context_prop)

        try:
            data_context_iter = iter(child_data_ctx)
        except TypeError:
            data_context_iter = [child_data_ctx]

        # create a map of column names to submodel instances, so we can
        # efficently create aggregators once the loop is done, i.e.
        #  { 'column_name1': [ model_instance1, model_instance2, ...], ... }
        models_for_col_names = defaultdict(list)

        for data_context in data_context_iter:
            kwargs = dict(parent_model.ctor_kwargs)
            kwargs['data_context'] = data_context

            submodel = factory_fn(parent_model, data_context, kwargs)

            for col_name in submodel.columns.keys():
                models_for_col_names[col_name].append(submodel)

        # Aggregators apply an operation across many submodels, e.g.
        # sum all columns of the submodels
        aggregators = {}
        for col_name, models in models_for_col_names.iteritems():
            aggregators[col_name] = SubModelComputation(col_name, models)

        return aggregators
 def testDataContextNavigatorObj(self):
     
     dc = TestObj1()
     
     dcn = DataContextNavigator(dc)
     
     self.assertEqual(dcn.navigate('foo'), dc.foo)
     self.assertEqual(dcn.navigate('foo.that'), dc.foo.that)
     self.assertEqual(dcn.navigate('foo.err'), dc.foo.err)
     self.assertEqual(dcn.navigate('foo.err.ha'), 'ja')
     self.assertEqual(dcn.navigate('bar'), dc.bar)        
     self.assertEqual(dcn.navigate('bar.who'), dc.bar.who)
示例#4
0
文件: model.py 项目: ddd00a/plato
    def __setup_data_context(self):

        dc = self.data_context
        dc_navigator = DataContextNavigator(dc)
        for col_name, col in self.columns.iteritems():
            metadata = Column.get_metadata(col)
            if metadata.uses_data_context:

                dc_prop = col_name if metadata.data_context == True else\
                    metadata.data_context
                try:
                    column_context = dc_navigator.navigate(dc_prop)
                except (AttributeError, KeyError):
                    raise Exception(
                        type(self).__name__ +
                        ', missing data_context value for column '
                        '%(col_name)s: %(dc_prop)s'
                        '\n\t data_context = %(dc)s' % locals())

                # allow binding primitives to single argument functions
                if not isinstance(column_context, dict):
                    args = inspect.getargspec(metadata.inner_func).args
                    if args == ['self']:
                        column_context = {tuple(): column_context}
                    else:
                        raise Exception(
                            type(self).__name__ +
                            ', data_context error for column '
                            '%s(col_name), unexpected data type. '
                            'Must be dictionary, since the function'
                            ' takes multiple arguments '
                            '(not just "self"\ndata_context='
                            '%(column_context)' % column_context)

                for args, val in column_context.iteritems():
                    col.set_value(args, val)
 def testDataContextNavigatorDict(self):
             
     dc = {'foo': 'bar',
           'this': [1,2,3], 
           'that': {'wat': 1, 'err': {'ha':'ja'}}}
     
     dcn = DataContextNavigator(dc)
     
     self.assertEqual(dcn.navigate('.'), dc)
     self.assertEqual(dcn.navigate('foo'), 'bar')
     self.assertEqual(dcn.navigate('this'), [1,2,3])
     self.assertEqual(dcn.navigate('that'), {'wat': 1, 'err': {'ha':'ja'}})
     self.assertEqual(dcn.navigate('that.err'), {'ha':'ja'})
     self.assertEqual(dcn.navigate('that.err.ha'), 'ja')