Im getting TypeError: '<' not supported between instances of 'BST' and 'int' when I initially set root to None(see in code)
class BST:
def __init__(self,key):
self.key = key
self.lchild = None
self.rchild = None
def insert(self,data):
if self.key is None:
self.key = BST(data)
return
if self.key < data:
if self.lchild:
self.lchild.insert(data)
else:
self.lchild = BST(data)
else:
if self.rchild:
self.rchild.insert(data)
else:
self.rchild = BST(data)
def preorder(self):
print(self.key)
if self.lchild:
self.lchild.preorder()
if self.rchild:
self.rchild.preorder()
root = BST(None)
list1 = [20,34,1,3,4,78]
for i in list1:
root.insert(i)
root.preorder()
When I set root to an int it works fine
The comments say you should debug. They aren't wrong, but maybe it's hard to see how to do that when you don't understand the error.
The error means generally that you are comparing a BST to an int somewhere and that this is not defined.
Let's have a look at the types by adding
print(f"{type(self.key)=},\t{type(data)=}")
to the beginning of your insert function.
We get:
type(self.key)=<class 'NoneType'>, type(data)=<class 'int'>
type(self.key)=<class '__main__.BST'>, type(data)=<class 'int'>
Traceback (most recent call last):
File "tmp.py", line 36, in <module>
root.insert(i)
File "tmp.py", line 12, in insert
if self.key < data:
TypeError: '<' not supported between instances of 'BST' and 'int'
So apparently we first execute the function correctly once with the key being None and then again with the key being a BST object. The data is always an int.
But you have not defined how an int and a BST can be compared.
Do you want to do that? Probably not. Because your key is supposed to be an int, right? You have a BST but the key inside the BST should be an int. So you don't need the BST(data) when you assign the key in the case where it is none. instead:
if self.key is None:
self.key = data
return
Just set root = None initially.
And check if root == None in insert method.
In my main method, when I try to test my insert function, after three inserts, the function suddenly throws an error saying it isn't defined.
I'm not really sure how to go about this. My assumption is that it has something to do with the space of the function being capped, and thus instead of going on with the remaining operations, the compiler cannot continue.
class MerkleTree:
def __init__ (self, rootHash, rootNode):
self.rootHash = rootHash
self.rootNode = rootNode
root = MerkleTree(None, None)
class MerkleNode():
def __init__ (self, value, left, right, leafValue):
self.value = value
self.left = left
self.right = right
self.leafValue = leafValue
class Entry():
def __init__ (self, key, value):
self.key = key
self.value = value
class MerkleTreeInterface:
#recursive Insert Helper
def insertHelp(self, root, node):
if (root.rootNode.right != None):
insertHelp(root.rootNode.right, node)
else:
root.rootNode.right = node
#insert function, finds the appropriate available slot to insert data
def insert(self, key, value):
entry = Entry(key, value)
node = MerkleNode(value, None, None, entry)
if(root.rootNode == None):
root.rootNode = node
else:
if(root.rootNode.left == None):
root.rootNode.left = node
elif(root.rootNode.right == None):
root.rootNode.right = node
else:
insertHelp(root, node)
class main():
tree = MerkleTreeInterface()
tree.insert("b", 2)
tree.insert("a", 1)
tree.insert("c", 3)
tree.insert("d", 4)
The expected result is to simply insert everything (so in this case, the list should be [a,b,c,d] when I traverse it (I only want the keys), but upon the 4th insertion, the error occurs. Here is the log:
Traceback (most recent call last):
File "/Users/User/Desktop/temp.py", line 75, in <module>
class main():
File "/Users/User/Desktop/temp.py", line 81, in main
tree.insert("d", 4)
File "/Users/User/Desktop/temp.py", line 42, in insert
insertHelp(root, node)
NameError: name 'insertHelp' is not defined
[Finished in 0.8s with exit code 1]
In the insertHelp method, insertHelp(root.rootNode.right, node) should be self.insertHelp(root.rootNode.right, node).
In the insert method, insertHelp(root, node) should be self.insertHelp(root, node).
You did not get this error while executing:
tree.insert("b", 2)
tree.insert("a", 1)
tree.insert("c", 3)
Because they did not execute insertHelp(root, node). But:
tree.insert("d", 4)
Tried to execute that statement and you got the error.
As the insertHelp method is an instance method, we need to use self.insertHelp to access it.
Search Python instance method vs Static method vs Class method to understand it clearly.
I am trying to define condition to class, if my object Does not meet the conditions:
The conditions: all the organs is vectors, the shape of the vectors is the same.
When I try to casting the object the function will return None.
Thats what I tried so far:
class Matrix:
def __init__(self,m1):
dic_len = {}
self.m1 = tuple(m1)
checking = 0
for i in self.m1:
dic_len[len(i)] = 'check'
if type(i) != Vector:
self.m1 = None
checking = 1
if len(dic_len) != 1:
self.m1 = None
if len(dic_len) == 1 and checking == 0:
self.content = self.m1 = tuple(m1)
self.shape = (len(m1),len(m1[0]))
def __repr__(self):
if self.m1 != None:
return "(" + ", ".join(str(i) for i in self.m1) + ")"
else:
return None
But I get this error:
>>>v1 = Vector([1,2])
>>>v2 = Vector([4,5,6])
>>>m = Matrix([v1,v2])
>>>print(m)
TypeError: __str__ returned non-string (type NoneType)
i wish the function will return None.
return str(None)
instead of
return None
The CPython docs state for the __repr__ method state that
The return value must be a string object.
So returning None isn't going to work.
>>> class C:
... def __repr__(self):
... return None
...
>>> c = C()
>>> repr(c)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __repr__ returned non-string (type NoneType)
If you're going to share your code with others, it might be better to code __repr__ to produce its coventional output:
...this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment)...
And override __str__ to produce a representation that indicates the validity of the object instance (though note __str__ must also return a string).
I have an object (Person) that has multiple subobjects (Pet, Residence) as properties. I want to be able to dynamically set the properties of these subobjects like so:
class Person(object):
def __init__(self):
self.pet = Pet()
self.residence = Residence()
class Pet(object):
def __init__(self,name='Fido',species='Dog'):
self.name = name
self.species = species
class Residence(object):
def __init__(self,type='House',sqft=None):
self.type = type
self.sqft=sqft
if __name__=='__main__':
p=Person()
setattr(p,'pet.name','Sparky')
setattr(p,'residence.type','Apartment')
print p.__dict__
Currently I get the wrong output: {'pet': <__main__.Pet object at 0x10c5ec050>, 'residence': <__main__.Residence object at 0x10c5ec0d0>, 'pet.name': 'Sparky', 'residence.type': 'Apartment'}
As you can see, instead of setting the name attribute on the Pet subobject of the Person, a new attribute pet.name is created on the Person.
I cannot specify person.pet to setattr() because different sub-objects will be set by the same method, which parses some text and fills in the object attributes if/when a relevant key is found.
Is there a easy/builtin way to accomplish this?
Or perhaps I need to write a recursive function to parse the string and call getattr() multiple times until the necessary subobject is found and then call setattr() on that found subobject?
You could use functools.reduce:
import functools
def rsetattr(obj, attr, val):
pre, _, post = attr.rpartition('.')
return setattr(rgetattr(obj, pre) if pre else obj, post, val)
# using wonder's beautiful simplification: https://stackoverflow.com/questions/31174295/getattr-and-setattr-on-nested-objects/31174427?noredirect=1#comment86638618_31174427
def rgetattr(obj, attr, *args):
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return functools.reduce(_getattr, [obj] + attr.split('.'))
rgetattr and rsetattr are drop-in replacements for getattr and setattr,
which can also handle dotted attr strings.
import functools
class Person(object):
def __init__(self):
self.pet = Pet()
self.residence = Residence()
class Pet(object):
def __init__(self,name='Fido',species='Dog'):
self.name = name
self.species = species
class Residence(object):
def __init__(self,type='House',sqft=None):
self.type = type
self.sqft=sqft
def rsetattr(obj, attr, val):
pre, _, post = attr.rpartition('.')
return setattr(rgetattr(obj, pre) if pre else obj, post, val)
def rgetattr(obj, attr, *args):
def _getattr(obj, attr):
return getattr(obj, attr, *args)
return functools.reduce(_getattr, [obj] + attr.split('.'))
if __name__=='__main__':
p = Person()
print(rgetattr(p, 'pet.favorite.color', 'calico'))
# 'calico'
try:
# Without a default argument, `rgetattr`, like `getattr`, raises
# AttributeError when the dotted attribute is missing
print(rgetattr(p, 'pet.favorite.color'))
except AttributeError as err:
print(err)
# 'Pet' object has no attribute 'favorite'
rsetattr(p, 'pet.name', 'Sparky')
rsetattr(p, 'residence.type', 'Apartment')
print(p.__dict__)
print(p.pet.name)
# Sparky
print(p.residence.type)
# Apartment
For an out of the box solution, you can use operator.attrgetter:
from operator import attrgetter
attrgetter(dotted_path)(obj)
For one parent and one child:
if __name__=='__main__':
p = Person()
parent, child = 'pet.name'.split('.')
setattr(getattr(p, parent), child, 'Sparky')
parent, child = 'residence.type'.split('.')
setattr(getattr(p, parent), child, 'Sparky')
print p.__dict__
This is simpler than the other answers for this particular use case.
unutbu's answer (https://stackoverflow.com/a/31174427/2683842) has a "bug". After getattr() fails and is replaced by default, it continues calling getattr on default.
Example: rgetattr(object(), "nothing.imag", 1) should equal 1 in my opinion, but it returns 0:
getattr(object(), 'nothing', 1) == 1.
getattr(1, 'imag', 1) == 0 (since 1 is real and has no complex component).
Solution
I modified rgetattr to return default at the first missing attribute:
import functools
DELIMITER = "."
def rgetattr(obj, path: str, *default):
"""
:param obj: Object
:param path: 'attr1.attr2.etc'
:param default: Optional default value, at any point in the path
:return: obj.attr1.attr2.etc
"""
attrs = path.split(DELIMITER)
try:
return functools.reduce(getattr, attrs, obj)
except AttributeError:
if default:
return default[0]
raise
This should be a
def getNestedAttr(obj,nestedParam):
next = obj
for p in nestedParam.split('.'):
next = getattr(next,p)
return next
class Issue : pass
issue = Issue()
issue.status = Issue()
issue.status.name = "Hello"
getattr(issue,'status.name')
'''
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Issue' object has no attribute 'status.name'
'''
getNestedAttr(issue,'status.name')
#'Hello'
simple solution
I made a simple version based on ubntu's answer called magicattr that also works on attrs, lists, and dicts by parsing and walking the ast.
For example, with this class:
class Person:
settings = {
'autosave': True,
'style': {
'height': 30,
'width': 200
},
'themes': ['light', 'dark']
}
def __init__(self, name, age, friends):
self.name = name
self.age = age
self.friends = friends
bob = Person(name="Bob", age=31, friends=[])
jill = Person(name="Jill", age=29, friends=[bob])
jack = Person(name="Jack", age=28, friends=[bob, jill])
You can do this
# Nothing new
assert magicattr.get(bob, 'age') == 31
# Lists
assert magicattr.get(jill, 'friends[0].name') == 'Bob'
assert magicattr.get(jack, 'friends[-1].age') == 29
# Dict lookups
assert magicattr.get(jack, 'settings["style"]["width"]') == 200
# Combination of lookups
assert magicattr.get(jack, 'settings["themes"][-2]') == 'light'
assert magicattr.get(jack, 'friends[-1].settings["themes"][1]') == 'dark'
# Setattr
magicattr.set(bob, 'settings["style"]["width"]', 400)
assert magicattr.get(bob, 'settings["style"]["width"]') == 400
# Nested objects
magicattr.set(bob, 'friends', [jack, jill])
assert magicattr.get(jack, 'friends[0].friends[0]') == jack
magicattr.set(jill, 'friends[0].age', 32)
assert bob.age == 32
It also won't let you/someone call functions or assign a value since it doesn't use eval or allow Assign/Call nodes.
with pytest.raises(ValueError) as e:
magicattr.get(bob, 'friends = [1,1]')
# Nice try, function calls are not allowed
with pytest.raises(ValueError):
magicattr.get(bob, 'friends.pop(0)')
And a easy to understand three-liner based on jimbo1qaz's answer, reduced to the very limit:
def rgetattr(obj, path, default):
try:
return functools.reduce(getattr, path.split(), obj)
except AttributeError:
return default
Usage:
>>> class O(object):
... pass
... o = O()
... o.first = O()
... o.first.second = O()
... o.first.second.third = 42
... rgetattr(o, 'first second third', None)
42
Just keep in mind that "space" is not a typical delimiter for this use case.
Thanks for the accepted answer above. It was helpful.
In case anyone wants to extend the use for hasattr use the code below:
def rhasattr(obj, attr):
_nested_attrs = attr.split(".")
_curr_obj = obj
for _a in _nested_attrs[:-1]:
if hasattr(_curr_obj, _a):
_curr_obj = getattr(_curr_obj, _a)
else:
return False
return hasattr(_curr_obj, _nested_attrs[-1])
Ok so while typing the question I had an idea of how to do this and it seems to work fine. Here is what I came up with:
def set_attribute(obj, path_string, new_value):
parts = path_string.split('.')
final_attribute_index = len(parts)-1
current_attribute = obj
i = 0
for part in parts:
new_attr = getattr(current_attribute, part, None)
if current_attribute is None:
print 'Error %s not found in %s' % (part, current_attribute)
break
if i == final_attribute_index:
setattr(current_attribute, part, new_value)
current_attribute = new_attr
i+=1
def get_attribute(obj, path_string):
parts = path_string.split('.')
final_attribute_index = len(parts)-1
current_attribute = obj
i = 0
for part in parts:
new_attr = getattr(current_attribute, part, None)
if current_attribute is None:
print 'Error %s not found in %s' % (part, current_attribute)
return None
if i == final_attribute_index:
return getattr(current_attribute, part)
current_attribute = new_attr
i += 1
I guess this solves my question, but I am still curious if there is a better way to do this?
I feel like this has to be something pretty common in OOP and python, so I'm surprised gatattr and setattr do not support this natively.
Here's something similar to ChaimG's answer, but it works with an arbitrary number of cases. However, it only supports get attributes, not setting them.
requested_attr = 'pet.name'
parent = Person()
sub_names = requested_attr.split('.')
sub = None
for sub_name in sub_names:
try:
sub = parent.__getattribute__(sub_name)
parent = sub
except AttributeError:
raise Exception("The panel doesn't have an attribute that matches your request!")
pets_name = sub
I just love recursive functions
def rgetattr(obj,attr):
_this_func = rgetattr
sp = attr.split('.',1)
if len(sp)==1:
l,r = sp[0],''
else:
l,r = sp
obj = getattr(obj,l)
if r:
obj = _this_func(obj,r)
return obj
I know this post is pretty old but below code might help some one.
def getNestedObjectValue(obj={}, attr=""):
splittedFields = attr.split(".")
nestedValue = ""
previousValue = ""
for field in splittedFields:
previousValue = nestedValue
nestedValue = (
obj.get(field) if previousValue == "" else previousValue.get(field)
)
return nestedValue
print(
getNestedObjectValue(
obj={
"name": "ADASDASD",
"properties": {"somefield": {"value": "zxczxcxczxcxzc"}},
},
attr="properties.somefield.value",
)
)
Output
PS C:\myprograms\samples> python .\sample.py
zxczxcxczxcxzc
I have a handy class that I use to allow me to easily add a set of "summariser" functions to a GDB pretty printer (for example, a Rect class could have an [Area] field, computed by Python). it then prints all the existing children as well, so you can see everything at once.
class SummaryAndFieldIterator:
"""
Iterator to first go through a list of summariser functions,
then display all the fields in the object in order
"""
def __init__ (self, obj, summaries):
self.count = 0
self.obj = obj;
self.summaries = summaries;
self.keys = sorted(obj.type.iterkeys())
def __iter__(self):
return self
def __next__(self):
if (self.count >= len(self.keys) + len(self.summaries)):
raise StopIteration
elif self.count < len(self.summaries):
name, retVal = self.summaries[self.count](self.obj)
# FIXME: this doesn't seem to work when a string is returned
# in retVal?
result = "[%s]" % name, retVal
else:
field = self.count - len(self.summaries)
result = self.keys[field], self.obj[self.keys[field]]
self.count += 1
return result
next = __next__
class MyObjectPrinter:
def __init__(self, val):
self.val = val
def get_int(self):
return "meaning", 42
def get_string(self):
return "hoopiness", "Forty-two"
def children(self):
return SummaryAndFieldIterator(self.val, [self.get_string])
This works very well for the summarisers which return numeric values, but for strings, it ends up displaying as an array, so that I get
NAME VALUE
myobj {..}
|-->[meaning] 42
|-->[hoopiness]
|-->[0] 'F'
|-->[1] 'o'
.....
|-->real_field 34234
This is presumably becuase the string that comes from
name, retVal = self.summaries[self.count](self.obj)
does not generate a sufficiently "stringy" gdb.Value object when it is returned by SummaryAndFieldIterator's __next__ method. Adjusting the display_hint() method of MyObjectPrinter doesn't seem to have any effect (but I doubt it would, as this is the child, not the object).
Anyone know how to return a string from the children() iterator and get it to display as a string?
Okay, apparently this may be a bug related to the way that GDB/MI communicates with pretty-printers, Bugzilla created here : https://sourceware.org/bugzilla/show_bug.cgi?id=18282