|
1
|
|
|
#! /usr/bin/env python |
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
|
|
""" |
|
7
|
|
|
# skchem.io.objects |
|
8
|
|
|
|
|
9
|
|
|
Implementing object serialization and deserialization. |
|
10
|
|
|
""" |
|
11
|
|
|
|
|
12
|
|
|
import importlib |
|
13
|
|
|
import json |
|
14
|
|
|
import yaml |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
def read_config(conf): |
|
18
|
|
|
|
|
19
|
|
|
""" Deserialize an object from a config dict. |
|
20
|
|
|
|
|
21
|
|
|
Args: |
|
22
|
|
|
conf (dict): |
|
23
|
|
|
The config dict to deseriailize. |
|
24
|
|
|
|
|
25
|
|
|
Returns: |
|
26
|
|
|
object |
|
27
|
|
|
|
|
28
|
|
|
Note: |
|
29
|
|
|
`config` is different from `params`, in that it specifies the class. |
|
30
|
|
|
The `params` dict is a subdict in `config`. |
|
31
|
|
|
""" |
|
32
|
|
|
|
|
33
|
|
|
(name, params), = conf.items() |
|
34
|
|
|
mod, kls = name.rsplit('.', 1) |
|
35
|
|
|
m = importlib.import_module(mod) |
|
|
|
|
|
|
36
|
|
|
return getattr(m, kls).from_params(params) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def write_config(obj): |
|
40
|
|
|
|
|
41
|
|
|
""" Serialize an object to a config dict. """ |
|
42
|
|
|
|
|
43
|
|
|
return obj.to_dict() |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
def read_json(conf): |
|
47
|
|
|
|
|
48
|
|
|
""" Deserialize an object from a json file, filename or str. |
|
49
|
|
|
|
|
50
|
|
|
Args: |
|
51
|
|
|
json (str or filelike): |
|
52
|
|
|
The json file to deserialize. |
|
53
|
|
|
|
|
54
|
|
|
Returns: |
|
55
|
|
|
object |
|
56
|
|
|
""" |
|
57
|
|
|
|
|
58
|
|
|
if isinstance(conf, str): |
|
59
|
|
|
try: |
|
60
|
|
|
with open(conf, 'r') as f: |
|
|
|
|
|
|
61
|
|
|
conf = json.load(f) |
|
62
|
|
|
except (FileNotFoundError, OSError): |
|
|
|
|
|
|
63
|
|
|
conf = json.loads(conf) |
|
64
|
|
|
else: |
|
65
|
|
|
conf = json.load(conf) |
|
66
|
|
|
return read_config(conf) |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def write_json(obj, target=None): |
|
70
|
|
|
|
|
71
|
|
|
""" Serialize a scikit-chem object as json. """ |
|
72
|
|
|
|
|
73
|
|
|
return obj.to_json(target) |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
def read_yaml(conf): |
|
77
|
|
|
|
|
78
|
|
|
""" Deserialize an object from a yaml file, filename or str. |
|
79
|
|
|
|
|
80
|
|
|
Args: |
|
81
|
|
|
yaml (str or filelike): |
|
82
|
|
|
The yaml file to deserialize. |
|
83
|
|
|
|
|
84
|
|
|
Returns: |
|
85
|
|
|
object |
|
86
|
|
|
""" |
|
87
|
|
|
if isinstance(conf, str): |
|
88
|
|
|
try: |
|
89
|
|
|
with open(conf, 'r') as f: |
|
|
|
|
|
|
90
|
|
|
conf = yaml.load(f) |
|
91
|
|
|
except (FileNotFoundError, OSError): |
|
|
|
|
|
|
92
|
|
|
conf = yaml.load(conf) |
|
93
|
|
|
|
|
94
|
|
|
else: |
|
95
|
|
|
conf = yaml.load(conf) |
|
96
|
|
|
return read_config(conf) |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
def write_yaml(obj, target=None): |
|
100
|
|
|
|
|
101
|
|
|
""" Serialize a scikit-chem object to yaml. """ |
|
102
|
|
|
|
|
103
|
|
|
return obj.to_yaml(target) |
|
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.