Unpacking a dictionary and passing to a function as keyword parameters
Asked Answered
C

1

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!

Cachinnate answered 10/7, 2017 at 14:25 Comment(6)
How is your function not working, could you give an error message?Psilocybin
Yes @JonasAdler . Thank you. I'll add error message example for what I've tried until nowCachinnate
what does self.topic refer to? an instance of the Topic class or just the class itself?Tater
@Tater some object in google's sub/pub api that let you get access to publish() function for pub/sub channel.Cachinnate
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.Sawtelle
@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 :)Cachinnate
P
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.

Psilocybin answered 10/7, 2017 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.