sirvon
sirvon

Reputation: 2625

passing multiple parameters to dispatcher

I have this code working right now in my project.....

  def newChannel(cName):
      queue = j.queue(cName)
      r = queue.add_subscribers(*[subscriberCreateChanTable, subscriberSortScenes])

  def newNetwork(hName):
     queue = j.queue(hName)
     r = queue.add_subscribers(*[subscriber1a])

  def loginError(nName):
     pass

  def hName(ok):
     return ok[11][1]

  def cName(ok):
     return ok[12][1]

  def nName(ok):
     return ok[10][1]

  def eventType(ok):
     return ok[9][1]


  action = {
     'newChannel': (newChannel, cName),
     'newNetwork': (newNetwork, hName),
     'loginError': (loginError, nName)
     }

How can I tweak it to now accept multiple parameters?

  def newChannel(cName, hname, time):
      queue = j.queue(cName)
      r = queue.add_subscribers(*[subscriberCreateChanTable, subscriberSortScenes, hname])
      rd = time

  action = {
     'newChannel': (newChannel, cName, ??, ??),
     'newNetwork': (newNetwork, hName, ??),
     'loginError': (loginError, nName)
     }

   ok = parse_qsl(urlparse(u).query, keep_blank_values=True)
   handler, getter  = action.get(eventType(ok))
   handler(getter(ok))???

EDIT: What if i did this.....

         def cName(ok):
             return ok[9][1]
             return ok[12][1]
             return ok[8][1]

         action = {
         'newChannel': (newChannel, cName)
         }

         ok = parse_qsl(urlparse(u).query, keep_blank_values=True)
         handler, getter  = action.get(eventType(ok))
         handler(getter(ok))

Upvotes: 0

Views: 424

Answers (2)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

Quick and dirty:

def prepare_args(ok, *args):
    return [arg(ok) if callable(arg) else arg for arg in args]

action = {
    'newChannel': (newChannel, (cName, hName, whatever,),
    'newNetwork': (newNetwork, (hName,)),
    'loginError': (loginError, (nName,))
    }

ok = parse_qsl(urlparse(u).query, keep_blank_values=True)
handler, args  = action.get(eventType(ok))
args = prepare_args(ok, *args)
handler(*args)

but it might be better to start using a class to provide the context:

class QueryHandler(object):
    def __init__(self, u):
        self.ok = parse_qsl(urlparse(u).query, keep_blank_values=True)

    def newNetwork(self):
         queue = j.queue(self.hName)
         r = queue.add_subscribers(subscriber1a)

    def newChannel(self):
          queue = j.queue(self.cName)
          r = queue.add_subscribers(
              subscriberCreateChanTable,
              subscriberSortScenes,
              self.hname
              )
          rd = self.time

    def loginError(self):
        pass


    @property
    def time(self):
        return whatever_time_is_supposed_to_be

    @property
    def hName(self):
        return self.ok[11][1]

    @property
    def cName(self):
        return self.ok[12][1]

    @property
    def nName(self):
        return self.ok[10][1]

    @property
    def eventType(self):
        return self.ok[9][1]


    actions = {
        'newChannel': newChannel,
        'newNetwork': newNetwork,
        'loginError': loginError,
        }

    def dispatch(self):
        handler  = self.actions.get(self.eventType)
        handler(self)

Upvotes: 1

jonrsharpe
jonrsharpe

Reputation: 122061

The syntax for arbitrary parameters in Python is:

def func(*args, **kwargs):
    print(args) # positional
    print(kwargs) # keyword

Where the * is for tuple (un)packing of positional arguments and the ** is for dictionary un(packing) of keyword arguments. For example:

>>> func(1, 2, x=3, y=4)
(1, 2)
{'x':3, 'y':4} # not necessarily in that order

You can combine this with specified positional and keyword arguments:

def func2(pos1, *args, kw1="3", **kwargs):
    print(pos1)
    print(args)
    print(kw1)
    print(kwargs)

which would give:

>>> func2(1, 2, kw2=4)
1
(2,)
3
{'kw2': 4}

Upvotes: 0

Related Questions