6 Replies - 585 Views - Last Post: 20 January 2013 - 01:57 PM Rate Topic: -----

#1 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Better way to write this?

Posted 18 January 2013 - 06:03 PM

I don't know if there is an easier way to write this in python

#SEARCH BY NAME
            if user_choice == '1' and action == '1':
                print_files((search_by_name(all_list)))

            elif user_choice == '1' and action == '2':
                print_first_line_of_text((search_by_name(all_list)))

            elif user_choice == '1' and action == '3':
                copy_file((search_by_name(all_list)))

            elif user_choice == '1' and action == '4':
                touch_file((search_by_name(all_list)))

            ###############

            #SEARCH BY NAME ENDING
            elif user_choice == '2' and action == '1':
                print_files((search_by_name_ending(all_list)))

            elif user_choice == '2' and action == '2':
                print_first_line_of_text((search_by_name_ending(all_list)))

            elif user_choice == '2' and action == '3':
                copy_file((search_by_name_ending(all_list)))

            elif user_choice == '2' and action == '4':
                touch_file((search_by_name_ending(all_list)))

            #################

            #SEARCH BY SIZE
            elif user_choice == '3' and action == '1':
                print_files((search_by_size(all_list)))

            elif user_choice == '3' and action == '2':
                print_first_line_of_text((search_by_size(all_list)))

            elif user_choice == '3' and action == '3':
                copy_file((search_by_size(all_list)))

            elif user_choice == '3' and action == '4':
                touch_file((search_by_size(all_list)))

         ############                           



there are 2 menus thats the user can choose from, user_choice and action

Is This A Good Question/Topic? 0
  • +

Replies To: Better way to write this?

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,065
  • Joined: 12-December 12

Re: Better way to write this?

Posted 18 January 2013 - 06:35 PM

    #SEARCH BY NAME
    if user_choice == '1':
        if action == '1':
            print_files((search_by_name(all_list)))
        elif action == '2':
           print_first_line_of_text((search_by_name(all_list)))
        elif action == '3':
            copy_file((search_by_name(all_list)))
        elif action == '4':
            touch_file((search_by_name(all_list)))
    #SEARCH BY NAME ENDING
    elif user_choice == '2':
        pass # etc..


There are other ways using dictionaries but they get a little messy with nested loops.

This post has been edited by andrewsw: 18 January 2013 - 06:42 PM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,065
  • Joined: 12-December 12

Re: Better way to write this?

Posted 18 January 2013 - 06:41 PM

Actually..

#SEARCH BY NAME
if user_choice == '1':
    {   '1': print_files,
        '2': print_first_line_of_text,
        '3': copy_file,
        '4': touch_file
    }.get(action)(search_by_name(all_list))
#SEARCH BY NAME ENDING
elif user_choice == '2':
    pass # etc..


I haven't tested this but I'm sure it will work :)

This post has been edited by andrewsw: 18 January 2013 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,806
  • Joined: 16-October 07

Re: Better way to write this?

Posted 19 January 2013 - 04:57 AM

Looking at the logic, this would be my first go:
user_choice_results = None
if user_choice == '1':
	user_choice_results = search_by_name(all_list)
elif user_choice == '2':
	user_choice_results = search_by_name_ending(all_list)
elif user_choice == '3'
	user_choice_results = search_by_size(all_list)

if action == '1':
	print_files(user_choice_results)
elif action == '2':
	print_first_line_of_text(user_choice_results)
elif action == '3':
	copy_file(user_choice_results)
elif action == '4':
	touch_file(user_choice_results)



That's the most straight forward cleanup.

However, a more pythony way to do it would be to make a list. If your values are validated, always numeric, and sequential, you can do:
user_choice_funcs = [ search_by_name, search_by_name_ending, search_by_size ]
action_funcs = [ print_files, print_first_line_of_text, copy_file, touch_file ]

action_func = action_funcs[ int(action)-1 ]
user_choice_func = user_choice_funcs[ int(user_choice)-1 ]

action_func( user_choice_func(all_list) )



The dictionary option has already been give, for if your selections aren't sequential numbers. I'm afraid this is my favorite game and I had to join in.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2151
  • View blog
  • Posts: 3,306
  • Joined: 21-June 11

Re: Better way to write this?

Posted 19 January 2013 - 05:06 AM

You're always calling two functions. The first only depends on action and the second only depends on user_choice. So instead of checking each possible combination of action and user_choice, you should just check each variable separately:

if action == '1':
  io_function = print_files
elif action == '2':
  io_function = print_first_line_of_text
# and so on

if user_choice == '1':
  search_function = search_by_name
# and so on

io_function(search_function(all_list))



Instead of if-elif you could also use dictionaries like andrewsw suggested.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3737
  • View blog
  • Posts: 13,065
  • Joined: 12-December 12

Re: Better way to write this?

Posted 20 January 2013 - 12:21 PM

Just to turn this discussion on its head for a moment.. there is an argument to be made to keep the very first version, or my first alternative to it.

The revisions are made by recognising patterns in the code. If, in six months time, a new function needs to be incorporated that doesn't match the current pattern, then it would require a little re-coding/ re-interpretation. With the earlier versions this would be a simple matter.

The difference in performance is insignificant and, of course, the most recent version is the most logically-direct. I, of course!, like my second, dictionary version: it's pretty :online2long:
Was This Post Helpful? 0
  • +
  • -

#7 farazamiruddin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-January 13

Re: Better way to write this?

Posted 20 January 2013 - 01:57 PM

View Postandrewsw, on 20 January 2013 - 12:21 PM, said:

Just to turn this discussion on its head for a moment.. there is an argument to be made to keep the very first version, or my first alternative to it.

The revisions are made by recognising patterns in the code. If, in six months time, a new function needs to be incorporated that doesn't match the current pattern, then it would require a little re-coding/ re-interpretation. With the earlier versions this would be a simple matter.

The difference in performance is insignificant and, of course, the most recent version is the most logically-direct. I, of course!, like my second, dictionary version: it's pretty :online2long:/>


Thank you for all the help, it is for a school project so I am assuming I will have to be changing throughout the semester. I agree yours looks much cleaner! Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1