0

I have this dispatch dict that i want to fill with 1000s of keys, mostly tied to one function. Although assigning keys to different functions is a future requirement. Currently tho, there's gotta be something better than this:

  art = json.dumps(work['artist_name'])
  phonenumber = json.dumps(work['From'])
  x = getnewmusic()                                         
  artists = {'drake':x, 'jayz':x, 'nas':x,'lil wayne':x,'schoolboyq':x,
             'kendrick lamar':x, 'j cole':x} 

  if art in artists:
      artists[art](art,phonenumber)

Anything fastest, better standard practice, future problems?

sirvon
  • 2,547
  • 1
  • 31
  • 55
  • If the majority of your keys map to the same function, you'd benefit from not specifying them at all in your dictionary - use `get` and you can specify that common function as the value if the key is not in your dictionary. Something like `artist_function = artists.get(name, x)` - but it's not at all clear what you're trying to accomplish here. – Peter DeGlopper Apr 08 '14 at 03:40
  • Peter's comment above is the best thing to do. However, one short coming of having a `default` function is that you can not validate the artist. You would need some way of validating the artist besides "if art in artists". – jaime Apr 08 '14 at 03:46
  • @PeterDeGlopper in that way how would i pass params on to function x. – sirvon Apr 08 '14 at 03:55
  • Hi there, is it really necessary to have your artists in a dictionary?, all artists invoke the same function so you can have them in a list and just invoke the function while traversing the list. – iferminm Apr 08 '14 at 04:13
  • @israelord can you post an example as an answer,it might be the way. – sirvon Apr 08 '14 at 04:14
  • looking up in list, isnt that slower than looking up in dict – sirvon Apr 08 '14 at 04:15
  • 1
    yes, but you're actually doing a double lookup, you get your key from a dictionary and then converting that to json (that's what I understand), it's not really clear what you're trying to do, but the "art in artists" statement, will perform a lookup in the dictionary's key list. Then, you perform a O(1) lookup to get the key and invoke the function in the body of your "if" statement. – iferminm Apr 08 '14 at 04:22
  • *"dispatch dict with 1000s of keys, mostly tied to one function"* That's a very strong code smell that you should just use one function and add special-cases to it. – smci Jul 17 '18 at 01:35

1 Answers1

1

Why don't you put your artists in a list and just invoke the function, all of them seems to be invoking the same function, you can do the following:

def get_new_music(artist, phonenumber):
    #logic to get new music
    pass

art = json.dumps(work['artist_name'])
phonenumber = json.dumps(work['From'])                                        
artists = ['drake', 'jayz', 'nas','lil wayne','schoolboyq', 'kendrick lamar', 'j cole']

if art in artists:
    get_new_music(art, phonenumber)

Since all of them seems to be invoking the same function, I think this is an easier way to do it.

iferminm
  • 2,019
  • 19
  • 34