Skip to content

Commit

Permalink
Moved get_descendants_sorted from FEproblem to Base.
Browse files Browse the repository at this point in the history
Now any FEbabel object can use it!  Not that I expect them to, but hey.
  • Loading branch information
randyheydon committed Jan 12, 2012
1 parent 0b6d38e commit ee49a69
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 33 deletions.
37 changes: 34 additions & 3 deletions febabel/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class Base(object):
this means that it should be safe to call get_children recursively on all
children, sub-children, etc., without causing an infinite loop.
The get_descendants method is also required, but will likely not need to be
overridden by subclasses. It returns a set containing all of the object's
children, grandchildren, etc."""
The get_descendants and get_descendants_sorted methods are also required,
but will likely not need to be overridden by subclasses."""

__slots__ = []

Expand All @@ -24,6 +23,9 @@ def get_children(self):


def get_descendants(self):
"""Returns the set of all of the object's descendants, including its
children, grandchildren, etcetera."""

children = self.get_children()
if children is None:
return set()
Expand All @@ -35,6 +37,35 @@ def get_descendants(self):
return descendants


def get_descendants_sorted(self):
"""Returns all descendants, sorted into a dictionary by type.
Descendants can by placed under multiple types (eg. Nodes will also end
up in Constrainables).
If a descendant is not any of the sorted types, it will be placed under
None."""

ds = {
f.geometry.Node: set(),
f.geometry.Element: set(),
f.materials.Material: set(),
f.constraints.LoadCurve: set(),
Constrainable: set(),
Switch: set(),
None: set()
}

for x in self.get_descendants():
placed = False
for cls,st in ds.iteritems():
if cls is not None and isinstance(x, cls):
st.add(x)
placed = True
if not placed:
ds[None].add(x)

return ds




class Constrainable(Base):
Expand Down
30 changes: 0 additions & 30 deletions febabel/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,6 @@ def get_children(self):
return set(chain( *self.sets.values() ))


# FIXME: Maybe this should be a method of Base?
def get_descendants_sorted(self):
"""Returns all descendants, sorted into a dictionary by type.
Descendants can by placed under multiple types (eg. Nodes will also end
up in Constrainables).
If a descendant is not any of the sorted types, it will be placed under
None."""

ds = {
geo.Node: set(),
geo.Element: set(),
mat.Material: set(),
Constrainable: set(),
con.LoadCurve: set(),
Switch: set(),
None: set()
}

for x in self.get_descendants():
placed = False
for cls,st in ds.iteritems():
if cls is not None and isinstance(x, cls):
st.add(x)
placed = True
if not placed:
ds[None].add(x)

return ds


def read(self, filename):
"""Convenience function to run the appropriate reader method.
Currently guesses based on file extension."""
Expand Down

0 comments on commit ee49a69

Please sign in to comment.