Skip to main content

defaultenv

Environment and config file reader for python3. Warrning: slightly magic inside. This module magically read and use environment value both from '.env' file and environment itself.

Since version 0.0.6 .env file will be rereaded on the fly on next env call, so now your environment is always up to date.

$ echo "MY_VAL='test'" > .env
$ python
>>> from defaultenv import env
>>> env('PATH')
'/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin'
>>> env('TEST')
>>> env('MY_VAL')
'test'
>>> import os; os.environ['MY_VAL']
'test'

env method may be used to check the value of variable. If variable is not defined env method will return None. If both environment variable and corresponding .env record is exist then .env have a priority. And yes, you can use os.environ instead of env(), all records from .env will be exported immidiately.

For additional convinience you can use env() with default argument, for example:

>>> from defaultenv import env
>>> env('TEST', 'no test')
'no test'
>>> env('UID')
'1000'
>>> env('UID', int)
1000
>>> env('HOME', pathlib.Path)
PosixPath("/home/bobuk")
>>> env('PATH', lambda x: [pathlib.Path(_) for _ in x.split(':')])
[PosixPath('/usr/local/sbin'), PosixPath('/usr/local/bin'), PosixPath('/usr/sbin'), PosixPath('/usr/bin'),
 PosixPath('/sbin'), PosixPath('/bin')]

If default argument for env() is not empty, and key what you looking for is not exists env() will return you value of default. But if default is callable (like object, lambda or function) then instead value of key from environment will be passed to this callable. My favorite is to send just int because it's the easiest way to convert your default to integer.

Since version 0.0.2 for more convinience two classes (ENV and ENVC) was added. You can use your environment variable name without method calling.

$ python
>>> from defaultenv import ENV
>>> ENV.PATH
'/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin'

ENV usage removing unnesesery typing of perenthesis and quotes. In this example i've save 4 keystrokes for you. But it's not very convinient to type uppercased name everytime. Let's imagine what my pinky finger is killing me.

$ python
>>> from defaultenv import ENVC as env
>>> env.shell
'/usr/local/bin/zsh'
>>> env.home
'/home/bobuk'

As you can see ENVC convert your variable name to uppercase. For both ENVC and ENV there's a method defaults to add default values of callables (as for env above).

>>> from defaultenv import ENVC as env
>>> env.defaults(test = 1, path = lambda x: x.split(':'), pid=int)
>>> env.test
1
>>> env.path
['/usr/local/sbin', '/usr/local/bin', '/usr/sbin', '/usr/bin', '/sbin', '/bin']

What the difference between os.environ.get('PATH', None) and env.path? It's easy to calculate and the result is 21 (which is half of 42).

Since version 0.0.9 you can use even more authomated ENVCD which is ENVC but with predefined defaults. Every path (or colon-separated paths list) will be defaulted to PosixPath, every digital value converted to int.

>>> from defaultenv import ENVCD as env
>>> env.path
[PosixPath('/usr/local/bin'), PosixPath('/usr/local/bin'), PosixPath('/usr/bin'), PosixPath('/bin'), PosixPath('/usr/sbin')]

Metadata

Release files for defaultenv 0.0.14

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defaultenv 0.0.14
File Size Uploaded
defaultenv-0.0.14.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for defaultenv 0.0.14
File Interpreter ABI Platform
defaultenv-0.0.14-py3-none-any.whl Python 3 none any Details

Total release size: 9.0 kB

Release files / defaultenv-0.0.14.tar.gz

Download URL defaultenv-0.0.14.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8fdaa393e4bc20403cc4c22fc56803b1bd57a2da0a90ba5e807a13f2f2cd03a2
BLAKE2b-256 checksum
How to use checksums
89eaecb87225510b9af8098b9d2e92d0d862a90a127994ef93716861b295f313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.3 Darwin/19.5.0

Release files / defaultenv-0.0.14-py3-none-any.whl

Download URL defaultenv-0.0.14-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
feed4e30e236a44dbdc976f0f910d420cf1403bbda3b0396d8ba8e73d768b670
BLAKE2b-256 checksum
How to use checksums
03efc7942b5d8ca0c02ab62725fd38b1c97904104168b422e5e281aab6d0f503
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.3 Darwin/19.5.0

Release history Release notifications | RSS feed

This release

0.0.14 This release

2 release files

0.0.12

1 release file

0.0.11

1 release file

0.0.10

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page