Skip to main content

An easy way to handle application configuration (and open a schism to a dimention of endless pain and suffering).

Project description

# Lament

__"An easy way to handle application configuration (and open a schism to a dimention of endless pain and suffering)."__

## Example

Here I will go through a number of examples and explain the finer details of how to write a configuration parser using Lament.

First things first, here is the simplest example imaginable, a parser with a single option that simply overwrites the privious value but does no type checking:

class Example(LamentConfig):
# 'myConfigOption': 'abc'
# -> lamentconfig.myConfigOption = 'abc'
@config('myConfigOption', str)
def myConfigOption(self, old, new):
return new

Each parser inherits `LamentConfig` and each option has a decorated handler function. In this case the decorator is `@config` and takes two params, the name of the option and it's default type.

The handler function itself also takes two params, `old` and `new`. `old` is the current value of that option (If the option's value is never set it is infered to be the default return value of the type, so in this case `str()`). `new` is the value that was read from the config file. Because Lament stores config in JSON format, most of the time these values will be strings or numbers, but they can also be lists or dicts. If you want Lament to hold these in the form of some custom object, this handler is your chance to convert it. Anything you return will be set as the new value.

# 'myConfigOption': 5
# -> lamentconfig.myConfigOption = CustomType(5)
@config('myConfigOption', CustomType)
def myConfigOption(self, old, new):
if isinstance(new, int):
return CustomType(new)
logging.warn('can't use type %s as a value for myConfigOption' % type(new))
return old

In the above example, the input is checked to make sure it's an `int`, then it's used to instansiate a new `CustomType` object. If someone accidentally passes in the wrong type, it warns them and sticks with the old config value.

# 'myConfigOption': 'abc'
# -> lamentconfig.myConfigOption.add('abc')
# 'myConfigOption': 'abc!'
# -> lamentconfig.myConfigOption.discard('abc')
@config('myConfigOption', set)
def myConfigOption(self, old, new):
if isinstance(new, str)
if new[-1] == '!':
return old
return old
logging.warn('can't use type %s as a value for myConfigOption' % type(new))
return old

This is a complex example where the config option stores a `set` of strings. If the `str` provided in the config ends with an '!', it's removed from the set, otherwise it's added.

### Example using regexs

You can also create a config options that behave like dictionaries, holding multiple key/value pairs. Best of all, you can control which of the keys are valid with regular expressions. This is done with the `@regex_config` decorator:

# 'host': 'localhost:80'
# ->[''] = ('localhost', 80)
@regex_config('hosts', '.*', tuple)
def hosts(self, old, new):
host, port = new.split(':')[:1]
return (host, int(port))

The `@regex_config` decorator takes 3 params, the option name, a regular expression used to filter keys and the default type. Apart from that the method acts just like the other handlers.

### Exporting custom data

You can also explicitly define handlers to be used for any config option during export. This is especially handy if you've stored the data with a custom type.

def export_numbers(self, obj):
return [z for z in obj if isinstance(z, int)]

With these handlers you just receive the current value of that config option, in this case it's named `obj`, which gets filtered so as to only return values of type int. Lament takes the return value and dumps it to the new (JSON) config file.

## The Lament Configuration

This project's name was inspired by the puzzle box in the [Hellraiser movies](

## Licence

`lament` is released under the [MIT License](

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for lament, version v0.5
Filename, size File type Python version Upload date Hashes
Filename, size lament-v0.5.tar.gz (5.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page