forked from ydataai/ydata-profiling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
64 lines (47 loc) · 2.08 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Configuration for the package is handled in this wrapper for confuse."""
import argparse
import confuse
from pandas_profiling.utils.paths import get_config_default
class Config(object):
"""This is a wrapper for the python confuse package, which handles setting and getting configuration variables via
various ways (notably via argparse and kwargs).
"""
config = None
"""The confuse.Configuration object."""
def __init__(self):
"""The config constructor should be called only once."""
if self.config is None:
self.config = confuse.Configuration("PandasProfiling", __name__)
self.config.set_file(str(get_config_default()))
def set_args(self, namespace: argparse.Namespace, dots: bool) -> None:
"""
Set config variables based on the argparse Namespace object.
Args:
namespace: Dictionary or Namespace to overlay this config with. Supports nested Dictionaries and Namespaces.
dots: If True, any properties on namespace that contain dots (.) will be broken down into child dictionaries.
"""
self.config.set_args(namespace, dots)
def _set_kwargs(self, reference, values: dict):
"""Helper function to set config variables based on kwargs."""
for key, value in values.items():
if key in reference:
if type(value) == dict:
self._set_kwargs(reference[key], value)
else:
reference[key].set(value)
else:
raise ValueError('Config parameter "{}" does not exist.'.format(key))
def set_kwargs(self, kwargs) -> None:
"""
Helper function to set config variables based on kwargs.
Args:
kwargs: the arguments passed to the .profile_report() function
"""
self._set_kwargs(self.config, kwargs)
def __getitem__(self, item):
return self.config[item]
def __setitem__(self, key, value):
self.config[key].set(value)
def dump(self):
return self.config.dump()
config = Config()