Functional¶
Convert an object to a dict.
Note that when converting a set
object, it may be converted to a tuple
object if its values is not hashable.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Object to be converted. |
required |
Returns:
Type | Description |
---|---|
Mapping | Sequence | Set
|
A dict. |
Examples:
>>> to_dict(1)
1
>>> to_dict([1, 2, 3])
[1, 2, 3]
>>> to_dict((1, 2, 3))
(1, 2, 3)
>>> to_dict({1, 2, 3})
{1, 2, 3}
>>> to_dict({'a': 1, 'b': 2})
{'a': 1, 'b': 2}
>>> to_dict(Variable(1))
1
>>> to_dict(FlatDict(a=[[[[[FlatDict(b=1)]]]]]))
{'a': [[[[[{'b': 1}]]]]]}
>>> to_dict(FlatDict(a={FlatDict(b=1)}))
{'a': ({'b': 1},)}
Source code in chanfig/flat_dict.py
options: heading_level: 0
Save FlatDict
to file.
Raises:
Type | Description |
---|---|
ValueError
|
If save to |
TypeError
|
If save to unsupported extension. |
Alias:
save
Examples:
>>> obj = {"a": 1, "b": 2, "c": 3}
>>> save(obj, "test.yaml")
>>> save(obj, "test.json")
>>> save(obj, "test.conf")
Traceback (most recent call last):
TypeError: `file='test.conf'` should be in ('json',) or ('yml', 'yaml'), but got conf.
>>> with open("test.yaml", "w") as f:
... save(obj, f)
Traceback (most recent call last):
ValueError: `method` must be specified when saving to IO.
Source code in chanfig/functional.py
options: heading_level: 0
Load a file into a FlatDict
.
This function simply calls cls.load
, by default, cls
is NestedDict
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
PathStr
|
The file to load. |
required |
cls |
type[FlatDict]
|
The class of the file to load. Defaults to |
NestedDict
|
*args |
Any
|
The arguments to pass to |
()
|
**kwargs |
Any
|
The keyword arguments to pass to |
{}
|
See Also
Examples:
>>> from chanfig import load
>>> config = load("tests/test.yaml")
>>> config
NestedDict(
('a'): 1
('b'): 2
('c'): 3
)
Source code in chanfig/functional.py
options: heading_level: 0
Apply func
to all children of obj
.
Note that this method is meant for non-in-place modification of obj
and should return the original object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Object to apply function. |
required |
func |
Callable
|
Function to be applied. |
required |
*args |
Any
|
Positional arguments to be passed to |
()
|
**kwargs |
Any
|
Keyword arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Return value of |
See Also
apply_
: Apply an in-place operation.
Source code in chanfig/nested_dict.py
options: heading_level: 0
Apply func
to all children of obj
.
Note that this method is meant for non-in-place modification of obj
and should return a new object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Any
|
Object to apply function. |
required |
func |
Callable
|
Function to be applied. |
required |
*args |
Any
|
Positional arguments to be passed to |
()
|
**kwargs |
Any
|
Keyword arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Return value of |
See Also
apply_
: Apply a non-in-place operation.
Source code in chanfig/nested_dict.py
options: heading_level: 0