Nested Menus

Victor Subervi victorsubervi at gmail.com
Sat Oct 10 11:05:29 EDT 2009


in line responses...

On Thu, Oct 8, 2009 at 5:58 PM, Dennis Lee Bieber <wlfraed at ix.netcom.com>wrote:

> On Thu, 8 Oct 2009 10:38:02 -0500, Victor Subervi
> <victorsubervi at gmail.com> declaimed the following in
> gmane.comp.python.general:
>
>        First suggestion... Get rid of the confusing dat[0] and dat[1] and
> use names that make sense... You can do this via tuple unpacking in the
> "for" statement...
>
> for (flda, fldb) in list_of_tuples:
>

Nice!

>
>        cursor.execute(
>                "select Category from categories order by Parent, ID")
>        Categories = [itm[0] for itm in cursor] #untuple single column
>        cursor.execute(
>                "select Parent from categories order by Parent, ID")
>        Parents = [itm[0] for itm in cursor]
>
>        The above will create the equivalent of your primary two lists --
> while also keeping them linked positionally.
>

Also nice! But doesn't the following achieve the same goal even more
cleanly?

       cursor.execute(
               "select Category, Parent from categories order by Parent,
ID")
       Categories, Parents = [itm[0], itm[1] for itm in cursor]


> MAXLEVEL = 15
>
> cursor.execute("""create table if not exists Categories
>                                        (       ID      integer
> auto_increment primary key,
>                                                Name varchar(40) not null,
>                                                unique (Name)
>                                        )"""    )
>
> cursor.execute("""create table if not exists Relationship
>                                        (       ID integer auto_increment
> primary key,
>                                                Parent  integer not null
>                                                        foreign key
> (Categories.ID),
>                                                Child   integer not null
>                                                        foreign key
> (Categories.ID),
>                                                check (Parent <> Child) )"""
>    )
>

The MySQL manual states "The CHECK clause is parsed but ignored by all
storage
engines." So, how does this help?


>
> #       Note that in this schema you
> #       1)      never duplicate NAMES
> #       2)      separate the NAMES from the parent<>child linkage
>
> def expand(fetched):
>        aDict = {}
>        for (name, ) in fetched:
>                aDict[name] = {}
>        return aDict
>

Does this not simply make an empty dictionary for every item? I see where
you call it here:
               levelDict[nm] = expand(cursor.fetchall())
but don't understand its purpose.


>
> def getChildren(levelDict, level = 0):
>        if level > MAXLEVEL:
>                return  #possibly the data has a cycle/loop
>        for (nm, dt) in levelDict:
>                cursor.execute("""select c.name from Categories as c
>                                                        inner join
> Relationship as r
>                                                                on c.ID =
> r.Child
>                                                        inner join
> Categories as p
>                                                                on r.Parent
> = p.ID
>                                                        where p.Name = %s
>                                                        order by c.name""",
>                                                        (nm,)   )
>                levelDict[nm] = expand(cursor.fetchall())
>                #recursive call to do next level
>                getChildren(levelDict[nm], level + 1)
>        # no data return as we are mutating dictionaries in place
>

"nm" must refer to "name". What is "dt"? It appears it's never used. What is
its purpose?

Thanks, Dennis! A really good lesson here! Look forward to learning exactly
how to do this. Thanks for your help!
V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091010/e7bd4626/attachment.html>


More information about the Python-list mailing list