Re: [Tutor] dict['_find']
Steven, Alan, Knacktus, thanks for your help. I was indeed very confused because I thought '_find' was calling something special instead of just being added to the dictionary (the confusion stemming from http://www.python.org/dev/peps/pep-0004/ where find module is obsolete). When I ran the code, the error I got was: max:python max$ python ex40.py State? (ENTER to quit) > CA Traceback (most recent call last): File "ex40.py", line 22, in city_found = cities['_find'](cities, state) File "ex40.py", line 8, in find_city return themap(state) TypeError: 'dict' object is not callable Two main mistakes: (1) not including the actual search in the while loop and (2) wrong parentheses in return themap(state) instead of return themap[state]. Fixed version which runs: cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} def find_city(themap, state): if state in themap: return themap[state] else: return "Not found." cities['_find'] = find_city while True: print "State? (ENTER to quit)", state = raw_input ("> ") if not state: break city_found = cities['_find'](cities, state) print city_found Thanks for your help, especially the comments about keeping things separate and explicit. Now to find out why Zed Shaw thinks this is a good way of doing it... Best, Max On Sun, Feb 20, 2011 at 9:40 AM, Knacktus wrote: > Am 20.02.2011 05:14, schrieb Max Niederhofer: > >> Hello all, > > Hello Max, >> >> first post, please be gentle. I'm having serious trouble finding an >> alternative for the deprecated find module for dictionaries. >> >> The code (from Zed Shaw's Hard Way, exercise 40) goes something like >> this. Hope indentation survives. >> >> cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} > > I use a naming convention for dicts that has made me very happy on several > occasion ;-): > key_to_value, in your case > state_to_city = {...} >> >> def find_city(themap, state): >> if state in themap: >> return themap[state] >> else: >> return "Not found." >> >> cities['_find'] = find_city > > Did you put this entry into the same dictionary as the data on purpose? > Or is the purpose a kind of dispatch? Something that could be a dict on its > own, like > private_function_name_to_function = {'_find': find_city} > You should try to keep things seperate and explicit. >> >> while True: >> print "State? (ENTER to quit)", >> state = raw_input("> ") >> >> if not state: break >> >> city_found = cities['_find'](cities, state) >> print city_found >> >> My question is - how do I rewrite this using an alternate module given >> find is deprecated? Grateful for all suggestions or pointers. For >> reference, I'm using 2.6.1 on darwin. >> >> Thanks so much for your help. >> >> Best, >> Max >> >> -- >> Dr. Maximilian Niederhofer >> Founder, Qwerly >> http://qwerly.com/ | http://qwerly.com/max >> +44 78 3783 8227 >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Dr. Maximilian Niederhofer Founder, Qwerly http://qwerly.com/ | http://qwerly.com/max +44 78 3783 8227 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict['_find']
Am 20.02.2011 05:14, schrieb Max Niederhofer: Hello all, Hello Max, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation survives. cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} I use a naming convention for dicts that has made me very happy on several occasion ;-): key_to_value, in your case state_to_city = {...} def find_city(themap, state): if state in themap: return themap[state] else: return "Not found." cities['_find'] = find_city Did you put this entry into the same dictionary as the data on purpose? Or is the purpose a kind of dispatch? Something that could be a dict on its own, like private_function_name_to_function = {'_find': find_city} You should try to keep things seperate and explicit. while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break city_found = cities['_find'](cities, state) print city_found My question is - how do I rewrite this using an alternate module given find is deprecated? Grateful for all suggestions or pointers. For reference, I'm using 2.6.1 on darwin. Thanks so much for your help. Best, Max -- Dr. Maximilian Niederhofer Founder, Qwerly http://qwerly.com/ | http://qwerly.com/max +44 78 3783 8227 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict['_find']
"Max Niederhofer" wrote first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. I think you are misunderstanding some of the terminology. There is no deprecated find module. You are not using any modules in your code. And you are not doing anything special with "_fnd", it is just a string which you use as a key in your dictionary. cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} def find_city(themap, state): if state in themap: return themap[state] else: return "Not found." cities['_find'] = find_city What this does is assign the function find_city as the value corresponding to the key "_find" in your dictionary. You could use any name you like: 'check' would be one meaningful example... However, there is no obvious reason to put the function in the dictionary at all...? while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break This loops round collecting state names from the user and throwing them away until the user eventually gets fed up and hits enter. At that point state holds an empty string. I don't think you want that. city_found = cities['_find'](cities, state) print city_found I think you want this inside the loop above... My question is - how do I rewrite this using an alternate module given find is deprecated? '_find' is just a key, as such it is not deprecated. What made you think it was? Did you get an error message? If so always post the entire error text, it helps us enormously. reference, I'm using 2.6.1 on darwin. With the slight change to the indentyation of the last two lines your code should work. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dict['_find']
Max Niederhofer wrote: Hello all, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. What find module for dictionaries? The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation survives. cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} def find_city(themap, state): if state in themap: return themap[state] else: return "Not found." cities['_find'] = find_city What is the purpose of this? You have a dictionary called "cities" that contains four items. The first three are pairs of State:City, which makes sense. The fourth is a pair of the word "_find" matched with a function find_city. I don't understand what the purpose of this is. while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break city_found = cities['_find'](cities, state) print city_found I think you need to include the actual search inside the while loop. Otherwise, the loop keeps asking for a new state, but doesn't do anything with it until you exit the loop. while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break city_found = cities['_find'](cities, state) print city_found But I don't understand the purpose of placing the function inside the dictionary. Much clearer and simpler is: while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break print find_city(cities, state) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] dict['_find']
Hello all, first post, please be gentle. I'm having serious trouble finding an alternative for the deprecated find module for dictionaries. The code (from Zed Shaw's Hard Way, exercise 40) goes something like this. Hope indentation survives. cities = {'CA': 'San Francisco', 'MI': 'Detroit', 'FL': 'Jacksonville'} def find_city(themap, state): if state in themap: return themap[state] else: return "Not found." cities['_find'] = find_city while True: print "State? (ENTER to quit)", state = raw_input("> ") if not state: break city_found = cities['_find'](cities, state) print city_found My question is - how do I rewrite this using an alternate module given find is deprecated? Grateful for all suggestions or pointers. For reference, I'm using 2.6.1 on darwin. Thanks so much for your help. Best, Max -- Dr. Maximilian Niederhofer Founder, Qwerly http://qwerly.com/ | http://qwerly.com/max +44 78 3783 8227 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor