ValueError: No object matches name: in Maya Python - python

I have a code that returns an error of
ValueError: No object matches name: s
I'm not sure why it is looking for the object s.
The code is as follows
import maya.cmds as cmds
def createOffsetGrp(objSel):
for obj in objSel:
p = cmds.listRelatives(obj,parent=True)
print (p)
createOffsetGrp('spine02_jnt')
By expectation, the print command should spit out Spine01_jnt which is the parent of the Spine02_jnt
Is there something I missed?

Thanks to duck typing in Python, sometimes such errors can be hard to catch. What is happening here is your function is expecting an array as argument, but you are passing a string.
Python supports iterating over a string as well, by listing out individual characters, which is why it is looking for the s in spine02_jnt. Passing your string within an array should solve your problem:
createOffsetGrp(['spine02_jnt'])

in addition of what crazyGamer, you can provide some support on string like this :
import maya.cmds as cmds
def createOffsetGrp(objSel):
# isinstance is used to check the type of the variable :
# i.e: isinstance(objSel, int)
# basestring is a type combining unicode and string types
if isinstance(objSel, basestring):
objSel = [objSel]
for obj in objSel:
p = cmds.listRelatives(obj,parent=True)
print (p)
createOffsetGrp('spine02_jnt')

Related

Python dataclass evaluating type-hinted Dict fields [duplicate]

I'm trying to generate some JavaScript based on the type annotations I have provided in on some Python functions by using the signature() function in the inspect module.
This part works as I expect when the type is a simple builtin class:
import inspect
def my_function() -> dict:
pass
signature = inspect.signature(my_function)
signature.return_annotation is dict # True
Though I'm not sure how to unwrap and inspect more complex annotations e.g:
from typing import List
import inspect
def my_function() -> List[int]:
pass
signature = inspect.signature(my_function)
signature.return_annotation is List[int] # False
Again similar problem with forward referencing a custom class:
def my_function() -> List['User']:
pass
...
signature.return_annotation # typing.List[_ForwardRef('User')]
What I'm looking to get out is something like this - so I can branch appropriately while generating the JavaScript:
type = signature.return_annotation... # list
member_type = signature.return_annotation... # int / 'User'
Thanks.
Python 3.8 provides typing.get_origin() and typing.get_args() for this!
assert get_origin(Dict[str, int]) is dict
assert get_args(Dict[int, str]) == (int, str)
assert get_origin(Union[int, str]) is Union
assert get_args(Union[int, str]) == (int, str)
See https://docs.python.org/3/library/typing.html#typing.get_origin
List is not a map of types to GenericMeta, despite the syntax. Each access to it generates a new instance:
>>> [ id(List[str]) for i in range(3) ]
[33105112, 33106872, 33046936]
This means that even List[int] is not List[int]. To compare two instances, you have multiple options:
Use ==, i.e., signature.return_annotation == List[int].
Store an instance of your type in a global variable and check against that, i.e.,
a = List[int]
def foo() -> a:
pass
inspect.signature(foo).return_annotation is a
Use issubclass. The typing module defines that. Note that this might do more than you'd like, make sure to read the _TypeAlias documentation if you use this.
Check against List only and read the contents yourself. Though the property is internal, it is unlikely that the implementation will change soon: List[int].__args__[0] contains the type argument starting from Python 3.5.2, and in earlier versions, its List[int].__parameters__[0].
If you'd like to write generic code for your exporter, then the last option is probably best. If you only need to cover a specific use case, I'd personally go with using ==.
Take note, this applies to Python 3.5.1
For Python 3.5.2 take a look at phillip's answer.
You shouldn't be checking with the identity operator as Phillip stated, use equality to get this right.
To check if a hint is a subclass of a list you could use issubclass checks (even though you should take note that this can be quirky in certain cases and is currently worked on):
issubclass(List[int], list) # True
To get the members of a type hint you generally have two watch out for the cases involved.
If it has a simple type, as in List[int] the value of the argument is located in the __parameters__ value:
signature.return_annotation.__parameters__[0] # int
Now, in more complex scenarios i.e a class supplied as an argument with List[User] you must again extract the __parameter__[0] and then get the __forward_arg__. This is because Python wraps the argument in a special ForwardRef class:
d = signature.return_annotation.__parameter__[0]
d.__forward_arg__ # 'User'
Take note, you don't need to actually use inspect here, typing has a helper function named get_type_hints that returns the type hints as a dictionary (it uses the function objects __annotations__ attribute).

TypeError: sequence item 0: expected str instance, int found? [duplicate]

I have some code like:
def example(parameter):
global str
str = str(parameter)
print(str)
example(1)
example(2)
The first call to example works, but then the second time around I get an error like:
Traceback (most recent call last):
File "test.py", line 7, in <module>
example(2)
File "test.py", line 3, in example
str = str(parameter)
TypeError: 'str' object is not callable
Why does this happen, and how can I fix it?
If you are in an interactive session and encountered a problem like this, and you want to fix the problem without restarting the interpreter, see How to restore a builtin that I overwrote by accident?.
Where the code says:
global str
str = str(parameter)
You are redefining what str() means. str is the built-in Python name of the string type, and you don't want to change it.
Use a different name for the local variable, and remove the global statement.
Note that if you used code like this at the Python REPL, then the assignment to the global str will persist until you do something about it. You can restart the interpreter, or del str. The latter works because str is not actually a defined global variable by default - instead, it's normally found in a fallback (the builtins standard library module, which is specially imported at startup and given the global name __builtins__).
While not in your code, another hard-to-spot error is when the % character is missing in an attempt of string formatting:
"foo %s bar %s coffee"("blah","asdf")
but it should be:
"foo %s bar %s coffee"%("blah","asdf")
The missing % would result in the same TypeError: 'str' object is not callable.
In my case I had a class that had a method and a string property of the same name, I was trying to call the method but was getting the string property.
Note that TypeError: 'str' object is not callable means only that there is an attempt to call (i.e., use function-call syntax) a string (i.e., any name that previously had a string assigned to it). Using any other built-in method as variable name can cause the exact same error message.
You can get this error if you have variable str and trying to call str() function.
Whenever that happens, just issue the following ( it was also posted above)
>>> del str
That should fix it.
Another case of this: Messing with the __repr__ function of an object where a format() call fails non-transparently.
In our case, we used a #property decorator on the __repr__ and passed that object to a format(). The #property decorator causes the __repr__ object to be turned into a string, which then results in the str object is not callable error.
Check your input parameters, and make sure you don't have one named type. If so then you will have a clash and get this error.
str = 'Hello World String'
print(str(10)+' Good day!!')
Even I faced this issue with the above code as we are shadowing str() function.
Solution is:
string1 = 'Hello World String'
print(str(10)+' Good day!!')
I had the same error. In my case wasn't because of a variable named str. But because I named a function with a str parameter and the variable the same.
same_name = same_name(var_name: str)
I run it in a loop. The first time it run ok. The second time I got this error. Renaming the variable to a name different from the function name fixed this. So I think it's because Python once associate a function name in a scope, the second time tries to associate the left part (same_name =) as a call to the function and detects that the str parameter is not present, so it's missing, then it throws that error.
This error can also occur as a result of trying to call a property (as though it were a function):
class Example:
#property
def value():
return 'test'
e = Example()
print(e.value()) # should just be `e.value` to get the string
This problem can be caused by code like:
"Foo" ("Bar" if bar else "Baz")
You can concatenate string literals by putting them next to each other, like "Foo" "Bar". However, because of the open parenthesis, the code was interpreted as an attempt to call the string "Foo" as if it were a function.
it could be also you are trying to index in the wrong way:
a = 'apple'
a(3) ===> 'str' object is not callable
a[3] = l
it is recommended not to use str int list etc.. as variable names, even though python will allow it.
this is because it might create such accidents when trying to access reserved keywords that are named the same
This error could also occur with code like:
class Shape:
def __init__(self, colour):
self.colour = colour
def colour(self):
print("colour:", self.colour)
myShape = Shape("pink")
myShape.colour()
In the __init__ method, we assign an attribute colour, which has the same name as the method colour. When we later attempt to call the method, the instance's attribute is looked up instead. myShape.colour is the string "pink", which is not callable.
To fix this, change either the method name or the variable name.
I also got this error.
For me it was just a typo:
I wrote:
driver.find_element_by_id("swal2-content").text()
while it should have been:
driver.find_element_by_id("swal2-content").text
In my case, I had a Class with a method in it. The method did not have 'self' as the first parameter and the error was being thrown when I made a call to the method. Once I added 'self,' to the method's parameter list, it was fine.
FWIW I just hit this on a slightly different use case. I scoured and scoured my code looking for where I might've used a 'str' variable, but could not find it. I started to suspect that maybe one of the modules I imported was the culprit... but alas, it was a missing '%' character in a formatted print statement.
Here's an example:
x=5
y=6
print("x as a string is: %s. y as a string is: %s" (str(x) , str(y)) )
This will result in the output:
TypeError: 'str' object is not callable
The correction is:
x=5
y=6
print("x as a string is: %s. y as a string is: %s" % (str(x) , str(y)) )
Resulting in our expected output:
x as a string is: 5. y as a string is: 6
It also give same error if math library not imported,
import math
I realize this is not a runtime warning, but PyCharm gave me this similarly-worded IDE warning:
if hasattr(w, 'to_json'):
return w.to_json()
# warning, 'str' object is not callable
This was because the IDE assumed w.to_json was a string. The solution was to add a callable() check:
if hasattr(w, 'to_json') and callable(w.to_json):
return w.to_json()
Then the warning went away. This same check may also prevent the runtime exception in the original question.

How to get the docstring of a function into a variable?

None of these commands will retrieve the docstring of a function and assign it to a variable. How can it be achieved?
I attempted various things. One of which is the help function, but it seems to activate an entire program as opposed to returning a string. I have tried various commands but none of them work to retrieve the docstring.
import PIL
PILCommands=dir('PIL')
ListA=[]
ListB=[]
ListC=[]
ListD=[]
ListE=[]
LisfF=[]
ListG=[]
ListH=[]
for x in PILCommands:
print(x)
try:
ListA.append(x.__doc__)
except:
pass
try:
ListB.append(x.__doc__())
except:
pass
try:
ListC.append(str(x))
except:
pass
try:
ListD.append(help(x))
except:
pass
try:
ListE.append(eval("x.__doc__"))
except:
pass
try:
ListF.append(eval("inspect.getdoc(x)"))
except:
pass
try:
ListG.append(eval("dir(x)"))
except:
pass
try:
ListH.append(eval("PIL.x.__doc__"))
except:
pass
print
print("Command1: x.__doc__")
print(ListA)
print
print("Command1: x.__doc__()")
print(ListB)
print
print("Command1: str(x)")
print(ListC)
print
print("help(x)")
print(ListD)
print
print('Command1: eval("eval("x.__doc__")')
print(ListE)
print
print('Command1: eval("inspect.getdoc(x)")')
print(ListE)
print
print('Command1: eval("dir(x)")')
print(ListG)
print
print('Command1: eval("PIL.x.__doc__")')
print(ListG)
Answer :
python << EOF
import inspect
import PIL
doc = inspect.getdoc(PIL)
print doc
print type(doc)
EOF
So it has no documentation .
You can use inspect.getdoc, that will process the docstring of the object and return it as string:
>>> import inspect
>>> doc = inspect.getdoc(I)
>>> print(doc)
This is the documentation string (docstring) of this function .
It contains an explanation and instruction for use .
Generally the documentation is stored in the __doc__ attribute:
>>> doc = I.__doc__
>>> print(doc)
This is the documentation string (docstring) of this function .
It contains an explanation and instruction for use .
But the __doc__ won't be cleaned: it might contain leading and trailing empty newlines and the indentation may not be consistent. So inspect.getdoc should be the preferred option.
The following is based on your original question:
To get the documentation of PIL functions you could use:
>>> import PIL
>>> import inspect
>>> doc = inspect.getdoc(PIL.Image.fromarray)
>>> print(doc)
Creates an image memory from an object exporting the array interface
(using the buffer protocol).
If obj is not contiguous, then the tobytes method is called
and :py:func:`~PIL.Image.frombuffer` is used.
:param obj: Object with array interface
:param mode: Mode to use (will be determined from type if None)
See: :ref:`concept-modes`.
:returns: An image object.
.. versionadded:: 1.1.6
To get the documentations of all functions in a module you need to use getattr:
for name in dir(PIL.Image):
docstring = inspect.getdoc(getattr(PIL.Image, name)) # like this
To get a list of all docstrings:
list_of_docs = [inspect.getdoc(getattr(PIL, obj)) for obj in dir(PIL)]
Or if you need to corresponding name then a dict would be better:
list_of_docs = {obj: inspect.getdoc(getattr(PIL, obj)) for obj in dir(PIL)}
However not everything actually has a documentation. For example the PIL.Image module has no docstring:
>>> PIL.Image.__doc__
None
>>> inspect.getdoc(PIL.Image)
None
and when attempting to get the docstring of an instance you might get surprising results:
>>> inspect.getdoc(PIL.__version__)
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.
That's because PIL.__version__ is a string instance and simply shows the docstring of its class: str:
>>> inspect.getdoc(str) # the built-in "str"
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str
Create a new string object from the given object. If encoding or
errors is specified, then the object must expose a data buffer
that will be decoded using the given encoding and error handler.
Otherwise, returns the result of object.__str__() (if defined)
or repr(object).
encoding defaults to sys.getdefaultencoding().
errors defaults to 'strict'.

Unpacking Python's Type Annotations

I'm trying to generate some JavaScript based on the type annotations I have provided in on some Python functions by using the signature() function in the inspect module.
This part works as I expect when the type is a simple builtin class:
import inspect
def my_function() -> dict:
pass
signature = inspect.signature(my_function)
signature.return_annotation is dict # True
Though I'm not sure how to unwrap and inspect more complex annotations e.g:
from typing import List
import inspect
def my_function() -> List[int]:
pass
signature = inspect.signature(my_function)
signature.return_annotation is List[int] # False
Again similar problem with forward referencing a custom class:
def my_function() -> List['User']:
pass
...
signature.return_annotation # typing.List[_ForwardRef('User')]
What I'm looking to get out is something like this - so I can branch appropriately while generating the JavaScript:
type = signature.return_annotation... # list
member_type = signature.return_annotation... # int / 'User'
Thanks.
Python 3.8 provides typing.get_origin() and typing.get_args() for this!
assert get_origin(Dict[str, int]) is dict
assert get_args(Dict[int, str]) == (int, str)
assert get_origin(Union[int, str]) is Union
assert get_args(Union[int, str]) == (int, str)
See https://docs.python.org/3/library/typing.html#typing.get_origin
List is not a map of types to GenericMeta, despite the syntax. Each access to it generates a new instance:
>>> [ id(List[str]) for i in range(3) ]
[33105112, 33106872, 33046936]
This means that even List[int] is not List[int]. To compare two instances, you have multiple options:
Use ==, i.e., signature.return_annotation == List[int].
Store an instance of your type in a global variable and check against that, i.e.,
a = List[int]
def foo() -> a:
pass
inspect.signature(foo).return_annotation is a
Use issubclass. The typing module defines that. Note that this might do more than you'd like, make sure to read the _TypeAlias documentation if you use this.
Check against List only and read the contents yourself. Though the property is internal, it is unlikely that the implementation will change soon: List[int].__args__[0] contains the type argument starting from Python 3.5.2, and in earlier versions, its List[int].__parameters__[0].
If you'd like to write generic code for your exporter, then the last option is probably best. If you only need to cover a specific use case, I'd personally go with using ==.
Take note, this applies to Python 3.5.1
For Python 3.5.2 take a look at phillip's answer.
You shouldn't be checking with the identity operator as Phillip stated, use equality to get this right.
To check if a hint is a subclass of a list you could use issubclass checks (even though you should take note that this can be quirky in certain cases and is currently worked on):
issubclass(List[int], list) # True
To get the members of a type hint you generally have two watch out for the cases involved.
If it has a simple type, as in List[int] the value of the argument is located in the __parameters__ value:
signature.return_annotation.__parameters__[0] # int
Now, in more complex scenarios i.e a class supplied as an argument with List[User] you must again extract the __parameter__[0] and then get the __forward_arg__. This is because Python wraps the argument in a special ForwardRef class:
d = signature.return_annotation.__parameter__[0]
d.__forward_arg__ # 'User'
Take note, you don't need to actually use inspect here, typing has a helper function named get_type_hints that returns the type hints as a dictionary (it uses the function objects __annotations__ attribute).

Triple quotation in python

So I understand that if I do the following
print """ Anything I
type in here
works. Multiple LINES woohoo!"""
But what if following is my python script
""" This is my python Script. Just this much """
What does the above thing do? Is it taken as comment? Why is it not a syntax error?
Similarly, if I do
"This is my Python Script. Just this. Even with single quotes."
How are the above two scripts interpreted?
Thanks
The triple quotes ''' or """ are just different ways of representing strings. The advantage of triple quotes is that it can span multiple lines and sometimes serve as docstrings.
The reason:
"hadfasdfas"
doesn't raise any error is because python simply creates the string and then doesn't assign it to anything. For the python interpreter, it is perfectly fine if you have a pointless statement in your code as long as there are no syntax or semantics errors
Hope that helps.
The string is just evaluated, and the interpreter noticing it wasn't assigned to anything, throws it away.
But in some special places, this string is actually assigned to the __doc__ property of the item:
def func(arg):
"""
Does stuff. This string will be evaluated and assigned to func.__doc__.
"""
pass
class Test:
"""
Same for Test.__doc__
"""
pass
At the top of module.py:
"""
module does stuff. this will be assigned to module.__doc__
"""
def func():
...
In addition to #sshashank124 answer I have to add that triple quoted strings are also used in testing https://docs.python.org/2/library/doctest.html
So consider this code snippet:
def some_function(x, y):
"""This function should simply return sum of arguments.
It should throw an error if you pass string as argument
>>> some_function(5, 4)
9
>>> some_function(-5, 4)
-1
>>> some_function("abc", 4)
Traceback (most recent call last):
...
ValueError: arguments must numbers
"""
if type(x, str) or type(y, str):
raise ValueError("arguments must numbers")
else:
return x + y
if __name__ == "__main__":
import doctest
doctest.testmod()
If you import this tiny module, you'll get the some_function function.
But if you invoke this script directly from shell, tests given in the triple quoted string will be evaluated and the report will be printed to the output.
So triple quoted strings can be treated as values of type string, as comment, as docstrings and as containers for unittests.

Categories