Putting Leaves on wx.TreeCtrl() Twigs

Presumably you've got some way in your data storage to associate your leaf
data items with their parent,

Robin,

   Yes, they are. When I retrieve the records from the database they're put
into a list of (leaf, twig) tuples.

To add them to the tree you can simply locate all the leaf items for a
particular twig when you add that twig, and then add the leaves to the
twig the same way.

   OK. If I understand correctly, I can walk through the list and append each
leaf to its appropriate twig. I was focused on identifying the ID of each
twig rather than extracting that name from the list.

   I'm back to work now.

Many thanks,

Rich

···

On Fri, 15 Dec 2006, Robin Dunn wrote:

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863

Rich Shepard wrote:

     for item in self.appData.polNat:
       ngc = self.polTree.AppendItem(self.natID, item[0])
       p = self.polTree.GetItemText(ngc)
       print "This is the policy: ", p
       for p in self.appData.subPols:
         print "This should be the list of subpolicies: ", self.appData.subPols[0][p]

   This bombs because python wants an integer, not a character reference in
the tuple index.

Your first problem, you're reusing 'p' in two contexts that appear to be
conflicting.

If I'm guessing correctly, the setup is thus:

. subPols is a list (or tuple, even?) of tuples
. Each entry of subPols is (masterPolicy, subPolicy)
. As you go through the list of subPols, you want to only inspect the
  ones that have a matching masterPolicy.

Is that accurate?

If so, try this, names changed to prevent ambiguity

  for item in self.appData.polNat:
    ngc = self.polTree.AppendItem(self.natID, item[0])
    policy = self.polTree.GetItemText(ngc)
    print "This is the policy: ", policy
    for subpolicy in self.appData.subPols:
      print "This should be the list of subpolicies: ",
      if subpolicy[0] == policy \
             print subPolicy[1],
    print '\n'

"List comprehensions" would also make this fairly straight-forward, once
you get your head around them:

  for item in self.appData.polNat:
    ngc = self.polTree.AppendItem(self.natID, item[0])
    policy = self.polTree.GetItemText(ngc)
    print "This is the policy: ", policy

    subpolicies = \
        [sp[1] for sp in self.appData.subPols if sp[0]==policy]

    print '...and the subpolicies are', subpolicies

What that is doing is going through the list of subPols, and each one
that matches gets tossed into another list.

Now from a software engineering perspective, I might suggest using
objects rather than tuples, so the fields can be named:

class Subpolicy:
  def __init__(self, parent, name):
    self.parent = parent
    self.name = name

...and then throw those into your subpols list, but that's another
discussion for another forum. :wink:

-tom!

···

--

Your first problem, you're reusing 'p' in two contexts that appear to be
conflicting.

Tom,

   That's because what I posted was one of my groping attempts to get the
right combination of variables in the right place.

If I'm guessing correctly, the setup is thus:

. subPols is a list (or tuple, even?) of tuples
. Each entry of subPols is (masterPolicy, subPolicy)
. As you go through the list of subPols, you want to only inspect the
ones that have a matching masterPolicy.
Is that accurate?

   Almost. subPols is a list of tuples retrieved from the database table.
Each entry of subPols is (subpol_name, pol_name) -- but this is minor.

   The pol_name are the twigs on a wx.TreeCtrl(), and the subpol_names are
the leaves on each twig. Not all policies will have subpolicies.

   As I go through the list of subPols tuples, I want to append each
subpol_name to the matching pol_name.

If so, try this, names changed to prevent ambiguity

for item in self.appData.polNat:
   ngc = self.polTree.AppendItem(self.natID, item[0])
   policy = self.polTree.GetItemText(ngc)
   print "This is the policy: ", policy
   for subpolicy in self.appData.subPols:
     print "This should be the list of subpolicies: ",
     if subpolicy[0] == policy \
            print subPolicy[1],
   print '\n'

   The print statements are strictly for debugging, and I'll change the index
of subpolicy to [1], then give it a try a bit later today (after the house
maintenance and yard work are finished).

"List comprehensions" would also make this fairly straight-forward, once
you get your head around them:

for item in self.appData.polNat:
   ngc = self.polTree.AppendItem(self.natID, item[0])
   policy = self.polTree.GetItemText(ngc)
   print "This is the policy: ", policy

   subpolicies = \
       [sp[1] for sp in self.appData.subPols if sp[0]==policy]

   This makes sense as I read it. I don't yet have enough experience to write
it myself.

What that is doing is going through the list of subPols, and each one
that matches gets tossed into another list.

   Instead, I'll append each subPol to the matching Pol.

Now from a software engineering perspective, I might suggest using
objects rather than tuples, so the fields can be named:

class Subpolicy:
def __init__(self, parent, name):
   self.parent = parent
   self.name = name

...and then throw those into your subpols list, but that's another
discussion for another forum. :wink:

   All data live in database tables. What I'm working on is loading stored
values into the proper widgets when a model database is opened.

   Many thanks. I'll report back, probably tomorrow some time.

Rich

···

On Sat, 16 Dec 2006, Tom Plunket wrote:

--
Richard B. Shepard, Ph.D. | The Environmental Permitting
Applied Ecosystem Services, Inc.(TM) | Accelerator
<http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863