|
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 parcel out work among a bunch of worker
threads?
Use the Queue module to create a queue containing a list
of jobs. The Queue class maintains a list of objects
with .put(obj) to add an item to the queue and .get() to
return an item. The class will take care of the locking
necessary to ensure that each job is handed out exactly
once.
Here's a trivial example:
import threading, Queue, time
# The worker thread gets jobs off the queue. When the
queue is empty, it
# assumes there will be no more work and exits.
# (Realistically workers will run until terminated.)
def worker ():
print 'Running worker'
time.sleep(0.1)
while True:
try:
arg = q.get(block=False)
except Queue.Empty:
print 'Worker', threading.currentThread(),
print 'queue empty'
break
else:
print 'Worker', threading.currentThread(),
print 'running with argument', arg
time.sleep(0.5)
# Create queue
q = Queue.Queue()
# Start a pool of 5 workers
for i in range(5):
t = threading.Thread(target=worker, name='worker %i' %
(i+1))
t.start()
# Begin adding work to the queue
for i in range(50):
q.put(i)
# Give threads time to run
print 'Main thread sleeping'
time.sleep(5)
When run, this will produce the following output:
Running worker Running worker Running worker Running
worker Running worker Main thread sleeping Worker <Thread(worker
1, started)> running with argument 0 Worker <Thread(worker
2, started)> running with argument 1 Worker <Thread(worker
3, started)> running with argument 2 Worker <Thread(worker
4, started)> running with argument 3 Worker <Thread(worker
5, started)> running with argument 4 Worker <Thread(worker
1, started)> running with argument 5 ...
How do I delete a file? (And other file questions...)
Use os.remove(filename) or os.unlink(filename);
How do I copy a file?
The shutil module contains a copyfile() function.
How do I read (or write) binary data?
or complex data formats, it's best to use the struct
module. It allows you to take a string containing binary
data (usually numbers) and convert it to Python objects;
and vice versa.
For example, the following code reads two 2-byte
integers and one 4-byte integer in big-endian format
from a file:
import struct
f = open(filename, "rb") # Open in binary mode for
portability
s = f.read(8)
x, y, z = struct.unpack(">hhl", s)
The '>' in the format string forces big-endian data; the
letter 'h' reads one "short integer" (2 bytes), and 'l'
reads one "long integer" (4 bytes) from the string.
How do I run a subprocess with pipes connected to both
input and output?
Use the popen2 module. For example:
import popen2
fromchild, tochild = popen2.popen2("command")
tochild.write("input\n")
tochild.flush()
output = fromchild.readline()
How can I mimic CGI form submission (METHOD=POST)?
I would like to retrieve web pages that are the result
of POSTing a form. Is there existing code that would let
me do this easily?
Yes. Here's a simple example that uses httplib:
#!/usr/local/bin/python
import httplib, sys, time
### build the query string
qs = "First=Josephine&MI=Q&Last=Public"
### connect and send the server a path
httpobj = httplib.HTTP('www.some-server.out-there', 80)
httpobj.putrequest('POST', '/cgi-bin/some-cgi-script')
### now generate the rest of the HTTP headers...
httpobj.putheader('Accept', '*/*')
httpobj.putheader('Connection', 'Keep-Alive')
httpobj.putheader('Content-type',
'application/x-www-form-urlencoded')
httpobj.putheader('Content-length', '%d' % len(qs))
httpobj.endheaders()
httpobj.send(qs)
### find out what the server said in response...
reply, msg, hdrs = httpobj.getreply()
if reply != 200:
sys.stdout.write(httpobj.getfile().read())
Note that in general for URL-encoded POST operations,
query strings must be quoted by using urllib.quote().
For example to send name="Guy Steele, Jr.":
>>> from urllib import quote
>>> x = quote("Guy Steele, Jr.")
>>> x
'Guy%20Steele,%20Jr.'
>>> query_string = "name="+x
>>> query_string
'name=Guy%20Steele,%20Jr.'
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
|