List object is not callable IN PYTHON - python

def new(l):
return l%2==0
list(filter(new,range(4,31)))
please someone resolve this. I am getting a error that list object is not callable

This one will work. Only reason it will fail because you have declared list as a variable somewhere in your code like list = ...., so the builtin type gets overwritten. If you are doing from script change the variable name using Find and Replace in Text editor, or if you are in python shell then use del list, or restart the shell but you have to re-do things (but python history file will help you to restore)

Related

Type error when accessing Django request.POST

I'm attempting to build an XML document out of request.POST data in a Django app:
ElementTree.Element("occasion", text=request.POST["occasion"])
PyCharm is giving me an error on the text parameter saying Expected type 'str', got 'Type[QueryDict]' instead. I only bring up PyCharm because I know its type checker can be overzealous sometimes. However, I haven't been able to find anything about this issue specifically.
Am I doing something wrong? Or should I try to silence this error?
Assuming you're not posting anything unusual, like json, request.POST['occasion'] should return a string, either the field 'occasion' or the last value of the list submitted with that name (or an error, if empty. Use request.POST.get('occasion') to avoid).
There are apparently some httprequest related issues with pycharm, but the way to doublecheck if this is happening here would be to print out and/or type check request.POST['occasion'] prior to that line to make sure of what it returns, eg:
occasion = request.POST['occasion']
print(type(occasion), occasion)
ElementTree.Element("occasion", text=occasion)
In the last line, using a variable assigned ahead of time might be a simple way to remove the pycharm error without turning off warnings, depending on your tolerance for extra code.

What is the pylance subscript error trying to tell me about my dictionary?

I have the following piece of code:
for table in list(my_dict.keys()):
if table in my_other_dict:
for file in my_dict[table]:
if file in my_other_dict[table]:
my_dict[table].remove(file)
if not my_dict[table]:
del my_dict[table]
But the second, third and fourth my_dict reference is underlined with the following error:
Subscript for class "dict" will generate runtime exception; enclose
type annotation in quotes
The only more or less relevant internet source I can find about it is this post, but I do not understand how I can apply it to my case.
Indeed, when this little piece of code is executed, there is no runtime exception.
Moreover, the last my_dict reference is underlined with the following error message:
Generic class type cannot be deleted
This dictionary is built and returned by the following function (signature only, content is too big):
def meta_from_iso_to_utf(self, correct_meta_files):
How do I fix my code so that pylance stops complaining about these problems which do not seem to be problems as the code runs as expected ?
Edit 1 (to answer Frank's comment):
my_dict is defined like this:
some_dict, my_dict = object.meta_from_iso_to_utf(
argument
)
In the method, the my_dict is defined like so in a loop:
my_dict[key].append[value[0]]
table is the variable containing the keys of my_dict during the loop, which is a string. This variable is used only in this loop.
Concerning my editor, it is visual studio code 1.60.1, with the Python extension from microsoft version v2021.9.1246542782

Controlling LabVIEW front panel from python returns typeerror nonetype object is not callable

Using code below where vi_path is .vi file path, control_name and ind_name are control name and indicator name strings
labview = win32com.client.Dispatch("Labview.Application")
VI = labview.getvireference(vi_path)
print(f'Name: {VI.Name}')
VI.setcontrolvalue(control_name,f) # this works to set float values
s=(VI.getcontrolvalue(ind_name))
works fine. But
VI.Run() #async = False default i.e wait for exec to finish
is creating the TypeError:NoneType object is not callable error
Saw this line added in a code sample elsewhere VI._FlagAsMethod('Run')
before using VI.Run()
This fixed the problem. Apparently some of the active x methods in LabVIEW are not getting recognized properly- odd since getcontrolvalue and setcontrolvalue methods dont need to be flagged as methods but Run method does. Anyway, recommend using _FlagAsMethod before using any methods that are generating this TypeError Nonetype object not callable error as well.

Removing a variable in Python Gurobi

I am using Python/Gurobi to optimize a problem. I generated variables using GRBaddVar through this code:
x[1,i,j,t] = model.addVar(vtype="B", name="x(1,%s,%s,%s)" % (i,j,t))
I also want to remove some variables in order to save some space. The variable is removed when a certain condition is true. Suppose the condition is when R = 1. To remove the variable I use the following code.
if R == 1:
x[1,i,j,t] = model.delVars(vtype="B", name="x(1,%s,%s,%s)" % (i,j,t))
However, it resulted in the following error:
AttributeError: 'gurobipy.Model' object has no attribute 'delVars'
I have no idea what's wrong with the code since I just follow the documentation (attached below) from Gurobi. Thanks for your help!
As David noted, you are looking at the documentation for the C API.
In the Python API, you can remove a single variable with the Model.remove() method, using the Var object the argument. E.g.,
model.remove(x[1,i,j,t])
You can use this same method to remove, e.g., a list of Var objects at once.
Note that you should call Model.update() before removing variables in this manner.
"You can use this same method to remove, e.g., a list of Var objects at once."
Can anyone suggest how to remove multiple variables at once. I have the variable to be removed in a dict/list format. I am trying to the below code, but error appears: GurobiError: Item to be removed not a Var, MVar, Constr, MConstr, SOS, QConstr, or GenConstr
model.remove(var_to_remove_dict.keys())

Python range() builtin function, erm... malfunctioning with Django

What, if anything, is wrong with this line of python code:
daterange = [begin + timedelta(n) for n in range((end - begin).days)]
Where begin and end are datetime.date objects with valid values.
I'm using this in a Django view to process some data, but everytime the view this is in gets called I get the following error with the aforementioned line highlighted:
UnboundLocalError at /url/of/error/creating/view/here/
local variable 'range' referenced before assignment
If I execute this line inside the interpreter it works fine, but somehow it doesn't fly inside a Django view. I don't understand why range is being interpreted as a variable name at all. Is there actually something wrong with this line, or is it something else in the code that's making Django complain?
Help!
There's nothing wrong with Django. You create a local variable range in the same scope (by assigning one). For instance range = None in the very last line of a function makes Python consider an occurrence of range in the first line of the same function a reference to that local variable. Since it doesn't have a value assigned at that point, you get an UnboundLocalError.

Categories

Resources