Completed
Push — master ( 5cb87e...e8ffd6 )
by Rich
01:24
created

AtomView.__len__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
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
14
class ChemicalObject(object):
15
16
    """ A mixin for each chemical object in scikit-chem """
17
18
    @classmethod
19
    def from_super(cls, obj):
20
21
        """A method that converts the class of an object of parent class to that of the child. """
22
23
        obj.__class__ = cls
24
        return obj
25
26
class AtomView(object):
27
28
    """ Atom interface wrapper """
29
30
    def __init__(self, owner):
31
        self.owner = owner
32
33
    def __getitem__(self, index):
34
        from .atom import Atom
35
        return Atom.from_super(self.owner.GetAtomWithIdx(index))
36
37
    def __len__(self):
38
        return self.owner.GetNumAtoms()
39
40
    def __iter__(self):
0 ignored issues
show
Bug introduced by
__iter__ should return an iterator.
Loading history...
41
        return AtomIterator(self.owner)
42
43
class AtomIterator(AtomView):
44
45
    """ Atom iterator """
46
47
    def __init__(self, owner):
48
        super(AtomIterator, self).__init__(owner)
49
        self._current = 0
50
        self._high = self.owner.GetNumAtoms()
51
52
    def __next__(self):
53
        if self._current >= self._high:
54
            raise StopIteration
55
        else:
56
            self._current += 1
57
            return self[self._current - 1]
58
59
60
class PropertyView(object):
61
62
    """ Property object wrapper """
63
64
    def __init__(self, owner):
65
        self.owner = owner
66
67
    def __getitem__(self, index):
68
        return self.owner.GetProp(str(index))
69
70
    def __setitem__(self, key, value):
71
        if not isinstance(value, str):
72
            warnings.warn("""RDKit property keys and values can only be
73
                        `str`.  Using `{value}` as a `str`.""".format(value=value))
74
        self.owner.SetProp(str(key), str(value))
75
76
    def __iter__(self):
77
        return iter(self.keys())
78
79
    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...
80
        if index in self.keys():
81
            return self[index]
82
        else:
83
            return default
84
85
    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...
86
        if default:
87
            val = self.get(index, default)
88
        else:
89
            val = self[index]
90
        self.remove(index)
91
        return val
92
93
    def remove(self, index):
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...
94
        self.owner.ClearProp(index)
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 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...
101
        return list(k for k in self.owner.GetPropNames() if k[:1] != '_')
102
103
    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...
104
        return list((k, self[k]) for k in self.keys())
105
106
    def __str__(self):
107
        return str(dict(self))
108
109
    def __repr__(self):
110
        return '<{klass} values="{values}" at {address}>'.format(
111
            klass=self.__class__.__name__,
112
            values=str(self),
113
            address=hex(id(self)))
114