Python Interview Questions And Answers Set - 5
How do I send mail from a Python script? Use the standard library module smtplib.
Here's a very simple interactive mail sender that uses it. This method will work on any host that supports an SMTP listener.
import sys, smtplib
fromaddr = raw_input("From: ") toaddrs = raw_input("To: ").split(',') print "Enter message, end with ^D:" msg = '' while 1: line = sys.stdin.readline() if not line: break msg = msg + line
The actual mail send
server = smtplib.SMTP('localhost') server.sendmail(fromaddr, toaddrs, msg) server.quit()
A Unix-only alternative uses sendmail. The location of the sendmail program varies between systems; sometimes it is /usr/lib/sendmail, sometime /usr/sbin/sendmail. The sendmail manual page will help you out. Here's some sample code:
SENDMAIL = "/usr/sbin/sendmail" # sendmail location import os p = os.popen("%s -t -i" % SENDMAIL, "w") p.write("To: receiver@example.com\n") p.write("Subject: test\n") p.write("\n") # blank line separating headers from body p.write("Some text\n") p.write("some more text\n") sts = p.close() if sts != 0: print "Sendmail exit status", sts
How do I avoid blocking in the connect() method of a socket? The select module is commonly used to help with asynchronous I/O on sockets.
Are there any interfaces to database packages in Python? Yes.
Python 2.3 includes the bsddb package which provides an interface to the BerkeleyDB library. Interfaces to disk-based hashes such as DBM and GDBM are also included with standard Python.
How do I generate random numbers in Python? The standard module random implements a random number generator. Usage is simple:
import random random.random()
This returns a random floating point number in the range . Note that it doesn't enter the module into any namespace -- it only ensures it has been initialized and is stored in sys.modules.
You can then access the module's attributes (i.e. any name defined in the module) as follows:
attr = PyObject_GetAttrString(module, "");
Calling PyObject_SetAttrString() to assign to variables in the module also works.
How do I interface to C++ objects from Python? Depending on your requirements, there are many approaches. To do this manually, begin by reading the "Extending and Embedding" document. Realize that for the Python run-time system, there isn't a whole lot of difference between C and C++ -- so the strategy of building a new Python type around a C structure (pointer) type will also work for C++ objects.
How do I tell "incomplete input" from "invalid input"? Sometimes you want to emulate the Python interactive interpreter's behavior, where it gives you a continuation prompt when the input is incomplete (e.g. you typed the start of an "if" statement or you didn't close your parentheses or triple string quotes), but it gives you a syntax error message immediately when the input is invalid.
In Python you can use the codeop module, which approximates the parser's behavior sufficiently. IDLE uses this, for example.
The easiest way to do it in C is to call PyRun_InteractiveLoop() (perhaps in a separate thread) and let the Python interpreter handle the input for you. You can also set the PyOS_ReadlineFunctionPointer to point at your custom input function. See Modules/readline.c and Parser/myreadline.c for more hints.
However sometimes you have to run the embedded Python interpreter in the same thread as your rest application and you can't allow the PyRun_InteractiveLoop() to stop while waiting for user input. The one solution then is to call PyParser_ParseString() and test for e.error equal to E_EOF, which means the input is incomplete). Here's a sample code fragment, untested, inspired by code from Alex Farber: #include <Python.h> #include <node.h> #include <errcode.h> #include <grammar.h> #include <parsetok.h> #include <compile.h>
int testcomplete(char code) / code should end in \n / / return -1 for error, 0 for incomplete, 1 for complete */ { node *n; perrdetail e;
n = PyParser_ParseString(code, &_PyParser_Grammar, Py_file_input, &e); if (n == NULL) { if (e.error == E_EOF) return 0; return -1; }
PyNode_Free(n); return 1; }
Another solution is trying to compile the received string with Py_CompileString(). If it compiles without errors, try to execute the returned code object by calling PyEval_EvalCode(). Otherwise save the input for later. If the compilation fails, find out if it's an error or just more input is required - by extracting the message string from the exception tuple and comparing it to the string "unexpected EOF while parsing". Here is a complete example using the GNU readline library (you may want to ignore SIGINT while calling readline()): #include <stdio.h> #include <readline.h>
#include <Python.h> #include <object.h> #include <compile.h> #include <eval.h>
int main (int argc, char* argv) { int i, j, done = 0; /* lengths of line, code */ char ps1 = ">>> "; char ps2 = "... "; char *prompt = ps1; char *msg, *line, *code = NULL; PyObject *src, *glb, *loc; PyObject *exc, *val, *trb, *obj, *dum;
Py_Initialize (); loc = PyDict_New (); glb = PyDict_New (); PyDict_SetItemString (glb, "builtins", PyEval_GetBuiltins ());
while (!done) { line = readline (prompt);
if (NULL == line) /* CTRL-D pressed */ { done = 1; } else { i = strlen (line);
if (i > 0) add_history (line); /* save non-empty lines */
if (NULL == code) /* nothing in code yet */ j = 0; else j = strlen (code);
code = realloc (code, i + j + 2); if (NULL == code) /* out of memory */ exit (1);
if (0 == j) /* code was empty, so / code = '\0'; / keep strncat happy */
strncat (code, line, i); /* append line to code / code = '\n'; / append '\n' to code */ code = '\0';
src = Py_CompileString (code, " ", Py_single_input);
if (NULL != src) /* compiled just fine - / { if (ps1 == prompt || / ">>> " or / '\n' == code) / "... " and double '\n' / { / so execute it */ dum = PyEval_EvalCode ((PyCodeObject )src, glb, loc); Py_XDECREF (dum); Py_XDECREF (src); free (code); code = NULL; if (PyErr_Occurred ()) PyErr_Print (); prompt = ps1; } } / syntax error or E_EOF? */
else if (PyErr_ExceptionMatches (PyExc_SyntaxError)) { PyErr_Fetch (&exc, &val, &trb); /* clears exception! */
if (PyArg_ParseTuple (val, "sO", &msg, &obj) && !strcmp (msg, "unexpected EOF while parsing")) /* E_EOF / { Py_XDECREF (exc); Py_XDECREF (val); Py_XDECREF (trb); prompt = ps2; } else / some other syntax error / { PyErr_Restore (exc, val, trb); PyErr_Print (); free (code); code = NULL; prompt = ps1; } } else / some non-syntax error */ { PyErr_Print (); free (code); code = NULL; prompt = ps1; }
free (line); } }
Py_XDECREF(glb); Py_XDECREF(loc); Py_Finalize(); exit(0); }