[FAQTS] Python Knowledge Base Update -- June 26th, 2000

Fiona Czuczman fiona at sitegnome.com
Mon Jun 26 00:19:22 EDT 2000


Greetings,

Below are the entries I've either entered or edited within 
http://python.faqts.com today.

cheers, Fiona


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


-------------------------------------------------------------
Is there any way, other than using the curses library, to check if a key has been pressed, and if so, which one?
Is there any way to prevent curses.initscr() from messing w/ normal prints?
http://www.faqts.com/knowledge-base/view.phtml/aid/3988
-------------------------------------------------------------
Fiona Czuczman
Erno Kuusela

Yep, you can do it with the termios module (or with system() & stty) if
you dare.

Here is some code for one-key-at-a-time input that I had laying
around (might be borrowed from somewhere):

import termios, TERMIOS, sys, os

def getkey():
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO
        new[6][TERMIOS.VMIN] = 1
        new[6][TERMIOS.VTIME] = 0
        termios.tcsetattr(fd, TERMIOS.TCSANOW, new)
        c = None
        try:
                c = os.read(fd, 1)
        finally:
                termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old)
        return c

if __name__ == '__main__':
        print 'type something'
        s = ''
        while 1:
                c = getkey()
                if c == '\n':
                        break
                print 'got', c
                s = s + c

        print s

However this doesn't do it in a non-blocking way. You should
probably check for data with the select module prior to the read()
call. Set the timeout to 0 and it returns immediately iirc.
(it's also possible to set stdin to non-blocking mode, or use the
FIONREAD ioctl, or ...)


-------------------------------------------------------------
Can I initiate script B at the end of script A?
http://www.faqts.com/knowledge-base/view.phtml/aid/3990
-------------------------------------------------------------
Fiona Czuczman
David Bolen

Try the os.exec* functions in the os module.  It sounds pretty much
like what you want to do.  The exec* functions replace the current
running executable with another executable - only one process slot is
involved, and the original executing code does not exist following the
exec call.

Note however that since you'll be replacing your current process, you
actually need to exec python giving it the new script as an argument,
rather than just directly-exec()ing the script.

You can use sys.executable to determine the python that is executing
the current script and just exec() that with an argument of the new
script.

You don't note your platform, but I believe this is documented for
both Windows and Unix.


-------------------------------------------------------------
Is there a quick way to see if a string is a valid date?
http://www.faqts.com/knowledge-base/view.phtml/aid/3991
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

Marc Lemburg's mxDateTime module (do a search at www.vex.net/parnassus)
returns a RangeError if you create a date that doesn't exist. I've 
created a checkDate method below which returns 0 or 1.

mxDateTime is an excellent module if you're going to be doing anything
complicated with dates or times.

import DateTime

def checkDate(y,m,d):
    "year,month,day"
    try:
        checkDate=DateTime.DateTime(y,m,d)
    except DateTime.RangeError:
        return 0
    else:
        return 1
def testDates(y,m,d):
    print '%d/%d/%d' %(y,m,d)
    if checkDate(y,m,d):
        print 'is ok'
    else:
        print 'is not valid'

def main():
    testDates(2000,3,10)
    testDates(2000,13,3)

if __name__=='__main__':
    main()


-------------------------------------------------------------
What are the contents of .pyo file?
When is a .pyo file generated?
What is a .pyo file used for?
http://www.faqts.com/knowledge-base/view.phtml/aid/3993
-------------------------------------------------------------
Fiona Czuczman
Kalle Svensson

It's optimized byte code, generated by importing a module or using
py_compile.compile() in a python process run with the -O command line
option. The advantages of the .pyo over the .pyc file is just that it's
optimized and thus runs faster. I guess... :)


-------------------------------------------------------------
I can send mail via the smtplib but how do I attach files to the email?
http://www.faqts.com/knowledge-base/view.phtml/aid/3992
-------------------------------------------------------------
Fiona Czuczman
Jürgen Hermann, Grant Edwards

Just did this today (you're lucky), you need a combination of cStringIO
(create file objects from your text, optional), MimeWriter (create the 
MIME msg), mimetools (encode the parts, f.x. in base64), and smtplib 
(send the mail).

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

Take a look at <ftp://ftp.visi.com/users/grante/stuff/mfetch.py>.

There's a class called SmtpWriter that has a "Message" method that 
probably does what you want...

def Message(self,sender="", subject="", recipients=[], body="", 
attachments=[]):
    if self.__debugLevel > 2:
        sys.stderr.write("SmtpWriter: Building RFC822 message From: %s; 
Subject: %s; (Length=%d with %d
attachments)\n" % 
                          (sender, subject, len(body),len(attachments)))
[...]

The MimeWriter library module is where most of the magic happens...


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


-------------------------------------------------------------
I'm new to Python and would appreciate advice on what book(s) I should be looking at?
http://www.faqts.com/knowledge-base/view.phtml/aid/2795
-------------------------------------------------------------
Fiona Czuczman
Chris Lada, Snoopy :-)), Paul Winkler, Dana Booth, Frank V. Castellucci,richard_chamberlain

There a lots,

Go to http://www.python.org/psa/bookstore/

for a list.  The manning and oreilly books have sample chapters online 
you could look at.

A range of books that should be useful when starting out with Python:

- "Quick Python" by Harms, and McDonald.

- If you already program, David Beasly's "Python Essential Reference" is 
very good. Another plus is that the book is high quality. It covers 
1.5.2, is tight fast text with no little fluffy animal or snakes 
writhing through the text, just down to the bone reading.

Snoopy :-)) wrote:

If you are New to Programming, then In addition to the Tutorials found 
on Python's Home Page, you should get as the 1st. choice "Teach Yourself 
Python in 24hrs".

BTY: "Learning Python" is also a very good book, but as a Newbie I found 
it quite overwhelming, to the point that I almost stopped learning 
Python. On the other hand I am very happy that I bought the "Teach 
Yourself Python in 24hrs". I find it considerable easier to comprehend 
the concepts, etc.

You can read the book online at the following:
                        http://www.pauahtun.org/TYPython/

Paul Winkler wrote:

OTOH, I got "Learning Python" and I've found that after reading just
the first half of the book and doing maybe 1/4 of the exercises, I
had a very good grasp of the language, including classes and
exceptions (both new ideas to me). From there the next step is to
skim the second half looking for useful bits, or poke around in the
library reference manual.

But then, it did take more than 24 hours. :)







More information about the Python-list mailing list