[FAQTS] Python Knowledge Base Update -- July 1st, 2000

Fiona Czuczman fiona at sitegnome.com
Sat Jul 1 23:19:26 EDT 2000


Greetings All!

Below are the latest entries to be entered into http://python.faqts.com

regards,

Fiona Czuczman


## New Entries #################################################


-------------------------------------------------------------
What is bbox? In the context of the PhotoImage put(data, bbox).
http://www.faqts.com/knowledge-base/view.phtml/aid/4133
-------------------------------------------------------------
Fiona Czuczman
Keith Murphy

The bounding box is returned as a 4-tuple defining the left, upper,
right, and lower pixel coordinate.

Here's what I was doing..

self.photo.put(tuple(["#000000"],), tuple([5,5,6,6]))

it draws a single black pixel at (5,5)


-------------------------------------------------------------
How do you print the traceback string from an exception?
http://www.faqts.com/knowledge-base/view.phtml/aid/4134
-------------------------------------------------------------
Fiona Czuczman
Jürgen Hermann

try:
   whatever()
except:
            import traceback, string
            msg = string.join(traceback.format_exception(
                    sys.exc_info()[0], sys.exc_info()[1],
sys.exc_info()[2]), "")
            print msg


-------------------------------------------------------------
How can I concatenate external files (all the little *.htm files in a directory) into one big file?
http://www.faqts.com/knowledge-base/view.phtml/aid/4135
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

import sys, string, glob, os

#First I create a list:
fls = glob.glob('C:\\wxpython\\wxPython-2.1.16\\docs\\wx\\wx*.htm')

#Then I guess I should open an output file for writing
outfile = open('temp.txt,'w')

for x in fls:
               file=open(x,'r')
               data=file.read()
               file.close()
               outfile.write(data)

outfile.close()

We then iterate over fls to give us each file name and open each one.
We then call the read method against it, which loads in the entire file
and assign that to data. we close the file and write the data to our
outfile. And then on to the next file. Finally we close the outfile.

You have to use read() with some caution because it (trys) to load the
whole file into memory which maybe an issue if it was sizable. In your
case it isn't an issue because they are simple html files.


-------------------------------------------------------------
What is lambda?
http://www.faqts.com/knowledge-base/view.phtml/aid/4136
-------------------------------------------------------------
Fiona Czuczman
Warren Postma, david_ullrich

1) Meta-Answer: Read the FAQ and Manual sections.

2) Instant Gratification Answer

Here's a standard function definition:

def func(x):
    return x *2

Here's a way to write the same thing, in effect, but using Lambda:

func = lambda (x): x * 2

While that example isn't useful, it gives you the idea.  Think of lambda 
as a way to generate a function and return that function as an object, 
without having to give it a name. I primarily use it to pass an 
algorithm or expression as a parameter to a function, or other places 
where I want to pass code in a variable instead of passing a reference 
to a method containing that code.

------------

What it does is allow you to construct "anonymous functions".

   Oops, not my own words. The syntax

lambda x: [expression in x]

is itself an expression - the _value_ of the expression "lambda x: 
[expression in x]" is a function which returns [expression in x] when 
passed x. For example "lambda x: x+x" is a function that returns x+x; 
saying

f = lambda x: x+x

has the same effect as saying

def f(x):
  return x + x

But you don't usually use it that way - people use lambda when they want 
to pass a function to some routine without having to make up a name for 
the function. For example map takes a function as a parameter; saying

print map(lambda x: x+x, [1,2,3])

is the same as saying

def f(x):
  return x + x

print map(f, [1,2,3]])

You should note there's varying opinions on whether lambda is a good 
thing - if you're a beginner at programming as well as with Python it's 
not the first thing you should worry about.


## Edited Entries ##############################################


-------------------------------------------------------------
How can I use the smtplib module to send email from Python?
How can I send mail using the os module?
How can I send mail through python?
http://www.faqts.com/knowledge-base/view.phtml/aid/2607
-------------------------------------------------------------
Nathan Wallace, Fiona Czuczman, George Jansen
http://www.python.org/doc/FAQ.html#4.71,Justin Sheehy

On Unix, it's very simple, using 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" % SENDMAIL, "w")
  p.write("To: cary at ratatosk.org\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

Check out the os module doc for more info:

  http://www.python.org/doc/current/lib/module-os.html


On non-Unix systems (and on Unix systems too, of course!), you can use
SMTP to send mail to a nearby mail server. A library for SMTP
(smtplib.py) is included in Python 1.5.1; in 1.5.2 it will be documented
and extended. Here's a very simple interactive mail sender that uses 
it: 

    import sys, smtplib

    fromaddr = raw_input("From: ")
    toaddrs  = string.splitfields(raw_input("To: "), ',')
    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()

This method will work on any host that supports an SMTP listener;
otherwise, you will have to ask the user for a host.

    http://www.python.org/doc/current/lib/module-smtplib.html

On Windows 9x systems, you may also use the COM interface. We have 
recently used PythonWin to (what else?) spam a number of recipients 
over Novell GroupWise. It took very little time to work this out.







More information about the Python-list mailing list