Why this below exec command is not working - python

Why these below statements are giving error
>>> exec("x={}".format('b'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'b' is not defined
I need the result to be
x='b'

You should provide another pair of quotes
>>> exec("x={}".format("'b'"))
>>> x
'b'
Why?
When you write
exec("x={}".format('b'))
you are trying to write
x=b
obviously python doesn't know what b is unless you have defined it before.
Where as when you write
exec("x={}".format("'b'"))
It is same as
x='b'

Related

Modules imported using `exec` are not available if imported by a function

In Python 3, when I import a module using exec in the global scope, it works. But when I do it within a function, even though I get no import error, Python does not recognize the module name.
Importing and using sys successfully, in the global scope:
>>> sys.argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> exec('import sys')
>>> sys.argv
['']
No import error, though cannot use os which has been imported from a function:
>>> def import_os():
... exec('import os')
... os.listdir('.')
...
>>> import_os()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in import_os
NameError: name 'os' is not defined
>>>
Any idea how to make this work from within a function?

GDB Python resolve overloaded method

How do I look up overloaded methods in GDB using the python interface?
I have a class which has several methods called 'el', one of which takes two ints. GDB is stopped at a breakpoint, with a member variable called _Dr in scope in the inferior process. I do this to get a Python gdb.Value object representing _Dr:
(gdb) python _Dr = gdb.parse_and_eval('_Dr')
Now I want to get the el(int,int) method:
(gdb) python el = _Dr['el']
Traceback (most recent call last):
File "<string>", line 1, in <module>
gdb.error: cannot resolve overloaded method `el': no arguments supplied
Error while executing Python code.
How do I tell it the types of the arguments to resolve the overload?
I've tried this:
(gdb) python el = _Dr['el(int,int)']
Traceback (most recent call last):
File "<string>", line 1, in <module>
gdb.error: There is no member or method named el(int,int).
Error while executing Python code.
and this:
(gdb) python el = _Dr['el', 'int', 'int']
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: Could not convert Python object: ('el', 'int', 'int').
Error while executing Python code.
and this:
(gdb) python el = _Dr['el(1,1)']
Traceback (most recent call last):
File "<string>", line 1, in <module>
gdb.error: There is no member or method named el(1,1).
Error while executing Python code.
What's the right way of doing this?
The best way to do this is to iterate over the fields of the type, looking for the one you want.
Something like:
for field in _Dr.type.fields():
if field.name == 'el':
... check field.type here ...
See the node "Types in Python" in the gdb manual for more details.

how to use python help to file functions

If I want to see the str.replace() function: help(str.replace)
the result is:
Help on method_descriptor:
replace(...)
S.replace(old, new[, count]) -> str
Return a copy of S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
(END)
but how use help file.read or readlines?
for example, help(file.read) and help(read) are both errors:
>>> help(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(file.read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> help(read)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'read' is not defined
How can I use help see file functions?
The file type has been removed from Python 3. Look at the io module instead:
>>> import io
>>> help(io.TextIOBase.read)
Help on method_descriptor:
read(...)
Read at most n characters from stream.
Read from underlying buffer until we have n characters or we hit EOF.
If n is negative or omitted, read until EOF.

Pydoc not working in Python 3.3 in Windows

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.

"argument 1 has unexpected type 'str'"

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")

Categories