def duck_type_collection(specimen, default=None): " does the same thing as the sqla function, but handles proxy dict " guess = sqla_duck_type_collection(specimen, default=None) if guess is not None: return guess isa = isinstance(specimen, type) and issubclass or isinstance if isa(specimen, _AssociationDict): return dict return default
self.func = func self.args = args self.kwargs = kwargs def populate(self): data = self.func(*self.args, **self.kwargs) if not isinstance(data, dict): raise Exception('function did not return a dict') self.update(data) self.initialized = True def __getitem__(self, key): if not self.initialized: self.populate() return dict.__getitem__(self, key) def get(self, key, default=None): if not self.initialized: self.populate() return dict.get(self, key, default) if __name__ == '__main__': items = [ _AssociationList, _AssociationDict, _AssociationSet, ] for item in items: print(sqla_duck_type_collection(item), duck_type_collection(item))
self.initialized = False self.func = func self.args = args self.kwargs = kwargs def populate(self): data = self.func(*self.args, **self.kwargs) if not isinstance(data, dict): raise Exception('function did not return a dict') self.update(data) self.initialized = True def __getitem__(self, key): if not self.initialized: self.populate() return dict.__getitem__(self, key) def get(self, key, default=None): if not self.initialized: self.populate() return dict.get(self, key, default) if __name__ == '__main__': items = [ _AssociationList, _AssociationDict, _AssociationSet, ] for item in items: print (sqla_duck_type_collection(item), duck_type_collection(item))