Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2020-11-17 Thread lars van gemerden
Thanks Mike, I oversimplified a little bit (the factory function takes more arguments than self.stops, not part of the Planning class), but if i create the factory with partial I think it will work. On Monday, November 16, 2020 at 6:16:24 PM UTC+1 Mike Bayer wrote: > I would use a memoized

Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2020-11-16 Thread Mike Bayer
I would use a memoized descriptor for that and have it evaluate when called. Amazingly, i can't find public examples of Python memoize decorators that aren't overwrought.we use one that is completely efficient and simple: class memoized_property(object): """A read-only @property that is

Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2020-11-16 Thread lars van gemerden
Hi Mike, What if during reconstruction you need a one-to-many attribute, like: class Planning(SqlaBase): stops = relationship("Stop", back_populates="planning", lazy="joined") matrix_factory = Matrix def __init__(self, **kwargs): super().__init__(**kwargs)

Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2017-08-06 Thread Mike Bayer
On Aug 6, 2017 1:33 PM, "Shane Carey" wrote: Hey Mike, I can expand my example. I have an orm mapped attribute like this class Obj(Base): _evaluator = Column(String) def __init__(self, **kwargs): super().__init__(**kwargs) self._eval_func =

Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2017-08-06 Thread Shane Carey
Hey Mike, I can expand my example. I have an orm mapped attribute like this class Obj(Base): _evaluator = Column(String) def __init__(self, **kwargs): super().__init__(**kwargs) self._eval_func = eval(self._evaluator) @orm.reconstructor def init_on_load(self):

Re: [sqlalchemy] avoid setting attribute on init and or.reconstructor

2017-08-05 Thread Mike Bayer
On Fri, Aug 4, 2017 at 6:07 PM, Shane Carey wrote: > I am trying to remove duplicate code in this scenario described here > > http://docs.sqlalchemy.org/en/latest/orm/constructors.html > > class Obj(Base): > def __init__(self, **kwargs): >

[sqlalchemy] avoid setting attribute on init and or.reconstructor

2017-08-04 Thread Shane Carey
I am trying to remove duplicate code in this scenario described here http://docs.sqlalchemy.org/en/latest/orm/constructors.html class Obj(Base): def __init__(self, **kwargs): super().__init__(**kwargs) self.extra = # ... some complicated construction based off of a kwarg