bobbie¶
| Version | |
| Status | |
| Documentation | |
| Tools | |
| Compatibility | |
| Stats | |
What is bobbie?¶
Named after Roberta Draper ("Bobbie") from "The Expanse," bobbie provides
a lightweight and easy-to-use way to store and access configuration settings for a Python project.
Why use bobbie?¶
There are numerous options for storing configuration settings in Python.
So, what makes bobbie different?
- Flexible: a
Settingsinstance is easily built from adict, Python module, or file (ini, json, toml, and yaml formats are supported). - Lightweight: an efficient codebase ensures a very small memory footprint.
- Intuitive: a
createclass method constructsSettings(from any data source). - Convenient: unlike
configparser, automatic data and type validation is performed whenSettingsis created.
The comparison table
below shows how bobbie compares to the other major options that store
configuration options for Python projects.
Getting started¶
Installation¶
To install bobbie, use pip:
Create a Settings instance¶
bobbie supports several ways to create a Settings instance. You can either:
* If the settings are in a file, call a class method specific to the file type (from_yaml in the second example below);
* Call a class method for the general type of source data (from_dict or from_file);
* Call the create class method and it will automatically figure out the appropriate constructor; or
* If the settings data is in a dict-like object, pass it as the first argument to Settings.
From a dict¶
import bobbie
configuration = {
'general': {
'verbose': False,
'seed': 43,
'parallelize': False},
'files': {
'source_format': 'csv',
'file_encoding': 'windows-1252',
'float_format': '%.4f',
'export_results': True}}
# You can pick a specific constructor method.
settings = bobbie.Settings.from_dict(configuration)
# Or, may use the general `create` method.
settings = bobbie.Settings.create(configuration)
# Or, just send a `dict` to `Settings` itself.
settings = bobbie.Settings(configuration)
From a file¶
import bobbie
# You may use the general `create` method.
settings = bobbie.Settings.create('settings_file.yaml')
# Or, the `from_file` method.
settings = bobbie.Settings.from_file('settings_file.yaml')
# Or, the `from_yaml` method. They all do the same thing.
settings = bobbie.Settings.from_yaml('settings_file.yaml')
If the file is a Python module, it must contain a variable named settings in the module namespace
(unless you change the global setting for the variable name).
Type inference¶
ini and json files often store numbers, booleans, and lists as strings, so
bobbie converts their values to the matching Python types:
'42'becomes42and'2.5'becomes2.5.'true','yes','false', and'no'(in any case) becomeTrueorFalse.'[x, y, z]'and'x, y, z'become['x', 'y', 'z'].'{a: 1, b: [x, y]}'becomes{'a': 1, 'b': ['x', 'y']}.
Items in lists and dicts are converted the same way and may be nested. Put an
item in quotes (such as ["a, b", '1']) to keep it as a string.
toml, yaml, and Python module values are not converted because those
formats already store their own types. So, a toml value of "[a, b]" stays a
string.
Contributing¶
Contributors are always welcome. Feel free to grab an issue to work on or make a suggested improvement. If you wish to contribute, please read the Contribution Guide and Code of Conduct.
Similar projects¶
There are a lot of great packages for storing project settings. The table below shows the features of the leading libraries.
Feature Comparison of Python Configuration Libraries¶
| Library | Typing | Secrets | dict | ini | json | py | toml | yaml |
|---|---|---|---|---|---|---|---|---|
bobbie |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
configParser |
✅ | |||||||
dynaconf |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ||
Parser-it |
✅ | ✅ | ✅ | ✅ | ✅ | |||
python-decouple |
✅ | ✅ | ✅ | |||||
pyconfig |
✅ | |||||||
pydantic-settings |
✅ | ✅ |
As you can see, bobbie lacks a method for storing passwords, encryption keys,
and other secrets. That is because it is focused on internal Python
projects and the goal of keeping its resource usage as low as possible. So, if
you need secrets stored in your project settings, there are several good options
linked above that you should explore.
License¶
Use of this repository is authorized under the Apache Software License 2.0.