i too avoid running test code or checks for test code in production.

instead i switch on startup (i.e. in __init__.py:configure)

here i check for flags in the setting and then register (scan) or ignore
certain files or folders (i.e. to mock external authentication in
development etc.)

this means that the views, tests etc. don't have to concern themselves
with such distinctions.

IMHO it's the same pattern we already use by protecting views with
permissions and/or registering views for particular methods - which
means you don't have any if/then constructs in your view checking for
permissions or request methods.

pyramid makes it easy for you to write small, concise view code, hooray! :-)

here's a sample snippet from a project of mine:

if settings.get('testing'):
  config.include('.testing')
  ignore = ['.testing', '.tests']
if not asbool(settings.get('demo')): # pragma: no cover, tests run in
demo mode
  ignore.append('.demo') # don't setup demo services in non-demo mode
config.scan(ignore=ignore)
if asbool(settings.get('preview')): # pragma: no cover, tests run in
preview mode
  config.add_static_view(name='/_preview_',
path=settings.get('preview_templates',
'backrest:templates/preview_templates'))

hth,

tom


On 09/29/2015 10:02, Mike Orr wrote:
> On Mon, Sep 28, 2015 at 8:28 PM, Dmitry Komarov <d9k...@gmail.com> wrote:
>> I want kind of #ifdef in my web service code, condition which checks if this
>> code runs in production or in development mode at the time of check.
>> What's the best and simpliest way?
> 
> I'm using settings like 'appname.devel', 'appname.devel.foo' to enable
> development-only views and the like.
> 
>> How can I get config name from `pserve config_name.ini` command in my view
>> code?
> 
> request.registry.settings["__file__"]     # Absolute path of config file.
> request.registry.settings["here"]   # Directory containing config file.
> 
> However, I'd avoid assuming too much about the filename, whether it
> implies development mode or not. Because it may guess wrong sometime.
> They may be named the developer's initials, or a server name, or a
> name like 'beta', etc.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to pylons-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
Visit this group at http://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to