Untitled
unknown
python
3 years ago
618 B
110
Indexable
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
Editor is loading...