Skip to content

Commit

Permalink
Updated casts docs section (#23)
Browse files Browse the repository at this point in the history
* Updated casts docs section

* Removed --quiet arg since it's breaking the build
  • Loading branch information
hernantz authored and osantana committed Apr 8, 2019
1 parent 7f1fc66 commit ddbbc8a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .ci/deps.osx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -x

# Install packages with brew
brew update >/dev/null
brew outdated pyenv || brew upgrade --quiet pyenv
brew outdated pyenv || brew upgrade pyenv

# Install required python version for this build
pyenv install -ks $PYTHON_VERSION
Expand Down
64 changes: 64 additions & 0 deletions docs/source/casts.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Casts
-----

Buitin Casts
~~~~~~~~~~~~

#. ``config.boolean`` - converts values like ``On|Off``, ``1|0``, ``yes|no``,
``true|false`` into booleans.
#. ``config.eval`` - safely evaluate strings with Python literals to Python
objects (alias to Python's ``ast.literal_eval``).
#. ``config.list`` - converts comma separated strings into lists.
#. ``config.tuple`` - converts comma separated strings into tuples.
#. ``config.option`` - get a return value based on specific options:

.. code-block:: python
environments = {
"production": ("spam", "eggs"),
"local": ("spam", "eggs", "test"),
}
# Will return a tuple with ("spam", "eggs") when
# ENVIRONMENT is undefined or defined with `production`
# and a tuple with ("spam", "eggs", "test") when
# ENVIRONMENT is set with `local`.
MODULES = config("ENVIRONMENT",
default="production",
cast=Option(environment))
Custom casts
~~~~~~~~~~~~

You can implement your own custom casting function:

.. code-block:: python
def number_list(value):
return [int(v) for v in value.split(";")]
NUMBERS = config("NUMBERS", default="1;2;3", cast=number_list)
Useful third-parties casts
~~~~~~~~~~~~~~~~~~~~~~~~~~

Django is a popular python web framework that imposes some structure on the way
its settings are configured. Here are a few 3rd party casts that help you adapt
strings into that inner structures:

* `dj-database-url`_ - Parses URLs like ``mysql://user:pass@server/db`` into
Django ``DATABASES`` configuration format.
* `django-cache-url`_ - Parses URLs like ``memcached://server:port/prefix``
into Django ``CACHES`` configuration format.
* `dj-email-url`_ - Parses URLs like
``smtp://[email protected]:[email protected]:465/?ssl=True`` with
parameters used in Django ``EMAIL_*`` configurations.
* `dj-admins-setting`_ - Parses emails lists for the ``ADMINS`` configuration.


.. _dj-database-url: https://github.com/kennethreitz/dj-database-url
.. _django-cache-url: https://github.com/ghickman/django-cache-url
.. _dj-email-url: https://github.com/migonzalvar/dj-email-url
.. _dj-admins-setting: https://github.com/hernantz/dj-admins-setting
4 changes: 2 additions & 2 deletions docs/source/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ See the lookup order of configurations below
.. _contributions: https://github.com/henriquebastos/python-decouple/pull/5


How does prettyconf compare to python-dotenv?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
How does prettyconf compare to python-dotenv_?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

python-dotenv_ reads the key, value pair from .env file and adds them to
environment variable. It is good for some tools that simply proxy the env to
Expand Down
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Contents:
introduction.rst
installation.rst
usage.rst
casts.rst
advanced.rst
loaders.rst
faq.rst
Expand Down
65 changes: 4 additions & 61 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ a specific value type:
The ``boolean`` cast converts strings values like ``On|Off``, ``1|0``,
``yes|no``, ``true|False`` into Python boolean ``True`` or ``False``.

.. seealso::
Find out more about other casts or how to write
your own at :doc:`Casts<casts>`.


Configuration files discovery
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -56,64 +60,3 @@ start looking for configuration files at ``project/app`` until it finds
:py:class:`RecursiveSearch<prettyconf.loaders.RecursiveSearch>` loader.
:doc:`Loaders<loaders>` will help you customize how configuration
discovery works. Find out more at :ref:`discovery-customization`.


Casts
~~~~~

Buitin Casts
++++++++++++

#. ``config.boolean`` - converts values like ``On|Off``, ``1|0``, ``yes|no``,
``true|false`` into booleans.
#. ``config.eval`` - safely evaluate strings with Python literals to Python
objects (alias to Python's ``ast.literal_eval``).
#. ``config.list`` - converts comma separated strings into lists.
#. ``config.tuple`` - converts comma separated strings into tuples.
#. ``config.option`` - get a return value based on specific options:

.. code-block:: python
environments = {
"production": ("spam", "eggs"),
"local": ("spam", "eggs", "test"),
}
# Will return a tuple with ("spam", "eggs") when
# ENVIRONMENT is undefined or defined with `production`
# and a tuple with ("spam", "eggs", "test") when
# ENVIRONMENT is set with `local`.
MODULES = config("ENVIRONMENT",
default="production",
cast=Option(environment))
Custom casts
++++++++++++

You can implement your own custom casting function:

.. code-block:: python
def number_list(value):
return [int(v) for v in value.split(";")]
NUMBERS = config("NUMBERS", default="1;2;3", cast=number_list)
Useful third-parties casts
++++++++++++++++++++++++++

* `dj-database-url`_ - Parses URLs like ``mysql://user:pass@server/db`` into
Django ``DATABASES`` configuration format.
* `django-cache-url`_ - Parses URLs like ``memcached://server:port/prefix``
into Django ``CACHES`` configuration format.
* `dj-email-url`_ - Parses URLs like
``smtp://[email protected]:[email protected]:465/?ssl=True`` with
parameters used in Django ``EMAIL_*`` configurations.



.. _dj-database-url: https://github.com/kennethreitz/dj-database-url
.. _django-cache-url: https://github.com/ghickman/django-cache-url
.. _dj-email-url: https://github.com/migonzalvar/dj-email-url

0 comments on commit ddbbc8a

Please sign in to comment.