pip install python-dotenv
Example №1:
# file: .env
FirstVar = Hello, world!
SecondVar = 42
ThirdVar = 23
from envserv import EnvBase
class MyEnv(EnvBase):
__envfile__ = '.env'
FirstVar:str
SecondVar:int
ThirdVar:float
env = MyEnv()
print(env) # EnvServ(FirstVar:<class 'str'> = Hello, world!, SecondVar:<class 'int'> = 42, ThirdVar:<class 'float'> = 23.0)
print(env.FirstVar, env.SecondVar,env.ThirdVar) # Hello, world! 42 23.0
print(type(env.FirstVar), type(env.SecondVar), type(env.ThirdVar)) # <class 'str'> <class 'int'> <class 'float'>
Example №2:
from envserv import EnvBase
class MyEnv(EnvBase):
__envfile__ = '.env'
FirstVar:str
env = MyEnv()
env.FirstVar = "New variable value" # Also changes a variable in the .env file
print(env) # EnvServ(FirstVar:<class 'str'> = New variable value)
print(env.FirstVar) # New variable value
print(type(env.FirstVar)) # <class 'str'>
Example №3:
# file: .env
pass = 100
from envserv import EnvBase, variable
class MyEnv(EnvBase):
__envfile__ = '.env'
pass_:int = variable(alias='pass',overwrite=False)
env = MyEnv()
print(env) # EnvServ(pass_:<class 'int'> = 100)
print(env.pass_) # 100
print(type(env.pass_)) # <class 'int'>
env.pass_ = 1 # envserv.errors.EnvVariableError: Error overwriting variable pass_: It cannot be overwritten
Example №4:
# file: .env
A = Text
ERR = this is error
C = [1, 2, 3, 4, 5]
D = {1: 2, 3: 4}
E = null
F = {1,2,3,4,5}
from envserv import EnvBase, Variable
class MyEnv(EnvBase):
__envfile__ = '.env'
A:str
B:int = Variable(alias="ERR", error=False)
C:list
D:dict
E:None
F:set
env = MyEnv()
print(env.all())
print(env.json())
# Вывод:
# {'A': 'Text', 'B': 'this is error', 'C': [1, 2, 3, 4, 5], 'D': {1: 2, 3: 4}, 'E': None, 'F': {1, 2, 3, 4, 5}}
# {"A": "Text", "B": "this is error", "C": [1, 2, 3, 4, 5], "D": {"1": 2, "3": 4}, "E": null, "F": [1, 2, 3, 4, 5]}
- Model added
- Added variable change
- Added class instance information output
- Added rules for variable (beta)
- Added setting for docker-compose (Variable __envfile__ does not need to be written)
- Fix alias param
- Fix error message
- Added support for the list, dict and None variable
- Added parameter encoding to the class instance
- Added function all() to display the dictionary
- Code refactoring has been completed
- Added class Variable
- Added
toString
parameter to all() function and added json() function