code down sizing

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

code down sizing

Brain Murphy
I am having problems down sizing my code. My parents asked me to make a program were they can go into and find the recipe that they are looking for, and it will tell them the issue it is in.
 
My code is huge and I have not been able to make it smaller
<<<
def print_options():
    print " Cover Recipes"
    print "Options"
    print "'P' Print options"
    print " please chose a-z corresponding with the recipe name you are looking for."
    print "'B' capital B to go back"
choice = "P"
while choice !="B":
    if choice == "a":
        import a
    elif choice == "b":
        print "No recipes were found that start with B"
    elif choice == "c":
        import c
........................
        elif choice == "z":
         print "No recipes were found that start with Z"
    elif choice != "B":
        print_options()
    choice = raw_input("Enter choice: ")>>>
 
as you can see it is huge for its size, I know it can be smaller like
choice  = raw_input("Enter letter:%") for % in %'s list
list = az-aZ-AZ
if choice in list = % import %
 
or something along those lines but I can not figure it out. Can any one help me with this?
Brian


 


The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider.
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: code down sizing

Sean Perry
Brain Murphy wrote:
>   or something along those lines but I can not figure it out. Can any one help me with this?

What you want is a lookup device. Consider:

def do_a():
     import a
     # other code

def do_b():
     import b
     # other code

def do_c():
     import c
     # other code

choices = {'a': do_a,
            'b': do_b,
            'c': do_c,}
choice = raw_input("Enter choice: ")
action = choices[choice]
action()

(Rapidly entered but close to accurate ...)
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: code down sizing

Andy Wiggin
The standard "cmd" module provides a class which does the suggested
lookup/dispatch, but using reflection. You just define methods like
do_a(), then "a" automatically becomes a recognized command.  That
might be slightly simpler, and you get command recall, etc. -Andy
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: code down sizing

Marilyn Davis
In reply to this post by Brain Murphy

----- On Friday, September 1, 2006 [hidden email] wrote:

> Brain Murphy wrote:
>>   or something along those lines but I can not figure it out. Can any one
> help me with this?
>
> What you want is a lookup device. Consider:
>
> def do_a():
> import a
> # other code
>
> def do_b():
> import b
> # other code
>
> def do_c():
> import c
> # other code
>
> choices = {'a': do_a,
> 'b': do_b,
> 'c': do_c,}
> choice = raw_input("Enter choice: ")
> action = choices[choice]
> action()
>
> (Rapidly entered but close to accurate ...)

Sean's idea about a lookup device is a good one to learn.  However, for your special case, where the module names exactly match the chosen letters, you might like this trick:

try:
    exec("import " + choice.lower())
except ImportError:
    print "No recipes were found that start with " + choice

You might find, in the long run, that storing your recipies as python code is not the most flexible possibility.  You might prefer to keep them in a file that you read and write.  That will allow you to add new recipes more easily.  And it will leave you in position to write code so that your parents can type "enchilada cassarole" when they really want the "green chili enchilada cassarole"

Good luck!

Marilyn Davis


_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Fwd: code down sizing

Shannon -jj Behrens
In reply to this post by Brain Murphy
I couldn't figure out why everyone kept saying the same things I did
until I realized I forgot to CC the list:

---------- Forwarded message ----------
From: Shannon -jj Behrens <[hidden email]>
Date: Sep 1, 2006 2:26 PM
Subject: Re: [Baypiggies] code down sizing
To: Brain Murphy <[hidden email]>


Here are some ideas:

* Consolidate multiple prints into a single print:

print """\
hi
there"""

* Use dynamic imports (i.e. __import__).

* Use the cmd module.

-jj

On 9/1/06, Brain Murphy <[hidden email]> wrote:

>
> I am having problems down sizing my code. My parents asked me to make a
> program were they can go into and find the recipe that they are looking for,
> and it will tell them the issue it is in.
>
> My code is huge and I have not been able to make it smaller
> <<<
> def print_options():
>     print " Cover Recipes"
>     print "Options"
>     print "'P' Print options"
>     print " please chose a-z corresponding with the recipe name you are
> looking for."
>     print "'B' capital B to go back"
> choice = "P"
> while choice !="B":
>     if choice == "a":
>         import a
>     elif choice == "b":
>         print "No recipes were found that start with B"
>     elif choice == "c":
>         import c
> ........................
>         elif choice == "z":
>          print "No recipes were found that start with Z"
>     elif choice != "B":
>         print_options()
>     choice = raw_input("Enter choice: ")>>>
>
> as you can see it is huge for its size, I know it can be smaller like
> choice  = raw_input("Enter letter:%") for % in %'s list
> list = az-aZ-AZ
> if choice in list = % import %
>
> or something along those lines but I can not figure it out. Can any one help
> me with this?
> Brian
>
>
>
>
>
>  ________________________________
>  The all-new Yahoo! Mail goes wherever you go - free your email address from
> your Internet provider.
>
>
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> http://mail.python.org/mailman/listinfo/baypiggies
>
>
>
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: code down sizing

Keith Dart-2
In reply to this post by Marilyn Davis
Marilyn Davis wrote the following on 2006-09-01 at 16:01 PDT:
===
> You might find, in the long run, that storing your recipies as python
> code is not the most flexible possibility.  You might prefer to keep
> them in a file that you read and write.
===

Naw, what he really needs is a relational database with web UI using
Django. ;-)

As an exercise he could write an ISAM in Python. I did those in BASIC
as a kid, but writing one in Python should be easier.

Another alternative is to use the "shelve" module to store dictionaries.


--

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   Keith Dart <[hidden email]>
   public key: ID: 19017044
   <http://www.dartworks.biz/>
   =====================================================================
_______________________________________________
Baypiggies mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/baypiggies