Adding method to Python's NoneType - python

I'm using BeautifulSoup to do some crawling, and want to chain find calls, for example:
soup.find('div', class_="class1").find('div', class_="class2").find('div', class_="class3")
Of course, this breaks whenever one of the divs cannot be found, throwing an
AttributeError: 'NoneType' object has no attribute 'find'
Is there a way to modify NoneType to add a find method such as
class NoneType:
def find(*args):
return None
so that I can do something like
thing = soup.find('div', class_="class1").find('div', class_="class2").find('div', class_="class3")
if thing:
do more stuff
instead of
thing1 = soup.find('div', class_="class1")
if thing1:
thing2 = thing1.find('div', class_="class2")
if thing2:
thing3 = thing2.find('div', class_="class3")
etc.
I think I might be able to do something similar by using a parser with XPath capabilities, but the question is not specific to this use case and is more about modifying/overriding built in classes.

Why not use a try/except statement instead (since you cannot modify NoneType)?
try:
thing = soup.find('div', class_="class1").find('div', class_="class2").find('div', class_="class3")
do more stuff
except AttributeError:
thing = None # if you need to do more with thing

You can't modify builtin class such as NoneType or str:
>>> nt = type(None)
>>> nt.bla = 23
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'NoneType'
For some of them (eg str), you can inherit from:
>>> class bla(str):
... def toto(self): return 1
>>> bla('2123').toto()
1
It's not possible with NoneType. And it won't help you either:
>>> class myNoneType(nt):
... def find(self): return 1
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
type 'NoneType' is not an acceptable base type

You cannot modify the class and the real question is why you would try? NoneType means there is no data there so when you attempt a .find() on that type even if it did exist you would only get null or no values from it. I would reccomend something like this.
try:
var = soup.find('div', class_="class1").find('div', class_="class2").find('div', class_="class3")
except AttributeError:
do something else instead or message saying there was no div

You can't inherit from None:
>>> class Noneish(type(None)):
... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: type 'NoneType' is not an acceptable base type

An approach might be to have a
class FindCaller(object):
def __init__(self, *a, **k):
self.a = a
self.k = k
def __call__(self, obj):
return obj.find(*self.a, **self.k)
def callchain(root, *fcs):
for fc in fcs:
root = fc(root)
if root is None: return
return root
and then do
thing = callchain(soup,
FindCaller('div', class_="class1"),
FindCaller('div', class_="class2"),
FindCaller('div', class_="class3"),
)

You can't. For good reasons...
In fact, NoneType is even less accessible than other built-in types:
type(None).foo = lambda x: x
# ---------------------------------------------------------------------------
# TypeError Traceback (most recent call last)
# <ipython-input-12-61bbde54e51b> in <module>()
# ----> 1 type(None).foo = lambda x: x
# TypeError: can't set attributes of built-in/extension type 'NoneType'
NoneType.foo = lambda x: x
# ---------------------------------------------------------------------------
# NameError Traceback (most recent call last)
# <ipython-input-13-22af1ed98023> in <module>()
# ----> 1 NoneType.foo = lambda x: x
# NameError: name 'NoneType' is not defined
int.foo = lambda x: x
# ---------------------------------------------------------------------------
# TypeError Traceback (most recent call last)
# <ipython-input-14-c46c4e33b8cc> in <module>()
# ----> 1 int.foo = lambda x: x
# TypeError: can't set attributes of built-in/extension type 'int'
As suggested above, use try: ... except AttributeError: clause.

Related

Getting the TypeError: 'list' object is not callable in Python

class Student:
def __init__(self,first,last,id):
self._first_name = first
self._last_name = last
self._id_number = id
self._enrolled_in = []
def enroll_in_course(self,course):
self._enrolled_in.append(course)
return self._enrolled_in()
s1 = Student("kathy","lor","323232")
s1.enroll_in_course("hello")
print(s1._enrolled_in)
In the code above, i am getting the error as:
Traceback (most recent call last):
File "main.py", line 14, in
s1.enroll_in_course("hello") File "main.py", line 10, in enroll_in_course
return self._enrolled_in()
TypeError: 'list' object is not callable
I am trying to solve the error, but unable to do so. Can anybody help me here.
You have defined self._enrolled_in but you're adding it to self.enrolled_in
Missed the underscore (self._enrolled_in.append)
You have called the attribute _enrolled_in in your __init__() method. In the enroll_in_course() method you're trying to append to enrolled_in which does not exist. So try by adding the underscore in front.
You are missing an _ in the appended statement. You should write self._enrolled_in.append(course) on your enroll_in_course method.

NameError creating instance of imported class

I have a subclassed Course class as follows:
# course.py
class Course:
"""Represent a single Course"""
kind = 'Gen Ed'
def __init__(self, name, number) :
self._name = name # 'private' instance variable\
self._number = number
self.__display()
def display(self):
print(self.kind,"Course:" , self._name, self._number, sep=" ")
__display = display # private copy
class CSCourse(Course):
"""Represent a single CS Course"""
kind = 'CS' # class variable shared by all CSCourses
def __init__(self, name, number, language, numberOfPrograms):
Course.__init__(self, name, number)
self._language = language
self._numberOfPrograms = numberOfPrograms
def display(self):
Course.display(self)
print('Language', self._language,
'Number Of programs:', self._numberOfPrograms, sep = ' ')
I import the module as follows:
>>>from course import *
This does not throw any exception, but then when I issue the following to call the constructor, I get the error below?
>>> cs360=CSCourse("Special Topics", 360, "python", 21)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'CSCourse' is not defined
What am I doing wrong please? I did also try to see what methods are available in the classes imported. It seems nothing is being imported!
>>> import inspect
>>> inspect.getmembers(Course, lambda a:not(inspect.isroutine(a)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Course' is not defined
>>> inspect.getmembers(CSCourse, lambda a:not(inspect.isroutine(a)))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'CSCourse' is not defined
For anyone else having this problem, check if you have circular imports (in file a.py from b import * and in file b.py from a import *). Python doesn't seem to raise an exception when that happens, but the import doesn't work. Restructuring the code to remove the circular import fixed the problem for me.

iPython widget : Interact doesn't work with class methods - AttributeError

I'm faced with a problem in the ipython notebook. I would like to implement the iphython widgets in some class meethods but it seems that it's not yet possible in the ipython v2 as mentionned in this github issue : https://github.com/ipython/ipython/issues/6278
Quote from fperez
This code:
from IPython.html.widgets import interact
class Foo(object):
def show(self, x):
print x
f = Foo()
interact(f.show, x=(1,10))
produces this exception:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-58-b03b8685dfc0> in <module>()
7 f = Foo()
8
----> 9 interact(f.show, x=(1,10))
/home/fperez/usr/lib/python2.7/site-> packages/IPython/html/widgets/interaction.pyc in interact(__interact_f, > **kwargs)
235 f = __interact_f
236 w = interactive(f, **kwargs)
--> 237 f.widget = w
238 display(w)
239 return f
AttributeError: 'instancemethod' object has no attribute 'widget'
I tried to wrap the interact widget in a lambda function but my attempt doesn't seem to work
def constructWidgets(self):
interact(lambda a:self.getWidgetValue(a),a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0, description="WidgetNameFoo"))
I have got this error message
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
TypeError: <lambda>() got an unexpected keyword argument 'WidgetNameFoo'
I understand that I have a conflict between the description field (in this case 'WidgetNameFoo') and the variable name 'a'. When the description field mathches the variable name, ipython doesn't return the TypeError message. For example :
def constructWidgets(self):
interact(lambda a:self.getWidgetValue(a),a=widgets.FloatSliderWidget(min=-10.0, max=10.0, step=0.1, value=5.0, description="a"))
I need to transmit a description field which varies depends on the case.
Thanks for your help ! =)

python: TypeError: 'str' object is not callable

i'm trying to load a binary file with pickle that has a record in a list, like so
import pickle
class player_energy_usage():
def __init__(self):
self.weapons = 25
elf.shields = 25
self.life_support = 25
self.engines = 25
def p_eu_init():
global p_energy
p_energy = []
player_ship_energy = player_energy_usage()
p_energy.append(player_ship_energy)
pickle.dump(p_energy,open('p_energy.dat','wb'))
p_eu_init()
pickle.load('rb'('p_energy.dat'))
print('Weapons are using {0}% of energy'.format(p_energy[0].weapons))
print('Shields are using {0}% of energy'.format(p_energy[0].shields))
print('Life Support is using {0}% of energy'.format(p_energy[0].life_support))
print('Engines is using {0}% of energy'.format(p_energy[0].engines))
However i get a type error,
Traceback (most recent call last):
File "E:/Python texted based game/Tests/file loading test.py", line 18, in <module>
pickle.load('rb'('p_energy.dat'))
TypeError: 'str' object is not callable
thanks for the help.
That is not the correct syntax. It should be instead:
p_energy = pickle.load(open('p_energy.dat', 'rb'))
What you're actually doing is:
'rb'('p_energy.dat') is trying to call the str object 'rb' with an argument of 'p_energy.dat', which is why you get the error 'str' object is not callable.

in python ,if i edit this: type("myclass", (foo,), {"bar()", barbar}, how can i access the attribute "bar()"

>>> FooChild = type("FooChild", (Foo,), {"echobar()":echo_bar})
>>> FooChild().echobar()
Traceback (most recent call last):
File "<pyshell#214>", line 1, in <module>
FooChild().echobar()
AttributeError: 'FooChild' object has no attribute 'echobar'
>>> FooChild().echobar
Traceback (most recent call last):
File "<pyshell#215>", line 1, in <module>
FooChild().echobar
AttributeError: 'FooChild' object has no attribute 'echobar'
>>> hasattr(FooChild, "echobar()")
True
>>> FooChild().echobar()()
Traceback (most recent call last):
File "<pyshell#217>", line 1, in <module>
FooChild().echobar()()
AttributeError: 'FooChild' object has no attribute 'echobar'
Remove those parentheses:
FooChild = type("FooChild", (Foo,), {"echobar":echo_bar})
The name of a function is without the parentheses. Appending them means to call the function. Without the parentheses you have a reference on the function itself (e. g. for passing a function to things like sort or map).
echobar() is an invalid identifier in python, so you can't access it directly i.e using the dot syntax:
>>> FooChild = type("FooChild", (Foo,), {"echobar()":10})
Use __dict__ or getattr:
>>> FooChild.__dict__['echobar()']
10
>>> getattr(FooChild, 'echobar()')
10
If you want to use it as an attribute then simply get rid of the parenthesis:
>>> FooChild = type("FooChild", (Foo,), {"echobar":10})
>>> FooChild.echobar
10
If you want to use it as a method, then:
>>> def echobar(self):return 10
>>> FooChild = type("FooChild", (Foo,), {'echobar':echobar})
>>> FooChild().echobar()
10
If you pretend to have fancy function with name echobar() in you class, only mean of accessing it is getattr:
class Foo(object):pass
echo_bar =lambda *a: 'bar'
FooChild = type("FooChild", (Foo,), {"echobar()":echo_bar})
print getattr(FooChild(), 'echobar()')()
# bar

Categories

Resources