Skip to content

Commit

Permalink
cleanup/docs, additional test, renamed post_remove to _remove
Browse files Browse the repository at this point in the history
  • Loading branch information
ctk3b committed Oct 22, 2014
1 parent f8051d9 commit 785588f
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 37 deletions.
4 changes: 2 additions & 2 deletions mbuild/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ def referenced_ports(self):
from mbuild.port import Port
return [port for port in self.labels.values() if isinstance(port, Port)]

def post_remove(self, removed_part):
super(Compound, self).post_remove(removed_part)
def _remove(self, removed_part):
super(Compound, self)._remove(removed_part)

# If removing an atom, make sure to remove the bonds it's part of.
if isinstance(removed_part, Atom):
Expand Down
4 changes: 2 additions & 2 deletions mbuild/has_parts_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def remove(self, objs_to_remove):
objs_to_remove.difference_update(intersection)

for removed_part in intersection:
self.post_remove(removed_part)
self._remove(removed_part)

# Remove the part recursively from sub-components.
for part in self.parts:
if isinstance(part, HasPartsMixin) and len(objs_to_remove) > 0:
part.remove(objs_to_remove)

def post_remove(self, removed_part):
def _remove(self, removed_part):
removed_part.parent = None
# Remove labels in the hierarchy pointing to this part.
referrers_to_remove = set()
Expand Down
21 changes: 19 additions & 2 deletions mbuild/tests/test_compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

"""Tests for `mbuild.compound` module. """
import pytest

from mbuild.compound import Compound
from mbuild.examples.ethane.methyl import Methyl
Expand All @@ -11,14 +12,30 @@
class TestCompound:

def test_load_and_create(self):
methyl = Compound.load(get_fn('methyl.pdb'))
Compound.load(get_fn('methyl.pdb'))

def test_update_from_file(self):
methyl = Methyl()
methyl.update_from_file(get_fn("methyl.pdb"))

def test_write(self):
def test_save(self):
methyl = Compound.load(get_fn('methyl.pdb'))
methyl.save(filename='methyl_out.pdb')

@pytest.fixture
def ethane(self):
from mbuild.examples.ethane.ethane import Ethane
return Ethane()

def test_atom_list_by_kind(self, ethane):
non_ports = ethane.atom_list_by_kind(excludeG=True)
assert sum([1 for x in non_ports if x.kind != 'G']) == 8

with_G = ethane.atom_list_by_kind()
assert len(with_G) == 24

only_H = ethane.atom_list_by_kind('H')
assert sum([1 for x in only_H if x.kind == 'H']) == 6

only_G = ethane.atom_list_by_kind('G')
assert sum([1 for x in only_G if x.kind == 'G']) == 16
2 changes: 1 addition & 1 deletion mbuild/tests/test_examples_methane.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def methane(self):
from mbuild.examples.methane.methane import Methane
return Methane()

def test_n_atoms(self, methane):
def test_numbers(self, methane):
assert methane.n_atoms == 5
assert methane.n_bonds == 4

Expand Down
50 changes: 21 additions & 29 deletions mbuild/treeview.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# import os
# import glob
import Tkinter
import ttk
from compound import *
from compound import Compound
from atom import Atom
from bond import Bond

class TreeView(object):

class TreeView(object):
def __init__(self, compound, **kwargs):
self.compound = compound
self.nodemap = {"root": compound}
Expand All @@ -15,50 +15,42 @@ def populate_tree(self, tree, node):
if tree.set(node, "type") != 'compound':
return

# path = tree.set(node, "fullpath")
# tree.delete(*tree.get_children(node))

parent = tree.parent(node)

compound = self.nodemap[node]


for child in tree.get_children(node):
# print tree.item(child)
if tree.item(child)['text'] == 'dummy':
tree.delete(child)

for v in compound.parts:
ptype = None
if isinstance(v,Compound):
if isinstance(v, Compound):
ptype = "compound"
if isinstance(v,Atom):
if isinstance(v, Atom):
ptype = "atom"
if isinstance(v,Bond):
if isinstance(v, Bond):
ptype = "bond"

if not v in self.nodemap.values():
# find a reference to the object
# Find a reference to the object.
k = 'na'
for rk,rv in compound.labels.iteritems():
for rk, rv in compound.labels.iteritems():
if v is rv:
k = rk
break

id = tree.insert(node, "end", text=k, values=[k, ptype, v.__str__(), str(v.__class__)])
id = tree.insert(node, "end", text=k,
values=[k, ptype, v.__str__(),
str(v.__class__)])
self.nodemap[id] = v

if ptype == 'compound':
tree.insert(id, "end", text="dummy")


def populate_roots(self, tree):
k = ""
ptype = "compound"
v = self.compound
node = tree.insert("", "end", text="root", values=[k, ptype, v.kind, str(v.__class__)])
node = tree.insert("", "end", text="root",
values=[k, ptype, v.kind, str(v.__class__)])

# node = tree.insert('', 'end', text=str(self.compound.__class__), values=[str(self.compound.__class__), "compound"])
self.nodemap[node] = self.compound
self.populate_tree(tree, node)

Expand Down Expand Up @@ -91,12 +83,11 @@ def show(self):
hsb = ttk.Scrollbar(orient="horizontal")

tree = ttk.Treeview(columns=("label", "type", "kind", "class"),
displaycolumns=("label", "kind", "class"), yscrollcommand=lambda f, l: self.autoscroll(vsb, f, l),
xscrollcommand=lambda f, l: self.autoscroll(hsb, f, l))

displaycolumns=("label", "kind", "class"),
yscrollcommand=lambda f, l: self.autoscroll(vsb, f, l),
xscrollcommand=lambda f, l: self.autoscroll(hsb, f, l))
tree.compoundTreeView = self


vsb['command'] = tree.yview
hsb['command'] = tree.xview

Expand All @@ -110,10 +101,9 @@ def show(self):
tree.bind('<<TreeviewOpen>>', self.update_tree)
tree.bind('<Double-Button-1>', self.change_dir)


self.populate_roots(tree)

# Arrange the tree and its scrollbars in the toplevel
# Arrange the tree and its scrollbars in the toplevel.
tree.grid(column=0, row=0, sticky='nswe')
vsb.grid(column=1, row=0, sticky='ns')
hsb.grid(column=0, row=1, sticky='ew')
Expand All @@ -122,7 +112,9 @@ def show(self):

root.mainloop()


if __name__ == "__main__":
from ethane import Ethane
from mbuild.examples.ethane.ethane import Ethane

ethane = Ethane()
TreeView(ethane).show()
2 changes: 1 addition & 1 deletion mbuild/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__author__ = 'sallai'

short_version="0.4.2"
short_version = "0.4.2"
version = short_version + "beta"

0 comments on commit 785588f

Please sign in to comment.