statement level resumable exception

I

ilejn

Hello!

I have a sequence of a function calls. Basically it looks like

f(arg1)
f(arg2)
....
f(argN)

though real arguments are complex multilayer lists.

The problem is some arguments are not known and I get NameError
exceptions.

The solutions I know
1. wrap every f call in try/except block
2. make (currently global) argument attributes of a class and use
__getattr__ to convert unknown attributes to something recognizable by
f.


Is it possible to use something more elegant?
I had a hope decorators are helpful here, though apparently they are
not ...
Suggestions?

Thanks.

PS. The question is about Python 2.4
 
C

Chris Rebert

Hello!

I have a sequence of a function calls. Basically it looks like

f(arg1)
f(arg2)
...
f(argN)

though real arguments are complex multilayer lists.

The problem is some arguments are not known and I get NameError
exceptions.

Why aren't they known? It's a very bad code smell to have possibly
completely-undefined variables.

Cheers,
Chris
 
I

ilejn

Chris,

this is a long story why arguments may be not known.
Briefly these arguments come (or may come) from XML and may be not
specified.

A function call which does not have all arguments defined must be
skipped as gracefully as possible.
What I am asking about is how to achieve this goal.

Thanks.


Why aren't they known? It's a very bad code smell to have possibly
completely-undefined variables.

Cheers,
Chris
--http://blog.rebertia.com

Best regards,
Ilja Golshtein.
 
A

Arnaud Delobelle

ilejn said:
Hello!

I have a sequence of a function calls. Basically it looks like

f(arg1)
f(arg2)
...
f(argN)

though real arguments are complex multilayer lists.

The problem is some arguments are not known and I get NameError
exceptions.

The solutions I know
1. wrap every f call in try/except block
2. make (currently global) argument attributes of a class and use
__getattr__ to convert unknown attributes to something recognizable by
f.

for name in 'arg1', 'arg2', ... 'argN':
try:
arg = globals()[name]
except NameError:
continue
f(arg)

But this is a strange problem... Sounds like you should do it
differently.
 
I

ilejn

Arnaud,

good idea, though I think it is not applicable in my case,
because my arg1 ... argN are "complex multilayer lists".

In reality it is not just
f(arg1),
it is more like
f([[subarg1, 'aa', subarg2], []])

Regarding your remark it is a strange problem ... well, may be it
is ;)

Thanks anyway.

ilejn said:
I have a sequence of a function calls. Basically it looks like

though real arguments are complex multilayer lists.
The problem is some arguments are not known and I get NameError
exceptions.
The solutions I know
1. wrap every f call in try/except block
2. make (currently global) argument attributes of a class and use
__getattr__ to convert unknown attributes to something recognizable by
f.

for name in 'arg1', 'arg2', ... 'argN':
    try:
        arg = globals()[name]
    except NameError:
        continue
    f(arg)

But this is a strange problem...  Sounds like you should do it
differently.

Best regards,
Ilja Golshtein.
 
A

Arnaud Delobelle

ilejn said:
Arnaud,

good idea, though I think it is not applicable in my case,
because my arg1 ... argN are "complex multilayer lists".

In reality it is not just
f(arg1),
it is more like
f([[subarg1, 'aa', subarg2], []])

Regarding your remark it is a strange problem ... well, may be it
is ;)

Thanks anyway.

Are these lists automatically generated?
 
E

Emile van Sebille

On 1/20/2011 11:49 AM ilejn said...
Chris,

this is a long story why arguments may be not known.
Briefly these arguments come (or may come) from XML and may be not
specified.

A function call which does not have all arguments defined must be
skipped as gracefully as possible.
What I am asking about is how to achieve this goal.


I might try using sets:

required = set(('a','b','c'))
if not (required - set(locals())): f(a,b,c)

HTH,

Emile
 
I

ilejn

Arnaud,

these lists are not generated.

Actually these lists are a sort of interpreted programs and contain
some application logic.

Here is an example
[
[PUSH, [get_modified_interface, req]],
[TIMEOUT, 3],
[PULL, [out_interface, '']],
[PULL, [err_interface, '']],
[PULL, [out_mined_interface, req]],
]

If any interface name is unknown the list must not be invoked (in
other words, f function
call with this list must be somehow bypassed).

Thanks.


ilejn said:
good idea, though I think it is not applicable in my case,
because my arg1 ... argN are "complex multilayer lists".
In reality it is not just
f(arg1),
it is more like
f([[subarg1, 'aa', subarg2], []])
Regarding your remark it is a strange problem ... well, may be it
is ;)
Thanks anyway.

Are these lists automatically generated?

Best regards,
Ilja Golshtein.
 
A

Arnaud Delobelle

ilejn said:
Arnaud,

these lists are not generated.

Actually these lists are a sort of interpreted programs and contain
some application logic.

Here is an example
[
[PUSH, [get_modified_interface, req]],
[TIMEOUT, 3],
[PULL, [out_interface, '']],
[PULL, [err_interface, '']],
[PULL, [out_mined_interface, req]],
]

If any interface name is unknown the list must not be invoked (in
other words, f function
call with this list must be somehow bypassed).

Thanks.

You could still use the same idea and delay evaluation of the lists. E.g.

prg1 = """[
[PUSH, [get_modified_interface, req]],
[TIMEOUT, 3],
...
"""

prg2 = """[
[OPCODE, [arguments, blah]],
...
"""

....

prgN = """..."""

for prg in prg1, prg2, ..., prgN:
try:
prg = eval(prg)
except NameError:
continue
f(prg)
 
I

ilejn

Arnaud,

it looks like a solution.
Perhaps it is better than plain try/accept and than proxy class with
__getattr__.
It is not for free, e.g. because syntax check such as parentheses
matching is lazy too, though looks
very interesting.

Thanks a lot!

ilejn said:
these lists are not generated.
Actually these lists are a sort of interpreted programs and contain
some application logic.
Here is an example
        [
        [PUSH, [get_modified_interface, req]],
        [TIMEOUT, 3],
        [PULL, [out_interface, '']],
        [PULL, [err_interface, '']],
        [PULL, [out_mined_interface, req]],
        ]
If any interface name is unknown the list must not be invoked (in
other words, f function
call with this list must be somehow bypassed).

You could still use the same idea and delay evaluation of the lists. E.g.

prg1 = """[
    [PUSH, [get_modified_interface, req]],
    [TIMEOUT, 3],
    ...
"""

prg2 = """[
    [OPCODE, [arguments, blah]],
    ...
"""

...

prgN = """..."""

for prg in prg1, prg2, ..., prgN:
    try:
        prg = eval(prg)
    except NameError:
        continue
    f(prg)

Best regards,
Ilja Golshtein
 
J

Jon Clements

Arnaud,

it looks like a solution.
Perhaps it is better than plain try/accept and than proxy class with
__getattr__.
It is not for free, e.g. because syntax check such as parentheses
matching is lazy too, though looks
very interesting.

Thanks a lot!

ilejn said:
Arnaud,
these lists are not generated.
Actually these lists are a sort of interpreted programs and contain
some application logic.
Here is an example
        [
        [PUSH, [get_modified_interface, req]],
        [TIMEOUT, 3],
        [PULL, [out_interface, '']],
        [PULL, [err_interface, '']],
        [PULL, [out_mined_interface, req]],
        ]
If any interface name is unknown the list must not be invoked (in
other words, f function
call with this list must be somehow bypassed).
Thanks.
You could still use the same idea and delay evaluation of the lists. E.g.
prg1 = """[
    [PUSH, [get_modified_interface, req]],
    [TIMEOUT, 3],
    ...
"""
prg2 = """[
    [OPCODE, [arguments, blah]],
    ...
"""

prgN = """..."""
for prg in prg1, prg2, ..., prgN:
    try:
        prg = eval(prg)
    except NameError:
        continue
    f(prg)

Best regards,
Ilja Golshtein

Not sure if a good idea or not, but:

I would probably use pyparsing and create a small grammar to parse
your list data. If parsing an entry with an unknown interface, then
skip to the next list entry. If the entire list parses, then you can
execute your function calls.

hth

Jon.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top