Why 'int' object is not callable? - python-3.x

try:
from logging import getLogger, ERROR
getLogger('scapy.runtime').setLevel(ERROR)
from scapy.all import *
conf.verb(0)
except ImportError:
print("[!]failed to import scapy")
sys.exit(1)
Error:
Traceback (most recent call last):
File "/home/dontalion/Desktop/python-programming/untitled/test-mitm.py", line 10, in <module>
conf.verb(0)
TypeError: 'int' object is not callable

Do you perhaps mean to do conf.verb = 0? conf.verb is an integer, so you cannot call it like a function as you are doing with conf.verb(0). You can see this in the source code here.
There's also this Stack Overflow question about setting Scapy's verbosity to 0.

Related

Class assignment: object not callable [duplicate]

As a starting developer in Python I've seen this error message many times appearing in my console but I don't fully understand what does it means.
Could anyone tell me, in a general way, what kind of action produces this error?
That error occurs when you try to call, with (), an object that is not callable.
A callable object can be a function or a class (that implements __call__ method). According to Python Docs:
object.__call__(self[, args...]): Called when the instance is “called” as a function
For example:
x = 1
print x()
x is not a callable object, but you are trying to call it as if it were it. This example produces the error:
TypeError: 'int' object is not callable
For better understaing of what is a callable object read this answer in another SO post.
The other answers detail the reason for the error. A possible cause (to check) may be your class has a variable and method with the same name, which you then call. Python accesses the variable as a callable - with ().
e.g. Class A defines self.a and self.a():
>>> class A:
... def __init__(self, val):
... self.a = val
... def a(self):
... return self.a
...
>>> my_a = A(12)
>>> val = my_a.a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>>
The action occurs when you attempt to call an object which is not a function, as with (). For instance, this will produce the error:
>>> a = 5
>>> a()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
Class instances can also be called if they define a method __call__
One common mistake that causes this error is trying to look up a list or dictionary element, but using parentheses instead of square brackets, i.e. (0) instead of [0]
The exception is raised when you try to call not callable object. Callable objects are (functions, methods, objects with __call__)
>>> f = 1
>>> callable(f)
False
>>> f()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
I came across this error message through a silly mistake. A classic example of Python giving you plenty of room to make a fool of yourself. Observe:
class DOH(object):
def __init__(self, property=None):
self.property=property
def property():
return property
x = DOH(1)
print(x.property())
Results
$ python3 t.py
Traceback (most recent call last):
File "t.py", line 9, in <module>
print(x.property())
TypeError: 'int' object is not callable
The problem here of course is that the function is overwritten with a property.

Split results in Python for CPU usage

Been trying to get this to work for a few hours now. Nothing I try is splitting this text up. I only want the Current CPU from this
>>> from __future__ import print_function
>>> from urllib.request import urlopen
>>> import json
>>> import subprocess
>>> import requests
>>> import random
>>> import sys
>>> import os
>>> import time
>>> import datetime
>>> import MySQLdb as my
>>> import psutil
>>> os.popen('vcgencmd measure_temp').readline()
"temp=52.0'C\n"
>>> cpu = psutil.cpu_freq()
>>> cpu = cpu.split('current=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'scpufreq' object has no attribute 'split'
>>> psutil.cpu_freq()
scpufreq(current=600.0, min=600.0, max=1500.0)
>>> psutil.cpu_freq(percpu=True)
[scpufreq(current=600.0, min=600.0, max=1500.0)]
>>> cpu = psutil.cpu_freq(percpu=True)
>>> cpu.split('=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'split'
>>> AttributeError: 'list' object has no attribute 'split'
File "<stdin>", line 1
AttributeError: 'list' object has no attribute 'split'
^
SyntaxError: invalid syntax
>>> AttributeError: 'list' object has no attribute 'split'
File "<stdin>", line 1
AttributeError: 'list' object has no attribute 'split'
^
SyntaxError: invalid syntax
>>> psutil.cpu_freq(percpu=True).readline()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'readline'
>>> cpu = psutil.cpu_freq()
Where am I going wrong with this?
OS: Rasbian Buster
Python: python3
PIP: pip3
It looks mostly like you're ignoring your error messages:
>>> cpu = psutil.cpu_freq()
>>> cpu = cpu.split('current=')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'scpufreq' object has no attribute 'split'
The return value from psutil.cpu_freq() isn't a string, so it doesn't have a split method. If you just print the value...
>>> cpu
scpufreq(current=700.0, min=700.0, max=800.0)
...you get some idea of what attributes it has, and indeed, we can access those values like this:
>>> cpu.current
700.0
>>> cpu.max
800.0
When you set percpu=True, you're getting back a list:
>>> psutil.cpu_freq(percpu=True)
[scpufreq(current=600.0, min=600.0, max=1500.0)]
And once again, a list isn't a string, so there's no split method. Since there's only a single CPU, you get back a 1-item list, so you can access values like this:
>>> cpu = psutil.cpu_freq(percpu=True)
>>> cpu[0].current
700.0

"RuntimeWarning: invalid value encountered in double_scalars" error occurs when taking cube root in python3

from numpy import cos
from tabulate import tabulate
def f(x):
return (cos(x))**(1./3)
table=[i for i in range(50)]
table2=[]
for i in range(50):
table2.append(f(i))
tables=[table, table2]
print (tabulate(tables))
When I run this code, it gives me a weird error. I looked up on the net some but they were all related to zero division. My problem occurs at numpy.cos when I used it before without "root" operation it worked fine. It also returns an error about tabulate as "'int' object is not iterable" which I really dont get it why?. Here is the error:
limit.py:5: RuntimeWarning: invalid value encountered in double_scalars
return cos(x)**(1/3)
Traceback (most recent call last):
File "limit.py", line 12, in <module>
print (tabulate(table, table2))
File "/home/yav/anaconda3/lib/python3.6/site-packages/tabulate.py", line 1247, in tabulate
tabular_data, headers, showindex=showindex)
File "/home/yav/anaconda3/lib/python3.6/site-packages/tabulate.py", line 933, in _normalize_tabular_data
rows = list(map(list,rows))
TypeError: 'int' object is not iterable
I am just a beginner, so if you can explain more simpler, will be appreciated. Thank you very much

python spec works differently as mentioned in the documentation

>>> from mock import *
>>> from urllib import request
>>> mock = Mock(spec=request.Request)
>>> mock.has_data()
<mock.Mock object at 0x...>
>>> mock.has_data.assret_called_with()
From : https://docs.python.org/3/library/unittest.mock.html#autospeccing
1) How can the statement "mock.has_data()" work as it throws error on my system ?
I do not find has_data() mentod in the request library.
2) Assuming even if we have "has_data()", I doubt how the below call works and doesnt throw attirbute error ?
Eg: mock.has_data.assret_called_with()
In my case with below example it fails but above mentioned example in python documentation doesn't report any error
from mock import *
from urllib import request
mock = Mock(spec=request.Request)
mock.has_header()
mock.has_header.assret_called_with()
Traceback (most recent call last):
  File ".\test2.py", line 5, in <module>
    mock.has_header.assret_called_with()
  File "C:\Users\hai\AppData\Local\Programs\Python\Python35-32\lib\site-packages\mock\mock.py", line 703, in __getattr__
    raise AttributeError(name)
AttributeError: assret_called_with
Please clarify, Is something wrong with my understanding or wrong with the documentaion ?
The example in the documentation is outdated (it is a bug. You can help fixing it). It worked as described in Python 3.3. mock.has_data() should raise AttributeError in Python 3.4+:
Changed in version 3.4: The request methods add_data, has_data, get_data, get_type, get_host, get_selector, get_origin_req_host and is_unverifiable that were deprecated since 3.3 have been removed.
The documentation should use a method that exists on request.Request object (such as mock.has_header()). The point is to illustrate the difference between mock.nonexisting (raises AttributeError if request.Request has no such attribute) and mock.some_existing_attribute.some_nonexisting_attribute (the latter doesn't raise AttributeError even if mock.some_existing_attribute has no some_nonexisting_attribute attribute):
>>> from unittest.mock import Mock
>>> from urllib import request
>>> mock = Mock(spec=request.Request)
>>> mock.has_header() # there is request.Request.has_header
<Mock name='mock.has_header()' id='174952746'>
>>> mock.nonexisting # there is no request.Request.nonexisting
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/...python3.6/unittest/mock.py", line 582, in __getattr__
raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute 'nonexisting'
Mock object has no attribute 'nonexisting'
>>> mock.has_header.nonexisting # no exception but there is no has_header.nonexisting
<Mock name='mock.has_header.nonexisting' id='249631353'>
In other words, spec is not recursive.

matplotlib type error when trying to plot simple list

If I type this tutorial example into my Spyder IPython console or the editor:
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
I get:
Traceback (most recent call last):
File "<ipython-input-15-6209c29ae839>", line 2, in <module>
plt.plot([1,2,3,4])
TypeError: 'list' object is not callable
It makes no sense to me that a list shouldn't be callable. What am I doing wrong?

Resources