Closed Thread
Results 1 to 5 of 5

Thread: dictionary as a case statement

  1. #1
    random guy is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    48
    Rep Power
    0

    dictionary as a case statement

    hi im trying to implement something in python that in c languages i would usually do with a case statement.

    Code:
       
        # Enter the main program loop
        while 1==1:
            # Display the menu to the user
            print "Enter in a choice"
            print "  1) Enter a new ticker to market"
            print "  2) Buy a stock"
            print "  3) Sell a stock"
            print "  4) Display portfolio results"
            print "  5) Exit Simulation"
            print "Portfolio Information:"
            print "  Cash:", portfolio.portfolioCash
            print "Market Information:"
            print "  Trading day:", currentTradingDay
            
            # Allow the user to select an menu entry
            choice = raw_input("Menu: ")
            { 
            '1': addStockDialogue(market), 
            '2': buyStock(), 
            '3': sellStock(), 
            '4': displayResults(), 
            '5': quitSimulation()
            }.get(choice, "Error, improper input")
    the issue is that when the inturpreter is initializing the dictionary it calls the functions in my dictionary (including quit). i think i might need something like a function pointer but im not really familiar with python yet.

    i could use if and else but typically a 'case statement' is 'cleaner'. i want to write good code.

    thanks for the help.
    Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
    add to my rep. its quick and easy and definitely wont steal your girlfriend.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: dictionary as a case statement

    In python everything is a pointer to the C structure PyObject*.
    So when the interpreters sees blabla(), it looks for an object called blabla, and the checks if this object can be called(in which case it is a function or a method).
    what you need to do is just give the function name as the value, what you are doing there is giving the returned value by those functions.
    so, following your logic you should do:
    Code:
    choice = raw_input("Choice: ")
    func = {
    "1":foo,
    "2':bar,
    "3":anotherFunction,
    }.get(choice,"blabla")
    func()
    And yes, I hate it when it comes to this and there is no switch statement in Python. I also see that you give args at one of the function, there are two solutions to this:
    -either create another dictionary which contains the args according to the choice
    -or just use the stupid and classic if...elif..else structure.
    Code:
    args = {
    "1":(market),
    "2":(player,enemy),
    }.get(choice)
    func(*args)
    note that args is a tuple and that there is a star in front of it when I pass it to func.

  4. #3
    random guy is offline Learning Programmer
    Join Date
    Nov 2007
    Posts
    48
    Rep Power
    0

    Re: dictionary as a case statement

    ahh i see. thank you. that makes sense. ya it would be nice to have a regular case statement. maybe im just too used to C/C++/Java.

    Thanks for the help.
    Hey like something i said? Helped you out? Or you just like supporting the Random Guy?
    add to my rep. its quick and easy and definitely wont steal your girlfriend.

  5. #4
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0

    Re: dictionary as a case statement

    There are also a couple of other ways that you can call functions with arguments in this way.

    1) Use an inline "lambda" function:
    Code:
    choice = raw_input("Menu: ")
    { 
    '1': lambda : addStockDialogue(market), 
    '2': buyStock,
    }.get(choice, "Error, improper input")
    The keyword "lambda" makes a temporary function. In this example, the temporary function simply calls your function with a parameter.

    2) Use a "curried function":
    Code:
    choice = raw_input("Menu: ")
    { 
    '1': curry(addStockDialogue, market), 
    '2': buyStock,
    }.get(choice, "Error, improper input")
    Currying is an advanced concept from functional programming, but it can be thought of as choosing the values of some arguments to a function without fully evaluating the function. There is a good recipe for implementing the curry function here (you would need to copy and paste this curry function code into your file before this example would work).

  6. #5
    manux's Avatar
    manux is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    234
    Blog Entries
    1
    Rep Power
    14

    Re: dictionary as a case statement

    Hmm, I hadn't thought about lambda.
    Looking at the curry example, a simple decorator could've made the job too.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 10
    Last Post: 10-09-2011, 11:51 AM
  2. Replies: 6
    Last Post: 11-28-2010, 11:00 PM
  3. Replies: 5
    Last Post: 10-27-2010, 12:25 AM
  4. Trying to create IF or CASE statement in Postgres SQL
    By Eric The Red in forum Database & Database Programming
    Replies: 0
    Last Post: 03-23-2010, 09:45 PM
  5. Dictionary Pop-up
    By elaps in forum JavaScript and CSS
    Replies: 1
    Last Post: 09-10-2007, 02:42 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts