<commit_before># Centroid - Python

## Installation

The Centroid Python package is hosted at [PyPi](https://pypi.python.org/pypi/centroid). 

You can install Centroid using pip with `pip install centroid`, or you can download Centroid, unpack, and `python setup.py install` it.

## Usage

Begin by declaring your application's configuration values in JSON. The following example stores the database's server address.

```json
{
    "database": {
        "serverAddress": "my-server.local"
    }
}
```

With Centroid's API, Python applications can then use the configuration values in this JSON.

### Python API

The Python API is available through the `centroid.Config` class, an instance of which is used to consume the JSON. 

The `Config` class exposes the static `from_file` method, a constructor, and the instance method `for_environment`.

#### from_file(filename)

You can create an instance of `Config` from a JSON file using the static `Config.from_file(filename)` method.

In the example below, note the `serverAddress` configuration value is retrieved using the snake_case `server_address` property even though the value is specified as camelCase in the JSON.

```py
# from_file.py
config = Config.from_file("config.json")
server = config.database.server_address # => "my-server.local"
```

#### Config(json)

Alternatively, you can create an instance of `Config` by passing a JSON string to the `Config` constructor.

```py
# from_string.py
json = '{ "database": { "serverAddress": "my-server.local" } }'
config = Config(json)
server = config.database.server_address # => "my-server.local"
```

#### for_enviroment(environment)

Typically, real-world applications have different configuration values depending on the environment. For example, you might have *dev* and *prod* environments that use different servers, user accounts, etc. However, applications usually have other configuration values that are the same across all environments. Centroid makes it easy to retrieve all of the configuration values you need for a specific environment.

In environment-based configuration, the top-level objects in the JSON represent the various environments. Place the configuration values that are the same in all environments within the *all* environment. 

```json
{
    "dev": {
        "someResource": {
            "server": "resource-dev.local"
        }
    },
    "prod": {
        "someResource": {
            "server": "resource-prod.local"
        }
    },
    "all": {
        "keys": {
            "ssh": "path/to/id_rsa.pub"
        }
    }
}
```

Then, in the `Config` instance, use the instance method `for_environment` to retrieve the environment-based configuration. Centroid merges the requested environment's configuration values with the *all* environment configuration values. 

In the following example, the configuration for `prod` is merged with the configuration from `all`; the result is then available from a new instance of `Config`.

```py
# for_enviroment.py
config = Config.from_file("config.json").for_environment("prod")
server = config.some_resource.server # => "resource-prod.local"
solution_path = config.keys.ssh # => "path/to/id_rsa.pub"
```

## Contributing

See the [Contributing section of the main README](../README.md#contributing).<commit_msg>Corresponding slice 'n dice updates for Python<commit_after># Centroid - Python

This readme file includes information specific to Python in Centroid. Refer to the [Centroid Readme file] (../README.md) for general information, including information the JSON configuration file. 

## Installation

The Centroid Python package is hosted at [PyPi](https://pypi.python.org/pypi/centroid). 

Install Centroid using pip with `pip install centroid` or by downloading Centroid, unpacking it, and using `python setup.py install`.

## Centroid.Config Class with Python

In Python, the `Centroid.Config` class exposes the following: 

+ Static `from_file` method
+ Constructor
+ `for_environment` instance method

> *Note:* The examples given in the following sections are based on the JSON configuration file examples in the [Centroid Readme file] (../README.md). 

### from_file Method

Using the static `Config.from_file(filename)` method, you can create an instance of `Config` from a JSON file, as in the example below. 

```py
# from_file.py
config = Config.from_file("config.json")
server = config.database.server_address # => "my-server.local"
```

### Config Constructor

To load a string instead of a file, create an instance of `Config` by passing a JSON string to the `Config` constructor, as in the following example.

```py
# from_string.py
json = '{ "database": { "serverAddress": "my-server.local" } }'
config = Config(json)
server = config.database.server_address # => "my-server.local"
```

### for_enviroment Instance Method

In the `Config` instance, you can use the `for_environment` instance method to retrieve the configuration values for an environment. 

If you specify an environment in `for_environment`, Centroid will merge the requested environment's configuration values with the values in *all*. Refer to [Examples in the Centroid Readme file] (../README.md#examples) for information on creating an environment-based JSON configuration file. 

With the following example, Centroid will merge the configuration for *prod* with the configuration for *all*; the result is then available from a new instance of `Config`.

```py
# for_enviroment.py
config = Config.from_file("config.json").for_environment("prod")
server = config.some_resource.server # => "resource-prod.local"
solution_path = config.keys.ssh # => "path/to/id_rsa.pub"
```
{'fraction_non_alphanumeric': 0.09946902654867257, 'fraction_numerical': 0.0, 'mean_word_length': 4.465183752417795, 'pattern_counts': {'":': 15, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1825763', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1499, 'n_words': 628}