Multiple variables in a 'with' statement?
It is possible in Python 3 since v3.1 and Python 2.7. The new with
syntax supports multiple context managers:
with A() as a, B() as b, C() as c:
doSomething(a,b,c)
Unlike the contextlib.nested
, this guarantees that a
and b
will have their __exit__()
's called even if C()
or it's __enter__()
method raises an exception.
You can also use earlier variables in later definitions (h/t Ahmad below):
with A() as a, B(a) as b, C(a, b) as c:
doSomething(a, c)
As of Python 3.10, you can use parentheses:
with (
A() as a,
B(a) as b,
C(a, b) as c,
):
doSomething(a, c)
If statements with multiple variables
This has nothing to do with if statements and multiple variables, your 0 < day < 3
should actually read as 0 < day && day < 3
. BTW, you don't need to test it in every branch of the same if statement, it is unlikely to change.
If statement with multiple variables ending with a number
You can achieve this using Reflection
. This is obviously discouraged for this scenario, as the other answers provide better solutions, just wanted to show you it's doable the way you intended it to be done (which doesn't mean it's the correct way)
public class Test
{
private string filePath1 = null;
private string filePath2 = null;
private string filePath3 = null;
}
Usage:
Test obj = new Test();
//loop through the private fields of our class
foreach (var fld in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
.Where(x => x.Name.StartsWith("filePath"))) // filter
{
if (string.IsNullOrEmpty(fld.GetValue(obj) as string))
{
errors.Add("File Not Attached in variable: " + fld.Name);
}
}
Set multiple variables and case statement in 1 query
You can try a pivot table. The first part of the query is just creating temp tables to hold the data.
/*CREATE TEMP TABLES*/
DECLARE @a TABLE ( lot int, tid int)
DECLARE @b TABLE ( lot int, id int)
DECLARE @t TABLE ( id int, flag VARCHAR(1))
INSERT INTO @a (lot, tid) VALUES (100, 1) , (100, 2)
INSERT INTO @b (lot, id) VALUES (100, 123)
INSERT INTO @t (id, flag) VALUES (1, 'Y') , (2, 'N')
/*QUERY*/
SELECT [Y] [@id] ,
[N] [@id_not_flag]
FROM (
SELECT a.lot, a.tid, b.id , t.flag
--,ID = (CASE WHEN flag = 'Y' THEN a.tid END)
--,ID_Not_Flag = (CASE WHEN flag <> 'Y' THEN a.tid END)
FROM @a a
LEFT JOIN @b b
ON a.lot = b.lot
LEFT JOIN @t t
ON t.id = a.tid) AS Src
PIVOT (max(Src.tid) FOR flag in ([Y] , [N])) Pvt
How can I open multiple files using with open in Python?
As of Python 2.7 (or 3.1 respectively) you can write
with open('a', 'w') as a, open('b', 'w') as b:
do_something()
(Historical note: In earlier versions of Python, you can sometimes usecontextlib.nested()
to nest context managers. This won't work as expected for opening multiples files, though -- see the linked documentation for details.)
In the rare case that you want to open a variable number of files all at the same time, you can use contextlib.ExitStack
, starting from Python version 3.3:
with ExitStack() as stack:
files = [stack.enter_context(open(fname)) for fname in filenames]
# Do something with "files"
Note that more commonly you want to process files sequentially rather than opening all of them at the same time, in particular if you have a variable number of files:
for fname in filenames:
with open(fname) as f:
# Process f
Including multiple variables in return statement
You can also try in this way
$data = [
'posts' => $posts,
'standorts' => $standorts,
'abteilungs' => $abteilungs,
];
return view('posts.overview')->with($data);
Or,
return view('post.overview',compact('posts','standorts','abteilungs'));
Create new variable from multiple variables using DO IF command
One approach is to use nmiss
function:
compute var_1=1+(nmiss(var_2, var_3, var_4, var_5)>0).
So var_1 will be 1 unless the number of missing values in the rest of the variables if larger than 0, in which case var_1 becomes 2.
How to declare multiple variables inside IF statement blocks?
You are misplacing the BEGIN
statement
IF @WorkC = 'Cut'
BEGIN
SET @WorkC2 = '%Cut%'
SET @Chars = 4
END
Yough, you could remove the BEGIN
and END
all together.
IF @WorkC = 'Cut'
SET @WorkC2 = '%Cut%'
SET @Chars = 4
But this is usually this is done with a CASE
statement much more cleanly.
select
@WorkC2 = case
when @WorkC = 'Cut' then '%Cut%'
when @WorkC = 'Frame' then '%Frame%'
...
end
,@Chars = case
when @WorkC = 'Cut' then 4
when @WorkC = 'Frame' then 6
...
end
Though, you could remove the @WorkC
and set it explicitly with concatenation and save a LOT of writing:
set @WorkC = '%' + @WorkC + '%'
And, set @Chars
more easily too...note you need to do this before you add the % on each end, or make it -1 if you do it after
set @Chars = len(@WorkC) + 1
Assign multiple values to variable and proceed with if statement if any of the values matched in list
You're not using any()
correctly -- it needs to be a sequence of conditions, e.g. any(x in i if x in line)
.
But any()
won't tell you which element of the list matched. Instead, you can use a list comprehension to get all the matching elements and test whether this is not empty.
with open('hexfile', 'r') as file:
while line := file.read(32):
matches = [x in i if x in line]
if matches:
match = matches[0] # assuming there's never more than one match
# do things with match
Don't reuse the variable i
, since there's no way to restore it to the original value.
Related Topics
Sorting a List of Dot-Separated Numbers, Like Software Versions
Cannot Concatenate 'Str' and 'Float' Objects
What's the Difference Between %S and %D in Python String Formatting
Python Nested Functions Variable Scoping
How to Redirect Stdout and Stderr to Logger in Python
Download File Through Google Chrome in Headless Mode
Why Are There No ++ and -- Operators in Python
Writing a Dict to Txt File and Reading It Back
Setting Different Bar Color in Matplotlib Python
Pandas Make New Column from String Slice of Another Column
Defining a Discrete Colormap for Imshow in Matplotlib
Running Python on Windows for Node.Js Dependencies
Numpy Index Slice Without Losing Dimension Information
Cannot Find Vcvarsall.Bat When Running a Python Script
How to Start and Stop a Thread