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

Fiona Czuczman fiona at sitegnome.com
Mon Jun 5 18:05:07 EDT 2000


Hello All,

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

cheers,

Fiona Czuczman


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


-------------------------------------------------------------
Is there an easy way to do user input in python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3492
-------------------------------------------------------------
Fiona Czuczman
Steven Adams

use raw_input

>>> def get_SPAM_n_EGGS():
                    spam = raw_input('Spam? ')
                    eggs = raw_input('and Eggs? ')
                    print spam, eggs


>>> get_SPAM_n_EGGS()
Spam? loads
and Eggs? yessiree
loads yessiree
>>>


-------------------------------------------------------------
Where can I find an example on how to use python with Tcl/Tk to write GUI windows applications?
http://www.faqts.com/knowledge-base/view.phtml/aid/3490
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain

from Tkinter import *
root=Tk()
label=Label(root,width=12)
def clicker():
    label.configure(text="Hello Jilani")
button=Button(root,text="Click Me",command=clicker)
label.pack(side=TOP)
button.pack(side=TOP)
root.mainloop()

Try www.pythonware.com/library.htm

Also there is a good book by John Grayson (Python and Tkinter 
Programming) and one on the way by Frederik Lundh published by O'Reilly.


-------------------------------------------------------------
I'm currently using the storbinary method for transferring files, how can I get some indication that I've transferred xx% of the file, or 100,000 bytes so far?
http://www.faqts.com/knowledge-base/view.phtml/aid/3493
-------------------------------------------------------------
Fiona Czuczman
Peter Schneider-Kamp

Just define your own version of storbinary in your module:

def mystorbinary(self, cmd, fp, blocksize):
  '''Store a file in binary mode and print stuff.'''
  self.voidcmd('TYPE I')
  conn = self.transfercmd(cmd)
  counter = 0
  while 1:
    print counter,"Bytes read yet."
    buf = fp.read(blocksize)
    if not buf: break
    conn.send(buf)
    counter = counter + blocksize
  conn.close()
  return self.voidresp()


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


-------------------------------------------------------------
Where can I find information about how to manipulate Excel with Python?
Can I access an excel (.xls) file via com without having excel loaded on that machine?
http://www.faqts.com/knowledge-base/view.phtml/aid/3093
-------------------------------------------------------------
Fiona Czuczman
Robert Citek,Eric Hagemann, Scott

Read Hammond's & Robinson's "Programming Python in Win32".  There is a 
very good headstart there with an example of a class that eases the 
interaction w/Excel via COM.  Next get "Writing Excel Macros" by 
O'Reilly.  It is all about how to program Excel via Visual Basic but 
translates very well into Python COM.

Visit http://www.oreilly.com/catalog/pythonwin32

If you can't afford to buy it, it seems you can view it at 
http://www.netlibrary.com .  It's a neat site that allows you to "check 
out" books for a period of time  (usually 24 hours at a time) and read 
them online.  The only drawback is they only allow so many "check outs" 
of each book so it may or may not be available to read.

Here is an exact URL:
http://www.netlibrary.com/SUMMARY.ASP?EV=1239265&ID=24666&advquery=

and chapter 9 is the main Excel chapter.

Here is some sample code:

# this example starts Excel, creates a new workbook, 
# puts some text in the first and second cell
# closes the workbook without saving the changes
# and closes Excel.  This happens really fast, so
# you may want to comment out some lines and add them
# back in one at a time ... or do the commands interactively

from win32com.client import Dispatch

xlApp = Dispatch("Excel.Application")
xlApp.Visible = 1
xlApp.Workbooks.Add()
xlApp.ActiveSheet.Cells(1,1).Value = 'Python Rules!'
xlApp.ActiveWorkbook.ActiveSheet.Cells(1,2).Value = 'Python Rules 2!'
xlApp.Close(SaveChanges=0)
xlApp.Quit()
del xlApp

# raw_input("press Enter ...")


-------------------------------------------------------------
Where can I find some kind of helping material (tutorials etc) about how to use XML and Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3468
-------------------------------------------------------------
Fiona Czuczman, Fred Drake
Boudewijn Rempt, Harry George

Take a look at the Python/XML HOW-TOs and related documentation,
located at:

http://www.python.org/topics/xml/docs.html

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

Have a look here:

http://www.valdyas.org/python/index.html

dbobj - the repository is XML based
kura  - webserver that takes XML templates and spews out HTML, and an
        XML data import utility.
kxml  - a small PyKDE xml file browser (unfinished!)

A really simplistic reader/writer for Abiword.  Available at:
  http://www.seanet.com/~hgg9140/comp/index.html

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

Install PyXML, then try this.  Read the xml.dom.core for element names
and the basic functions.  If you are not familiar with xml itself, see
the XML Bible.  Try this routine on some known-good .xml file.

#!/usr/bin/env python
#/* helloxml.py 

"""
Usage: helloxml.py [options] filename
E.g.,  helloxml.py myfile
Options
-h,--help        this message
-v,--version     version

Revisions:
2000-02-23  Harry George   initial version
"""

import sys,os,getopt,time,re,string,exceptions

modname="helloxml"
__version__="0.1"

def debug(ftn,txt):
        sys.stdout.write(modname+'.'+ftn+':'+txt+'\n')
        sys.stdout.flush()

def fatal(ftn,txt):
        sys.stdout.write(modname+'.'+ftn+':FATAL:'+txt+'\n')
        sys.stdout.flush()
        sys.exit(1)

def usage():
        print __doc__

#====================================
from xml.dom import core, utils

def main():
        ftn = "main"
        pargs=[]    #positional args, default is empty
        opts,pargs=getopt.getopt(sys.argv[1:],'hv',['help','version'])
        for opt in opts:
                if opt[0]=='-h' or opt[0]=='--help':
                        usage()
                        sys.exit(0)
                elif opt[0]=='-v' or opt[0]=='--version':
                        print modname+": version="+__version__
                        sys.exit(0)

        #---get the DOM "doc"---
        if len(pargs)==0:
                        usage()
                        sys.exit(0)             
        filename = pargs[0]
        doc = utils.FileReader( filename ).document

        #---dump it out---
        doc.dump();

        #---convert DOM tree back to XML, and dump that
        xml = doc.toxml()
        print xml

        #---dump the doc manually---
        root=doc.get_documentElement()  
        mydump(root)

def mydump(node):
        ftn = "mydump"
        debug(ftn,"name=" +node.get_nodeName())
        #---dump attributes---
        attrs=node.get_attributes()
        keys=attrs.keys()
        for attr in keys:
                debug(ftn,"attr="+attr+" 
value="+attrs[attr].get_nodeValue())
        #---dump children---
        children=node.get_childNodes()
        for child in children:
        #--report only elements
                if child.get_nodeType()==core.ELEMENT: 
                        mydump(child)
                         
if __name__ == '__main__': main()







More information about the Python-list mailing list