Set attribute in callback, with lambda - python

Trying to figure out how to set a object attribute using a lambda function.
i got a case where i need to set the attribute with a UI callBack, that requires a function object.
was trying something like this but it dosnt work, and gives me a syntax error.
pm.button(command=lambda *args: uvOpts.grid = True)

this solves the syntax error (you cannot have a second "=" in pm.button) and works if uvOpts is in the scope.
pm.button(command=lambda *args: setattr(uvOpts, 'grid', True))
to set the object's attribute I'm using setattr, in order to avoid the use of a = symbol in lambda function definition.
I would like to add that if you need to make uvOpts visible inside the lambda scope you may eventually need to make it global, at least at module level (e.g. read the answers in Using global variables in a function other than the one that created them).

Related

Why does button.clicked.connect gives unexpected output when i set callback for it in pyqt6? [duplicate]

Im trying to build a calculator with PyQt4 and connecting the 'clicked()' signals from the buttons doesn't work as expected.
Im creating my buttons for the numbers inside a for loop where i try to connect them afterwards.
def __init__(self):
for i in range(0,10):
self._numberButtons += [QPushButton(str(i), self)]
self.connect(self._numberButtons[i], SIGNAL('clicked()'), lambda : self._number(i))
def _number(self, x):
print(x)
When I click on the buttons all of them print out '9'.
Why is that so and how can i fix this?
This is just, how scoping, name lookup and closures are defined in Python.
Python only introduces new bindings in namespace through assignment and through parameter lists of functions. i is therefore not actually defined in the namespace of the lambda, but in the namespace of __init__(). The name lookup for i in the lambda consequently ends up in the namespace of __init__(), where i is eventually bound to 9. This is called "closure".
You can work around these admittedly not really intuitive (but well-defined) semantics by passing i as a keyword argument with default value. As said, names in parameter lists introduce new bindings in the local namespace, so i inside the lambda then becomes independent from i in .__init__():
self._numberButtons[i].clicked.connect(lambda checked, i=i: self._number(i))
UPDATE: clicked has a default checked argument that would override the value of i, so it must be added to the argument list before the keyword value.
A more readable, less magic alternative is functools.partial:
self._numberButtons[i].clicked.connect(partial(self._number, i))
I'm using new-style signal and slot syntax here simply for convenience, old style syntax works just the same.
You are creating closures. Closures really capture a variable, not the value of a variable. At the end of __init__, i is the last element of range(0, 10), i.e. 9. All the lambdas you created in this scope refer to this i and only when they are invoked, they get the value of i at the time they are at invoked (however, seperate invocations of __init__ create lambdas referring to seperate variables!).
There are two popular ways to avoid this:
Using a default parameter: lambda i=i: self._number(i). This work because default parameters bind a value at function definition time.
Defining a helper function helper = lambda i: (lambda: self._number(i)) and use helper(i) in the loop. This works because the "outer" i is evaluated at the time i is bound, and - as mentioned before - the next closure created in the next invokation of helper will refer to a different variable.
Use the Qt way, use QSignalMapper instead.

Pass the value of a push button chosen among many others [duplicate]

Im trying to build a calculator with PyQt4 and connecting the 'clicked()' signals from the buttons doesn't work as expected.
Im creating my buttons for the numbers inside a for loop where i try to connect them afterwards.
def __init__(self):
for i in range(0,10):
self._numberButtons += [QPushButton(str(i), self)]
self.connect(self._numberButtons[i], SIGNAL('clicked()'), lambda : self._number(i))
def _number(self, x):
print(x)
When I click on the buttons all of them print out '9'.
Why is that so and how can i fix this?
This is just, how scoping, name lookup and closures are defined in Python.
Python only introduces new bindings in namespace through assignment and through parameter lists of functions. i is therefore not actually defined in the namespace of the lambda, but in the namespace of __init__(). The name lookup for i in the lambda consequently ends up in the namespace of __init__(), where i is eventually bound to 9. This is called "closure".
You can work around these admittedly not really intuitive (but well-defined) semantics by passing i as a keyword argument with default value. As said, names in parameter lists introduce new bindings in the local namespace, so i inside the lambda then becomes independent from i in .__init__():
self._numberButtons[i].clicked.connect(lambda checked, i=i: self._number(i))
UPDATE: clicked has a default checked argument that would override the value of i, so it must be added to the argument list before the keyword value.
A more readable, less magic alternative is functools.partial:
self._numberButtons[i].clicked.connect(partial(self._number, i))
I'm using new-style signal and slot syntax here simply for convenience, old style syntax works just the same.
You are creating closures. Closures really capture a variable, not the value of a variable. At the end of __init__, i is the last element of range(0, 10), i.e. 9. All the lambdas you created in this scope refer to this i and only when they are invoked, they get the value of i at the time they are at invoked (however, seperate invocations of __init__ create lambdas referring to seperate variables!).
There are two popular ways to avoid this:
Using a default parameter: lambda i=i: self._number(i). This work because default parameters bind a value at function definition time.
Defining a helper function helper = lambda i: (lambda: self._number(i)) and use helper(i) in the loop. This works because the "outer" i is evaluated at the time i is bound, and - as mentioned before - the next closure created in the next invokation of helper will refer to a different variable.
Use the Qt way, use QSignalMapper instead.

Using a function in an SQLAlchemy order_by clause

I have an order_by clause in an ORM query:
def ordering():
return sqlalchemy.sql.expression.case(
(User.id == current_user_id(), 1),
else_=2
)
...
query = User.query.order_by(ordering)
But when I try to use this, I get this error:
sqlalchemy.exc.InvalidRequestError: Can't invoke Python callable current_user_id() inside of lambda expression argument at <code object user_ordering at 0x7fefae4efd40, file "/web/eta/routes/user.py", line 24>; lambda SQL constructs should not invoke functions from closure variables to produce literal values since the lambda SQL system normally extracts bound values without actually invoking the lambda or any functions within it. Call the function outside of the lambda and assign to a local variable that is used in the lambda as a closure variable, or set track_bound_values=False if the return value of this function is used in some other way other than a SQL bound value.
I really do want the value to be evaluated every time the query is run. So I've tried track_bound_values:
query = User.query.order_by(sqlalchemy.lambda_stmt(ordering, track_bound_values=False))
But this seems to make no difference - I still see the same error. Is there a way of always using the current value of a function in an ordering expression like this?
Or, alternatively, is there a better way of making sure a particular value is always returned first?

Nuke ChannelMask_Knob required argument

I am trying to figure out what the required string argument for the nuke.ChannelMask_Knob() function corresponds to. On some other knob constructors the first argument seems to be the name/label, but that does not seem to be the case for the ChannelMask_Knob...
I have looked at the Nuke Python API, but I am unsure how to follow it back to the appropriate function definition to answer my question. My line of thinking is that this has to do with the init function which is overridden by the ChannelMask_Knob class, but the parameter list according to the API is just "..." which I believe means it has to do with a builtin function. Since I can't see the body of the init function, I have no idea what that argument is used for, thus my problem...
Here is an example of my issue:
test_knob = nuke.ChannelMask_Knob("required_argument")
node.addKnob(test_knob)
This works just fine, but I would like to know what the "required_argument" is used for since it is apparently not the name or label for the knob.
You must intentionally leave a blank space for parameter in ChannelMask_Knob('') method. It doesn't work. But for assigning a name and a label you need to use .setName('name') and .setLabel('label') methods respectively. It works fine.
Here is a code:
import nuke
noop = nuke.nodes.NoOp()
### maskKnob = nuke.ChannelMask_Knob('name', 'label', False)
maskKnob = nuke.ChannelMask_Knob('')
maskKnob.setName('name')
maskKnob.setLabel('label')
noop.addKnob(maskKnob)
For selection of channel in this dropdown menu it's better to use the following method:
nuke.selectedNode().knob('name').setValue('alpha')
nuke.selectedNode().knob('name').setValue('disparity')

How to pass parameters in a Python Dispatch Table

I am trying to construct a dispatch the following way:
def run_nn(type=None):
print type, 'nn'
return
def run_svm(type=None):
print type, 'svm'
return
action = {'nn' : run_nn( type=None),
'svm' : run_svm(type=None),}
I want the function to be executed only when called with something like:
action.get('nn',type='foo')
With expectation it to print:
foo nn
But it breaks giving:
TypeError: get() takes no keyword arguments
What's the right way to do it?
Furthermore, two functions run_nn() and run_svm() were executed without even being called. I don't want that. How can I avoid it?
You're calling the functions while building the dictionary. You should instead put the function objects in the dict without calling them. And afterwards, get the appropriate function from the dict and call it with the keyword argument.
What you want is:
action = {'nn' : run_nn,
'svm' : run_svm,}
...
action.get('nn')(type='foo') # get function object from dict and then call it.
I'll suggest you use action['nn'] over action.get('nn') since you're not specifying any default callable in the get method; the get method returns None when you don't specify one. A KeyError is much more intuitive than a TypeError NoneType object is not callable in this scenario.
On another note, you can drop those return statements as you aren't actually returning anything. Your function will still return without them.
BTW, I have the feeling your function(s) want to change behavior depending on type (although your type is counter-intuitive as it is always a string). In any case, you may have a look at functools.singledispatch. That'll transform your function(s) into a single-dispatch generic function with the possibility to create several overloaded implementations.
Finally, although type does make for a good argument name, you will run into problems when you need to use the builtin type in your function.

Categories