2019-12-16 16:40:21 +00:00
[![Py versions ](https://img.shields.io/pypi/pyversions/python-benedict.svg?color=blue&logo=python&logoColor=white )](https://img.shields.io/pypi/pyversions/python-benedict.svg?color=blue& logo=python& logoColor=white)
2019-12-18 09:01:02 +00:00
[![PyPI version ](https://img.shields.io/pypi/v/python-benedict.svg?color=blue )](https://pypi.org/project/python-benedict/)
[![PyPI downloads ](https://img.shields.io/pypi/dm/python-benedict.svg?color=blue )](https://pypi.org/project/python-benedict/)
[![Stars ](https://img.shields.io/github/stars/fabiocaccamo/python-benedict?logo=github )](https://github.com/fabiocaccamo/python-benedict/)
[![License ](https://img.shields.io/pypi/l/python-benedict.svg?color=blue )](https://github.com/fabiocaccamo/python-benedict/blob/master/LICENSE.txt)
2019-12-16 16:40:21 +00:00
[![CircleCI ](https://circleci.com/gh/fabiocaccamo/python-benedict.svg?style=svg )](https://circleci.com/gh/fabiocaccamo/python-benedict)
[![Build Status ](https://travis-ci.org/fabiocaccamo/python-benedict.svg?branch=master )](https://travis-ci.org/fabiocaccamo/python-benedict)
[![codecov ](https://codecov.io/gh/fabiocaccamo/python-benedict/branch/master/graph/badge.svg )](https://codecov.io/gh/fabiocaccamo/python-benedict)
[![Codacy Badge ](https://api.codacy.com/project/badge/Grade/0dbd5cc2089f4dce80a0e49e6822be3c )](https://www.codacy.com/app/fabiocaccamo/python-benedict)
2019-07-03 12:34:26 +00:00
[![Scrutinizer Code Quality ](https://scrutinizer-ci.com/g/fabiocaccamo/python-benedict/badges/quality-score.png?b=master )](https://scrutinizer-ci.com/g/fabiocaccamo/python-benedict/?branch=master)
2019-05-17 13:04:22 +00:00
[![Requirements Status ](https://requires.io/github/fabiocaccamo/python-benedict/requirements.svg?branch=master )](https://requires.io/github/fabiocaccamo/python-benedict/requirements/?branch=master)
2019-05-17 11:13:15 +00:00
# python-benedict
2019-11-07 16:52:13 +00:00
python-benedict is a dict subclass with **keypath** support, **I/O** shortcuts (`Base64`, `CSV` , `JSON` , `TOML` , `XML` , `YAML` , `query-string` ) and many **utilities** ... for humans, obviously.
2019-05-17 11:13:15 +00:00
2019-09-20 14:21:31 +00:00
## Index
2019-09-24 08:53:14 +00:00
- [Features ](#features )
- [Requirements ](#requirements )
- [Installation ](#installation )
- [Usage ](#usage )
- [Basics ](#basics )
- [Keypath ](#keypath )
2019-09-20 14:21:31 +00:00
- [Custom keypath separator ](#custom-keypath-separator )
2019-10-29 15:31:49 +00:00
- [Change keypath separator ](#change-keypath-separator )
2019-09-20 14:21:31 +00:00
- [Disable keypath functionality ](#disable-keypath-functionality )
2019-09-24 08:53:14 +00:00
- [API ](#api )
2019-10-03 16:49:01 +00:00
- [Utility ](#utility )
- [`clean` ](#clean )
- [`clone` ](#clone )
- [`dump` ](#dump )
- [`filter` ](#filter )
- [`flatten` ](#flatten )
- [`invert` ](#invert )
- [`items_sorted_by_keys` ](#items_sorted_by_keys )
- [`items_sorted_by_values` ](#items_sorted_by_values )
2019-10-29 15:31:49 +00:00
- [`keypaths` ](#keypaths )
2019-10-03 16:49:01 +00:00
- [`merge` ](#merge )
2019-10-04 13:55:32 +00:00
- [`move` ](#move )
2019-10-03 16:49:01 +00:00
- [`remove` ](#remove )
2019-10-29 15:31:49 +00:00
- [`standardize` ](#standardize )
2019-10-03 16:49:01 +00:00
- [`subset` ](#subset )
2019-10-04 13:55:32 +00:00
- [`swap` ](#swap )
2019-10-29 15:31:49 +00:00
- [`traverse` ](#traverse )
2019-10-14 12:47:49 +00:00
- [`unique` ](#unique )
2019-09-24 08:53:14 +00:00
- [I/O ](#io )
2019-10-03 16:49:01 +00:00
- [`from_base64` ](#from_base64 )
2019-11-07 16:52:13 +00:00
- [`from_csv` ](#from_csv )
2019-09-20 14:21:31 +00:00
- [`from_json` ](#from_json )
2019-10-14 12:47:49 +00:00
- [`from_query_string` ](#from_query_string )
2019-09-20 14:21:31 +00:00
- [`from_toml` ](#from_toml )
2019-09-23 12:14:06 +00:00
- [`from_xml` ](#from_xml )
2019-09-20 14:21:31 +00:00
- [`from_yaml` ](#from_yaml )
2019-10-03 16:49:01 +00:00
- [`to_base64` ](#to_base64 )
2019-11-07 16:52:13 +00:00
- [`to_csv` ](#to_csv )
2019-09-20 14:21:31 +00:00
- [`to_json` ](#to_json )
2019-10-14 12:47:49 +00:00
- [`to_query_string` ](#to_query_string )
2019-09-20 14:21:31 +00:00
- [`to_toml` ](#to_toml )
2019-09-23 12:14:06 +00:00
- [`to_xml` ](#to_xml )
2019-09-20 14:21:31 +00:00
- [`to_yaml` ](#to_yaml )
2019-09-24 08:53:14 +00:00
- [Parse ](#parse )
2019-09-20 14:21:31 +00:00
- [`get_bool` ](#get_bool )
- [`get_bool_list` ](#get_bool_list )
- [`get_datetime` ](#get_datetime )
- [`get_datetime_list` ](#get_datetime_list )
- [`get_decimal` ](#get_decimal )
- [`get_decimal_list` ](#get_decimal_list )
- [`get_dict` ](#get_dict )
- [`get_email` ](#get_email )
- [`get_float` ](#get_float )
- [`get_float_list` ](#get_float_list )
- [`get_int` ](#get_int )
- [`get_int_list` ](#get_int_list )
- [`get_list` ](#get_list )
- [`get_list_item` ](#get_list_item )
- [`get_phonenumber` ](#get_phonenumber )
- [`get_slug` ](#get_slug )
- [`get_slug_list` ](#get_slug_list )
- [`get_str` ](#get_str )
- [`get_str_list` ](#get_str_list )
2019-09-24 08:53:14 +00:00
- [Testing ](#testing )
- [License ](#license )
2019-09-20 14:21:31 +00:00
2019-05-17 11:13:15 +00:00
## Features
2019-10-29 15:31:49 +00:00
- Full **keypath** support using **keypath-separator** *(dot syntax by default)* or **list of keys** .
2019-11-07 16:52:13 +00:00
- Easy **I/O operations** with most common formats: `Base64` , `CSV` , `JSON` , `TOML` , `XML` , `YAML` , `query-string`
2019-06-17 15:34:04 +00:00
- Many **utility** and **parse methods** to retrieve data as needed *(all methods listed below)*
2019-09-24 14:25:53 +00:00
- Well **tested** , check the badges ;)
2019-05-30 11:16:59 +00:00
- 100% **backward-compatible** *(you can replace existing dicts without pain)*
2019-05-17 11:13:15 +00:00
## Requirements
2019-06-01 15:22:00 +00:00
- Python 2.7, 3.4, 3.5, 3.6, 3.7
2019-05-17 11:13:15 +00:00
## Installation
2019-05-30 11:16:59 +00:00
- Run `pip install python-benedict`
2019-05-17 11:13:15 +00:00
## Usage
2019-09-20 14:21:31 +00:00
### Basics
`benedict` is a `dict` subclass, so it is possible to use it as a normal dictionary *(you can just cast an existing dict)* .
2019-05-17 11:13:15 +00:00
```python
from benedict import benedict
2019-09-24 14:25:53 +00:00
# create a new empty instance
2019-07-09 14:04:36 +00:00
d = benedict()
2019-09-20 14:21:31 +00:00
# or cast an existing dict
2019-07-09 14:04:36 +00:00
d = benedict(existing_dict)
2019-07-10 09:24:43 +00:00
2019-10-14 12:47:49 +00:00
# or create from data source (filepath, url or data-string) in a supported format (base64, json, toml, xml, yaml, query-string)
2019-09-24 14:25:53 +00:00
d = benedict('https://localhost:8000/data.json')
2019-09-20 14:21:31 +00:00
# or in a Django view
params = benedict(request.GET.items())
page = params.get_int('p', 0)
```
2019-07-09 14:04:36 +00:00
### Keypath
2019-07-10 09:24:43 +00:00
`.` is the default keypath separator.
2019-07-09 14:04:36 +00:00
2019-09-20 14:21:31 +00:00
If you cast an existing dict and its keys contain the keypath separator a `ValueError` will be raised.
2019-10-29 15:31:49 +00:00
In this case you should use a [custom keypath separator ](#custom-keypath-separator ) or [disable keypath functionality ](#disable-keypath-functionality ).
2019-09-20 14:21:31 +00:00
2019-07-09 14:04:36 +00:00
```python
2019-05-17 11:13:15 +00:00
d = benedict()
2019-07-10 09:44:04 +00:00
# set values by keypath
2019-05-17 11:13:15 +00:00
d['profile.firstname'] = 'Fabio'
d['profile.lastname'] = 'Caccamo'
print(d) # -> { 'profile':{ 'firstname':'Fabio', 'lastname':'Caccamo' } }
print(d['profile']) # -> { 'firstname':'Fabio', 'lastname':'Caccamo' }
2019-07-10 09:44:04 +00:00
# check if keypath exists in dict
2019-05-17 11:13:15 +00:00
print('profile.lastname' in d) # -> True
2019-07-10 09:44:04 +00:00
# delete value by keypath
del d['profile.lastname']
2019-05-17 11:13:15 +00:00
```
2019-10-29 15:31:49 +00:00
It is possible to do the same using a **list of keys** :
2019-09-20 14:21:31 +00:00
```python
2019-10-29 15:31:49 +00:00
d = benedict()
# set values by keys list
d['profile', 'firstname'] = 'Fabio'
d['profile', 'lastname'] = 'Caccamo'
print(d) # -> { 'profile':{ 'firstname':'Fabio', 'lastname':'Caccamo' } }
print(d['profile']) # -> { 'firstname':'Fabio', 'lastname':'Caccamo' }
# check if keypath exists in dict
print(['profile', 'lastname'] in d) # -> True
# delete value by keys list
del d['profile', 'lastname']
2019-10-29 15:36:24 +00:00
```
2019-09-20 14:21:31 +00:00
2019-07-09 14:04:36 +00:00
#### Custom keypath separator
2019-07-10 09:24:43 +00:00
You can customize the keypath separator passing the `keypath_separator` argument in the constructor.
2019-10-29 15:38:51 +00:00
2019-10-29 15:31:49 +00:00
If you pass an existing dict to the constructor and its keys contain the keypath separator an `Exception` will be raised.
2019-07-09 14:04:36 +00:00
```python
d = benedict(existing_dict, keypath_separator='/')
```
2019-10-29 15:31:49 +00:00
#### Change keypath separator
You can change the `keypath_separator` at any time using the `getter/setter` property.
2019-10-29 15:38:51 +00:00
2019-10-29 15:31:49 +00:00
If any existing key contains the new `keypath_separator` an `Exception` will be raised.
```python
d.keypath_separator = '/'
```
2019-09-20 14:21:31 +00:00
#### Disable keypath functionality
You can disable the keypath functionality passing `keypath_separator=None` in the constructor.
2019-07-10 09:24:43 +00:00
2019-07-09 14:04:36 +00:00
```python
2019-09-20 14:27:16 +00:00
d = benedict(existing_dict, keypath_separator=None)
2019-07-09 14:04:36 +00:00
```
2019-10-29 15:31:49 +00:00
You can disable the keypath functionality using the `getter/setter` property.
```python
d.keypath_separator = None
```
2019-09-20 14:21:31 +00:00
## API
2019-10-04 08:45:31 +00:00
### Utility
2019-10-03 16:49:01 +00:00
These methods are common utilities that will speed up your everyday work.
2019-10-04 13:55:32 +00:00
Utilities that accepts key argument(s) also accepts keypath(s).
Utilities that return a dictionary always return a new `benedict` instance.
2019-10-04 08:45:31 +00:00
- #### clean
2019-10-03 16:49:01 +00:00
```python
# Clean the current dict removing all empty values: None, '', {}, [], ().
# If strings, dicts or lists flags are False, related empty values will not be deleted.
d.clean(strings=True, dicts=True, lists=True)
```
2019-10-04 08:45:31 +00:00
- #### clone
2019-10-03 16:49:01 +00:00
```python
# Return a clone (deepcopy) of the dict.
c = d.clone()
```
2019-10-04 08:45:31 +00:00
- #### dump
2019-10-03 16:49:01 +00:00
```python
# Return a readable representation of any dict/list.
# This method can be used both as static method or instance method.
s = benedict.dump(d.keypaths())
print(s)
# or
d = benedict()
print(d.dump())
```
2019-10-04 08:45:31 +00:00
- #### filter
2019-10-03 16:49:01 +00:00
```python
# Return a filtered dict using the given predicate function.
# Predicate function receives key, value arguments and should return a bool value.
predicate = lambda k, v: v is not None
f = d.filter(predicate)
```
2019-10-04 08:45:31 +00:00
- #### flatten
2019-10-03 16:49:01 +00:00
```python
# Return a flatten dict using the given separator to concat nested dict keys.
f = d.flatten(separator='_')
```
2019-10-04 08:45:31 +00:00
- #### invert
2019-10-03 16:49:01 +00:00
```python
2019-10-04 13:55:32 +00:00
# Return an inverted dict where values become keys and keys become values.
# Since multiple keys could have the same value, each value will be a list of keys.
# If flat is True each value will be a single value (use this only if values are unique).
2019-10-03 16:49:01 +00:00
i = d.invert(flat=False)
```
2019-10-04 08:45:31 +00:00
- #### items_sorted_by_keys
2019-10-03 16:49:01 +00:00
```python
2019-10-04 08:45:31 +00:00
# Return items (key/value list) sorted by keys.
2019-10-04 13:55:32 +00:00
# If reverse is True, the list will be reversed.
2019-10-03 16:49:01 +00:00
items = d.items_sorted_by_keys(reverse=False)
```
2019-10-04 08:45:31 +00:00
- #### items_sorted_by_values
2019-10-03 16:49:01 +00:00
```python
2019-10-04 08:45:31 +00:00
# Return items (key/value list) sorted by values.
# If reverse is True, the list will be reversed.
2019-10-03 16:49:01 +00:00
items = d.items_sorted_by_values(reverse=False)
```
2019-10-29 15:31:49 +00:00
- #### keypaths
```python
# Return a list of all keypaths in the dict.
k = d.keypaths()
print(k)
```
2019-10-04 08:45:31 +00:00
- #### merge
2019-10-03 16:49:01 +00:00
```python
# Merge one or more dictionary objects into current instance (deepupdate).
# Sub-dictionaries keys will be merged toghether.
d.merge(a, b, c)
```
2019-10-04 13:55:32 +00:00
- #### move
```python
# Move an item from key_src to key_dst.
# It can be used to rename a key.
2019-10-14 12:47:49 +00:00
# If key_dst exists, its value will be overwritten.
2019-10-04 13:55:32 +00:00
d.move('a', 'b')
```
2019-10-04 08:45:31 +00:00
- #### remove
2019-10-03 16:49:01 +00:00
```python
# Remove multiple keys from the dict.
2019-10-04 13:55:32 +00:00
# It is possible to pass a single key or more keys (as list or *args).
2019-10-03 16:49:01 +00:00
d.remove(['firstname', 'lastname', 'email'])
```
2019-10-29 15:31:49 +00:00
- #### standardize
```python
# Standardize all dict keys, e.g. "Location Latitude" -> "location_latitude".
d.standardize()
```
2019-10-04 08:45:31 +00:00
- #### subset
2019-10-03 16:49:01 +00:00
```python
# Return a dict subset for the given keys.
2019-10-04 13:55:32 +00:00
# It is possible to pass a single key or more keys (as list or *args).
2019-10-03 16:49:01 +00:00
s = d.subset(['firstname', 'lastname', 'email'])
```
2019-10-04 13:55:32 +00:00
- #### swap
```python
# Swap items values at the given keys.
d.swap('firstname', 'lastname')
```
2019-10-29 15:31:49 +00:00
- #### traverse
```python
# Traverse a dict passing each item (dict, key, value) to the given callback function.
def f(d, key, value):
print('dict: {} - key: {} - value: {}'.format(d, key, value))
d.traverse(f)
```
2019-10-14 12:47:49 +00:00
- #### unique
```python
# Remove duplicated values from the dict.
d.unique()
```
2019-09-20 14:21:31 +00:00
### I/O
2019-09-24 14:25:53 +00:00
2019-11-07 16:52:13 +00:00
It is possible to create a `benedict` instance directly from data source (filepath, url or data-string) by passing the data source and the data format (default 'json') in the constructor.
2019-09-24 14:25:53 +00:00
```python
# filepath
2019-11-07 16:52:13 +00:00
d = benedict('/root/data.yml', format='yaml')
2019-09-24 14:25:53 +00:00
# url
2019-11-07 16:52:13 +00:00
d = benedict('https://localhost:8000/data.xml', format='xml')
2019-09-24 14:25:53 +00:00
# data-string
d = benedict('{"a": 1, "b": 2, "c": 3, "x": 7, "y": 8, "z": 9}')
```
2019-11-07 16:52:13 +00:00
These methods simplify I/O operations with most common formats: `base64` , `csv` , `json` , `toml` , `xml` , `yaml` , `query-string`
In all `from_*` methods, the first argument can be: **url** , **filepath** or **data-string** .
In all `to_*` methods, if `filepath='...'` kwarg is specified, the output will be also **saved** at the specified filepath.
2019-10-03 16:49:01 +00:00
2019-10-04 08:45:31 +00:00
- #### from_base64
2019-10-03 16:49:01 +00:00
```python
# Try to load/decode a base64 encoded data and return it as benedict instance.
# Accept as first argument: url, filepath or data-string.
2019-11-07 16:52:13 +00:00
# It's possible to choose the subformat used under the hood (`csv`, `json`, `query-string`, `toml`, `xml`, `yaml`), default: 'json'.
# It's possible to choose the encoding, default 'utf-8'.
2019-10-03 16:49:01 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
d = benedict.from_base64(s, subformat='json', encoding='utf-8', **kwargs)
```
- #### from_csv
```python
# Try to load/decode a csv encoded data and return it as benedict instance.
# Accept as first argument: url, filepath or data-string.ù
# It's possible to specify the columns list, default: None (in this case the first row values will be used as keys).
# It's possible to pass decoder specific options using kwargs: https://docs.python.org/3/library/csv.html
# A ValueError is raised in case of failure.
d = benedict.from_csv(s, columns=None, columns_row=True, **kwargs)
2019-10-03 16:49:01 +00:00
```
2019-09-10 14:58:26 +00:00
2019-10-04 08:45:31 +00:00
- #### from_json
2019-09-10 14:58:26 +00:00
```python
2019-09-24 14:25:53 +00:00
# Try to load/decode a json encoded data and return it as benedict instance.
# Accept as first argument: url, filepath or data-string.
# It's possible to pass decoder specific options using kwargs: https://docs.python.org/3/library/json.html
2019-09-10 14:58:26 +00:00
# A ValueError is raised in case of failure.
2019-09-24 14:25:53 +00:00
d = benedict.from_json(s, **kwargs)
2019-09-10 14:58:26 +00:00
```
2019-12-12 17:30:21 +00:00
- #### from_query_string
2019-09-10 14:58:26 +00:00
```python
2019-12-12 17:30:21 +00:00
# Try to load/decode a query-string and return it as benedict instance.
2019-09-24 14:25:53 +00:00
# Accept as first argument: url, filepath or data-string.
2019-09-10 14:58:26 +00:00
# A ValueError is raised in case of failure.
2019-12-12 17:30:21 +00:00
d = benedict.from_query_string(s, **kwargs)
2019-09-10 14:58:26 +00:00
```
2019-12-12 17:30:21 +00:00
- #### from_toml
2019-10-14 12:47:49 +00:00
```python
2019-12-12 17:30:21 +00:00
# Try to load/decode a toml encoded data and return it as benedict instance.
2019-10-14 12:47:49 +00:00
# Accept as first argument: url, filepath or data-string.
2019-12-12 17:30:21 +00:00
# It's possible to pass decoder specific options using kwargs: https://pypi.org/project/toml/
2019-10-14 12:47:49 +00:00
# A ValueError is raised in case of failure.
2019-12-12 17:30:21 +00:00
d = benedict.from_toml(s, **kwargs)
2019-10-14 12:47:49 +00:00
```
2019-10-04 08:45:31 +00:00
- #### from_xml
2019-09-23 12:14:06 +00:00
```python
2019-09-24 14:25:53 +00:00
# Try to load/decode a xml encoded data and return it as benedict instance.
# Accept as first argument: url, filepath or data-string.
# It's possible to pass decoder specific options using kwargs: https://github.com/martinblech/xmltodict
2019-09-23 12:14:06 +00:00
# A ValueError is raised in case of failure.
2019-09-24 14:25:53 +00:00
d = benedict.from_xml(s, **kwargs)
2019-09-23 12:14:06 +00:00
```
2019-10-04 08:45:31 +00:00
- #### from_yaml
2019-09-17 09:50:47 +00:00
```python
2019-09-24 14:25:53 +00:00
# Try to load/decode a yaml encoded data and return it as benedict instance.
# Accept as first argument: url, filepath or data-string.
# It's possible to pass decoder specific options using kwargs: https://pyyaml.org/wiki/PyYAMLDocumentation
2019-09-17 09:50:47 +00:00
# A ValueError is raised in case of failure.
2019-09-24 14:25:53 +00:00
d = benedict.from_yaml(s, **kwargs)
2019-09-17 09:50:47 +00:00
```
2019-10-04 08:45:31 +00:00
- #### to_base64
2019-10-03 16:49:01 +00:00
```python
2019-11-07 16:52:13 +00:00
# Return the dict instance encoded in base64 format and optionally save it at the specified 'filepath'.
# It's possible to choose the subformat used under the hood ('csv', json', `query-string`, 'toml', 'xml', 'yaml'), default: 'json'.
# It's possible to choose the encoding, default 'utf-8'.
2019-10-14 12:47:49 +00:00
# It's possible to pass decoder specific options using kwargs.
2019-10-03 16:49:01 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_base64(subformat='json', encoding='utf-8', **kwargs)
```
- #### to_csv
```python
# Return a list of dicts encoded in csv format and optionally save it at the specified filepath.
# It's possible to specify the key of the item (list of dicts) to encode, default: 'values'.
# It's possible to specify the columns list, default: None (in this case the keys of the first item will be used).
# A ValueError is raised in case of failure.
d = benedict.to_csv(key='values', columns=None, columns_row=True, **kwargs)
2019-10-03 16:49:01 +00:00
```
2019-10-04 08:45:31 +00:00
- #### to_json
2019-09-17 09:50:47 +00:00
```python
2019-09-20 14:21:31 +00:00
# Return the dict instance encoded in json format and optionally save it at the specified filepath.
2019-09-24 14:25:53 +00:00
# It's possible to pass encoder specific options using kwargs: https://docs.python.org/3/library/json.html
2019-09-17 09:50:47 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_json(**kwargs)
2019-09-06 15:40:32 +00:00
```
2019-10-14 12:47:49 +00:00
- #### to_query_string
```python
# Return the dict instance as query-string and optionally save it at the specified filepath.
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_query_string(**kwargs)
2019-10-14 12:47:49 +00:00
```
2019-10-04 08:45:31 +00:00
- #### to_toml
2019-07-19 09:02:18 +00:00
```python
2019-09-20 14:21:31 +00:00
# Return the dict instance encoded in toml format and optionally save it at the specified filepath.
2019-09-24 14:25:53 +00:00
# It's possible to pass encoder specific options using kwargs: https://pypi.org/project/toml/
2019-09-20 14:21:31 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_toml(**kwargs)
2019-07-19 09:02:18 +00:00
```
2019-10-04 08:45:31 +00:00
- #### to_xml
2019-09-23 12:14:06 +00:00
```python
# Return the dict instance encoded in xml format and optionally save it at the specified filepath.
2019-09-24 14:25:53 +00:00
# It's possible to pass encoder specific options using kwargs: https://github.com/martinblech/xmltodict
2019-09-23 12:14:06 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_xml(**kwargs)
2019-09-23 12:14:06 +00:00
```
2019-10-04 08:45:31 +00:00
- #### to_yaml
2019-07-19 09:02:18 +00:00
```python
2019-11-07 16:52:13 +00:00
# Return the dict instance encoded in yaml format.
# If filepath option is passed the output will be saved ath
2019-09-24 14:25:53 +00:00
# It's possible to pass encoder specific options using kwargs: https://pyyaml.org/wiki/PyYAMLDocumentation
2019-09-20 14:21:31 +00:00
# A ValueError is raised in case of failure.
2019-11-07 16:52:13 +00:00
s = d.to_yaml(**kwargs)
2019-07-19 09:02:18 +00:00
```
2019-10-04 08:45:31 +00:00
### Parse
2019-07-10 12:36:51 +00:00
These methods are wrappers of the `get` method, they parse data trying to return it in the expected type.
2019-07-09 14:04:36 +00:00
2019-10-04 08:45:31 +00:00
- #### get_bool
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as bool.
2019-05-20 15:30:01 +00:00
# Values like `1`, `true`, `yes`, `on`, `ok` will be returned as `True`.
d.get_bool(key, default=False)
```
2019-10-04 08:45:31 +00:00
- #### get_bool_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of bool values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_bool_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_datetime
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as datetime.
# If format is not specified it will be autodetected.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_datetime(key, default=None, format=None, choices=[])
2019-05-17 11:13:15 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_datetime_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of datetime values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_datetime_list(key, default=[], format=None, separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_decimal
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as Decimal.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_decimal(key, default=Decimal('0.0'), choices=[])
2019-05-17 11:13:15 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_decimal_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of Decimal values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_decimal_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_dict
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as dict.
# If value is a json string it will be automatically decoded.
2019-05-17 11:13:15 +00:00
d.get_dict(key, default={})
```
2019-10-04 08:45:31 +00:00
- #### get_email
2019-05-17 11:13:15 +00:00
2019-06-17 15:34:04 +00:00
```python
# Get email by key or keypath and return it.
# If value is blacklisted it will be automatically ignored.
# If check_blacklist is False, it will be not ignored even if blacklisted.
2019-12-12 17:30:21 +00:00
d.get_email(key, default='', choices=None, check_blacklist=True)
2019-06-17 15:34:04 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_float
2019-07-10 09:24:43 +00:00
```python
# Get value by key or keypath trying to return it as float.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_float(key, default=0.0, choices=[])
2019-07-10 09:24:43 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_float_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of float values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_float_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_int
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as int.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_int(key, default=0, choices=[])
2019-05-17 11:13:15 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_int_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of int values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_int_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_list_item
2019-07-10 09:24:43 +00:00
2019-06-10 16:05:28 +00:00
```python
# Get list by key or keypath and return value at the specified index.
# If separator is specified and list value is a string it will be splitted.
d.get_list_item(key, index=0, default=None, separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_phonenumber
2019-07-10 09:24:43 +00:00
2019-06-17 15:34:04 +00:00
```python
2019-06-18 09:46:12 +00:00
# Get phone number by key or keypath and return a dict with different formats (e164, international, national).
# If country code is specified (alpha 2 code), it will be used to parse phone number correctly.
d.get_phonenumber(key, country_code=None, default=None)
2019-06-17 15:34:04 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_slug
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as slug.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_slug(key, default='', choices=[])
2019-05-17 11:13:15 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_slug_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of slug values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_slug_list(key, default=[], separator=',')
```
2019-10-04 08:45:31 +00:00
- #### get_str
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as string.
# Encoding issues will be automatically fixed.
2019-12-12 17:30:21 +00:00
# If choices and value is in choices return value otherwise default.
d.get_str(key, default='', choices=[])
2019-05-17 11:13:15 +00:00
```
2019-10-04 08:45:31 +00:00
- #### get_str_list
2019-07-10 09:24:43 +00:00
2019-05-17 11:13:15 +00:00
```python
2019-05-17 13:04:22 +00:00
# Get value by key or keypath trying to return it as list of str values.
# If separator is specified and value is a string it will be splitted.
2019-05-17 11:13:15 +00:00
d.get_str_list(key, default=[], separator=',')
```
2019-09-20 14:21:31 +00:00
## Testing
2019-12-12 17:30:21 +00:00
```bash
# create python 3.8 virtual environment
virtualenv testing_benedict -p "python3.8" --no-site-packages
# activate virtualenv
cd testing_benedict & & . bin/activate
# clone repo
git clone https://github.com/fabiocaccamo/python-benedict.git src & & cd src
# install requirements
pip install --upgrade pip
pip install -r requirements.txt
pip install tox
# run tests using tox
tox
# or run tests using unittest
python -m unittest
# or run tests using setuptools
python setup.py test
```
2019-09-20 14:21:31 +00:00
2019-05-17 11:13:15 +00:00
## License
2019-12-13 16:55:53 +00:00
Released under [MIT License ](LICENSE.txt ).