Inline Calculation of Dictionary Values

I

++imanshu

Hi,

Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
modify_state()
val = f()
restore_state()
return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu
 
C

Chris Rebert

    Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

     For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block".

Based on what I can find about "bare blocks", Nope. And we like it that way :)
I am
trying to avoid this :-

def f2():
    modify_state()
    val = f()
    restore_state()
    return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

FWIW, I don't see what's wrong with this. You could probably refactor
f2() to use the `with` statement and a context manager, but that's
getting tangential.
However, the question arises: Why do you have global state in the first place?

Cheers,
Chris
 
D

Dave Angel

++imanshu said:
Hi,

Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
modify_state()
val = f()
restore_state()
return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu
How about something like:

mymap = {
'key1': f(),
'key2': [modify_state(), f(), restore_state()][1],
'key3': f(),
}

This builds a list of three values, and uses only the [1] item from the
list.

DaveA
 
I

++imanshu

++imanshu said:
     Is it possible to something along these lines in python :-
map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}
      For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-
def f2():
     modify_state()
     val = f()
     restore_state()
     return val
map = {
'key1': f(),
'key2': f2()
'key3': f(),
}
Thank You,
Himanshu

How about something like:

mymap = {
'key1': f(),
'key2': [modify_state(), f(), restore_state()][1],
'key3': f(),

}

This builds a list of three values, and uses only the [1] item from the
list.

DaveA

This is cool. I'll use it.

Thank You,
Himanshu
 
I

++imanshu

Based on what I can find about "bare blocks", Nope. And we like it that way :)




FWIW, I don't see what's wrong with this. You could probably refactor
f2() to use the `with` statement and a context manager, but that's
getting tangential.
However, the question arises: Why do you have global state in the first place?

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

f() = flavor independent os api for getting path to a folder, uses
effective user id (eg Folder.FSFindFolder(Folders.kUserDomain,
Folders.kInternetPlugInFolderType, Folders.kDontCreateFolder))
modify_state() = change current effective user id
restore_state() = restore to old user id

Thanks for the reply.

Thank You,
Himanshu
 
P

Peter Otten

Dave said:
++imanshu said:
Hi,

Is it possible to something along these lines in python :-

map = {
'key1': f(),
'key2': modify_state(); val = f(); restore_state(); val,
'key3': f(),
}

For 'key2' I want to store the value returned by f() but after
modifying the state. Do we have something like a "bare block". I am
trying to avoid this :-

def f2():
modify_state()
val = f()
restore_state()
return val

map = {
'key1': f(),
'key2': f2()
'key3': f(),
}

Thank You,
Himanshu
How about something like:

mymap = {
'key1': f(),
'key2': [modify_state(), f(), restore_state()][1],
'key3': f(),
}

This builds a list of three values, and uses only the [1] item from the
list.

Please think of the children ;)

The function based approach is the way to go:

def f2():
cookie = modify_state()
try:
return f()
finally:
restore_state(cookie)

This will restore the state even if f() raises an exception.

If you want something more fancy you can parameterize f2() by the three
functions or look into context managers.

Peter
 

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,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top