Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ îðèãèíàëüíîãî äîêóìåíòà : http://www.stsci.edu/hst/training/events/Python/class4.pdf
Äàòà èçìåíåíèÿ: Wed Jun 15 23:11:23 2005
Äàòà èíäåêñèðîâàíèÿ: Sun Dec 23 00:37:18 2007
Êîäèðîâêà:

Ïîèñêîâûå ñëîâà: http www.astronet.ru
STSCI Python Introduction

Class 4 Jim Hare

Today's Agenda
· · · · · string Module Scripts and Programs stpydb Module ­ database interface module User defined class examples - Catalog Module examples

1


string Module
· Contains constants and functions for manipulating strings. · Most of these functions are available in the built-in string methods we covered in Class1 · raw_input function only input is a string

string Example1
import string stringx = `2' x = string.atoi(stringx) # converts to integer Or x = int(stringx) #same as above # x is 2 y = string.atoi(raw_input("Input Integer")) z = string.atof(raw_input("Input Floating Point"))

2


string Example 2
import string string1 = "hello" String1 = string1.capitalize() Or String1 = string.capitalize(string1)

Scripts and Programs
· Scripts
­ Step by step procedures that run programs and use define objects ­ They do not contain definitions of functions or classes to be reused by other modules

· Programs
­ Define functions and classes ­ Execute algorithms and are designed for reuse

3


Script Examples
· Timecard .py ­ mails time card reminder. · install.py ­ install updates to the replication area of SPST scripts and programs · regression _load_files.py ­ database setup for a test release · moving_target.py ­ a test for moving targets

MyDir

4


MyDir Class?
· Data Dictionary Record for MyDir - lastName - emails - address
- firstName - homePhone - workPhone - nicknames - groups - birthday - other - spouse - children

-parents

· Store and update Mydir record in a file · Search and retrieve a Mydir from a file · Send a formatted directory to a file or printer

PhotoDir Class?
· Data Dictionary Record for PhotoDir
fileName film groups brief directory camera RGB long - date - quality -contrast

· · · ·

Store and update Mydir record in a file Search and retrieve a Mydir from a file Send a formatted directory to a file or printer Display photograph

5


Other Catalog Classes
· Fishing and crabbing logs · Car, Boat, and House maintenance logs · Merchandise, Manufacturer catalogs for my wife · Home Projects log · Home Computer Directory · Holiday, Party to do Lists...

Catalog

MyDir

6


Catalog Class?
· Data Dictionary Record for Catalog
­ {}

· · · · ·

Set keys/fields to store in Catalog records Store the Catalog record in a file Retrieve a Catalog record from a file Set format for formatting Catalog records Send a formatted Catalog record to a file or printer

Database Functions
· dbtools.py ­ use with sql flat files. · stpydb.py ­ use to program queries and get results in variables to use in the module.

7


stpydb Functions
· stpydb() - Connecting to the database. stpydb is a class object. · getParamModeChar - Returns the query param substitution char · setParamModeChar - Defines the query param substitution char · setParam - Defines value for a named parameter · execute - Executes the query returning one row at a time · executeAll - Executes the query returning all rows · executeUpdate - Executes an update/insert/delete query

stpydb Functions continued
· rowcount - Returns #rows affected by a query · beginTransaction - Starts a transaction on a given connection · commitTransaction - Commits a transaction on a given connection · rollbackTransaction - Aborts a transaction on a given connection · clear - Clears an unfinished query

8


stpydb Functions continued
· getDescription - Returns meta data info on last select query · printDescription - Prints meta data report on last select query · query - Pass a query string · close - Shuts down the connection

stpydb Constructor
· Look at def __init__ in /ess5/psdpython/stpydb.py, the constructor · Examples: q1 = stpydb.stpydb() q2 = stpydb.stpydb('NOMAD') q3 = stpydb.stpydb(dbmsName='psttest4') q4 = stpydb.stpydb('NOMAD','psttest4') q5 = stpydb.stpydb(moduleName='testpydb.py') q6 = stpydb.stpydb('R2D2','pstdev1','testpydb.py')

9


Parameters, Queries and Execution - executeUpdate
· Ex. Update ­ from /ess5/psdpython/spssdb.py
db= stpydb.stpydb(serverName=server,dbmsName=database) param = [['PROP', proposal_id],['OBSET', obset_id], ['BEG',window_beg], ['END',window_end]] db.query('update qbwindows set window_beg = @BEG, ' 'window_end = @END where proposal_id = @PROP ' 'and obset_id = @OBSET') db.setParam(param) db.executeUpdate() return None

Parameters, Queries and Execution - execute
· Ex. Execute ­ returns one record at a time
param = ['sunit',suId[0]] db.query('select qs.proposal_id,qs.obset_id') db.query(' from qsbranching qs ') db.query('where qs.sunit_id = @sunit') db.setParam(param) result = {} while db.execute(result): obsetList = obsetList + \ ['%s:%s:01' % (result['proposal_id'],result['obset_id'])]

10


Parameters, Queries and Execution - executeAll
· Ex. executeAll ­ returns list of records currlist = [{}] q1.query("select d_scenar_acq, d_use_fgs_1, d_use_fgs_2, d_use_fgs_3 ") q1.query("from qbacqd_def") q1.executeAll(currlist) for currdict in currlist: for field in currdict.keys(): print currdict[field]

Transactions
· q3.beginTransaction() q3.query(querynew1) q3.executeUpdate() q3.query(querynew2) q3.executeUpdate() #NO error condition if you don't rollback! q3.rollbackTransaction()

11


Tools and Examples
­ spssdb.py ­ functions related to spss database ­ siRecon.py ­ a tool to get 10.2 table data ­ stpydb.py ­ the tool developed for sybase interface ­ testpydb.py ­ tests developed to test stpydb

12