|
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
Why don't my signal handlers work?
The most common problem is that the signal handler is
declared with the wrong argument list. It is called as
handler(signum, frame)
so it should be declared with two arguments:
def handler(signum, frame):
...
How do I test a Python program or component?
Python comes with two testing frameworks. The doctest
module finds examples in the docstrings for a module and
runs them, comparing the output with the expected output
given in the docstring.
The unittest module is a fancier testing framework
modelled on Java and Smalltalk testing frameworks.
For testing, it helps to write the program so that it
may be easily tested by using good modular design. Your
program should have almost all functionality
encapsulated in either functions or class methods -- and
this sometimes has the surprising and delightful effect
of making the program run faster (because local variable
accesses are faster than global accesses). Furthermore
the program should avoid depending on mutating global
variables, since this makes testing much more difficult
to do.
The "global main logic" of your program may be as simple
as
if __name__=="__main__":
main_logic()
at the bottom of the main module of your program.
Once your program is organized as a tractable collection
of functions and class behaviours you should write test
functions that exercise the behaviours. A test suite can
be associated with each module which automates a
sequence of tests. This sounds like a lot of work, but
since Python is so terse and flexible it's surprisingly
easy. You can make coding much more pleasant and fun by
writing your test functions in parallel with the
"production code", since this makes it easy to find bugs
and even design flaws earlier.
"Support modules" that are not intended to be the main
module of a program may include a self-test of the
module.
if __name__ == "__main__":
self_test()
Even programs that interact with complex external
interfaces may be tested when the external interfaces
are unavailable by using "fake" interfaces implemented
in Python.
None of my threads seem to run: why?
As soon as the main thread exits, all threads are
killed. Your main thread is running too quickly, giving
the threads no time to do any work.
A simple fix is to add a sleep to the end of the program
that's long enough for all the threads to finish:
import threading, time
def thread_task(name, n):
for i in range(n): print name, i
for i in range(10):
T = threading.Thread(target=thread_task, args=(str(i), i))
T.start()
time.sleep(10) # <----------------------------!
But now (on many platforms) the threads don't run in
parallel, but appear to run sequentially, one at a time!
The reason is that the OS thread scheduler doesn't start
a new thread until the previous thread is blocked.
A simple fix is to add a tiny sleep to the start of the
run function:
def thread_task(name, n):
time.sleep(0.001) # <---------------------!
for i in range(n): print name, i
for i in range(10):
T = threading.Thread(target=thread_task, args=(str(i), i))
T.start()
time.sleep(10)
Instead of trying to guess how long a time.sleep() delay
will be enough, it's better to use some kind of
semaphore mechanism. One idea is to use the Queue module
to create a queue object, let each thread append a token
to the queue when it finishes, and let the main thread
read as many tokens from the queue as there are threads.
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
|