Skip to main content

Loads environment into class attributes

Project description

EnvAttributes

v1.1.1

Loads environment into class attributes

Install:

pip install env_attributes

Example:

Environment:

HOST=localhost
PORT=5050

Usage:

Variant 1:

from env_attributes import Environment

path_to_env = 'example/.env'
env = Environment(env_path=path_to_env)

print(env.host, env.port)

output:

localhost 5050

Variant 2:

Correct if the .env file is in the root directory

from env_attributes import env

print(env.host, env.port)

output:

localhost 5050

Additional usage:

print(env.get('host'), env['port'])

Register is not important:

print(env.get('HoSt'), env['PORT'])

You can work with the env object as with a dict:

for key in env:
    value = env.get(key)
    print(key, value)

output:

host localhost
port 5050

You can output all environment variables:

print(env)

output:

{"host": "localhost", "port": "5050"}

You can also get all keys or values from env:

print(env.keys())
print(env.values())

output:

['host', 'port']
['localhost', '5050']

You can get length of env:

print(len(env))

output:

2

NEW

Added ability to specify type annotations for environment variables

Example:

Environment:

BOOL=False
INT=123456789
FLOAT=3.14159265
STRING='Hello World!'
LIST='[123, 2.22, [1, 2, 3], {"key1": "val1", "key2": "val2"}]'
TUPLE='(1, 22, 333, 4444)'
DICT='{"key1": "val1", "key2": "val2", "key3": {"key3.1": 1, "key3.2": 2}}'
CUSTOM='1, 2, 3, 4, 5, 6, 7, 8, 9'

Usage

from env_attributes import Environment, EnvTypes


class EnvironmentTypes(EnvTypes):
    bool: bool
    int: int
    float: float
    string: str
    list: list
    tuple: tuple
    dict: dict

env = Environment(env_types=EnvironmentTypes)

print(env.bool, type(env.bool))
print(env.int, type(env.int))
print(env.float, type(env.float))
print(env.string, type(env.string))
print(env.list, type(env.list))
print(env.tuple, type(env.tuple))
print(env.dict, type(env.dict))

output:

False <class 'bool'>
123456789 <class 'int'>
3.14159265 <class 'float'>
Hello World! <class 'str'>
[123, 2.22, [1, 2, 3], {'key1': 'val1', 'key2': 'val2'}] <class 'list'>
('1', ' 22', ' 333', ' 4444') <class 'tuple'>
{'key1': 'val1', 'key2': 'val2', 'key3': {'key3.1': 1, 'key3.2': 2}} <class 'dict'>

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

env_attributes-1.1.1.tar.gz (3.7 kB view details)

Uploaded Source

File details

Details for the file env_attributes-1.1.1.tar.gz.

File metadata

  • Download URL: env_attributes-1.1.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for env_attributes-1.1.1.tar.gz
Algorithm Hash digest
SHA256 db16a022dee8ba7ca4b8782041c3072b2f38e439669c64c4a12cb9f4ab8a09d1
MD5 4600cc773dfe411afdd605b75830d1c9
BLAKE2b-256 c3a2ce5dbb3a7c43c9156e854c9ce6b6ed7c7e50526c16a6c7271cf89dcd0574

See more details on using hashes here.

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