Storing nothing in a dictionary and passing it to a function

6

63q2o4i02

Hi,

I'm writing a hand-written recursive decent parser for SPICE syntax
parsing. In one case I have one function that handles a bunch of
similar cases (you pass the name and the number of tokens you're
looking for). In another case I have a function that handles a
different set of tokens and so it can't use the same arguments as the
first one, and in fact takes no arguments. However, these functions
are semantically similar and are called from the same place one right
after the other.

I'd like to have a dictionary (actually a nested dictionary) to call
these functions so I can avoid if-then-elsing everything. Eath
dictionary item has three things in it: the function to be called, a
string to pass to the function (which is also the key to the dict), and
a tuple to pass to the function. In the case of the function with no
arguments, obviously I'd like not to pass anything.

I'm trying to do this 'functionally' (i guess), by avoiding
if-then-elses and just calling out the functions by accessing them and
their arguments from the dictionary.

something like this:
alldict = \
{'pulse': {'func': self.arbtrandef, 'args':(2,5)},\
'sin' : {'func': self.arbtrandef, 'args':(2,3)},\
'exp' : {'func': self.arbtrandef, 'args':(2,4)},\
'pwl' : {'func': self.pwldef , 'args': (None,)},\ <------- how
do I store "no" arguments?
'sffm' : {'func': self.arbtrandef, 'args':(5,0)}}

for it in alldict.items():
name = it[0]
args = (name,) + it[1]['args']
it[1]['func'](*args)

So basically this doesn't work. I am either trying to pass a tuple of
(name, None,) to a function (pwldef) that doesn't take any arguments,
or I'm trying to pass None itself, which also doesn't work. I could
try changing pwldef to take 3 arguments and then just throw them away,
but that's cheesy. It almost seems like in 'args' there should be a
little function that creates an argument. I tried using a lambda in
another situation to "do" something (an assignment) rather than
"return" something, but it didn't work.

Any thoughts?
thanks
ms
 
K

Kent Johnson

Hi,

I'm writing a hand-written recursive decent parser for SPICE syntax
parsing. In one case I have one function that handles a bunch of
similar cases (you pass the name and the number of tokens you're
looking for). In another case I have a function that handles a
different set of tokens and so it can't use the same arguments as the
first one, and in fact takes no arguments. However, these functions
are semantically similar and are called from the same place one right
after the other.

I'd like to have a dictionary (actually a nested dictionary) to call
these functions so I can avoid if-then-elsing everything. Eath
dictionary item has three things in it: the function to be called, a
string to pass to the function (which is also the key to the dict), and
a tuple to pass to the function. In the case of the function with no
arguments, obviously I'd like not to pass anything.

I'm trying to do this 'functionally' (i guess), by avoiding
if-then-elses and just calling out the functions by accessing them and
their arguments from the dictionary.

something like this:
alldict = \
{'pulse': {'func': self.arbtrandef, 'args':(2,5)},\
'sin' : {'func': self.arbtrandef, 'args':(2,3)},\
'exp' : {'func': self.arbtrandef, 'args':(2,4)},\
'pwl' : {'func': self.pwldef , 'args': (None,)},\ <------- how
do I store "no" arguments?
'sffm' : {'func': self.arbtrandef, 'args':(5,0)}}

for it in alldict.items():
name = it[0]
args = (name,) + it[1]['args']
it[1]['func'](*args)

So basically this doesn't work.

Any thoughts?

You could omit the 'args' entry completely and test for this in the
dispatch:
'pwl' : {'func': self.pwldef},\

for name, params in alldict.items():
try
args = (name,) + params['args']
except KeyError:
args = ()
params['func'](*args)

Or include the 'name' parameter in the arg list and use an empty tuple
for the arg to pwldef:

'exp' : {'func': self.arbtrandef, 'args':('exp', 2,4)},\
'pwl' : {'func': self.pwldef , 'args': ()},\

for name, params in alldict.items():
params['func'](*args)

Kent
 
M

Maric Michaud

Le Lundi 05 Juin 2006 19:18, (e-mail address removed) a écrit :
Any thoughts?

In [24]: a, b = (lambda : 'works like this'), (lambda a, b : (a,b))

In [25]: a(*())
Out[25]: 'works like this'

In [26]: b(4,3)
Out[26]: (4, 3)


--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
M

Maric Michaud

Le Lundi 05 Juin 2006 19:40, Maric Michaud a écrit :
Le Lundi 05 Juin 2006 19:18, (e-mail address removed) a écrit :
oups wanted to wirte this :

In [27]: a, b = (lambda : 'works like this'), (lambda *a : a)

In [28]: a(*())
Out[28]: 'works like this'

In [29]: b(*())
Out[29]: ()

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 
R

Roberto Bonvallet

(e-mail address removed):
I'd like to have a dictionary (actually a nested dictionary) to call
these functions so I can avoid if-then-elsing everything. Eath
dictionary item has three things in it: the function to be called, a
string to pass to the function (which is also the key to the dict), and
a tuple to pass to the function. In the case of the function with no
arguments, obviously I'd like not to pass anything. [...]
something like this:
alldict = \
{'pulse': {'func': self.arbtrandef, 'args':(2,5)},\
'sin' : {'func': self.arbtrandef, 'args':(2,3)},\
'exp' : {'func': self.arbtrandef, 'args':(2,4)},\
'pwl' : {'func': self.pwldef , 'args': (None,)},\ <------- how
do I store "no" arguments?
'sffm' : {'func': self.arbtrandef, 'args':(5,0)}}

for it in alldict.items():
name = it[0]
args = (name,) + it[1]['args']
it[1]['func'](*args)

I would do it like this:

alldict = {
'pulse': (self.arbtrandef, (2, 5)), # function and args packed in a tuple
'sin' : (self.arbtrandef, (2, 3)),
'exp' : (self.arbtrandef, (2, 4)),
'pwl' : (self.pwldef, ()), # empty tuple represented by ()
'sffm' : (self.arbtrandef, (5, 0)),
}
for (fname, (func, args)) in alldict.items(): # items unpacked directly
func(fname, *args)

Best regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top