Skip to main content

A library for defining, parsing, and validating settings passed as environment variables

Project description

# Varsity

Varsity helps you define your application’s settings, read them from environment variables, parse them into native Python types, and validate them.

## Load a simple string

Assume the FOO environment variable as been set to ‘3’. Here we define a loader, use it to give our ‘foo’ setting a name, and tie it to the ‘FOO’ environment variable. We get back a dictionary with a key for each setting we’ve added to the loader.

>>> from varsity import Loader
>>>
>>> l = Loader()
>>> l.add('foo', 'FOO')
>>> settings = l.load()
>>> settings['foo']
'3'

The object returned from .load() also provides attribute-style access to settings.

>>> settings.foo
'3'

You can provide a ‘typ’ callable that will be used to convert the environment variable string into the type of your choice.

>>> l.add('foo', 'FOO', typ=int)
>>> settings = l.load()
>>> settings.foo
3

You can provide defaults that will be returned if the environment variable is not present.

>>> l.add('some_unset_var', 'SOME_UNSET_VAR', default=0)
>>> settings = l.load()
>>> settings.some_unset_var
0

If you don’t provide a default, and the environment variable is not set, ValueError will be raised.

You can provide your own callable as the ‘typ’ argument. Assume the ‘TODAY’ environment variable is set to ‘2016-05-05’.

>>> from varsity import Loader
>>> from iso8601 import parse_date
>>> l = Loader()
>>> l.add('today', 'TODAY', typ=lambda x: parse_date(x).date())
>>> settings = l.load()
>>> settings.today
datetime.date(2016, 5, 5)

If you don’t provide a ‘typ’, but you do provide a default, then the environment variable will be cast to the same type as the default. (Here we get back an int 3 instead of the string ‘3’, because the default is an int.)

>>> l.add('foo', 'FOO', default=0)
>>> settings = l.load()
>>> settings.foo
3

If you access a setting with the attribute-style syntax, then nested dictionaries can also be accessed with that syntax. In this example, the FOO environment variable is set to ‘{“bar”: {“baz”: 1.23}}’

>>> l.add('foo', 'FOO', typ=json.loads)
>>> settings = l.load()
>>> settings.foo.bar.baz
1.23

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

varsity-0.0.2.tar.gz (2.5 kB view details)

Uploaded Source

File details

Details for the file varsity-0.0.2.tar.gz.

File metadata

  • Download URL: varsity-0.0.2.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for varsity-0.0.2.tar.gz
Algorithm Hash digest
SHA256 6fdcda7c4808d0107a2c5175db037507c723d62910391fcbb9653be9ed9616e2
MD5 62c928c80be920331a12b2ef2ed3da7b
BLAKE2b-256 6bc8b1ce5312286868f2d5a490da1816e8d8a9c759bf8f6c5535516e3433fc54

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