Why do I get a syntax error in Python despite using exitStack() as in the doc? - python-3.x

I'm trying to execute some python's code and can't figure out what's the issue :
def elboguide(design, dim=10):
# pyro.set_rng_seed(10)
theta_mu = pyro.param("theta_mu", torch.zeros(dim, 1, 2, 2))
theta_sig = pyro.param("theta_sig", torch.ones(dim, 1, 2, 2),
constraint=torch.distributions.constraints.positive)
batch_shape = design.shape[:-2]
with ExitStack() as stack:
for plate in iter_plates_to_shape(batch_shape):
stack.enter_context(plate)
theta_shape = batch_shape + theta_mu.shape[-2:]
pyro.sample("theta", dist.Normal(
theta_mu.expand(theta_shape),
theta_sig.expand(theta_shape)).to_event(2)
)
The issue is in this function, more specifically on the line
with ExitStack() as stack:
When running the code I only get the error :
File "C:\Users\natha\Documents\prog\python_dev\RL-BOED\RL-BOED\source.py", line 52
with ExitStack() as stack:
^
SyntaxError: invalid syntax
the documentation of contextlib shows an use case where the syntax is exactly the same :
https://docs.python.org/3/library/contextlib.html
What is wrong with this code ? I'm using Python 3.11.0 by the way.

Related

Why is there an error: "File "stdin1", line 1, invaild syntax"

Code:
def profile(name, age, lang1,lang2,lang3, lang4, lang5):
print("이름 : {0}\t나이 : {1}\t".format(name,age), end="")
print(lang1,lang2,lang3,lang4,lang5)
SyntaxError: invalid syntax
C:/Users/man/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/man/Desktop/python work space/edu.py"
File "", line 1
C:/Users/man/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/man/Desktop/python work space/edu.py"
I don't know how to fix it please help me.

Python Dictionary - Invalid Syntax

Why does the following code throw an invalid syntax error?
grads = {"dW1": dW1,
"db1": db1,
"dW2": dW2,
"db2": db2}
It gives the following error
File "<ipython-input-27-f129b09e7ac8>", line 40
grads = {"dW1": dW1,
^
SyntaxError: invalid syntax
There is nothing wrong with the scope you are sharing. You have a syntax error above / before declaring grads

How do I fix this syntax error when using a function within another function?

I'm trying to figure out how to call the function within another function, but I am unable to solve this invalid syntax error on line "t_calc = calc_temp(d_curr,volt,tbase)." Can you not call a function within another function?
def calc_temp(current,voltage,tbase):
power_diss = current * voltage
thermal_resistance = THICK/(K*AREA)
temp = tbase + (power_diss * thermal_resistance)
return(temp)
def diode_i_terr(volt,tguess,tbase):
d_curr = I0(np.exp((Q*volt)/(IDE*KB*tguess))
t_calc = calc_temp(d_curr,volt,tbase)
t_err = t_calc - tguess
return(d_curr, t_err)
t_calc = calc_temp(d_curr,volt,tbase)
^
SyntaxError: invalid syntax

Groovy: Get a specific value in a file

I'm quite new in Groovy.
Basically I load a text file, then I need to get a specific value at one line (actually the 6th).
The line is like:
STATIC_ASSERT(VERSION == 888888, "blablabla");
I need to get the 888888 value.
I found a way using multiple split but it's ugly.
I also think of using something like:
line.substring(ind+"VERSION ==".length(), line.length()-10).trim();
But the "blablabla" length can change..
Thank you.
Edit: It works using an hardcoded string like this.
But when I try to run it from the file I get this error:
test' is failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
script1516208488151762512206.groovy: 4: expecting '}', found '' # line 4, column 69.
ne.contains('VERSION ==')
^
1 error
Here is my code:
${groovy:
String Ver
def file = new File("C:\\test.cpp")
def data = file.filterLine { line ->
line.contains('VERSION ==')
}
Ver = data.split("==")[1].split(",")[0].trim()
logger.info(Ver)
}
--
I also tried something like this:
${groovy:
String Ver
def file = new File("C:\\test.cpp")
while ((line = file.readLine())!=null)
{
int ind = line.indexOf("VERSION ==")
if (ind >= 0)
{
Ver = line.split("==")[1].split(",")[0].trim()
}
}
logger.info(Ver)
}
But I get same kind of weird error:
expecting '}', found '' # line 9, column 58.
("==")[1].split(",")[0].trim()
^
1 error
:(
You do as shown below:
def line = 'STATIC_ASSERT(VERSION == 888888, "blablabla");'
println line.split('==')[1].split(',')[0].trim()

Syntax error when trying to input errors for a set of data

I'm trying to create a graph using Jupyter Notebooks but when I input errors for a data set "syntax error" appears referring to the word error, any ideas on the problem?
measured_time2 = q.MeasurementArray(
data = [0,0.2,1.0,2.2,4.0,6.2,9.0,12.2,16.0,20.2,25]
error = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
File "<ipython-input-36-96c22be70112>", line 8
error = [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1])
^
SyntaxError: invalid syntax

Resources