IceKhan13/purplecaffeine

Classes parameters through env variables

IceKhan13 opened this issue · 0 comments

What is the expected enhancement?

In order to smoothly integrate purple caffeine with other system it might be beneficial to be able to create object without exactly specifing all constructor parameters, but provide them though env variables.

Let me explain. Instead of this

class Trial:
  def __init__(self, name: str, storage: Optional[BaseStorage] = None):
    self.name = name
    self.storage = storage

have something like this

class Trial:
  def __init__(self, name: Optional[str] = None, storage: Optional[BaseStorage] = None):
    self.name = name or os.environ.get("PURPLE_CAFFEINE_TRIAL_NAME")
    if self.name is None:
      raise PurplecaffeineException("Please specify name or trial or configure it using env variables")
    if storage is None:
      storage_type = os.environ.get("PURPLE_CAFFEINE_STORAGE_CLASS", "LocalStorage")
      storage_mapping: Dict[str, BaseStorage] = {
        "LocalStorage": LocalStorage,
        "S3Storage": S3Storage,
        "ApiStorage": ApiStorage
      }
      self.storage = storage_type.get(storage_type)()
    else:
      self.storage = storage

Now if we have all env variable properly set we can just write

with Trial() as trial:
    trial.add_...

We need to have the same behavior with all parameters for Trial, LocalStorage, S3Storage, ApiStorage.