Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63590

Re: nested dictionaries and functions in data structures.

Subject Re: nested dictionaries and functions in data structures.
From Sean Murphy <mhysnm1964@gmail.com>
Date 2014-01-09 21:08 +1100
References <59989803.3050665.1389097176191.JavaMail.root@sequans.com>
Newsgroups comp.lang.python
Message-ID <mailman.5253.1389262128.18130.python-list@python.org> (permalink)

Show all headers | View raw


Thanks for that. I will have a play and see how I can apply your example. 
On 07/01/2014, at 11:19 PM, Jean-Michel Pichavant <jeanmichel@sequans.com> wrote:

> ----- Original Message -----
>> Thanks for that. It resolved the issue and it was so simple compared
>> to everything else I saw on the net.
>> 
>> Only outstanding thing I have to work out is how to execute functions
>> from a dictionary. I will continue searching on the net.
>> 
>> 
>> Sean
> 
> This may help you (untested code)
> 
> class Workers:
>  @staticmethod
>  def ospf(*args, **kwargs):
>    print args, kwargs
>    return 'Bar'
> 
>  @staticmethod
>  def bhp(*args, **kwargs):
>    return 'Foo'
> 
> outputs = {'ospf' : {1 : {'param1' : 'foo', 'param2' : 'foo2'}}
> 
> for command, value in outputs.items():
>  for counter, kwargs in value:
>    func = getattr(Workers, command) # get the function
>    func(**kwargs) # the actual call 
> 
> It would be possible to do it without the *args, **kwargs magic. You can write ospf and bhp with the explicit parameters, however you must make sure you pass the correct number of parameter in the call.
> 
> JM
> 
> 
> -- IMPORTANT NOTICE: 
> 
> The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: nested dictionaries and functions in data structures. Sean Murphy <mhysnm1964@gmail.com> - 2014-01-09 21:08 +1100

csiph-web