get_singleton() vs. Config(1)

29 views
Skip to first unread message

Udo Spallek

unread,
May 16, 2015, 8:20:58 AM5/16/15
to tryto...@googlegroups.com
Hi,

in [1][2] we introduce a ``get_singleton()`` method for Singelton
models.
In the modules we always use a patter like this::

Config = pool.get('xxx.configuration')
config = Config(1)

or

Configuration = pool.get('xxx.configuration')
config = Configuration(1)


instead of::

config = Config.get_singleton()

Is the use of ``get_singleton()`` recommended in module code or better
stay with config = Config(1)?

Best
Udo Spallek

[1]http://doc.tryton.org/3.6/trytond/doc/ref/models/models.html#trytond.model.ModelSingleton.get_singleton
http://doc.tryton.org/3.6/trytond/doc/ref/models/models.html#trytond.model.ModelSingleton.get_singleton
[2]
http://hg.tryton.org/trytond/file/0e853ce7b6b5/trytond/model/modelsingleton.py#l12

Cédric Krier

unread,
May 16, 2015, 2:40:03 PM5/16/15
to tryto...@googlegroups.com
On 16 May 14:20, Udo Spallek wrote:
> Hi,
>
> in [1][2] we introduce a ``get_singleton()`` method for Singelton
> models.
> In the modules we always use a patter like this::
>
> Config = pool.get('xxx.configuration')
> config = Config(1)
>
> or
>
> Configuration = pool.get('xxx.configuration')
> config = Configuration(1)
>
>
> instead of::
>
> config = Config.get_singleton()
>
> Is the use of ``get_singleton()`` recommended in module code or better
> stay with config = Config(1)?

Obviously it is not the recommended way.
Indeed I don't see any advantage in using get_singleton but I see one
for the current way, it prevent a search if the configuration is not
used.


--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Cédric Krier

unread,
May 16, 2015, 5:55:03 PM5/16/15
to tryto...@googlegroups.com
On 16 May 20:39, Cédric Krier wrote:
> On 16 May 14:20, Udo Spallek wrote:
> > Hi,
> >
> > in [1][2] we introduce a ``get_singleton()`` method for Singelton
> > models.
> > In the modules we always use a patter like this::
> >
> > Config = pool.get('xxx.configuration')
> > config = Config(1)
> >
> > or
> >
> > Configuration = pool.get('xxx.configuration')
> > config = Configuration(1)
> >
> >
> > instead of::
> >
> > config = Config.get_singleton()
> >
> > Is the use of ``get_singleton()`` recommended in module code or better
> > stay with config = Config(1)?
>
> Obviously it is not the recommended way.
> Indeed I don't see any advantage in using get_singleton but I see one
> for the current way, it prevent a search if the configuration is not
> used.

And I forget that get_singleton can return None if no one was yet
created. So it will make the code more complex.

Udo Spallek

unread,
May 18, 2015, 10:09:10 AM5/18/15
to tryto...@googlegroups.com
Sat, 16 May 2015 20:39:38 +0200
Cédric Krier <cedric...@b2ck.com>:
>On 16 May 14:20, Udo Spallek wrote:
...
>> Is the use of ``get_singleton()`` recommended in module code or
>> better stay with config = Config(1)?
>Obviously it is not the recommended way.
>Indeed I don't see any advantage in using get_singleton but I see one
>for the current way, it prevent a search if the configuration is not
>used.

Ok, sounds reasonable. Is a patch welcome, removing get_singleton()
from code and doc?

Regards Udo

Cédric Krier

unread,
May 18, 2015, 10:25:03 AM5/18/15
to tryto...@googlegroups.com
Why?

Udo Spallek

unread,
May 18, 2015, 2:58:53 PM5/18/15
to tryto...@googlegroups.com
Mon, 18 May 2015 16:21:29 +0200
Cédric Krier <cedric...@b2ck.com>:
>On 18 May 16:08, Udo Spallek wrote:
>> Sat, 16 May 2015 20:39:38 +0200
>> Cédric Krier <cedric...@b2ck.com>:
>> >On 16 May 14:20, Udo Spallek wrote:
>> ...
>> >> Is the use of ``get_singleton()`` recommended in module code or
>> >> better stay with config = Config(1)?
>> >Obviously it is not the recommended way.
>> >Indeed I don't see any advantage in using get_singleton but I see
>> >one for the current way, it prevent a search if the configuration
>> >is not used.
>> Ok, sounds reasonable. Is a patch welcome, removing get_singleton()
>> from code and doc?
>Why?

Ouch, I see, the method is solely for internal use in ModelSingleton.
So nothing else to do here. Thanks for clarification.

Regards Udo
Reply all
Reply to author
Forward
0 new messages