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 only evaluated once."""
def __init__(self, fget, doc=None):
self.fget = fget
self.__doc__ = doc or fget.__doc__
self.__name__ = fget.__name__
def __get__(self, obj, cls):
if obj is None:
return self
obj.__dict__[self.__name__] = result = self.fget(obj)
return result
then you can just set it up as:
class Planning(...):
@memoized_property
def matrix(self):
return self.matrix_factor(self.stops)
On Mon, Nov 16, 2020, at 11:43 AM, lars van gemerden wrote:
> 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)
> self.matrix = self.matrix_factory(self.stops)
>
> @reconstructor
> def init_on_load(self):
> self.matrix = self.matrix_factory(self.stops)
> The docs say self.stops will not be completely loaded in init_on_load, how
> could i make this work?
>
> Cheers, Lars
> On Monday, August 7, 2017 at 6:09:08 AM UTC+2 Mike Bayer wrote:
>>
>>
>> On Aug 6, 2017 1:33 PM, "Shane Carey" <[email protected]> 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 = eval(self._evaluator)
>>>
>>> @orm.reconstructor
>>> def init_on_load(self):
>>> self._eval_func = eval(self._evaluator)
>>>
>>> @property
>>> def evaluator(self):
>>> return self._eval_func
>>>
>>> @evaluator.setter
>>> def set_evaluator(ev):
>>> self._evaluator = ev
>>> self._eval_func = eval(self._evaluator)
>>>
>>> You can see that I have to explicitly set self._eval_func in three
>>> different places, when really I just want to set it every time
>>> self._evaluator is set.
>>>
>>> It looks to me like the orm events are just a different way of placing the
>>> different settings of this class attribute
>>>
>>> Also, I would like to not call eval in the getter of the property for the
>>> sake of performance (I know that would simplify the issue).
>>>
>>> Is there a way to intercept the setting of self._evaluator for all cases?
>>
>>
>> Use the init and load event listeners from my previous email on top of one
>> function. It will be called for init and load.
>>
>>
>>>
>>> --
>>> SQLAlchemy -
>>> The Python SQL Toolkit and Object Relational Mapper
>>>
>>> http://www.sqlalchemy.org/
>>>
>>> To post example code, please provide an MCVE: Minimal, Complete, and
>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
>>> description.
>>> ---
>>> You received this message because you are subscribed to the Google Groups
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an
>>> email to [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at https://groups.google.com/group/sqlalchemy.
>>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/e9b2b9ff-0979-43be-94f4-fdb3ceb48ae9n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/e9b2b9ff-0979-43be-94f4-fdb3ceb48ae9n%40googlegroups.com?utm_medium=email&utm_source=footer>.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/4beb162a-6698-487c-9b98-b64b2434d260%40www.fastmail.com.