confirmation = property(_get_confirmation, _set_confirmation)
confirmation.short_description = "Confirmation"
When I try the above I get an Exception I don't quite understand:
AttributeError: 'property' object has no attribute 'short_description'
This was an answer to another question on here but I couldn't comment on it as I don't have enough points or something. :-(
In other tests I've also got this error under similar circumstances:
TypeError: 'property' object has only read-only attributes (assign to .short_description)
Any ideas anybody?
The result of property() is an object where you can't add new fields or methods. It's immutable which is why you get the error.
One way to achieve what you want is with using four arguments to property():
confirmation = property(_get_confirmation, _set_confirmation, None, "Confirmation.")
or put the explanation into the docstring of _get_confirmation.
(see docs here, also supported in Python 2)
[EDIT] As for the answer, you refer to: I think the indentation of the example was completely wrong when you looked at it. This has been fixed, now.
Related
When looking at the documentation of the pandas method .hist(), it is written that it returns a matplotlib.AxesSubplot or numpy.ndarray. However when trying to type hint with hist: matplotlib.AxesSubplot = df.hist() it doesn't work (cf. error message in the title of the post).
When printing type() Python returns: Out[1]: matplotlib.axes._subplots.AxesSubplot. Should I type hint my variable hist with this (matplotlib.axes._subplots.AxesSubplot)?
Lovely question, made me dive into some new stuff I did not know. Here is my understanding of it:
It is not actually a class but a dynamic class created by a class factory, checking the mro (like type(df.hist()).mro()) of it you can see the whole inheritance.
Looking at the inheritance of AxesSubplot we see SubplotBase and Axes, so it inherits from both of these but essentially it is an Axes in a Subplot. Based on this I would have gone for matplotlib.axes._axes.Axes for type hinting.
Here is a good discussing that I derived these results from:
https://github.com/matplotlib/matplotlib/issues/18222
When the python help function is invoked with an argument of string type, it is interpreted by pydoc.Helper.help as a request for information on the topic, symbol, keyword or module identified by the value of the string. For other arguments, help on the object itself is provided, unless the object is an instance of a subclass of str. In this latter case, the pydoc.resolve function looks for a module with a name matching the value of the object and raises an exception if none is found.
To illustrate this, consider the example code:
class Extra(object):
def NewMethod(): return 'New'
Cls1 = type( 'FirstClass', (str,Extra), {'__doc__':'My new class','extra':'An extra attribute'})
inst1 = Cls1('METHODS')
help( 'METHODS' )
help( inst1 )
The first invocation of help produces information on the topic "METHODS", the 2nd produces an error message because the pydoc.resolve function is trying to find a module called "METHODS".
This means that it is difficult to provide effective documentation for user defined sub-classes of str. Would it not be possible for pydoc.resolve to use a test on the type of the object, as is done in pydoc.Helper.help, and allow instances of user defined sub-classes to be treated as other class instances?
This question follows from earlier discussion of a related question here.
The simple answer is that making user-defined subclasses of str is not the most common case—partly because the user-defined data but not the string data would be mutable. By the time you have to deal with such, it’s imagined that you know how to write help(type(x)), and using isinstance rather than type(…) is … is the correct default in general. (The other way, you’d have to use help(str(x)) if you wanted to use it, like any other string, to select a help topic, but that’s surely even rarer.)
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()
I'm using Python to work with networkx and draw some graphs.
I ran into a problem raising:
TypeError: 'dict' object is not callable
on this line of code:
set_node_color(num, list(Graph.node()))
I searched to find that this error is raised when I'm using a variable name dict.
The problem is, I'm not using any variables with the name dict, nor am I using any dictionary types anywhere in the code.
In case it's necessary, printing the type of Graph gives <class 'networkx.classes.digraph.Digraph'>.
I also tried printing the type for Graph.node() only to receive the same error, telling me 'dict' object is not callable.
So I suspect Graph.node() to be a dict type variable, but using (Graph.node()).items() raises the same TypeError.
Any help or advices would be nice. Thanks.
Maybe Graph.node is a dict object, so Graph.node() is not callable.
I'm having some trouble getting mypy to accept type objects. I am
convinced I'm just doing it wrong but my google searches have not led me to any answers so far.
class Good(object):
a = 1
def works(thing: Good):
print(thing.a)
o = Good()
works(o)
Bad = type('Bad', (object, ), dict(a=1))
def fails_mypy(thing: Bad):
print(thing.a)
s = Bad()
fails_mypy(s)
Things constructed like 'Good' are ok, while things constructed like 'Bad' fail mypy checks with:
error: Invalid type "test.Bad"
error: Bad? has no attribute "a"
Based on the Unsupported Python Features section of mypys wiki, runtime creation of classes like this isn't currently supported. It cannot understand what Bad is in your function definition. Using reveal_type(Good) and reveal_type(Bad) when executing mypy should make this clear.
An approach to silence these is by using Any. Either using Python 3.6 variable annotation syntax:
Bad: Any = type('Bad', (), {'a':1})
or, with Python < 3.6:
Bad = type('Bad', (), {'a':1}) # type: Any
(in both cases Any should first be imported from typing)
of course, this basically means that your function now accepts anything. A price to pay, but that's what you get with dynamic languages; since Bar is defined at runtime, it can theoretically be anything :-)