Append Problem

Victor Subervi victorsubervi at gmail.com
Sat Jan 2 05:53:26 EST 2010


Hi;
I have the following code snippet:

  print 'Original: ', catChains, '<br />'
  while i < MAXLEVEL:
    flag = 0
    j = 0
    while j < len(parents):
      for chain in catChains:
        if parents[j] == chain[len(chain)-1]:
          chain.append(children[j])
          print '1: ', catChains, '<br />'
          catChains.append(chain)
          print '2: ', catChains, '<br />'
          flag = 1
      j += 1
    i += 1
    if flag == 0:
      break
  print 'Final: ', catChains

which prints this:

Original: [['prodCat1'], ['prodCat2']]
1: [['prodCat1', 'prodCat3'], ['prodCat2']]
2: [['prodCat1', 'prodCat3'], ['prodCat2'], ['prodCat1', 'prodCat3']]
1: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5']]
2: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]
Final: [['prodCat1', 'prodCat3', 'prodCat5'], ['prodCat2'], ['prodCat1',
'prodCat3', 'prodCat5'], ['prodCat1', 'prodCat3', 'prodCat5']]

Why is it that my append statement *deletes* elements of my tuple? The
entire code follows:

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
import datetime, Cookie, random
from particulars import title
from templateFrame import top, bottom
from particulars import myCookie
import time
import fpformat
from sets import Set

def makeNav():
  print 'Content-type: text/html\r\n'
  print '<html>\n<body>'
  user, passwd, db, host = login()
  db = MySQLdb.connect(host, user, passwd, db)
  cursor= db.cursor()
  form = cgi.FieldStorage()
  store = form.getfirst('store')
  sql = 'select Category, Parent from categories%s;' % (store[0].upper() +
store[1:])
  cursor.execute(sql)
  children = [itm[0] for itm in cursor]
  parents = [itm[1] for itm in cursor]
  catChains = []
  i = 0
  while i < len(parents):
    if parents[i] is None:
      child = []
      child.append(children[i])
      catChains.append(child)
    i += 1
  i = 0
  MAXLEVEL = 10
  print 'Original: ', catChains, '<br />'
  while i < MAXLEVEL:
    flag = 0
    j = 0
    while j < len(parents):
      for chain in catChains:
        if parents[j] == chain[len(chain)-1]:
          chain.append(children[j])
          print '1: ', catChains, '<br />'
          catChains.append(chain)
          print '2: ', catChains, '<br />'
          flag = 1
      j += 1
    i += 1
    if flag == 0:
      break
  print 'Final: ', catChains
  lastChain = []
  lastChain.append('root')
  print "<ul id='nav' class='dropdown dropdown.horizontal'>"
  for chain in sorted(catChains):
    link = ''
    i = 0
    while i < len(chain):
      link += '%s/' % string.replace(chain[i], ' ', '_')
      i += 1
    link = '%s.py' % link[:-1]
    if len(chain) == len(lastChain)+1: # This is a new category level within
the current chain.
      print "%s<ul>" % (' ' * (len(chain)-1))
    elif len(chain) == len(lastChain): # This is a new category within in
the same parent level.
      pass # This is just here to remind me how this works!
    j = 0
    while len(chain) < len(lastChain)-j:
      j += 1
      print "%s</ul>" % (' ' * (len(chain)-j))
    print "%s<li id='n-%s'><a href='%s' class='dir'>%s</a></li>" % ((' ' *
len(chain)), chain[len(chain)-1].lower(), link, chain[len(chain)-1])
    lastChain = chain
  print '</ul>'

makeNav()

PS (Mainly, I believe, for Carsten): While programming is difficult for me,
I am writing (and have pretty much finished) this (almost) fully automated
shopping cart. I don't intend to write any other serious programming
project. However, a shopping cart is vital to my Web design business. I will
hereafter outsource my programming. But, as any good supervisor knows, one
has to know how to supervise! If I can't read code, then I'm at the mercy of
my programmer(s). If I can't afford to hire some top gun who can supervise,
then it's my responsibility. Plus, if my programmer(s) quit, I need to step
in and take over. I'm an ok businessman, and absolutely gifted in sales. And
in poetry ;)
beno

-- 
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/20100102/d9bb4bb5/attachment.html>


More information about the Python-list mailing list