> -----Original Message----- > From: [email protected] > [mailto:[email protected]] On Behalf Of werner > Sent: 25 January 2010 13:37 > To: [email protected] > Subject: Re: [sqlalchemy] dynamic_loader > > On 24/01/2010 16:57, werner wrote: > ... > > Next thing is to make "_get_translation" reusable for > different tables. > I got it down to this: > > def _do_translation(bInstance, tTable, fCrit, cLang, dLang): > try: > x = > sao.object_session(bInstance).query(tTable).with_parent(bInsta > nce).filter(fCrit==cLang) > return x.one() > except sao.exc.NoResultFound: > try: > x = > sao.object_session(bInstance).query(tTable).with_parent(bInsta > nce).filter(fCrit==dLang) > return x.one() > except sao.exc.NoResultFound: > return 'no translation found' > > class Country_B(Base): > __table__ = sa.Table(u'countries_b', metadata, > sa.Column(u'id', sa.Integer(), sa.Sequence('countries_b_id'), > primary_key=True, nullable=False), > .. > ) > > def _get_translation(self): > return _do_translation(self, Country_T, > Country_T.lang_code5, > getCurrentUserLang, getDefaultUserLang) > > country_t = sao.relation('Country_T', backref='country_b') > > country = property(_get_translation) > > But would like to remove the "def _get_translation" and call directly > _do_translation, something like this: > > country = property(_do_translation('Country_B', 'Country_T', > 'lang_code5', getCurrentUserLang, getDefaultUserLang)) > > But can't figure out how I would then get at the instance of > "Country_B" > within _do_translation. > > As always tips or pointers to documentation are very appreciated. > > Werner >
Hi Werner, You need to implement your own 'property'-like class that implements the descriptor protocol. This page might give you some clues: http://users.rcn.com/python/download/Descriptor.htm#descriptor-example If you had a TranslationProperty class that looked like this: class TranslationProperty(object): def __get__(self, obj, objtype): # Call _do_translation(obj, ...) here Then your Country class can look like: class Country_B(Base): country_t = TranslationProperty() When you access the country_t attribute, the __get__ method will be called with your Country_B instance as the obj parameter, and the Country_B class as the objtype parameter, which you can hopefully pass on to the _do_translation function. I hope that helps, Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
