Skip to content

Commit

Permalink
Fixed multiple bugs for ListElement.
Browse files Browse the repository at this point in the history
  • Loading branch information
stsouko committed Mar 3, 2021
1 parent aee5d3e commit 8564fe1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
4 changes: 2 additions & 2 deletions CGRtools/containers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def substructure(self, atoms: Iterable[int], *, meta: bool = False, graph_type:
sub._atoms = ca = {}
for n in atoms:
atom = sa[n]
atom = atom_type.from_atomic_number(atom.atomic_number)(atom.isotope)
atom = atom_type.from_atom(atom)
ca[n] = atom
atom._attach_to_graph(sub, n)

Expand Down Expand Up @@ -443,7 +443,7 @@ def union(self, other: 'Graph', *, remap=False,

ua = u._atoms
for n, atom in other._atoms.items():
atom = atom_type.from_atomic_number(atom.atomic_number)(atom.isotope)
atom = atom_type.from_atom(atom)
ua[n] = atom
atom._attach_to_graph(u, n)

Expand Down
16 changes: 14 additions & 2 deletions CGRtools/periodictable/element/dynamic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2020, 2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand All @@ -16,8 +16,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, see <https://www.gnu.org/licenses/>.
#
from typing import Type
from typing import Type, Union
from .core import Core
from .element import Element
from ..._functions import tuple_hash
from ...exceptions import IsNotConnectedAtom

Expand Down Expand Up @@ -115,6 +116,17 @@ def from_atomic_number(cls, number: int) -> Type['DynamicElement']:
raise ValueError(f'DynamicElement with number "{number}" not found')
return element

@classmethod
def from_atom(cls, atom: Union['Element', 'DynamicElement']) -> 'DynamicElement':
"""
get DynamicElement object from Element object or copy of DynamicElement object
"""
if isinstance(atom, Element):
return cls.from_atomic_number(atom.atomic_number)(atom.isotope)
elif not isinstance(atom, DynamicElement):
raise TypeError('Element or DynamicElement expected')
return atom.copy()

@property
def neighbors(self):
try:
Expand Down
18 changes: 17 additions & 1 deletion CGRtools/periodictable/element/dynamic_query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2020, 2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand All @@ -19,6 +19,8 @@
from typing import Tuple, Dict, Type, Union
from .core import Core
from .dynamic import Dynamic, DynamicElement
from .element import Element
from .query import QueryElement, AnyElement
from ..._functions import tuple_hash
from ...exceptions import IsNotConnectedAtom

Expand Down Expand Up @@ -130,6 +132,20 @@ def from_atomic_number(cls, number: int) -> Type[Union['DynamicQueryElement', 'D
raise ValueError(f'DynamicQueryElement with number "{number}" not found')
return element

@classmethod
def from_atom(cls, atom: Union['Element', 'DynamicElement', 'DynamicQueryElement', 'DynamicAnyElement',
'QueryElement', 'AnyElement']) -> Union['DynamicQueryElement', 'DynamicAnyElement']:
"""
get DynamicQueryElement or DynamicAnyElement object from Element or DynamicElement or QueryElement object or
copy of DynamicQueryElement or DynamicAnyElement
"""
if isinstance(atom, (Element, DynamicElement, QueryElement, AnyElement)):
return cls.from_atomic_number(atom.atomic_number)(atom.isotope)
elif not isinstance(atom, (DynamicQueryElement, DynamicAnyElement)):
raise TypeError('Element, DynamicElement, DynamicQueryElement, DynamicAnyElement,'
' QueryElement or AnyElement expected')
return atom.copy()

def __eq__(self, other):
if isinstance(other, DynamicElement):
if self.atomic_number == other.atomic_number and \
Expand Down
11 changes: 10 additions & 1 deletion CGRtools/periodictable/element/element.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2020, 2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -112,6 +112,15 @@ def from_atomic_number(cls, number: int) -> Type['Element']:
raise ValueError(f'Element with number "{number}" not found')
return element

@classmethod
def from_atom(cls, atom: 'Element') -> 'Element':
"""
get Element copy
"""
if not isinstance(atom, Element):
raise TypeError('Element expected')
return atom.copy()

def __eq__(self, other):
"""
compare attached to molecules elements
Expand Down
15 changes: 13 additions & 2 deletions CGRtools/periodictable/element/query.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2020 Ramil Nugmanov <nougmanoff@protonmail.com>
# Copyright 2020, 2021 Ramil Nugmanov <nougmanoff@protonmail.com>
# This file is part of CGRtools.
#
# CGRtools is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -148,6 +148,17 @@ def from_atomic_number(cls, number: int) -> Type[Union['QueryElement', 'AnyEleme
raise ValueError(f'QueryElement with number "{number}" not found')
return element

@classmethod
def from_atom(cls, atom: Union['Element', 'QueryElement', 'AnyElement']) -> Union['QueryElement', 'AnyElement']:
"""
get QueryElement or AnyElement object from Element object or copy of QueryElement or AnyElement
"""
if isinstance(atom, Element):
return cls.from_atomic_number(atom.atomic_number)(atom.isotope)
elif not isinstance(atom, (QueryElement, AnyElement)):
raise TypeError('Element, QueryElement or AnyElement expected')
return atom.copy()

@Core.charge.setter
def charge(self, charge):
try:
Expand Down Expand Up @@ -344,7 +355,7 @@ def copy(self) -> 'ListElement':
Detached from graph copy of element
"""
copy = object.__new__(self.__class__)
copy._Core__isotope = self.__isotope
copy._Core__isotope = self._Core__isotope
copy._elements = self._elements
copy._numbers = self._numbers
return copy
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def finalize_options(self):

setup(
name='CGRtools',
version='4.1.12',
version='4.1.13',
packages=['CGRtools', 'CGRtools.algorithms', 'CGRtools.algorithms.components', 'CGRtools.containers',
'CGRtools.files', 'CGRtools.files._mdl', 'CGRtools.periodictable', 'CGRtools.periodictable.element',
'CGRtools.utils', 'CGRtools.attributes'],
Expand Down

0 comments on commit 8564fe1

Please sign in to comment.