Greg skrev:
> Nis,
> Thanks for the help.  We'll I got my view to work for website visitors
> searching by any combintation of Size, Price, and Color.  My code is
> not the most efficient and I wonder how the performance is going to be
> once I add more products and take it off of developmental version and
> into a production environment.
>
> If anybody has any suggestions on how to make the code more optimized
> would be appreciated.  Here is my view:
>   
What Tim said, and this:

It would make your code much more readable if you used meaningful
variable names.

Python makes the need for explicit looping a lot less frequent than most
other programming languages. Whenever you are looping through a
list/dictionary and comparing each value to some other value, chance is
that you should really be using "if a in b".

In general, dont use

my_dictionary = {}
my_dictionary[x] = x

Use

myset = set()
myset.add(x)

And finally:

Don't Repeat Yourself. Whenever you have two blocks of code that looks
almost the same, try to refactor your code. Example:
>                                       NO_COLOR = "---------------"
>                                       styles = 
> Choice.objects.get(id=h.id).style_set.all()
>                                       if request['color'] <> NO_COLOR: 
>                                               styles = 
> styles.filter(color_cat=request['color'])
>                                               for j in styles: 
>                                                       num = 0
>                                                       for a in dict:
>                                                               if a == j:
>                                                                       num = 1
>                                                       if num == 0:
>                                                               dict[j] = j
>                                       else:
>                                               for p in styles: 
>                                                       num = 0
>                                                       for a in dict:
>                                                               if a == p:
>                                                                       num = 1
>                                                       if num == 0:
>                                                               dict[p] = 
> p#assert False, styles
>   
First step: Rename variables so they match in the two branches:

                                        NO_COLOR = "---------------"
                                        styles = 
Choice.objects.get(id=h.id).style_set.all()
                                        if request['color'] <> NO_COLOR:
                                                styles = 
styles.filter(color_cat=request['color'])
                                                for style in styles: 
                                                        num = 0
                                                        for key in dict:
                                                                if key == style:
                                                                        num = 1
                                                        if num == 0:
                                                                dict[style] = 
style
                                        else:
                                                for style in styles: 
                                                        num = 0
                                                        for key in dict:
                                                                if key == style:
                                                                        num = 1
                                                        if num == 0:
                                                                dict[style] = 
style

Second step: Move the common block out of the conditional

                                        NO_COLOR = "---------------"
                                        styles = 
Choice.objects.get(id=h.id).style_set.all()
                                        if request['color'] <> NO_COLOR:
                                                styles = 
styles.filter(color_cat=request['color'])
                                        for style in styles: 
                                                num = 0
                                                for key in dict:
                                                        if key == style:
                                                                num = 1
                                                if num == 0:
                                                        dict[style] = style



Third step: Replace the logic "If the key is not in the dictionary, put it 
there" with "put the key in the dictionary". Since the value associated with 
the key is always the same, adding it multiple times does not break things.

                                        NO_COLOR = "---------------"
                                        styles = 
Choice.objects.get(id=h.id).style_set.all()
                                        if request['color'] <> NO_COLOR:
                                                styles = 
styles.filter(color_cat=request['color'])
                                        for style in styles: 
                                                dict[style] = style

Now that is almost reasonable. Replacing the dict with a set is left as an 
exercise for the reader.

Nis


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to