What is the recommended way to fetch registry (settings) for read only
outside of the view (where request object is not available)? I was
looking at the threadlocal.get_current_registry() but the docs say it
should not be used except maybe in unit tests.
I understand that the request object should be passed around, but what
are my options if I want to avoid that?
Thanks!
--
.oO V Oo.
To avoid passing deployment settings values around, your options are:
- Use get_current_registry()
- Use a pattern like
http://docs.pylonsproject.org/projects/pyramid_cookbook/en/latest/configuration.html#django-style-settings-py-configuration
You're encouraged to pass per-request values around to make writing unit
tests pleasant and to make it possible to use more than one Pyramid app
per process. But...
If you don't care about testing but you do care about being able to run
more than one instance of the application in the same process:
def get_setting(setting_name, default=None):
return get_current_registry().settings.get('a', default)
def somefunction(a, b):
beinghosed = get_setting('i-am-hosing-myself')
If you care neither about testing nor about being able to run more than
one Pyramid application per process, the "django-style settings"
cookbook entry linked above puts the gun in your hands; it's pre-pointed
at your feet.
- C
The globals are not my cup of tea so my feet are currently safe from
that Django pattern. :) I do care about testing and have written unit
tests (although the code I need this in is currently not covered by the
tests), but just to understand the issue at hand, what can go wrong if I
only read the setting like in your get_setting def example?
I know about thread safety and writing shared data, and I know from C
that even reading data which is in the middle of a write by another
thread can produce corrupt results (especially with nonatomic data
types). Is that the case even in Python? The settings I need to read are
some custom config values that never change in the life of the
application (and I say this fully aware of the fact that almost
everything in Python is a reference, especially when dealing with lists
and dictionaries which can be changed "accidentally").
Basically my problem is only that of the separation of concerns. I have
a database model that logs certain operational stuff (not python logger)
and I want it to mail the error entries to the admin, so I want it
"agnostic" to the request chain and views used, because it can be called
in views, or command line scripts (using pyramid.paster.bootstrap),
etc... It requires admin address and smpt server data given in the
config files.
Thanks!
.oO V Oo.
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
It means in test code you will need to do stupid things.
For example, to test this function:
def thething():
return get_current_registry().settings['a']
You will need to write this:
import unittest
from pyramid import testing
class TheTest(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
self.config.settings['a'] = 1
def tearDown(self):
testing.tearDown()
def test_the_thing(self):
from mycode import thething
result = thething()
self.assertEqual(result, 1)
If you had just created a "thething" function which accepted settings,
it would look more like this:
def thething(settings):
return settings['a']
And the test for it would be less dumb:
import unittest
from pyramid import testing
class TheTest(unittest.TestCase):
def test_the_thing(self):
from mycode import thething
result = thething({'a':1})
self.assertEqual(result, 1)
> I know about thread safety and writing shared data, and I know from C
> that even reading data which is in the middle of a write by another
> thread can produce corrupt results (especially with nonatomic data
> types). Is that the case even in Python? The settings I need to read are
> some custom config values that never change in the life of the
> application (and I say this fully aware of the fact that almost
> everything in Python is a reference, especially when dealing with lists
> and dictionaries which can be changed "accidentally").
You wont have any problem reading these values if they never change;
it's just a design concern.
> Basically my problem is only that of the separation of concerns. I have
> a database model that logs certain operational stuff (not python logger)
> and I want it to mail the error entries to the admin, so I want it
> "agnostic" to the request chain and views used, because it can be called
> in views, or command line scripts (using pyramid.paster.bootstrap),
> etc... It requires admin address and smpt server data given in the
> config files.
Even bootstrap returns a "request" object. The deployment settings can
be obtained via request.registry.settings.
- C
It's just a matter of the separation of concerns. The code I need it in
is confined to the model, defined in a "pluggable" model module. It's
not that I can't redesign the chain of calls in order to pass the
request object around, I was just looking at feasible alternatives to
the required refactoring if the request object was to be passed around.
.oO V Oo.
Style concerns aside, I think using or disusing the ZCA is not relevant
here; Vlad wants to be able to access deployment settings without
passing any contextual values around. He could use a global ZCA
registry to obtain global settings, but it's still a global, and has all
the downsides of using any other global.
- C
> To unsubscribe from this group, send email to pylons-discuss
> +unsub...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to pylons-discuss
> +unsub...@googlegroups.com.
Style concerns aside, I think using or disusing the ZCA is not relevant
here; Vlad wants to be able to access deployment settings without
passing any contextual values around. He could use a global ZCA
registry to obtain global settings, but it's still a global, and has all
the downsides of using any other global.
[...]
We're using the zca as the way our components interact, by instantiating them inside adapters of request, context, and sometimes view. This is still being done using the registry attached to request, so it's not the global zca registry or global zca api. ie we get them by doing:# get the right kind of Foobar for current context & viewadapter_of_foobar = request.registry.queryAdapter(request, context, view)
We're using the zca as the way our components interact, by instantiating them inside adapters of request, context, and sometimes view. This is still being done using the registry attached to request, so it's not the global zca registry or global zca api. ie we get them by doing:# get the right kind of Foobar for current context & viewadapter_of_foobar = request.registry.queryAdapter(request, context, view)
I guess we're going off on a tangent here, but how does queryAdapter know how to get the right kind of Foobar?
--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pylons-discuss/-/EqyPEdN-B2YJ.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.