1111# python-benedict
1212The Python dictionary for humans dealing with evil/complex data.
1313
14+ ## Index
15+ - [ Features] ( #features )
16+ - [ Requirements] ( #requirements )
17+ - [ Installation] ( #installation )
18+ - [ Usage] ( #usage )
19+ - [ Basics] ( #basics )
20+ - [ Keypath] ( #keypath )
21+ - [ List keypaths] ( #list-keypaths )
22+ - [ Custom keypath separator] ( #custom-keypath-separator )
23+ - [ Disable keypath functionality] ( #disable-keypath-functionality )
24+ - [ API] ( #api )
25+ - [ I/O] ( #io )
26+ - [ ` from_json ` ] ( #from_json )
27+ - [ ` from_toml ` ] ( #from_toml )
28+ - [ ` from_yaml ` ] ( #from_yaml )
29+ - [ ` to_json ` ] ( #to_json )
30+ - [ ` to_toml ` ] ( #to_toml )
31+ - [ ` to_yaml ` ] ( #to_yaml )
32+ - [ Parse] ( #parse )
33+ - [ ` get_bool ` ] ( #get_bool )
34+ - [ ` get_bool_list ` ] ( #get_bool_list )
35+ - [ ` get_datetime ` ] ( #get_datetime )
36+ - [ ` get_datetime_list ` ] ( #get_datetime_list )
37+ - [ ` get_decimal ` ] ( #get_decimal )
38+ - [ ` get_decimal_list ` ] ( #get_decimal_list )
39+ - [ ` get_dict ` ] ( #get_dict )
40+ - [ ` get_email ` ] ( #get_email )
41+ - [ ` get_float ` ] ( #get_float )
42+ - [ ` get_float_list ` ] ( #get_float_list )
43+ - [ ` get_int ` ] ( #get_int )
44+ - [ ` get_int_list ` ] ( #get_int_list )
45+ - [ ` get_list ` ] ( #get_list )
46+ - [ ` get_list_item ` ] ( #get_list_item )
47+ - [ ` get_phonenumber ` ] ( #get_phonenumber )
48+ - [ ` get_slug ` ] ( #get_slug )
49+ - [ ` get_slug_list ` ] ( #get_slug_list )
50+ - [ ` get_str ` ] ( #get_str )
51+ - [ ` get_str_list ` ] ( #get_str_list )
52+ - [ Utility] ( #utility )
53+ - [ ` clean ` ] ( #clean )
54+ - [ ` clone ` ] ( #clone )
55+ - [ ` dump ` ] ( #dump )
56+ - [ ` filter ` ] ( #filter )
57+ - [ ` flatten ` ] ( #flatten )
58+ - [ ` merge ` ] ( #merge )
59+ - [ ` remove ` ] ( #remove )
60+ - [ ` subset ` ] ( #subset )
61+ - [ Testing] ( #testing )
62+ - [ License] ( #license )
63+
1464## Features
1565- Full ** keypath** support * (using the dot syntax by default)*
16- - Easy ** I/O operations** with most common formats: ` base64 ` , ` json ` , ` query-string ` , ` toml ` , ` yaml ` , ` xml `
66+ - Easy ** I/O operations** with most common formats: ` json ` , ` toml ` , ` yaml `
1767- Many ** utility** and ** parse methods** to retrieve data as needed * (all methods listed below)*
1868- Give ** benediction** :) to ` dict ` values before they are returned * (they receive benedict casting)*
1969- 100% ** backward-compatible** * (you can replace existing dicts without pain)*
@@ -24,38 +74,32 @@ The Python dictionary for humans dealing with evil/complex data.
2474## Installation
2575- Run ` pip install python-benedict `
2676
27- ## Testing
28- - Run ` tox ` / ` python setup.py test `
29-
3077## Usage
31- ` benedict ` is a ` dict ` subclass, so it is possible to use it as a normal dictionary * (you can just cast an existing dict)* .
3278
33- ### Import
79+ ### Basics
80+ ` benedict ` is a ` dict ` subclass, so it is possible to use it as a normal dictionary * (you can just cast an existing dict)* .
3481
3582``` python
3683from benedict import benedict
37- ```
38-
39- ### Init
40- Create a new instance:
4184
42- ``` python
85+ # create a new instance
4386d = benedict()
44- ```
45-
46- ... or cast an existing ` dict ` :
4787
48- ``` python
88+ # or cast an existing dict
4989d = benedict(existing_dict)
50- ```
51-
52- If the existing dict keys contain the keypath separator a ` ValueError ` will be raised.
5390
54- In this case you should need to use a [ custom keypath separator] ( #custom-keypath-separator ) .
91+ # or in a Django view
92+ params = benedict(request.GET .items())
93+ page = params.get_int(' p' , 0 )
94+ ```
5595
5696### Keypath
5797` . ` is the default keypath separator.
5898
99+ If you cast an existing dict and its keys contain the keypath separator a ` ValueError ` will be raised.
100+
101+ In this case you should use a [ custom keypath separator] ( #custom-keypath-separator ) or [ disable keypath support] ( #disable-keypath-support ) .
102+
59103``` python
60104d = benedict()
61105
@@ -72,26 +116,33 @@ print('profile.lastname' in d) # -> True
72116del d[' profile.lastname' ]
73117```
74118
119+ #### List keypaths
120+ You can list all the ` keypaths ` available in the ` dict ` :
121+
122+ ``` python
123+ # return a list of all keypaths in the dict.
124+ k = d.keypaths()
125+ print (k)
126+ ```
127+
75128#### Custom keypath separator
76129You can customize the keypath separator passing the ` keypath_separator ` argument in the constructor.
77130
78131``` python
79132d = benedict(existing_dict, keypath_separator = ' /' )
80133```
81134
82- ### API
83-
84- #### Keypath methods
85-
86- - ##### keypaths
135+ #### Disable keypath functionality
136+ You can disable the keypath functionality passing ` keypath_separator=None ` in the constructor.
87137
88138``` python
89- # Return a list of all keypaths in the dict.
90- d.keypaths()
139+ d = benedict(existing_dict, keypath_separator = ' /' )
91140```
92141
93- #### I/O methods
94- These methods simplify I/O operations with most common formats: ` base64 ` , ` json ` , ` query-string ` , ` toml ` , ` yaml ` , ` xml `
142+ ## API
143+
144+ ### I/O
145+ These methods simplify I/O operations with most common formats: ` json ` , ` toml ` , ` yaml `
95146
96147- ##### from_json
97148
@@ -102,13 +153,13 @@ These methods simplify I/O operations with most common formats: `base64`, `json`
102153benedict.from_json(s)
103154```
104155
105- - ##### to_json
156+ - ##### from_toml
106157
107158``` python
108- # Return the dict instance encoded in json format and optionally save it at the specified filepath .
109- # It's possible to pass custom options to the encoder using kwargs, eg. sort_keys=True .
159+ # Try to load/decode a toml encoded string and return it as dict instance .
160+ # Accept as first argument: url, filepath or string .
110161# A ValueError is raised in case of failure.
111- s = d.to_json( filepath = ' ' , ** kwargs )
162+ benedict.from_toml(s )
112163```
113164
114165- ##### from_yaml
@@ -120,84 +171,34 @@ s = d.to_json(filepath='', **kwargs)
120171benedict.from_yaml(s)
121172```
122173
123- - ##### to_yaml
174+ - ##### to_json
124175
125176``` python
126- # Return the dict instance encoded in yaml format and optionally save it at the specified filepath.
127- # It's possible to pass custom options to the encoder using kwargs.
177+ # Return the dict instance encoded in json format and optionally save it at the specified filepath.
178+ # It's possible to pass custom options to the encoder using kwargs, eg. sort_keys=True .
128179# A ValueError is raised in case of failure.
129- s = d.to_yaml(filepath = ' ' , ** kwargs)
130- ```
131-
132- #### Utility methods
133- These methods are common utilities that will speed up your everyday work.
134-
135- - ##### clean
136-
137- ``` python
138- # Clean the current dict removing all empty values: None, '', {}, [], ().
139- # If strings, dicts or lists flags are False, related empty values will not be deleted.
140- d.clean(strings = True , dicts = True , lists = True )
141- ```
142-
143- - ##### clone
144-
145- ``` python
146- # Return a clone (deepcopy) of the dict.
147- d.clone()
148- ```
149-
150- - ##### dump
151-
152- ``` python
153- # Return a readable representation of any dict/list.
154- # This method can be used both as static method or instance method.
155- s = benedict.dump(d.keypaths())
156- print (s)
157- # or
158- d = benedict()
159- print (d.dump())
160- ```
161-
162- - ##### filter
163-
164- ``` python
165- # Return a filtered dict using the given predicate function.
166- # Predicate function receives key, value arguments and should return a bool value.
167- predicate = lambda k , v : v is not None
168- d.filter(predicate)
169- ```
170-
171- - ##### flatten
172-
173- ``` python
174- # Return a flatten dict using the given separator to concat nested dict keys.
175- d.flatten(separator = ' _' )
176- ```
177-
178- - ##### merge
179-
180- ``` python
181- # Merge one or more dictionary objects into current instance (deepupdate).
182- # Sub-dictionaries keys will be merged toghether.
183- d.merge(a, b, c)
180+ s = d.to_json(filepath = ' ' , ** kwargs)
184181```
185182
186- - ##### remove
183+ - ##### to_toml
187184
188185``` python
189- # Remove multiple keys from the dict.
190- d.remove([' firstname' , ' lastname' , ' email' ])
186+ # Return the dict instance encoded in toml format and optionally save it at the specified filepath.
187+ # It's possible to pass custom options to the encoder using kwargs.
188+ # A ValueError is raised in case of failure.
189+ s = d.to_toml(filepath = ' ' , ** kwargs)
191190```
192191
193- - ##### subset
192+ - ##### to_yaml
194193
195194``` python
196- # Return a dict subset for the given keys.
197- d.subset([' firstname' , ' lastname' , ' email' ])
195+ # Return the dict instance encoded in yaml format and optionally save it at the specified filepath.
196+ # It's possible to pass custom options to the encoder using kwargs.
197+ # A ValueError is raised in case of failure.
198+ s = d.to_yaml(filepath = ' ' , ** kwargs)
198199```
199200
200- #### Parse methods
201+ #### Parse
201202These methods are wrappers of the ` get ` method, they parse data trying to return it in the expected type.
202203
203204- ##### get_bool
@@ -355,12 +356,76 @@ d.get_str(key, default='', options=[])
355356d.get_str_list(key, default = [], separator = ' ,' )
356357```
357358
358- #### Django
359- ` benedict ` could be very useful in ` django ` views too:
359+ #### Utility
360+ These methods are common utilities that will speed up your everyday work.
361+
362+ - ##### clean
360363
361364``` python
362- params = benedict(request.GET .items())
365+ # Clean the current dict removing all empty values: None, '', {}, [], ().
366+ # If strings, dicts or lists flags are False, related empty values will not be deleted.
367+ d.clean(strings = True , dicts = True , lists = True )
368+ ```
369+
370+ - ##### clone
371+
372+ ``` python
373+ # Return a clone (deepcopy) of the dict.
374+ d.clone()
375+ ```
376+
377+ - ##### dump
378+
379+ ``` python
380+ # Return a readable representation of any dict/list.
381+ # This method can be used both as static method or instance method.
382+ s = benedict.dump(d.keypaths())
383+ print (s)
384+ # or
385+ d = benedict()
386+ print (d.dump())
387+ ```
388+
389+ - ##### filter
390+
391+ ``` python
392+ # Return a filtered dict using the given predicate function.
393+ # Predicate function receives key, value arguments and should return a bool value.
394+ predicate = lambda k , v : v is not None
395+ d.filter(predicate)
363396```
364397
398+ - ##### flatten
399+
400+ ``` python
401+ # Return a flatten dict using the given separator to concat nested dict keys.
402+ d.flatten(separator = ' _' )
403+ ```
404+
405+ - ##### merge
406+
407+ ``` python
408+ # Merge one or more dictionary objects into current instance (deepupdate).
409+ # Sub-dictionaries keys will be merged toghether.
410+ d.merge(a, b, c)
411+ ```
412+
413+ - ##### remove
414+
415+ ``` python
416+ # Remove multiple keys from the dict.
417+ d.remove([' firstname' , ' lastname' , ' email' ])
418+ ```
419+
420+ - ##### subset
421+
422+ ``` python
423+ # Return a dict subset for the given keys.
424+ d.subset([' firstname' , ' lastname' , ' email' ])
425+ ```
426+
427+ ## Testing
428+ - Run ` tox ` / ` python setup.py test `
429+
365430## License
366431Released under [ MIT License] ( LICENSE.txt ) .
0 commit comments