473,320 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1.     def search(self):
  2.         print '\nWhat do you wish to search for?\n'
  3.         print ' 1. Search for Title.'
  4.         print ' 2. Search for Year.'
  5.         print ' 3. ...'
  6.  
  7.         choice = raw_input('\nMenu choice: ')
  8.  
  9.         if choice == '1':
  10.             search_term = raw_input('\nSearch for a title: ')
  11.             print '\nResult: \n'
  12.             result = False
  13.             for i in range (len(movies)):
  14.                 if search_term in movies[i].title:
  15.                     print movies[i]
  16.                     result = True
  17.             if result == False:
  18.                 print 'No results found. \n'
  19.             user_library.search()
  20.  
  21.         if choice == '2':
  22.             search_term = raw_input('\nSearch for a year: ')
  23.             print '\nResult: \n'
  24.             result = False
  25.             for i in range (len(movies)):
  26.                 if search_term in movies[i].year:
  27.                     print movies[i]
  28.                     result = True
  29.             if result == False:
  30.                 print 'No results found. \n'
  31.             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:

Expand|Select|Wrap|Line Numbers
  1.     def search_menu(self):
  2.         print '\nWhat do you wish to search for?\n'
  3.         print ' 1. Search for Title.'
  4.         print ' 2. Search for Year.'
  5.         print ' 3. ...'
  6.  
  7.         choice = raw_input('\nMenu chocie: ')
  8.  
  9.         if choice == '1':
  10.             user_library.search('movies[i].title')
  11.  
  12.     def search(self, search_category):
  13.         self.search_category = search_category
  14.         search_term = raw_input('\nSearch: ')
  15.         print '\nResult: \n'
  16.         result = False
  17.         for i in range (len(movies)):
  18.             if search_term in search_category:
  19.                 print movies[i]
  20.                 result = True
  21.         if result == False:
  22.             print 'No results found. \n'
  23.         user_library.search()
However, then it just searches through the 'movies[i].title' string and it also gives me

Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 10 '09 #1
5 1813
bvdet
2,851 Expert Mod 2GB
Without seeing all your code, I am not sure what is going on.

In this line
Expand|Select|Wrap|Line Numbers
  1.         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...".
Dec 11 '09 #2
user_library.search() 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:
Expand|Select|Wrap|Line Numbers
  1. # if choice == '1':
  2. #             user_library.search('movies[i].title')
  3. #  
  4. #     def search(self, search_category):
  5. #         self.search_category = search_category
  6. #         search_term = raw_input('\nSearch: ')
  7. #         print '\nResult: \n'
  8. #         result = False
  9. #         for i in range (len(movies)):
  10. #             if search_term in search_category:
  11. #                 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!
Dec 11 '09 #3
bvdet
2,851 Expert Mod 2GB
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:
Expand|Select|Wrap|Line Numbers
  1.         if choice == '1':
  2.             user_library.search('title')
  3.  
  4.     def search(self, search_category):
  5.         self.search_category = search_category
  6.         search_term = raw_input('\nSearch for %s: ' % search_category)
  7.         for key in movies:
  8.             if search_term in movies[key][search_category]:
  9.                 return movie[key]
  10.         return False
where 'title' is a key in the subdictionary.
Dec 12 '09 #4
Glenton
391 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. In [8]: mylist=range(10)
  2. In [9]: command="mylist[i]"
  3. In [10]: for i in range(len(mylist)):
  4.    ....:     print command, eval(command)
  5.    ....:     
  6.    ....:     
  7. mylist[i] 0
  8. mylist[i] 1
  9. mylist[i] 2
  10. mylist[i] 3
  11. mylist[i] 4
  12. mylist[i] 5
  13. mylist[i] 6
  14. mylist[i] 7
  15. mylist[i] 8
  16. mylist[i] 9
  17.  
should show you the difference.

In your case, replacing line 18
Expand|Select|Wrap|Line Numbers
  1. if search_term in search_category:
  2.  
with
Expand|Select|Wrap|Line Numbers
  1. if search_term in eval(search_category)
  2.  
will probably work.
Dec 14 '09 #5
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.

:)
Dec 14 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Stephen Ferg | last post by:
I need a little help here. I'm developing some introductory material on Python for non-programmers. The first draft includes this statement. Is this correct? ...
0
by: Ralph Guzman | last post by:
TASK: I have to generate a report with all categories, subcategories and products in database. PROBLEM: I want to write one query that will return: 1. category 2. subcategory: determined by...
14
by: vic | last post by:
My manager wants me to develop a search program, that would work like they have it at edorado.com. She made up her requirements after having compared how search works at different websites, like...
4
by: Captain Wonky | last post by:
As the subject says... I'm a database novice even though I've been trying to learn Access for years. I've 'almost finished' several databases but always get stumped on something - this time it's...
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
1
by: Neil McGuigan | last post by:
Hi, I want to store product categories in my db and am a little lost as to where to start. They can be hierarchical, such as "Books" > "Cook Books", so my table is like this: int...
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
4
by: grabit | last post by:
Hi peoples I have the following query to return search results. How can i stop this from returning results containg words or phrases like "Hi Team, please check latest news. Cheers Rach" when i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.