Issues (942)

skchem/io/objects.py (7 issues)

1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2016 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6 1
"""
7
# skchem.io.objects
8
9
Implementing object serialization and deserialization.
10
"""
11
12 1
import importlib
13 1
import json
14 1
import yaml
0 ignored issues
show
The import yaml could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
15
16
17 1
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)
0 ignored issues
show
Coding Style Naming introduced by
The name m does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
36
    return getattr(m, kls).from_params(params)
37
38
39 1
def write_config(obj):
40
41
    """ Serialize an object to a config dict. """
42
43
    return obj.to_dict()
44
45
46 1
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:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
61
                conf = json.load(f)
62
        except (FileNotFoundError, OSError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'FileNotFoundError'
Loading history...
63
            conf = json.loads(conf)
64
    else:
65
        conf = json.load(conf)
66
    return read_config(conf)
67
68
69 1
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 1
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:
0 ignored issues
show
Coding Style Naming introduced by
The name f does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
90
                conf = yaml.load(f)
91
        except (FileNotFoundError, OSError):
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'FileNotFoundError'
Loading history...
92
            conf = yaml.load(conf)
93
94
    else:
95
        conf = yaml.load(conf)
96
    return read_config(conf)
97
98
99 1
def write_yaml(obj, target=None):
100
101
    """ Serialize a scikit-chem object to yaml. """
102
103
    return obj.to_yaml(target)
0 ignored issues
show
Final newline missing
Loading history...