"Envist is a simple .env file parser for Python. It's a single file module with no dependencies."
Project description
Envist
A simple, lightweight, and easy to use environment variable manager for Python.
Created by Md. Almas Ali
Table of Contents
Features
Envist a simple, lightweight, and easy to use environment variable manager for Python. It can be used to get, set, unset, and save environment variables to a file. It also supports casting environment variables to a specific types as you need. It not only supports the casting from language spacific types but also supports casting from .env
file spacific types. For example, if you have a .env
file like this:
# .env
name = John Doe
age = 20
is_admin = True
Then you can get the name
variable as str
, age
variable as int
, and is_admin
variable as bool
like this:
import envist
env = envist.Envist()
name = env.get('name', cast=str) # Output: 'John Doe'
age = env.get('age', cast=int) # Output: 20
is_admin = env.get('is_admin', cast=bool) # Output: True
It has a spacial feature like never before. Now, with envist
you can typecast your environment variables inside your .env
file. For example, if you have a .env
file like this:
# .env
name <str> = John Doe
age <int> = 20
is_admin <bool> = True
Then you can get the name
variable as str
, age
variable as int
, and is_admin
variable as bool
like this:
import envist
env = envist.Envist()
name = env.get('name') # Output: 'John Doe'
print(type(name)) # Output: <class 'str'>
age = env.get('age') # Output: 20
print(type(age)) # Output: <class 'int'>
is_admin = env.get('is_admin') # Output: True
print(type(is_admin)) # Output: <class 'bool'>
Here you don't need to specify the cast
parameter. It will automatically cast the environment variables to the types you specified in your .env
file. This is like a constant typecasting. You can also override the typecasting by specifying the cast
parameter.
Envist also supports variable expansion. For example, if you have a .env
file like this:
# .env
server <str> = 127.0.0.1
port <int> = 8080
url <str> = http://${server}:${port}
Then you can get the url
variable like this:
import envist
env = envist.Envist()
url = env.get('url') # Output: 'http://127.0.0.1:8080'
Installation
Install envist
using pip
:
pip install envist
Functions
Envist class has the following functions:
Function | Description | Parameters | Return Type |
---|---|---|---|
get |
Get a specific env variable | key (str), default (any), cast (type) |
any |
get_all |
Get all env variables | None | dict |
set |
Set a specific env variable | key (str), value (any) |
None |
set_all |
Set multiple env variables | variables (dict) |
None |
unset |
Unset a specific env variable | key (str) |
None |
unset_all |
Unset multiple env variables | keys (list) |
None |
save |
Save updated env variables to file | pretty (bool), sort_keys (bool) |
None |
Data Types
Envist supports the following data types:
Data Type | Description | Status |
---|---|---|
str |
String | Supported |
float |
Float | Supported |
bool |
Boolean | Supported |
list , List |
Array | Supported |
dict , Dict |
Object | Development |
tuple , Tuple |
Tuple | Development |
set , Set |
Set | Development |
CSV |
CSV | Development |
JSON |
JSON | Development |
Note: Multi-line expressions are not supported yet. It will be supported in the next version.
Examples
from envist import Envist
env = Envist()
# Print env object as string
print(env)
# <Envist path=".env">
# Get default env file path
print(env.path)
# .env
# Custom env file path or file name
env = Envist(path='my/path/.env')
# Get a specific env variable
env.get('name')
# Get a specific env variable with default value
env.get('name', default='John Doe')
# Get a specific env variable with default value and cast
env.get('name', default='John Doe', cast=int)
# Get all env variables
env.get_all()
# Set a specific env variable
env.set('name', 'John Doe')
env.set('age', 20)
env.set('is_admin', True)
# Set multiple env variables
env.set_all({
'name': 'John Doe',
'age': 20,
'is_admin': True
})
# Unset a specific env variable
env.unset('name')
# Unset multiple env variables
env.unset_all(['name', 'age'])
# Unset all env variables
env.unset_all()
# Save updated env variables to file
# Save to default file, i.e. .env. You can also specify a custom file path in Emvist constructor object.
env.save(
pretty=True, # Pretty print
sort_keys=True # Sort keys
)
# Get a list from env variable
# list_elements <list> = 1, 2, 3, 4, 5
env.get('list_elements', cast=list)
# Working with variables
# server <str> = 127.0.0.1
# port <int> = 8080
# url <str> = http://${server}:${port}
env.get('url') # Output: 'http://127.0.0.1:8080'
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
- Fork it.
- Create your feature branch (
git checkout -b feature/feature-name
). - Commit your changes (
git commit -am 'Add some feature'
). - Push to the branch (
git push origin feature/feature-name
). - Create a new Pull Request.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file envist-0.0.4.tar.gz
.
File metadata
- Download URL: envist-0.0.4.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7f1afb18d74a99a6cdf32b19494a881dd0e5103442800cec4d9a7cd4c606f206 |
|
MD5 | f040bc6c4a241f01d978f294df3c4c50 |
|
BLAKE2b-256 | eb6a1337d79deccfb1427d519c7617914095cad241f34455d4bbbc8e92fa55d0 |
File details
Details for the file envist-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: envist-0.0.4-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7550af15b6149e20cd5ce3b8b04dcd60bb6b69a2186c39ad43d33bb4c3d423e8 |
|
MD5 | c5b12035277c453b5899e28ee4bb67fa |
|
BLAKE2b-256 | 297d3fb867af1feae311e6c281b077de3cfa897e7da7474554375ec7bfb68b08 |