Completed
Push — master ( b60f49...c7f259 )
by Rich
01:20
created

AtomPropertyView   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 29
rs 10
c 1
b 0
f 1
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __delitem__() 0 3 2
A get() 0 2 2
A __setitem__() 0 4 3
A __getitem__() 0 4 2
A keys() 0 5 2
A __init__() 0 2 1
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2015-2016 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6
"""
7
## skchem.core.base
8
9
Define base classes for scikit chem objects
10
"""
11
12
import warnings
13
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy 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...
14
15
16
class ChemicalObject(object):
17
18
    """ A mixin for each chemical object in scikit-chem """
19
20
    @classmethod
21
    def from_super(cls, obj):
22
23
        """A method that converts the class of an object of parent class to that of the child. """
24
25
        obj.__class__ = cls
26
        return obj
27
28
class AtomView(object):
29
30
    """ Atom interface wrapper """
31
32
    def __init__(self, owner):
33
        self.owner = owner
34
        self.props = AtomPropertyView(self)
35
36
    def __getitem__(self, index):
37
        from .atom import Atom
38
        return Atom.from_super(self.owner.GetAtomWithIdx(index))
39
40
    def __len__(self):
41
        return self.owner.GetNumAtoms()
42
43
    def __iter__(self):
0 ignored issues
show
Bug introduced by
__iter__ should return an iterator.
Loading history...
44
        return AtomIterator(self.owner)
45
46
    def __str__(self):
47
        return str(list(str(atom) for atom in self))
48
49
    def __repr__(self):
50
        return '<{klass} values="{values}" at {address}>'.format(
51
            klass=self.__class__.__name__,
52
            values=str(self),
53
            address=hex(id(self)))
54
55
class AtomIterator(AtomView):
56
57
    """ Atom iterator """
58
59
    def __init__(self, owner):
60
        super(AtomIterator, self).__init__(owner)
61
        self._current = 0
62
        self._high = self.owner.GetNumAtoms()
63
64
    def __next__(self):
65
        if self._current >= self._high:
66
            raise StopIteration
67
        else:
68
            self._current += 1
69
            return self[self._current - 1]
70
71
72
class View(object):
73
74
    """ View wrapper interface """
75
76
    def keys(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
77
        raise NotImplemented
0 ignored issues
show
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
78
79
    def remove(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Coding Style introduced by
This method could be written as a function/class method.

If a method does not access any attributes of the class, it could also be implemented as a function or static method. This can help improve readability. For example

class Foo:
    def some_method(self, x, y):
        return x + y;

could be written as

class Foo:
    @classmethod
    def some_method(cls, x, y):
        return x + y;
Loading history...
80
        raise NotImplemented
0 ignored issues
show
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
81
82
    def get(self, index, default=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
83
        if index in self.keys():
84
            return self[index]
85
        else:
86
            return default
87
88
    def pop(self, index, default=None):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
89
        if default:
90
            val = self.get(index, default)
91
        else:
92
            val = self[index]
93
        self.remove(index)
94
        return val
95
96
    def clear(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
97
        for idx in self.keys():
98
            self.remove(idx)
99
100
    def items(self):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
101
        return list((k, self[k]) for k in self.keys())
102
103
    def remove(self, key):
0 ignored issues
show
Coding Style introduced by
This method should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Bug introduced by
method already defined line 79
Loading history...
104
        self.__delitem__(key)
105
106
    def __getitem__(self, key):
107
        raise NotImplemented
0 ignored issues
show
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
108
109
    def __setitem__(self, key, value):
110
        raise NotImplemented
0 ignored issues
show
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
111
112
    def __delitem__(self, key):
113
        raise NotImplemented
0 ignored issues
show
Best Practice introduced by
NotImplemented raised - should raise NotImplementedError
Loading history...
114
115
    def __iter__(self):
116
        return iter(self.keys())
117
118
    def __str__(self):
119
        return str(dict(self))
120
121
    def __len__(self):
122
        return len(self.keys())
123
124
    def __repr__(self):
125
        return '<{klass} values="{values}" at {address}>'.format(
126
            klass=self.__class__.__name__,
127
            values=str(self),
128
            address=hex(id(self)))
129
130
131
class PropertyView(View):
132
133
    """ Property object wrapper """
134
135
    def __init__(self, owner):
136
        self._owner = owner
137
138
    def keys(self):
139
        return list(k for k in self._owner.GetPropNames() if k[:1] != '_')
140
141
    def __getitem__(self, key):
142
143
        # we manually work out if it was a float that was stored, as GetProp
144
        # returns floats and ints set by SetDoubleProp and SetIntProp as strings
145
        value = self._owner.GetProp(str(key))
146
        try:
147
            return int(value)
148
        except ValueError:
149
            try:
150
                return float(value)
151
            except ValueError:
152
                return value
153
154
155
    def __setitem__(self, key, value):
156
157
        if not isinstance(key, str):
158
            warnings.warn("""RDKit property keys can only be of type `str`.  Using `{key}` as a `str`.""".format(key=key))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (122/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
159
            key = str(key)
160
161
        if key[0] == '_':
162
            warnings.warn("""`{value}` is a private RDKit property key.  Using this may have unintended consequences.""".format(value=value))
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (141/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
163
164
        if isinstance(value, str):
165
            self._owner.SetProp(key, value)
166
        elif isinstance(value, (int, np.int64, np.int32)):
167
            self._owner.SetIntProp(key, value)
168
        elif isinstance(value, (float, np.float64, np.float32)):
169
            self._owner.SetDoubleProp(key, value)
170
        else:
171
            warnings.warn("""RDKit property keys can only be `str`, `int` or `float`.
172
                          Using `{value}` as a `str`.""".format(value=value))
173
            self._owner.SetProp(key, str(value))
174
175
    def __delitem__(self, index):
176
        self._owner.ClearProp(index)
177
178
class AtomPropertyView(View):
179
180
    """ Atom property wrapper """
181
182
    def __init__(self, atom_view):
183
        self._atom_view = atom_view
184
185
    def keys(self):
186
        res = set()
187
        for atom in self._atom_view:
188
            res = res.union(set(atom.props.keys()))
189
        return list(res)
190
191
    def get(self, key, default=None):
192
        return [a.props.get(key, default) for a in self._atom_view]
193
194
    def __getitem__(self, key):
195
        if key not in self.keys():
196
            raise KeyError('No atoms have the property set.')
197
        return self.get(key, None)
198
199
    def __setitem__(self, key, value):
200
        assert len(self._atom_view) == len(value), "Must pass same number of values as atoms."
201
        for atom, val in zip(self._atom_view, value):
202
            atom.props[key] = val
203
204
    def __delitem__(self, key):
205
        for atom in self._atom_view:
206
            atom.props.remove(key)
207