Example #1
0
    def Model(models):
        def _m(name):
            model = models
            for n in name.split('.'):
                model = getattr(models, n)
            return model

        columns = locals()
        columns[source_id] = Column(ForeignKey(
                  _m(source_name).id, ondelete='cascade'),
                  nullable=False, primary_key=True)
        columns[source] = relationship(_m(source_name),
                remote_side=(
                    _m(source_side) if source_side else None),
                foreign_keys=[columns[source_id]],
                **source_rel_attrs)

        columns[target_id] = Column(ForeignKey(
                _m(target_name).id, ondelete='cascade'),
                nullable=False, primary_key=True)

        columns[target] = relationship(_m(target_name),
                remote_side=(
                    _m(target_side) if target_side else None),
                foreign_keys=[columns[target_id]],
                **target_rel_attrs)
        if ordered:
            columns['order_position'] = \
                    Column(Integer, nullable=False, default=0)
        if extra is not None:
            columns.update(return_locals(extra)(models))
        del _m
Example #2
0
    def test_return_locals_debugger_events(self):
        '''Insure debugging function gets events for function decorated with
        return_locals at least the same as for function itself.'''
        def collect_events(func):
            events = []

            def tracer(frame, event, args):
                events.append((frame.f_code, event))
                return tracer

            old_tracer = sys.gettrace()
            sys.settrace(tracer)
            try:
                func()
            finally:
                sys.settrace(old_tracer)
            return events

        def inner():
            return 2

        def outer():
            a = 1
            b = inner()

        events1 = set(collect_events(outer))
        events2 = set(collect_events(return_locals(outer)))
        self.assertTrue(events2.issuperset(events1))
Example #3
0
 def test_return_locals(self):
     def test_func(a, b=1, *args, **kwargs):
         '''Test function'''
         c = 2
     decorated = return_locals(test_func)
     self.assertEqual(test_func.__name__, decorated.__name__)
     self.assertEqual(test_func.__doc__, decorated.__doc__)
     d = decorated(0)
     self.assertIs(type(d), dict)
     self.assertEqual(d, {'c': 2})
Example #4
0
    def test_return_locals(self):
        def test_func(a, b=1, *args, **kwargs):
            '''Test function'''
            c = 2

        decorated = return_locals(test_func)
        self.assertEqual(test_func.__name__, decorated.__name__)
        self.assertEqual(test_func.__doc__, decorated.__doc__)
        d = decorated(0)
        self.assertIs(type(d), dict)
        self.assertEqual(d, {'c': 2})
Example #5
0
 def test_return_locals_profile_events(self):
     '''Insure profile function gets events for function decorated with
     return_locals at least the same as for function itself.'''
     def collect_events(func):
         events = []
         def tracer(frame, event, args):
             events.append((frame.f_code, event))
         old_tracer = sys.getprofile()
         sys.setprofile(tracer)
         try:
             func()
         finally:
             sys.setprofile(old_tracer)
         return events
     def inner():
         return 2
     def outer():
         a = 1
         b = inner()
     events1 = set(collect_events(outer))
     events2 = set(collect_events(return_locals(outer)))
     self.assertTrue(events2.issuperset(events1))
Example #6
0
 def get_attr(cls):
     if cls not in attrs:
         # Call func only once per class
         attrs[cls] = return_locals(func)()
     return attrs[cls][name]
Example #7
0
 def get_constructor(self, func):
     return return_locals(func)
Example #8
0
 def get_constructor(self, func):
     return return_locals(func)