Not Incrementing

Victor Subervi victorsubervi at gmail.com
Thu Dec 31 17:51:36 CET 2009


Hi;
This "pseudo-code" snippet was given to me by Dennis on this list (whose
last name escapes me):

def printTree(allTrees, level=0):
  tree = []
  for aTree in allTrees:
    for name in sorted(aTree.keys()):
      tree.append("%s%s" % ("\t" * level, name))
      printTree(aTree[name], level + 1)
  return tree

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.
TIA,
beno

#!/usr/bin/python

import sys,os
sys.path.append(os.getcwd())
import MySQLdb
from login import login
import re, string

def printTree(allTrees, level=0):
  tree = []
  for aTree in allTrees:
    for name in sorted(aTree.keys()):
      tree.append("%s%s" % ("\t" * level, name))
      printTree(aTree[name], level + 1)
      print '1'
  return tree

def expand(fetched):
  aDict = {}
  for (name, ) in fetched:
    aDict[name] = {}
  return aDict

def getChildren(levelDict, store, level = 0):
  user, passwd, db, host = login()
  db = MySQLdb.connect(host, user, passwd, db)
  cursor = db.cursor()
  MAXLEVEL = 7
  if level > MAXLEVEL:
    return  #possibly the data has a cycle/loop
  for name, data in levelDict.iteritems():
    sql = '''select * from categories%s as c
      inner join categoriesRelationships%s as r
      on c.ID = r.Child
      inner join categories%s as p
      on r.Parent = p.ID
      where p.Category = "%s"''' % (store[0].upper() + store[1:],
store[0].upper() + store[1:], store[0].upper() + store[1:], name)
    cursor.execute(sql)
    levelDict[name] = expand(cursor.fetchall())
    # recursive call to do next level
    getChildren(levelDict[name], level + 1)
  # no data return as we are mutating dictionaries in place

def catTree(allStores):
  user, passwd, db, host = login()
  db = MySQLdb.connect(host, user, passwd, db)
  cursor = db.cursor()
  finalTrees = []
  returnFlag = ''
  for store in allStores:
    allTrees = []
    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:])
    cursor.execute(sql)
    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:])
    cursor.execute(sql)
    cursor.execute('select Category, Parent from categories%s;' %
(store[0].upper() + store[1:]))
    data = cursor.fetchall()
    cursor.execute('select Category from categories%s order by Parent, ID;'
% (store[0].upper() + store[1:]))
    Categories = [itm[0] for itm in cursor] #untuple single column
    if len(Categories) > 0:
      cursor.execute('select Parent from categories%s order by Parent, ID;'
% (store[0].upper() + store[1:]))
      Parents = [itm[0] for itm in cursor]
      MAXLEVEL = 15
      # get top level
      cursor.execute('select category from categories%s order by Category;'
% (store[0].upper() + store[1:]))
      theTree = expand(cursor.fetchall())
      getChildren(theTree, store)
      db.commit()
      allTrees.append(theTree)
      returnFlag = 'gotStuff'
    else:
      returnFlag = 'gotNoStuff'
    finalTrees.append(printTree(allTrees))
  if returnFlag == 'gotStuff':
    return finalTrees
  else:
    return ''


-- 
The Logos has come to bear
http://logos.13gems.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091231/2133d0e4/attachment.html>


More information about the Python-list mailing list