|
Technical Interview Questions
C++ Interview Questions
Php Interview Questions
Xml
Interview Questions
C Interview Questions
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Python Interview Questions and Answers
How do I make python scripts executable?
On Windows 2000, the standard Python installer already
associates the .py extension with a file type (Python.File)
and gives that file type an open command that runs the
interpreter (D:\Program Files\Python\python.exe "%1"
%*). This is enough to make scripts executable from the
command prompt as 'foo.py'. If you'd rather be able to
execute the script by simple typing 'foo' with no
extension you need to add .py to the PATHEXT environment
variable.
On Windows NT, the steps taken by the installer as
described above allow you to run a script with 'foo.py',
but a longtime bug in the NT command processor prevents
you from redirecting the input or output of any script
executed in this way. This is often important.
The incantation for making a Python script executable
under WinNT is to give the file an extension of .cmd and
add the following as the first line:
@setlocal enableextensions & python -x %~f0 %* & goto :EOF
How do I debug an extension?
When using GDB with dynamically loaded extensions, you
can't set a breakpoint in your extension until your
extension is loaded.
In your .gdbinit file (or interactively), add the
command:
br _PyImport_LoadDynamicModule
Then, when you run GDB:
$ gdb /local/bin/python
gdb) run myscript.py
gdb) continue # repeat until your extension is loaded
gdb) finish # so that your extension is loaded
gdb) br myfunction.c:50
gdb) continue
Where is Freeze for Windows?
"Freeze" is a program that allows you to ship a Python
program as a single stand-alone executable file. It is
not a compiler; your programs don't run any faster, but
they are more easily distributable, at least to
platforms with the same OS and CPU.
Is a *.pyd file the same as a DLL?
Yes, .
How can I embed Python into a Windows application?
Embedding the Python interpreter in a Windows app can be
summarized as follows:
1. Do _not_ build Python into your .exe file directly.
On Windows, Python must be a DLL to handle importing
modules that are themselves DLL's. (This is the first
key undocumented fact.) Instead, link to pythonNN.dll;
it is typically installed in C:\Windows\System. NN is
the Python version, a number such as "23" for Python
2.3.
You can link to Python statically or dynamically.
Linking statically means linking against pythonNN.lib,
while dynamically linking means linking against
pythonNN.dll. The drawback to dynamic linking is that
your app won't run if pythonNN.dll does not exist on
your system. (General note: pythonNN.lib is the
so-called "import lib" corresponding to python.dll. It
merely defines symbols for the linker.)
Linking dynamically greatly simplifies link options;
everything happens at run time. Your code must load
pythonNN.dll using the Windows LoadLibraryEx() routine.
The code must also use access routines and data in
pythonNN.dll (that is, Python's C API's) using pointers
obtained by the Windows GetProcAddress() routine. Macros
can make using these pointers transparent to any C code
that calls routines in Python's C API.
Borland note: convert pythonNN.lib to OMF format using
Coff2Omf.exe first.
2. If you use SWIG, it is easy to create a Python
"extension module" that will make the app's data and
methods available to Python. SWIG will handle just about
all the grungy details for you. The result is C code
that you link into your .exe file (!) You do _not_ have
to create a DLL file, and this also simplifies linking.
3. SWIG will create an init function (a C function)
whose name depends on the name of the extension module.
For example, if the name of the module is leo, the init
function will be called initleo(). If you use SWIG
shadow classes, as you should, the init function will be
called initleoc(). This initializes a mostly hidden
helper class used by the shadow class.
The reason you can link the C code in step 2 into your
.exe file is that calling the initialization function is
equivalent to importing the module into Python! (This is
the second key undocumented fact.)
4. In short, you can use the following code to
initialize the Python interpreter with your extension
module.
#include "python.h"
...
Py_Initialize(); // Initialize Python.
initmyAppc(); // Initialize (import) the helper class.
PyRun_SimpleString("import myApp") ; // Import the
shadow class.
5. There are two problems with Python's C API which will
become apparent if you use a compiler other than MSVC,
the compiler used to build pythonNN.dll.
Problem 1: The so-called "Very High Level" functions
that take FILE * arguments will not work in a
multi-compiler environment because each compiler's
notion of a struct FILE will be different. From an
implementation standpoint these are very _low_ level
functions.
Problem 2: SWIG generates the following code when
generating wrappers to void functions:
Py_INCREF(Py_None);
_resultobj = Py_None;
return _resultobj;
Alas, Py_None is a macro that expands to a reference to
a complex data structure called _Py_NoneStruct inside
pythonNN.dll. Again, this code will fail in a mult-compiler
environment. Replace such code by:
return Py_BuildValue("");
It may be possible to use SWIG's %typemap command to
make the change automatically, though I have not been
able to get this to work (I'm a complete SWIG newbie).
6. Using a Python shell script to put up a Python
interpreter window from inside your Windows app is not a
good idea; the resulting window will be independent of
your app's windowing system. Rather, you (or the
wxPythonWindow class) should create a "native"
interpreter window. It is easy to connect that window to
the Python interpreter. You can redirect Python's i/o to
_any_ object that supports read and write, so all you
need is a Python object (defined in your extension
module) that contains read() and write() methods.
Page Numbers : 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|