Skip to main content

Conditionally include code according to environment variables

Project description

Condiment
=========

Conditionally include or remove code portion, according to the environment. It
support offline and on-thy-fly preprocessing.

Conditional features
--------------------

Let's say you want to release a version of your code with or without a feature,
like unlimited life in a game. It can be useful during development, but you
don't want the code to be available in production.

Condiment recognize the environment variables that starts with the prefix
"WITH_" all uppercase. We could name our feature: `WITH_GODMODE`.
In python, you need to include condiment, and install it. You can put it in
exclude block, in order to be removed during the offline preprocessing.

```python
#exclude
import condiment; condiment.install()
#endexclude

class Player:

def die(self):
if not WITH_GODMODE:
self.life -= 1
return self.life
```

You can run it without the godmode:

```
$ python main.py
```

Or by activating the godmode at runtime:

```
$ WITH_GODMODE=1 python main.py
```

You can generate the final version for production too:

```
$ WITH_GODMODE=1 condiment main.py > prod_main.py
$ cat prod_main.py

class Player:

def die(self):
self.life -= 1
return self.life
```

Replacing variables
-------------------

If you want to set an initial value, all the token founds in the environment
will be replaced during the generation. For example, a `WITH_LIFE` token could
have the initial number of life.

```python
#exclude
import condiment; condiment.install()
#endexclude

class Player:

def __init__(self):
Player.__init__(self)
self.life = 10
if WITH_LIFE:
self.life = WITH_LIFE

def die(self):
if not WITH_GODMODE:
self.life -= 1
return self.life
```


Why using condiment ?
---------------------

Compared to others existing preprocessor:

- condiment doesn't rewrite the module on import, it will just inject the
detected variables in the globals() of the module. This is avoiding double
import of your module.
- condiment use python expression for condition (only on if)
- condiment doesn't need you to declare the variable prior the usage of them.
Using environment variables allow you to declare them before launching the
app, and change the behavior of your app easilly.
- condiment will replace all the variables in offline version.

Related projects
----------------

- pypreprocessor
- preprocess

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

condiment-0.5.1.tar.gz (3.8 kB view details)

Uploaded Source

File details

Details for the file condiment-0.5.1.tar.gz.

File metadata

  • Download URL: condiment-0.5.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for condiment-0.5.1.tar.gz
Algorithm Hash digest
SHA256 463e1ecd71900dab80a03187b6c01d662651db25a30ac21aaaf1285dd5ecd7cb
MD5 6982ec80775fbd2975ea3574815d86a9
BLAKE2b-256 45d296494701e64b0ad9e021fdbba8b7595a1552ae7a468edc06b1425781e7d7

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