Why the co_argcount from the compile() in 'exec' mode is always 0? - python-3.9

The following code always gives co_argcount as 0. I don't understand why this is happening.
compile('def foo(c, a=3,b=5): return a+b+c', "<string>", "exec").co_argcount

Related

ADSError 857212673: Can't read variable values through name (pyads)

I've been having a problem lately where I can't access the value of any variable through their name. It's not a connection problem since I can read the value from the read() function just fine. However whenever I use the read_by_name function there's always the same error pyads.pyads_ex.ADSError: ADSError: Unknown Error (857212673).
Because it's an unknown error I haven't been able to find documentation on it. I'm using a BC9120 as a PLC and TwinCat 2. Does anyone know what might be causing this?
Code:
if self.plc.is_open == False:
self.plc.open()
print(self.plc.read(pyads.INDEXGROUP_MEMORYBIT, 0*8 + 0, pyads.PLCTYPE_BOOL))
print(self.plc.read_by_name("Test.M0", pyads.PLCTYPE_BOOL))
Logs:
False
Traceback (most recent call last):
File "c:\Users\MAP9AV\Documents\Banca\Modelo Preditivo\Thread_Sup.py", line 50, in run
print(self.plc.read_by_name("Test.M0", pyads.PLCTYPE_BOOL))
File "C:\Users\MAP9AV\AppData\Local\Programs\Python\Python39\lib\site-packages\pyads\connection.py", line 540, in read_by_name
return adsSyncReadByNameEx(
File "C:\Users\MAP9AV\AppData\Local\Programs\Python\Python39\lib\site-packages\pyads\pyads_ex.py", line 1154, in adsSyncReadByNameEx
handle = adsGetHandle(port, address, data_name)
File "C:\Users\MAP9AV\AppData\Local\Programs\Python\Python39\lib\site-packages\pyads\pyads_ex.py", line 890, in adsGetHandle
handle = adsSyncReadWriteReqEx2(
File "C:\Users\MAP9AV\AppData\Local\Programs\Python\Python39\lib\site-packages\pyads\pyads_ex.py", line 774, in adsSyncReadWriteReqEx2
raise ADSError(err_code)
pyads.pyads_ex.ADSError: ADSError: Unknown Error (857212673).

Repost: Discord.py & Sympy(mostly discord.py I think)

I'm trying to make a math discord bot.
I am pretty sure that my Sympy code is correct and it is just discord.py being funky.
Code:
#client.command()
async def solve(ctx, equation):
x, y, z, t = symbols('x y z t')
k, m, n = symbols('k m n', integer=True)
f, g, h = symbols('f g h', cls=Function)
equation = equation.split("=")
eqn = Eq(parse_expr(equation[0]), parse_expr(equation[1]))
await ctx.send(f"```{solve(eqn)}```")
Please assume that I have all imports necessary.
I am getting this error:
Ignoring exception in command solve:
Traceback (most recent call last):
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "math.py", line 42, in solve
eqn = Eq(parse_expr(equation[0]), parse_expr(equation[1]))
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\sympy\parsing\sympy_parser.py", line 1008, in parse_expr
return eval_expr(code, local_dict, global_dict)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\sympy\parsing\sympy_parser.py", line 902, in eval_expr
expr = eval(
File "<string>", line 1
Integer (2 )+Integer (3 )Symbol ('x' )
^
SyntaxError: invalid syntax
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\justi_zts5a0w\PycharmProjects\discord.py\venv\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: SyntaxError: invalid syntax (<string>, line 1)
Judging from the error you are getting, the problem is not from discord.py, it comes from sympy. And judging at the output of the code it has to do with the way you are introducing the parameter equation.
I did the following calls in SymPy Live and it outputted well:
>>> from sympy.parsing.sympy_parser import parse_expr
>>> equation = 'x+1 = 0'
>>> equation = equation.split('=')
>>> eqn = Eq(parse_expr(equation[0]), parse_expr(equation[1]))
>>> print(eqn)
Eq(x + 1, 0)
In your code, however, it is telling you that the parse_expr cannot evaluate the expression correctly due to the syntax being introduced. Make sure your input equation has a valid format.

h2o set_params() takes exactly 1 argument (2 given), even though only 1 given?

Getting error
TypeError: set_params() takes exactly 1 argument (2 given)
even though I only appear to be providing a single argument...
HYPARAMS = {
unicode(HYPER_PARAM): best_random_forest.params[unicode(HYPER_PARAM)][u'actual']
for HYPER_PARAM in list_of_hyperparams_names
}
assert isinstance(HYPARAMS, dict)
print 'Setting optimal params for full-train model...'
pp.pprint(HYPARAMS)
model = model.set_params(HYPARAMS)
#output
{ u'col_sample_rate_per_tree': 1.0,
u'max_depth': 3,
u'min_rows': 1024.0,
u'min_split_improvement': 0.001,
u'mtries': 5,
u'nbins': 3,
u'nbins_cats': 8,
u'ntrees': 8,
u'sample_rate': 0.25}
model = model.set_params(OPTIM_HYPARAMS)
TypeError: set_params() takes exactly 1 argument (2 given)
Looking at the source code,
def set_params(self, **parms):
"""Used by sklearn for updating parameters during grid search.
Parameters
----------
parms : dict
A dictionary of parameters that will be set on this model.
Returns
-------
Returns self, the current estimator object with the parameters all set as desired.
"""
self._parms.update(parms)
return self
there does not appear to be much going on that I see could go wrong. Anyone know what I'm missing here or what is happening to cause this error?
TLDR: Need to unpack the keys/values as **kwargs keywords in order to get the expected behavior of updating the _parms dict. So do
model = model.set_params(**HYPARAMS) #see https://stackoverflow.com/a/22384521/8236733
Example:
# here's a basic standin for the set_params method
>>> def kfunc(**parms):
... print parms
...
# what I was doing
>>> kfunc({1:2})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: kfunc() takes exactly 0 arguments (1 given)
# and also tried
>>> kfunc(parms={1:2})
{'parms': {1: 2}}
>>> kfunc({u'1':2, u'2':3})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: kfunc() takes exactly 0 arguments (1 given)
# what should have been done
>>> kfunc(**{'1':2})
{'1': 2}
>>> kfunc(**{u'1':2, u'2':3})
{u'1': 2, u'2': 3}
Can now see that this is not directly related to h2o, but keeping post up anyway so others with this problem may find, since did not immediately think to do this from just reading the popup docs for the method (and also since the other SE post commented in the example that I used to actually use variables as **kwarg keywords was not even on the first page of a Google search of "How to use python variable as keyword for kwargs parameter?" so would like to add more avenues to it).

Python ode first order, how to solve this using Sympy

When I try to solve this first ode by using Sympy as it shows below:
import sympy
y = sympy.Function('y')
t = sympy.Symbol('t')
ode = sympy.Eq(y(t).diff(t),(1/y(t))*sympy.sin(t))
sol = sympy.dsolve(ode,y(t))
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
ode_sol= sol.subs([(csol.rhs,csol.lhs)])
print(sympy.pprint(ode_sol))
It gives me this error:
Traceback (most recent call last):
File "C:/Users/Mohammed Alotaibi/AppData/Local/Programs/Python/Python35/ODE2.py", line 26, in <module>
csol=sol.subs([(t,0),(y(0),-4)]) # the I.C. is y(0) = 1
AttributeError: 'list' object has no attribute 'subs'
Your problem is that this ODE does not have a unique solution. Thus it returns a list of solution, which you can find out from the error message and by printing sol.
Do the evaluation in a loop,
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
ode_sol = psol.subs([(csol.rhs,csol.lhs)]);
print(sympy.pprint(ode_sol))
to find the next error, that substituting does not solve for the constant. What works is to define C1=sympy.Symbol("C1") and using
ode_sol= psol.subs([(C1, sympy.solve(csol)[0])]);
but this still feels hacky. Or better to avoid error messages for the unsolvability of the second case:
C1=sympy.Symbol("C1");
for psol in sol:
csol = psol.subs([(t,0),(y(0),-4)]);
for cc1 in sympy.solve(csol):
ode_sol= psol.subs([(C1, cc1)]);
print(sympy.pprint(ode_sol))

selenium simple test says xpath is not a legal expression

I started using SST (selenium simple test) but ran into a problem when executing what seems to be a valid xpath expression '//div[div#data-type="folder-name"]'. SST fails with the following traceback:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/sst/cases.py", line 207, in run_test_script exec self.code in self.context
File "./sst-one.py", line 7, in <module> names = get_elements_by_xpath('//div[div#data-type="folder-name"]')
File "/usr/local/lib/python2.7/dist-packages/sst/actions.py", line 1344, in >get_elements_by_xpath_raise(msg)
File "/usr/local/lib/python2.7/dist-packages/sst/actions.py", line 118, in _raise raise AssertionError(msg)
AssertionError: Element not found: Message: u'The given selector //div[div#data->type="folder-name"] is either invalid or does not result in a WebElement. The following >error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath >expression //div[div#data-type="folder-name"] because of the following >error:\n[Exception... "The expression is not a legal expression." code: "12" nsresult: >"0x805b0033 (SyntaxError)" location: "file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/driver_component.js> Line: 5916"]' ;
Stacktrace:
at FirefoxDriver.annotateInvalidSelectorError_ (file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/driver_component.js:8873)
at FirefoxDriver.prototype.findElementsInternal_ (file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/driver_component.js:8931)
at FirefoxDriver.prototype.findElements file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/driver_component.js:8935)
at DelayedCommand.prototype.executeInternal_/h file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/command_processor.js:10840)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/command_processor.js:10845)
at DelayedCommand.prototype.execute/< file:///tmp/tmp12zCta/extensions/fxdriver#googlecode.com/components/command_processor.js:10787)
The SST code line is:
names = get_elements_by_xpath('//div[div#data-type="folder-name"]')
If I run the same xpath statement using selenium (from python), not SST, it is a valid expression and returns the matching elements if there were any.
The pure selenium code line (where br is the firefox webdriver object) is:
elems = br.find_elements_by_xpath("//div[div/#data-type='folder-name']")
I understand this is a bit in the weeds, so if anyone has some hints as to how to debug the .js code that selenium creates in the /tmp directory that would be a big help.
It's not a valid XPath expression. This
//div[div#data-type="folder-name"]
Should probably read
//div[div/#data-type="folder-name"]
which is indeed what you have in one of your examples.
If you are trying to get all possible div tags with data-type="folder-name",
//div[#data-type="folder-name"]
If only children of the first div with data-type="folder-name",
//div[div/#data-type="folder-name"]
You don't show any markup, up i assuming you have
<div><div data-type="folder-name"></div></div>
you need:
elems = br.find_elements_by_xpath("//div//div[#data-type='folder-name']")

Resources