6

I am trying in python to unpack some dict into some function:

I have a function that get packet as parameter (that should be dict)

def queue(self, packet):
    self.topic.publish(self.message, self.client, **packet)

and I call it this way:

queue({
        'an_item': 1,
        'a_key': 'value'
    })

the publish function, is in 3rd party api (Google Pub/Sub API) and from what I looked at source:

def publish(self, message, client=None, **attrs):
    ...
    message_data = {'data': message, 'attributes': attrs}
    message_ids = api.topic_publish(self.full_name, [message_data])

it's accepting **attrs in order to pass all keyword parameters into another function.

Currently.. my queue() function isn't working.

How, if possible, can I fix my queue() function to unpack the packet dict argument into something publish() will accept?

Thanks!


EDIT:

Some error messages I got.

for:

def queue(self, packet):
    self.topic.publish(self.message, self.client, **packet)

I get: TypeError: 1 has type <class 'int'>, but expected one of: (<class 'bytes'>, <class 'str'>)


for:

def queue(self, packet):
    self.topic.publish(self.message, self.client, packet)

I get: publish() takes from 2 to 3 positional arguments but 4 were given


for:

def queue(self, **packet):
    self.topic.publish(self.message, self.client, packet)

I get: TypeError: queue() takes 1 positional argument but 2 were given


and for:

def queue(self, *packet):
    self.topic.publish(self.message, self.client, packet)

I get: TypeError: publish() takes from 2 to 3 positional arguments but 4 were given


EDIT 2:

as @gall suggested correctly, it is the data I was sending and there is no problem with the unpacking. with this function:

def queue(self, packet):
    self.topic.publish(self.message, self.client, **packet)

It works when I call it with strings only:

queue({
        'an_item': '1',
        'a_key': 'value'
    })

Thank you all!

ET-CS
  • 6,334
  • 5
  • 43
  • 73
  • 1
    How is your function not working, could you give an error message? – Jonas Adler Jul 10 '17 at 14:30
  • 1
    Yes @JonasAdler . Thank you. I'll add error message example for what I've tried until now – ET-CS Jul 10 '17 at 14:31
  • 1
    what does `self.topic` refer to? an instance of the `Topic` class or just the class itself? – danidee Jul 10 '17 at 14:32
  • 1
    @danidee some object in google's sub/pub api that let you get access to publish() function for pub/sub channel. – ET-CS Jul 10 '17 at 14:33
  • 3
    In the docstring of ``publish`` it says ``:type attrs: dict (string -> string)`` but you seem to have one value as an int in your example. Could it be the problem? The way you call it seems fine otherwise. – Gall Jul 10 '17 at 14:38
  • @Gall. Yes! trying to send { 'an_item': '1', 'a_key': 'value' } works. does it mean I can't pass int as argument? mm.. so I guess the unpack actually worked :) – ET-CS Jul 10 '17 at 14:42

1 Answers1

1

According to the docstring of publish, attr must be a string -> string dict.

You can fix the issue by replacing

queue({
    'an_item': 1,
    'a_key': 'value'
})

with purely string arguments, e.g.

queue({
    'an_item': '1',
    'a_key': 'value'
})

It seems your issue had nothing to do with dictionary unpacking.

Jonas Adler
  • 10,365
  • 5
  • 46
  • 73