Skip to content

Commit b643810

Browse files
committed
MAINT: Free your functions
1 parent adb3247 commit b643810

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

src/chemcoord/cartesian_coordinates/_cartesian_class_get_zmat.py

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ def _check_construction_table(construction_table):
4545
if not reference.isin(c_table.index[:row]).all():
4646
raise UndefinedCoordinateSystem(give_message(i=i))
4747

48+
49+
4850
def _get_frag_constr_table(self, start_atom=None, predefined_table=None,
4951
use_lookup=None, bond_dict=None):
5052
"""Create a construction table for a Zmatrix.
@@ -74,43 +76,21 @@ def _get_frag_constr_table(self, start_atom=None, predefined_table=None,
7476
if use_lookup is None:
7577
use_lookup = settings['defaults']['use_lookup']
7678

77-
def modify_priority(bond_dict, user_defined):
78-
def move_to_start(dct, key):
79-
"Due to PY27 compatibility"
80-
keys = dct.keys()
81-
if key in keys and key != keys[0]:
82-
root = dct._OrderedDict__root
83-
first = root[1]
84-
link = dct._OrderedDict__map[key]
85-
link_prev, link_next, _ = link
86-
link_prev[1] = link_next
87-
link_next[0] = link_prev
88-
link[0] = root
89-
link[1] = first
90-
root[1] = first[0] = link
91-
else:
92-
raise KeyError
93-
94-
for j in reversed(user_defined):
95-
try:
96-
try:
97-
bond_dict.move_to_end(j, last=False)
98-
except AttributeError:
99-
# No move_to_end method in python 2.x
100-
move_to_start(bond_dict, j)
101-
except KeyError:
102-
pass
103-
10479
if start_atom is not None and predefined_table is not None:
10580
raise IllegalArgumentCombination('Either start_atom or '
10681
'predefined_table has to be None')
10782
if bond_dict is None:
10883
bond_dict = self._give_val_sorted_bond_dict(use_lookup=use_lookup)
84+
10985
if predefined_table is not None:
11086
self._check_construction_table(predefined_table)
11187
construction_table = predefined_table.copy()
11288

113-
if predefined_table is None:
89+
i = construction_table.index[0]
90+
order_of_def = list(construction_table.index)
91+
user_defined = list(construction_table.index)
92+
construction_table = construction_table.to_dict(orient='index')
93+
else:
11494
if start_atom is None:
11595
molecule = self.get_distance_to(self.get_centroid())
11696
i = molecule['distance'].idxmin()
@@ -121,18 +101,13 @@ def move_to_start(dct, key):
121101
construction_table = {i: {'b': 'origin',
122102
'a': 'e_z',
123103
'd': 'e_x'}}
124-
else:
125-
i = construction_table.index[0]
126-
order_of_def = list(construction_table.index)
127-
user_defined = list(construction_table.index)
128-
construction_table = construction_table.to_dict(orient='index')
129104

130105
visited = {i}
131106
if len(self) > 1:
132107
parent = {j: i for j in bond_dict[i]}
133108
work_bond_dict = OrderedDict(
134109
[(j, bond_dict[j] - visited) for j in bond_dict[i]])
135-
modify_priority(work_bond_dict, user_defined)
110+
_modify_priority(work_bond_dict, user_defined)
136111
else:
137112
parent, work_bond_dict = {}, {}
138113

@@ -181,7 +156,7 @@ def move_to_start(dct, key):
181156
parent[j] = i
182157

183158
work_bond_dict = new_work_bond_dict
184-
modify_priority(work_bond_dict, user_defined)
159+
_modify_priority(work_bond_dict, user_defined)
185160
output = pd.DataFrame.from_dict(construction_table, orient='index')
186161
output = output.loc[order_of_def, ['b', 'a', 'd']]
187162
return output
@@ -746,3 +721,31 @@ def to_zmat(self, *args, **kwargs):
746721
warnings.simplefilter("always")
747722
warnings.warn(message, DeprecationWarning)
748723
return self.get_zmat(*args, **kwargs)
724+
725+
726+
def _modify_priority(bond_dict, user_defined):
727+
def move_to_start(dct, key):
728+
"Due to PY27 compatibility"
729+
keys = dct.keys()
730+
if key in keys and key != keys[0]:
731+
root = dct._OrderedDict__root
732+
first = root[1]
733+
link = dct._OrderedDict__map[key]
734+
link_prev, link_next, _ = link
735+
link_prev[1] = link_next
736+
link_next[0] = link_prev
737+
link[0] = root
738+
link[1] = first
739+
root[1] = first[0] = link
740+
else:
741+
raise KeyError
742+
743+
for j in reversed(user_defined):
744+
try:
745+
try:
746+
bond_dict.move_to_end(j, last=False)
747+
except AttributeError:
748+
# No move_to_end method in python 2.x
749+
move_to_start(bond_dict, j)
750+
except KeyError:
751+
pass

0 commit comments

Comments
 (0)