def getdotattr(obj_or_class, dot_path, condition=None): """ Allow dot-notated strings to be passed to `getattr`. :: getdotattr(SubSection, 'section.document') getdotattr(subsection, 'section.document') :param obj_or_class: Any object or class :param dot_path: Attribute path with dot mark as separator """ last = obj_or_class for path in str(dot_path).split('.'): getter = attrgetter(path) if is_sequence(last): tmp = [] for element in last: value = getter(element) if is_sequence(value): tmp.extend(value) else: tmp.append(value) last = tmp elif isinstance(last, InstrumentedAttribute): last = getter(last.property.mapper.class_) elif last is None: return None else: last = getter(last) if condition is not None: if is_sequence(last): last = [v for v in last if condition(v)] else: if not condition(last): return None return last
def invoke_callbacks(self, session, ctx, instances): callback_args = defaultdict(lambda: defaultdict(set)) for obj, callbacks in self.changed_objects(session): args = self.gather_callback_args(obj, callbacks) for (root_obj, func, objects) in args: if is_sequence(objects): callback_args[root_obj][func] = ( callback_args[root_obj][func] | set(objects)) else: callback_args[root_obj][func] = objects for root_obj, callback_objs in callback_args.items(): for callback, objs in callback_objs.items(): callback(root_obj, objs)
def invoke_callbacks(self, session, ctx, instances): callback_args = defaultdict(lambda: defaultdict(set)) for obj, callbacks in self.changed_objects(session): args = self.gather_callback_args(obj, callbacks) for (root_obj, func, objects) in args: if is_sequence(objects): callback_args[root_obj][func] = ( callback_args[root_obj][func] | set(objects) ) else: callback_args[root_obj][func] = objects for root_obj, callback_objs in callback_args.items(): for callback, objs in callback_objs.items(): callback(root_obj, objs)