On Thu, 2011-12-01 at 18:17 +0100, Vlad K. wrote:
> Excellent suggestions, thanks!
>
> 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?
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
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" 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/pylons-discuss?hl=en.