I am using ParaView-5.4.1 under Ubuntu 16.04LTS.
I have an object r of class PVDReader (in pvpython, but this is likely irrelevant).
For the method TimestepValues of that class
>>> callable(r.TimestepValues)
True
>>> type(r.TimestepValues)
<class 'paraview.servermanager.VectorProperty'>
>>> r.TimestepValues
[0.0, 0.002]
>>> r.TimestepValues()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/site-packages/paraview/servermanager.py", line 681, in __call__
raise RuntimeError ("Cannot invoke this property")
RuntimeError: Cannot invoke this property
>>> import inspect
>>> inspect.getargspec(r.TimestepValues)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/santiago/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/inspect.py", line 816, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: [0.0, 0.002] is not a Python function
So, on one hand, it is callable.
On the other hand, it behaves as if it were not.
How can this be understood?
Is this a bug?
tl;dr PS: Other more generic methods behave similarly (but not the same). E.g., compare with __class__:
>>> callable(r.__class__)
True
>>> type(fluid_pr_reader.__class__)
<type 'type'> <-- Rather obvious, just for completitude of the comparison with above
>>> r.__class__
<class 'paraview.servermanager.PVDReader'>
>>> r.__class__()
<paraview.servermanager.PVDReader object at 0x7f57ed56fd10> <-- No error here
>>> inspect.getargspec(r.__class__)
Traceback (most recent call last): <-- Same error here
File "<stdin>", line 1, in <module>
File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/inspect.py", line 816, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <class 'paraview.servermanager.PVDReader'> is not a Python function
EDIT: The result is the same if I do not access it as a straight property prior to accessing it as a callable
>>> callable(r.TimestepValues)
True
>>> r.TimestepValues()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/res/apps/ParaView-5.4.1-Qt5-OpenGL2-MPI-Linux-64bit/lib/python2.7/site-packages/paraview/servermanager.py", line 681, in __call__
raise RuntimeError ("Cannot invoke this property")
RuntimeError: Cannot invoke this property
Related
The package addict allows you to use dicts through attribute setting:
Example from the website:
from addict import Dict
body = Dict()
body.query.filtered.query.match.description = 'addictive'
body.query.filtered.filter.term.created_by = 'Mats'
Now when when I use for example a = body.B and I haven't included B yet it does not throw an error, but just returns nothing. How can I make it throw an error when the attribute was net yet set?
addict.Dict implements the __missing__ method to generate a value for any missing key(/attribute); the current implementation generates a new Dict instance. If you don't want this behaviour, you'll have to override it:
class MyDict(Dict):
def __missing__(self, name):
raise KeyError(name)
Note that this will throw a KeyError for attribute access, which may be confusing; you could also override __getattr__ if you want to throw an AttributeError instead. In use:
>>> body = MyDict()
>>> body.B
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "path/to/addict.py", line 62, in __getattr__
return self.__getitem__(item)
File "<stdin>", line 3, in __missing__
KeyError: 'B'
>>> body.B = "hello"
>>> body.B
'hello'
Note also that this will break the other examples you showed as well, as e.g. foo.bar.baz = qux calls __getattr__ on foo before calling __setattr__ on foo.bar (if successful):
>>> body.query.filtered.query.match.description = 'addictive'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "path/to/addict.py", line 62, in __getattr__
return self.__getitem__(item)
File "<stdin>", line 3, in __missing__
KeyError: 'query'
The following appears to have the same effect:
>>> raise NotImplementedError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError
>>> raise NotImplementedError()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError
Is there a difference, and if so, what are the pros and cons of each?
There is essentially no difference if the exception class requires no arguments for initialization:
If an exception class is passed, it will be implicitly instantiated by
calling its constructor with no arguments.
[Emphasis mine]
Otherwise, you'll get another exception complaining about the initialization of the instance:
class MyException(Exception):
def __init__(self, arg):
pass
raise MyException
Traceback (most recent call last):
File "python", line 6, in <module>
TypeError: __init__() takes exactly 2 arguments (1 given)
Apparently, you can do more by passing custom arguments to custom exception classes, or passing a custom message to a builtin exception class:
>>> raise ValueError('number must be 42')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: number must be 42
The above is much more informative (quite desirable from a users' perspective) than a barren ValueError.
There is no difference:
>>> try:
... raise NotImplementedError
... except Exception as e:
... pass
...
>>> e
NotImplementedError()
>>> type(e)
<type 'exceptions.NotImplementedError'>
>>> type(NotImplementedError)
<type 'type'>
>>> type(NotImplementedError())
<type 'exceptions.NotImplementedError'>
>>>
See section 8.4 of the docs: https://docs.python.org/3/tutorial/errors.html#raising-exceptions
I am trying to generate the help text at runtime and i am not able to use the pydoc command in Windows. When i type
>>> pydoc(atexit)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
I have already set up the environment variables for pydoc.py file. C:\Python33\Lib\pydoc.py.
This also not works like it works for >>help('atexit')
>>> pydoc('atexit')
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'pydoc' is not defined
Whats the possible reason for it.
Updates:
>>> import pydoc
>>> pydoc(sys)
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
>>> pydoc('sys')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'module' object is not callable
Like any library in Python, you need to import it before you can use it.
Edit What exactly are you trying to achieve? Modules are indeed not callable. pydoc.help is the function you want, although I don't really know why you need it, since as you note the standalone help function does the same thing already.
_eq seems to be the equal to self.assertEqual()
But is there also a self.assertNotEqual() in nose?
Thanks
Nose doesn't have an equivalent to self.assertNotEqual(), but you can use all of the unittest.TestCase assert methods via nose.tools. They are renamed to all-lowercase and with underscores. For example:
>>> from nose.tools import assert_not_equal
>>> assert_not_equal(1, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py", line 518, in assertNotEqual
raise self.failureException(msg)
AssertionError: 1 == 1
More info here.
I'm trying to use PyKDE, PyKDE.kdecore.KStandardDirs to be precise. This method is called with two strings according to the documentation and according to the PyQt4 documentation, I can use standard Python strs instead of QString.
This doesn't work:
>> KStandardDirs.locate()("socket", "foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: KStandardDirs.locate(): not enough arguments
>>> KStandardDirs.locate("socket", "foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: KStandardDirs.locate(): argument 1 has unexpected type 'str'
I can't use QString either because it doesn't seem to exist:
>>> from PyQt4.QtCore import QString
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name QString
>>> from PyQt4.QtCore import *
>>> QString
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'QString' is not defined
What am I doing wrong?
I suspect that PyKDE is not yet Python 3 ready, at least as far as that error message is concerned; try passing in a bytestring instead:
KStandardDirs.locate(b"socket", "foo")