Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
618 B
78
Indexable
Never
import os

class EnvVar(str):
    pass


def environment(cls):
    class Env:
        def __init__(self):
            self.env_vars = []
            for attribute, val in cls.__dict__.items():
                if isinstance(val, EnvVar):
                    setattr(self, attribute, os.environ.get(attribute))
                    self.env_vars.append(attribute)
    return Env


@environment
class MyEnvironment:
    FOO = EnvVar()


os.environ["FOO"] = "fizz"
print(MyEnvironment().FOO)
# prints fizz
print(MyEnvironment().env_vars)
# prints ['FOO']
os.environ["FOO"] = "buzz"
print(MyEnvironment().FOO)
# prints buzz