Utilities¶
chanfig.to_dict¶
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[str, Any]
|
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
chanfig.utils.apply¶
Apply func
to all children of obj
.
Note that this function 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 |
Positional arguments to be passed to |
()
|
|
**kwargs |
Keyword arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Return value of |
See Also
apply_
: Apply a in-place operation.
Source code in chanfig/utils.py
chanfig.utils.apply_¶
Apply func
to all children of obj
.
Note that this function 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 |
Positional arguments to be passed to |
()
|
|
**kwargs |
Keyword arguments to be passed to |
{}
|
Returns:
Type | Description |
---|---|
Any
|
Return value of |
See Also
apply_
: Apply a in-place operation.
Source code in chanfig/utils.py
chanfig.utils.Singleton¶
chanfig.utils.Null¶
Null
is an instance of NULL
.
Since the metaclass of NULL
is Singleton
, it is advised to use obj is Null
to determine if obj
is Null
.
chanfig.utils.NULL¶
NULL class.
get
method in CHANfiG may accept None
or Ellipse
(...
) as value of default
.
Therefore, it is mandatory to have a different default value for default
.
Null
is an instance of NULL
and is recommended to be used as obj is Null
.