Completed
Push — master ( ccc2ec...3371cb )
by Rich
01:26
created

PropertyView.__str__()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
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
        self._current = 0
33
        self._high = self.owner.GetNumAtoms()
34
35
    def __getitem__(self, index):
36
        from .atom import Atom
37
        return Atom.from_super(self.owner.GetAtomWithIdx(index))
38
39
    def __iter__(self):
0 ignored issues
show
Bug introduced by
__iter__ should return an iterator.
Loading history...
40
        return AtomIterator(self.owner)
41
42
class AtomIterator(AtomView):
43
44
    """ Atom iterator """
45
46
    def __init__(self, owner):
47
        super(AtomIterator, self).__init__(owner)
48
        self._current = 0
49
        self._high = self.owner.GetNumAtoms()
50
51
    def __next__(self):
52
        if self._current >= self._high:
53
            raise StopIteration
54
        else:
55
            self._current += 1
56
            return self[self._current - 1]
57
58
59
class PropertyView(object):
60
61
    """ Property object wrapper """
62
63
    def __init__(self, owner):
64
        self.owner = owner
65
66
    def __getitem__(self, index):
67
        return self.owner.GetProp(str(index))
68
69
    def __setitem__(self, key, value):
70
        if not isinstance(value, str):
71
            warnings.warn("""RDKit property keys and values can only be
72
                        `str`.  Using `{value}` as a `str`.""".format(value=value))
73
        self.owner.SetProp(str(key), str(value))
74
75
    def __iter__(self):
76
        return iter(self.keys())
77
78
    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...
79
        if index in self.keys():
80
            return self[index]
81
        else:
82
            return default
83
84
    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...
85
        if default:
86
            val = self.get(index, default)
87
        else:
88
            val = self[index]
89
        self.remove(index)
90
        return val
91
92
    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...
93
        self.owner.ClearProp(index)
94
95
    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...
96
        for idx in self.keys():
97
            self.remove(idx)
98
99
    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...
100
        return list(k for k in self.owner.GetPropNames() if k[:1] != '_')
101
102
    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...
103
        return list((k, self[k]) for k in self.keys())
104
105
    def __str__(self):
106
        return str(dict(self))
107
108
    def __repr__(self):
109
        return '<{klass} values="{values}" at {address}>'.format(
110
            klass=self.__class__.__name__,
111
            values=str(self),
112
            address=hex(id(self)))
113