why does f-string show both variable name and value - f-string

Braces around a variable evaluate the value of the variable in an f-string expression.
x = 1
f"{x}"
results in '1'
What is this?
x = 1
f"{x=}"
results in 'x=1'.
Why isn't the result either 1) an error, or 2) '1='
Discovered this by mistake. Do the docs explain this? Where?

Related

Getting the name of a function in a macro

I wrote a macro to make code cleaner and clearer by using error message templates stored in a dict. In this example it inserts name of the function, see discourse and #__FUNCTION__, that throws the error.
exception = Dict(:foo => ("bar ~", ArgumentError))
macro ⛔(id)
msg, type = exception[id]
quote
msg = replace($msg, "~" => StackTraces.stacktrace()[1].func)
throw($type(msg))
end
end
This works fine in f that uses positional arguments,
f(x, p) = p < 5 ? x : #⛔ foo
f(1, 10)
#> ERROR: ArgumentError: bar f
but displays #..# when a keyword argument is used.
g(x; p = 3) = p < 5 ? x : #⛔ foo
g(1, p = 10)
#> ERROR: ArgumentError: bar #g#N
Here N is the nth expression evaluated in the session. Redefining g with the same syntax increments this number.
I am stuck after I couldn't spot a difference between the code produced by #⛔.
#macroexpand function f(x, p) p < 5 ? x : #⛔ foo end
#macroexpand function g(x; p = 3) p < 5 ? x : #⛔ foo end
Question
What is happening in g that is different from f?
That is the name of the method the expanded code is called in -- definitions with keyword arguments are lowered to dispatch helper methods called "keyword sorters": https://docs.julialang.org/en/v1/devdocs/functions/#Keyword-arguments. These make keywords arguments use dispatch (internally) and compilation just as other functions, instead of just dictionary lookup as e.g. in Python.
I don't think you can do what you want easily in that case, as the conversion process always happens. Taking a previous stack frame would work, but then you have to know whether you are within a keyword method beforehand.
Maybe the following approach works: pattern match the method name (#<f>#<N>), and then check whether it is an actual kwsorter method of any method of f. If so, proceed with f.

pine script sum function returns only float I need integer

I made simple variable
and i want to count how many times it occur
so I made code like this
a = close>open(it is just examplary, not my real code)
gross = ~~~~~~
num = cum(a ? 1 : 0)
total = sum(a ? gross : na, num)
I put the num variable into length position of sum function
but it turns error message like this
cannot call 'sum' with 'length'=series[float]. the argument should be of type: series[integer];
so to check the value of num is float, I made plot(num,~~) and it returns 128.0000
I think condition a occured 128times.
so the value of num isn't float am i right?
I don't know why error message happens
I already used round() and `int()
please help me solve this problem

Does `if` work only in combination with `else` in Series.apply(lambda x)?

I'm getting a SyntaxError for:
housing['Lot Area'].apply(lambda x: x + 50000 if x > 20000)
When I add else, the code runs fine:
housing['Lot Area'].apply(lambda x: x + 50000 if x > 20000 else x)
Does if only work in combination with else here? I'd like to increment x with 50000 only if x > 20000 -- otherwise I'd like x to remain unchanged. I find the else part a bit redundant here. Besides the first question before, is there any way to write this code without the else part?
Base on your description , even apply is not need here
housing.loc[housing['Lot Area']>20000,'Lot Area']+=50000
Comment from Alex :
if the if statement resolves to False for a value, then apply() doesn't return and just lets the value in the Series as it is
you're getting a SyntaxError because you are typing invalid syntax. the ternary operator must be used like
expression if bool else other_expression

VBA policy on double sided inequalities?

Was fooling around with trying to reduce the length of the code so that it gives off fewer headaches to look at and debug, and I came across this curious little fact:
Debug.Print 5<9<8 'returns "True"
At first I thought this was because it just checked the first set, but then I found that
Debug.Print 5<4<8 '*also* returns "True"
Does VBA interpret this kind of triple inequality as an Or statement? I can't imagine why someone would choose to make that the interpretation VBA makes because it's almost certainly the less used option, but I struggle to think of another explanation.
Also, what is a quick and pretty way of writing If 5 < X < 8 Then (to use sample numbers), without having to resort to endless And statements, ie If 5 < x And X < 8 Then? It's okay for one statement, but the doubling of length adds up quick, especially since variables aren't typically named X.
Edit: okay, it's certainly not an Or because VBA also says that Debug.Print 8<6<2 is True. What on earth is it thinking?
I have no clue but my educated guess would be that it first evaluates the left side of the equation (5<9) which gives TRUE. Then, it proceeds to evaluate the rest (TRUE<8) and implicitly converts TRUE to its integer value (I believe this to be -1 in VB).
-1<8 -> TRUE
Works with the second case as well since FALSE will convert to 0 and 0<8.
Basically it would have everything to do with implicit conversion of boolean to integer and their respective value in VBA.
It's to do with the way VBA evaluates expressions and implicit conversion. The first part of the equation is evaluated and the result stored as a numeric value (the boolean is implicitly converted to an integer)
(well.... technically a boolean is just an integer, but we'll just go along like so...)
'// True = -1
'// False = 0
Debug.Print 5 < 9 < 8
Debug.Print CInt(5 < 9) '// Prints -1
Debug.Print -1 < 8 '// = True
Which is why the following gives "False" instead:
Debug.Print 5 < 9 < -1
Because
Debug.Print Cint(5 < 9) '// True = -1
Debug.Print -1 < -1 '// False
If you want to find out if something is in the middle of two other numbers then you have to use the And operator to force a separate evaluation (either side of the operator is then evaluated and compared logically)
Debug.Print (3 < 5 And 5 < 4) '// False
Looking at it from a parse tree perspective might shed more light about why it works that way.
Excluding whatever instruction comes after the THEN token, the parse tree for If 5 < X < 8 Then might look something like this (quite simplified):
The comparison operators being a binary operator, there's an expression on either side of it, and in order to resolve the Boolean expression for the IfBlockStatement, VBA needs to visit the tree nodes in a specific order: because VBA parses expressions left to right, the 5 < X part stands on its own as an expression, and then the result of that expression is used to resolve the {expression} < 8 part of the expression.
So when VBA resolves 5 < X, because that's a ComparisonExpression the result is a Boolean value; when that Boolean value then needs to be compared to the 8 integer literal, VBA performs an implicit type conversion and actually compares CInt({Boolean}) < 8, which will evaluate to True regardless of the result of the first expression, since False converts to 0 and True converts to -1 when expressed as an integer literal, and both are < 8.
These mechanics are built into how the runtime works, so in order to evaluate if X is between 5 and 8, you need to build your expression so that it's parsed as such:
If X > 5 And X < 8 Then
That gives you two distinct expression trees joined by a LogicalAndOperator, which then works off a valid Boolean expression on either sides.
5<9<8 = True<8 = True
5<4<8 = False<8 = True
The other answers covered up nicely the first part of your question, but didn't satisfactorily cover up the second part of it, i.e. What is a quick and pretty way of writing If 5 < X < 8 Then (to use sample numbers), without having to resort to endless And statements, i.e. If 5 < x And X < 8 Then?
There are two ways. The first:
Select Case X
Case 5 To 8
...
End Select
Here, the value before the To keyword must be the smaller value of the two. Also note that while this will work for integers, I have no idea if it works for types like Double and such (I suspect it won't though).
The second way, which works irrespective of whether the interval bounds are integers or not, is not necessarily shorter, but it evaluates things in a single comparison:
If Sgn(x - 5) + Sgn(x - 8) = 0 Then ...
This is an interesting way of evaluating whether a value is between some bounds, because it can also provide information on whether the value is equal to one of those bounds or is "outside" them (and on which "side" it is). For example, on a -∞..0..+∞ axis:
if x = 4, the expression above is -2, thus x is to the left of the (5..8) interval
if x = 5, the expression above is -1, thus x is the left bound of the (5..8) interval
if x = 6, the expression above is  0, thus x is inside the (5..8) interval, i.e. between its bounds
if x = 8, the expression above is  1, thus x is the right bound of the (5..8) interval
if x = 9, the expression above is  2, thus x is to the right of the (5..8) interval
Of course, if you want to include the bounds in the interval, say, test If 5 <= x And X <= 8 Then, the comparison above becomes If Abs(Sgn(x - 5) + Sgn(x - 8)) < 2 Then ..., which is another shortcut to check if the expression is -1, 0 or 1.
In the end, none of the ways above are as short as a Between(x, 5, 8) hypothetical function, but at least they are alternatives to the "classical" method.

Matlab- How does you name a new variable based on other variables' values? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to concatenate a number to a variable name in MATLAB?
MATLAB: How can I use a variables value in another variables name?
I want to name a variable using values of other variables given in a function.
So, if I have values for an x1,x2 I can make the new variable's name as:
x_(x1's value)_(x2's value) as a name.
I've checked out the eval, num2str, strcat functions, but as of yet I can't make it so that I have a variable with the name above which I can assign a value to.
Any help would be greatly appreciated.
Take a look at the following FAQ:
How can I create variables A1, A2,...,A10 in a loop?
It answers the "how" part of your question and recommends a better approach using arrays.
As Jonas suggests, if x1 and x2 are numbers this works:
x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));
If x1 and x2 are strings, this becomes:
x1 = 'foo';
x2 = 'bar';
newValue = 25;
eval(sprintf('x_%s_%s = newValue;',x1,x2));
or more simply (using concatenation instead of SPRINTF):
x1 = 'foo';
x2 = 'bar';
newValue = 25;
eval(['x_' x1 '_' x2 ' = newValue']);
I don't know what you're trying to accomplish, but this probably isn't the best way to go about it. EVAL should always be avoided. Creating variables in the using EVAL is (a.k.a. "poofing") is doubly bad.
If you're trying to associate parameters with values, structures are a much better solution:
x1 = 'foo';
x2 = 'bar';
newValue = 25;
x.([x1 '_' x2]) = newValue;
Assuming you have a really good reason why you'd want to do that (and assuming x1 and x2 have integer values), you can do this by combining EVAL and SPRINTF.
x1 = 3;
x2 = 4;
newValue = 25;
eval(sprintf('x_%i_%i = newValue;',x1,x2));
If x1 and x2 are floats, it'll be trickier since a variable name cannot have dots in it, though it would still be possible as long as you replace the dots with something else.
However, I really have to ask: Are you sure that you want to do that? Because at the moment I cannot imagine an application where would want to create variable names you don't know beforehand, which in turn makes it very hard to write an efficient program.
EDIT
There are many useful ways to store your data in arrays. If you really don't want that, you may be interested in accessing data via key/value pairs in a MAP, a feature which is available in more recent versions of Matlab. Thus, your key would become sprintf('%i_%i',x1,x2), and the corresponding value would be whatever it is you want to store.
You can also use dynamic field references. Loren at the Mathworks gives a writeup here:
Mathworks: use-dynamic-field-references