Skip to main content

"Envist is a simple .env file parser for Python. It's a single file module with no dependencies."

Project description

Envist

Envist Logo

PyPI version PyPI - Python Version PyPI - License Hits

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

  1. Fork it.
  2. Create your feature branch (git checkout -b feature/feature-name).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin feature/feature-name).
  5. 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

envist-0.0.2.tar.gz (6.8 kB view hashes)

Uploaded Source

Built Distribution

envist-0.0.2-py3-none-any.whl (6.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page