Different search categories in a list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicehulk
    New Member
    • Dec 2009
    • 3

    Different search categories in a list

    I'm doing a library for DVD-movies and I'm trying to figure out how to search different categories like director, title, actor etc. (which are all variables in a DVD-class).

    Previously I did like this:

    Code:
        def search(self):
            print '\nWhat do you wish to search for?\n'
            print ' 1. Search for Title.'
            print ' 2. Search for Year.'
            print ' 3. ...'
    
            choice = raw_input('\nMenu choice: ')
            
            if choice == '1':
                search_term = raw_input('\nSearch for a title: ')
                print '\nResult: \n'
                result = False
                for i in range (len(movies)):
                    if search_term in movies[i].title:
                        print movies[i]
                        result = True
                if result == False:
                    print 'No results found. \n'
                user_library.search()
    
            if choice == '2':
                search_term = raw_input('\nSearch for a year: ')
                print '\nResult: \n'
                result = False
                for i in range (len(movies)):
                    if search_term in movies[i].year:
                        print movies[i]
                        result = True
                if result == False:
                    print 'No results found. \n'
                user_library.search()
    However, I want to avoid using the same code, and would rather have just one method. This is as far as I've gotten:

    Code:
        def search_menu(self):
            print '\nWhat do you wish to search for?\n'
            print ' 1. Search for Title.'
            print ' 2. Search for Year.'
            print ' 3. ...'
    
            choice = raw_input('\nMenu chocie: ')
            
            if choice == '1':
                user_library.search('movies[i].title')
    
        def search(self, search_category):
            self.search_category = search_category
            search_term = raw_input('\nSearch: ')
            print '\nResult: \n'
            result = False
            for i in range (len(movies)):
                if search_term in search_category:
                    print movies[i]
                    result = True
            if result == False:
                print 'No results found. \n'
            user_library.search()
    However, then it just searches through the 'movies[i].title' string and it also gives me

    Code:
    TypeError: search() takes exactly 2 arguments (1 given)
    Does anyone have a fairly quick sollution to this? I really appreciate the help! Sorry if something similar has been posted, I couldn't find anything. If it has, please direct me there.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Without seeing all your code, I am not sure what is going on.

    In this line
    Code:
            user_library.search()
    it appears you are making a recursive call to search(), but you are not passing an argument. That would produce a TypeError.

    I don't know what you mean by "However, then it just searches through the 'movies[i].title' string...".

    Comment

    • nicehulk
      New Member
      • Dec 2009
      • 3

      #3
      user_library.se arch() simply points back to the search method so that you can search again (and is not really important for my question.

      What I mean is that when I write this:
      Code:
      # if choice == '1':
      #             user_library.search('movies[i].title')
      #  
      #     def search(self, search_category):
      #         self.search_category = search_category
      #         search_term = raw_input('\nSearch: ')
      #         print '\nResult: \n'
      #         result = False
      #         for i in range (len(movies)):
      #             if search_term in search_category:
      #                 print movies[i]
      Is that instead of searching for search_term in movies[i].title (thus all movie titles in the library) it searches through the string 'movies[i].title'. So if you search for "Die Hard" (which is in the library) you get no result, but if you search for "movies[i]title" you will gett all movies printed...

      Hope you understand!

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I am going to assume that "movies" is a dictionary of dictionaries, not knowing the methods available in your DVD class. I would expect the code snippet to look something like this:
        Code:
                if choice == '1':
                    user_library.search('title')
         
            def search(self, search_category):
                self.search_category = search_category
                search_term = raw_input('\nSearch for %s: ' % search_category)
                for key in movies:
                    if search_term in movies[key][search_category]:
                        return movie[key]
                return False
        where 'title' is a key in the subdictionary.

        Comment

        • Glenton
          Recognized Expert Contributor
          • Nov 2008
          • 391

          #5
          Bvdet, movies is a class that nicehulk has defined, as far as I can figure out.

          Nicehulk, a bit more code/explanation of how the class is structured would be infinitely helpful!

          Looking at your first post, however, it seems that line 10 passes the "movie[i].title" string to the search function. This is why you're searching movie[i].title!

          However, all is not lost! Python has the very powerful exec and eval functions, which can execute and evaluate strings. (warning: one needs to be careful with these functions and use them sparingly!)

          But for example:
          Code:
          In [8]: mylist=range(10)
          In [9]: command="mylist[i]"
          In [10]: for i in range(len(mylist)):
             ....:     print command, eval(command)
             ....:     
             ....:     
          mylist[i] 0
          mylist[i] 1
          mylist[i] 2
          mylist[i] 3
          mylist[i] 4
          mylist[i] 5
          mylist[i] 6
          mylist[i] 7
          mylist[i] 8
          mylist[i] 9
          should show you the difference.

          In your case, replacing line 18
          Code:
          if search_term in search_category:
          with
          Code:
          if search_term in eval(search_category)
          will probably work.

          Comment

          • nicehulk
            New Member
            • Dec 2009
            • 3

            #6
            Thank you very much for your assistance, I'm sorry that I didn't make things really clear. Your advice was exactly what was needed, thank you Glenton and thank you bvdet for trying even though you needed more code from me.

            :)

            Comment

            Working...