Skip to content

Instantly share code, notes, and snippets.

@Chimezirim-Bassey
Created December 5, 2024 21:15
Show Gist options
  • Save Chimezirim-Bassey/c08b1d7294d2f6551f0041d65332efd7 to your computer and use it in GitHub Desktop.
Save Chimezirim-Bassey/c08b1d7294d2f6551f0041d65332efd7 to your computer and use it in GitHub Desktop.
from typing import Self
from os import environ
from dotenv import load_dotenv, dotenv_values
class Config:
_instance: Self
# you can add type hints here if you want
AWS_ACCESS_KEY_ID: str
AWS_SECRET_ACCESS_KEY: str
my_secret: str
my_constant: float
def __new__(cls, *args, **kwargs):
if getattr(cls, '_instance', None) is None:
cls._instance = super(Config, cls).__new__(cls)
cls._instance.load_dotenv(**kwargs)
cls._initialized = True
return cls._instance
cls._instance.set_attributes(**kwargs)
return cls._instance
def __getattr__(self, item):
attr = environ.get(item)
setattr(self, item, attr) if attr is not None else None
return attr
def set_attributes(self, **kwargs):
[setattr(self, key, value) for key, value in kwargs.items()]
def load_dotenv(self,**kwargs):
params = ["dotenv_path", "stream", "verbose", "override", "interpolate", "encoding"]
params = {key: kwargs.pop(key) for key in params if key in kwargs}
load_dotenv(**params)
values = kwargs | dotenv_values()
self.set_attributes(**values)
env = Config(my_constant=3.14)
print(env.AWS_ACCESS_KEY_ID, env.AWS_SECRET_ACCESS_KEY, env.my_secret, env.my_constant, env.PATH)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment