AttributeError: 'ParentedTree' object has no attribute 'label' - python

I am basically working on parsed tree and trying to annotate tree nodes dominating empty categories(Empty node annotation).
I have defined a recurvsive function as below but the error that I am getting is "AttributeError: 'ParentedTree' object has no attribute 'label'".
def annotateTraceNodes(node):
numChildren = len(node);
numNone=0;
for child in node:
if isinstance(child,Tree):
annotateTraceNodes(child);
if(numChildren==0 or child.label().endswith("-NONE-")):
numNone+=1;
if(numChildren==numNone):
print "setting the label";
node.set_label(node.label()+"-NONE-");

Google suggests you're using NLTK, which I'm going to assume is the case.
A ParentedTree does not have a method called .label(). So when you write things like this:
child.label().endswith("-NONE-")
Python doesn't know what to do.
A Tree, on the other hand, does have a .label() method. Did you perhaps use a ParentedTree instead of a Tree somewhere?

Could also be a versioning issue.
Worked for me with version 3.3
pip install nltk==3.3

Related

I'm having issues with subroutines inside of __getitem__()

I have created a piece of code in python which makes use of the getitem subroutine, which I am not too familiar with.
Inside the getitem subroutine, there are multiple subroutines, one of which is:
def load(self,i):
return str(i)
However when I try to call it elsewhere in the project...
self.load(x)
... it says that it cannot find the subroutine:
self.load(x)
AttributeError: 'Calc' object has no attribute 'load'
I have tried using
self.__getitem__.load(x)
but that doesn't work either.
Can someone help me?

How to implement refining conflict in constraint programming

I use Docplex with python 3.7 to implement constraints programming. when it was infeasible, how can i proceed to list constraints those was to source of the conflict?
mdl.export_as_cpo(out="/home/..../MCP3.lp")
msol = mdl.solve(FailLimit=700000, TimeLimit=1600)
DInfos= msol.get_solver_infos()
mconflict=msol.CpoRefineConflictResult()
mconflict.get_all_member_constraints()
Error message:
mconflict=msol.CpoRefineConflictResult()
AttributeError: 'CpoSolveResult' object has no attribute 'CpoRefineConflictResult'
solve returns a SolveResult, and CpoRefineConflictResult is a class in docplex.cp.solution. So, the error message is correct: a SolveResult does not have an attribute CpoRefineConflictResult. You'd expect the CpoRefineConflictResult as the result of the conflict refiner.
You should probably read through the documentation a bit more http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solution.py.html
You can call the .refine_conflict() method on the CpoSolver object to obtain a CpoRefineConflictResult, as documented here http://ibmdecisionoptimization.github.io/docplex-doc/cp/docplex.cp.solver.solver.py.html#detailed-description
Perhaps you can provide a minimal, reproducible example, if you need a more specific solution to your problem. https://stackoverflow.com/help/minimal-reproducible-example
I have add:
from docplex.cp.solver.solver import CpoSolver
After, i have add those lines if the model is infeasible:
mconfl= CpoSolver(model)
mconf = mconfl.refine_conflict()

How to specify the object type so it is recognized prior to running the code in Python?

Suppose I have the following code excerpt:
import pickle
with open('my_object.pkl','r') as f:
object = pickle.load(f)
My question is:
Suppose object is from a class I defined previously, how can I specify this in the code such that my interpreter knows prior to running the code what the object class is ? My goal here is to have the auto-completion of my IDE (I use VSCode) recognize the object so I can auto-complete and easily search the methods and attributes of that object.
It depends on the version of Python and IDE, but in general looks like an additional statement with assertion instance type is the only way so far. This will trigger VS autocomplete settings
import pickle
with open('my_object.pkl','r') as f:
object = pickle.load(f)
assert isinstance(object, YourType)
# and now you can use autocompletion with the object
The following issue is tracking that feature: #82.

Python Mypy attribute error

I have a python3.4 project and I recently decided to use mypy for better understanding.
This chunk of code works but checking with mypy pops out an error :
import zipfile
def zip_to_txt(zip: typing.IO[bytes]) -> BytesIO:
zz = zipfile.ZipFile(zip)
output = BytesIO()
for line, info in enumerate(zz.filelist):
date = "%d-%02d-%02d %02d:%02d:%02d" % info.date_time[:6]
output.write(str.encode("%-46s %s %12d\n" % (info.filename, date, info.file_size)))
output.seek(0, 0)
return output
The error :
PyPreviewGenerator/file_converter.py:170: error: "ZipFile" has no attribute "filelist" (corresponds to this line : for line, info in enumerate(zz.filelist):)
But when I look inside the ZipFile class, I can clearly see that the attribute exists. 
So why does the error occurs ? and is there a way I can resolve it ?
In short, the reason is because the filelist attribute is not documented within Typeshed, the collection of type stubs for the stdlib/various 3rd party libraries. You can see this for yourself here.
Why is filelist not included? Well, because it doesn't actually appear to be a documented part of the API. If you search through the document, you'll see filelist is not mentioned anywhere.
Instead, you should call the infolist() method, which returns exactly what you want (see implementation here if you're curious). You'll notice infolist() is indeed listed within typeshed.

geom_dotplot in rpy2?

When I try to use gplot2.geom_dotplot I get the error:
AttributeError: 'module' object has no attribute 'geom_dotplot'
Does this function have a different name in Rpy2? thanks.
The mapping of that function is just missing. This is a bug with rpy2. The fix will in the repository shortly (will be released with version 2.3.4).
In the meantime a workaround can be to add the following to your code.:
from rpy2.robjects.lib import ggplot2
class GeomDotplot(ggplot2.Geom):
_constructor = ggplot2.ggplot2_env['geom_dotplot']
ggplot2.geom_dotplot = GeomDotplot.new

Categories