Hi;<br>This "pseudo-code" snippet was given to me by Dennis on this list (whose last name escapes me):<br><br>def printTree(allTrees, level=0):<br>  tree = []<br>  for aTree in allTrees:<br>    for name in sorted(aTree.keys()):<br>
      tree.append("%s%s" % ("\t" * level, name))<br>      printTree(aTree[name], level + 1)<br>  return tree<br><br>The code returns all the categories ("name") just fine. It doesn't, however, increment level. I know from my tests that printTree is only called twice (once for each store) and about half a dozen times recursively within one of those stores. Why doesn't this increment? Full code follows.<br>
TIA,<br>beno<br><br>#!/usr/bin/python<br><br>import sys,os<br>sys.path.append(os.getcwd())<br>import MySQLdb<br>from login import login<br>import re, string<br><br>def printTree(allTrees, level=0):<br>  tree = []<br>  for aTree in allTrees:<br>
    for name in sorted(aTree.keys()):<br>      tree.append("%s%s" % ("\t" * level, name))<br>      printTree(aTree[name], level + 1)<br>      print '1'<br>  return tree<br><br>def expand(fetched):<br>
  aDict = {}<br>  for (name, ) in fetched:<br>    aDict[name] = {}<br>  return aDict<br><br>def getChildren(levelDict, store, level = 0):<br>  user, passwd, db, host = login()<br>  db = MySQLdb.connect(host, user, passwd, db)<br>
  cursor = db.cursor()<br>  MAXLEVEL = 7<br>  if level > MAXLEVEL:<br>    return  #possibly the data has a cycle/loop<br>  for name, data in levelDict.iteritems():<br>    sql = '''select * from categories%s as c<br>
      inner join categoriesRelationships%s as r<br>      on c.ID = r.Child<br>      inner join categories%s as p<br>      on r.Parent = p.ID<br>      where p.Category = "%s"''' % (store[0].upper() + store[1:], store[0].upper() + store[1:], store[0].upper() + store[1:], name)<br>
    cursor.execute(sql)<br>    levelDict[name] = expand(cursor.fetchall())<br>    # recursive call to do next level<br>    getChildren(levelDict[name], level + 1)<br>  # no data return as we are mutating dictionaries in place<br>
<br>def catTree(allStores):<br>  user, passwd, db, host = login()<br>  db = MySQLdb.connect(host, user, passwd, db)<br>  cursor = db.cursor()<br>  finalTrees = []<br>  returnFlag = ''<br>  for store in allStores:<br>
    allTrees = []<br>    sql = 'create table if not exists categories%s (ID int(3) unsigned primary key auto_increment, Category varchar(40), Parent varchar(40));' % (store[0].upper() + store[1:])<br>    cursor.execute(sql)<br>
    sql = 'create table if not exists categoriesRelationships%s (ID integer auto_increment primary key, Parent integer not null, foreign key (Parent) references categories%s (ID), Child integer not null, foreign key (Child) references categories%s (ID), check (Parent <> Child) );' % (store[0].upper() + store[1:], store[0].upper() + store[1:], store[0].upper() + store[1:])<br>
    cursor.execute(sql)<br>    cursor.execute('select Category, Parent from categories%s;' % (store[0].upper() + store[1:]))<br>    data = cursor.fetchall()<br>    cursor.execute('select Category from categories%s order by Parent, ID;' % (store[0].upper() + store[1:]))<br>
    Categories = [itm[0] for itm in cursor] #untuple single column<br>    if len(Categories) > 0:<br>      cursor.execute('select Parent from categories%s order by Parent, ID;' % (store[0].upper() + store[1:]))<br>
      Parents = [itm[0] for itm in cursor]<br>      MAXLEVEL = 15<br>      # get top level<br>      cursor.execute('select category from categories%s order by Category;' % (store[0].upper() + store[1:]))<br>      theTree = expand(cursor.fetchall())<br>
      getChildren(theTree, store)<br>      db.commit()<br>      allTrees.append(theTree)<br>      returnFlag = 'gotStuff'<br>    else:<br>      returnFlag = 'gotNoStuff'<br>    finalTrees.append(printTree(allTrees))<br>
  if returnFlag == 'gotStuff':<br>    return finalTrees<br>  else:<br>    return ''<br><br clear="all"><br>-- <br>The Logos has come to bear<br><a href="http://logos.13gems.com/">http://logos.13gems.com/</a><br>