Re: Extending Python Syntax with @
Richie Hindle <richie@entrian .com> writes:
[color=blue]
>[Cameron][color=green]
>> Whenever you feel like a lambda, define a named function;[/color][/color]
[color=blue]
>[Kyler][color=green]
>> How do you cleanly do that?
>> foo = range(-10, 10)
>> my_op = lambda x: float(x) / max(map(abs, foo))
>> bar = map(my_op, foo)[/color][/color]
[color=blue]
>foo = range(-10, 10)
>def my_op(x):
> return float(x) / max(map(abs, foo))
>bar = map(my_op, foo)[/color]
[color=blue]
>...did I misunderstand?[/color]
Well, your solution depends on a global variable. That's going to
get *really* ugly if we move beyond the trivial example given here.
There are other problems but they're apparently not obvious with
that example.
How 'bout some non-working code to shine some more light on it? (If
pressed, I should be able to come up with similar code that works!)
def make_translatio n_function(GCPs , type, invert=False):
if type == 'LSF' and len(GCPs) < 12:
# Do lots of time-consuming magic to calculate A, B, ...
return(
lambda x, y: (
x * A + y * B +
x * y * C +
...,
x * G + y * H +
x * y * I +
...
)
)
elif ... # Repeat lots of times for variations.
map_im = foo.open('map.t if')
map_translation = make_translatio n_function(map_ im.get_GCPs, type='LSF')
path_im = foo.open('path. tif')
path_translatio n_i = make_translatio n_function(path _im.get_GCPs, type='LSF', invert=True)
# Mark all points on map that are red in path.
for (x, y) in all_red_pixels( path_im):
(lon, lat) = path_translatio n_i(x, y)
(x, y) = map_translation (lon, lat)
map_im.putpixel ((x, y), mark_val)
Now, let's pretend that this is part of a larger program that's doing lots
of things with, potentially, lots of images, and let's also say that we
allow user-defined/imported extensions to make_translatio n_function().
How are you going to make a *clean* solution using named functions?
--kyler
Richie Hindle <richie@entrian .com> writes:
[color=blue]
>[Cameron][color=green]
>> Whenever you feel like a lambda, define a named function;[/color][/color]
[color=blue]
>[Kyler][color=green]
>> How do you cleanly do that?
>> foo = range(-10, 10)
>> my_op = lambda x: float(x) / max(map(abs, foo))
>> bar = map(my_op, foo)[/color][/color]
[color=blue]
>foo = range(-10, 10)
>def my_op(x):
> return float(x) / max(map(abs, foo))
>bar = map(my_op, foo)[/color]
[color=blue]
>...did I misunderstand?[/color]
Well, your solution depends on a global variable. That's going to
get *really* ugly if we move beyond the trivial example given here.
There are other problems but they're apparently not obvious with
that example.
How 'bout some non-working code to shine some more light on it? (If
pressed, I should be able to come up with similar code that works!)
def make_translatio n_function(GCPs , type, invert=False):
if type == 'LSF' and len(GCPs) < 12:
# Do lots of time-consuming magic to calculate A, B, ...
return(
lambda x, y: (
x * A + y * B +
x * y * C +
...,
x * G + y * H +
x * y * I +
...
)
)
elif ... # Repeat lots of times for variations.
map_im = foo.open('map.t if')
map_translation = make_translatio n_function(map_ im.get_GCPs, type='LSF')
path_im = foo.open('path. tif')
path_translatio n_i = make_translatio n_function(path _im.get_GCPs, type='LSF', invert=True)
# Mark all points on map that are red in path.
for (x, y) in all_red_pixels( path_im):
(lon, lat) = path_translatio n_i(x, y)
(x, y) = map_translation (lon, lat)
map_im.putpixel ((x, y), mark_val)
Now, let's pretend that this is part of a larger program that's doing lots
of things with, potentially, lots of images, and let's also say that we
allow user-defined/imported extensions to make_translatio n_function().
How are you going to make a *clean* solution using named functions?
--kyler
Comment