lundi 23 mai 2016

What is python best practice to to detect then influence behavior if in development?

I'm new to python and I want to know the pythonic way to influence behavior differently between my run time test environment and production.

My use case is I'm using a decorator that needs different parameters in test vs production runs.

It looks like this:

# buffer_time should be 0 in test and 5000 lets say in prod
@retry(stop_max_attempt_number=7, wait_fixed=buffer_time)
def wait_until_volume_status_is_in_use(self, volume):
  if volume.status != 'in use':
    log_fail('Volume status is ... %s' % volume.status)
    volume.update()

  return volume.status

One solution is use os environment variables.

At the top of a file I can write something like this

# some variation of this
buffer_time = 0 if os.environ['MODE'] == 'TEST' else 5000

class Guy(object):
# Body where buffer_time is used in decorator

Another solution is to use a settings file

# settings.py
def init():
  global RETRY_BUFFER
  RETRY_BUFFER = 5000

# __init__.py
import settings

settings.init()

# test file
from my_module import settings
settings.RETRY_BUFFER = 0

from my_module.class_file import MyKlass

# Do Tests

# class file
import settings
buffer_time = settings.RETRY_BUFFER

class Guy(object):
# Body where buffer_time is used in decorator

Ultimately my problem with both solutions is they both used shared state.

I would like to know what is the standard way to accomplish this.

Aucun commentaire:

Enregistrer un commentaire