Sie sind auf Seite 1von 4

###################### 15.

1 Creating a very basic script


------------------------------- File script1.py ------------------------------def main():
print "this is our first test script file"
main()
###################### 15.1.2 Command line arguments
------------------------------- File script2.py ------------------------------import sys
def main():
print "this is our second test script file"
print sys.argv
main()
###################### 15.1.3 Redirecting the input and output of a script
------------------------------- File replace.py ------------------------------import string, sys
def main():
sys.stdout.write( string.replace(sys.stdin.read(), sys.argv[1], sys.argv[2])
)
main()
###################### 15.1.4 the getopt module
------------------------------- File script3.py ------------------------------import getopt, sys
def main():
(options, arguments) = getopt.getopt(sys.argv[1:], 'f:vx:y:z:')
print "options:",options
print "arguments:",arguments
main()
###################### 15.1.5 using the fileinput module
------------------------------- File script5.py ------------------------------import fileinput
def main():
for line in fileinput.input():
if line[:2] != '##':
print line,
main()
import fileinput
def main():
for line in fileinput.input():
if fileinput.isfirstline():

print "<start of file %s>" % fileinput.filename()


print line,
main()
###################### 15.3.1 Starting a script as a document or shortcut
------------------------------- File script6.py ------------------------------import sys, os
def main():
print os.getcwd()
print sys.argv
raw_input("Hit return to exit")
main()
###################### 15.4 Scripts on Windows versus scripts on UNIX
------------------------------- File script4a.py ------------------------------#! /usr/bin/env python import fileinput
def main():
for line in fileinput.input():
if line[:2] != '##':
print line,
main()
------------------------------- File script4b.py ------------------------------#! /usr/bin/env python
import fileinput, glob, sys, os
def main():
if os.name == 'nt':
sys.argv[1:] = glob.glob(sys.argv[1])
for line in fileinput.input():
if line[:2] != '##':
print line,
main()
------------------------------ File script4c.py ------------------------------#! /usr/bin/env python
import fileinput, glob, sys, os
def main():
if os.name == 'nt':
if sys.path[0]: os.chdir(sys.path[0])
sys.argv[1:] = glob.glob(raw_input("Name of input file:"))
sys.stdout = open(raw_input("Name of output file:"),'w')
for line in fileinput.input():
if line[:2] != '##':
print line,
main()
###################### 15.5 Scripts and Modules
------------------------------- File script7.py ------------------------------#! /usr/bin/env python
import sys

# conversion mappings
_1to9Dict = {'0':'', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five',
'6':'six', '7':'seven', '8':'eight', '9':'nine'}
_10to19Dict = {'0':'ten', '1':'eleven', '2':'twelve', '3':'thirteen', '4':'fourt
een', '5':'fifteen', '6':'sixteen',
'7':'seventeen', '8':'eighteen', '9':'nineteen'}
_20to90Dict = {'2':'twenty', '3':'thirty', '4':'forty', '5':'fifty', '6':'sixty'
, '7':'seventy', '8':'eighty', '9':'ninety'}
def num2words(numString):
if numString == '0': return('zero')
if len(numString) > 2: return "Sorry can't handle numbers with more than 2 d
igits"
numString = '0' + numString
# pad on left in case its a single digit num
ber
tens, ones = numString[-2], numString[-1]
if tens == '0': return _1to9Dict[ones]
elif tens == '1': return _10to19Dict[ones]
else: return _20to90Dict[tens] + ' ' + _1to9Dict[ones]
def main():
print num2words(sys.argv[1])
main()
------------------------------- File n2w.py ------------------------------#! /usr/bin/env python
"""n2w: number to words conversion module: contains function num2words. Can also
be run as a script
usage as a script: n2w num
(Convert a number to its English word description)
num: whole integer from 0 and 999,999,999,999,999 (commas are optional)
example: n2w 10,103,103
for 10,003,103 say: ten million three thousand one hundred three
"""
import sys, string
# conversion mappings
_1to9Dict = {'0':'', '1':'one', '2':'two', '3':'three', '4':'four', '5':'five',
'6':'six', '7':'seven', '8':'eight', '9':'nine'}
_10to19Dict = {'0':'ten', '1':'eleven', '2':'twelve', '3':'thirteen', '4':'fourt
een', '5':'fifteen', '6':'sixteen',
'7':'seventeen', '8':'eighteen', '9':'nineteen'}
_20to90Dict = {'2':'twenty', '3':'thirty', '4':'forty', '5':'fifty', '6':'sixty'
, '7':'seventy', '8':'eighty', '9':'ninety'}
_magnitudeList = [(0,''), (3,' thousand '), (6, ' million '), (9, ' billion '),
(12, ' trillion '),(15,'')]
def num2words(numString):
"""num2words(numString): converts number represented by string to English wo
rds"""
# handle the special conditions (number is zero or too large)
if numString == '0': return('zero')
numLength = len(numString)
maxDigits = _magnitudeList[-1][0]
if numLength > maxDigits:

return "Sorry, can't handle numbers with more than %d digits" % (maxDig
its)
# working from least to most significant digit create a string containing t
he number
numString = '00' + numString
# pad the number on the left
wordString = ''
# initiate string for number
for mag, name in _magnitudeList:
if mag >= numLength: return wordString
else:
hundreds, tens, ones = numString[-mag-3], numString[-mag-2], numStri
ng[-mag-1]
if not (hundreds == tens == ones == '0'):
wordString = _handle1to999(hundreds, tens, ones) + name + wordS
tring
def _handle1to999(hundreds, tens, ones):
if hundreds == '0': return _handle1to99(tens,ones)
else: return _1to9Dict[hundreds] + ' hundred ' + _handle1to99(tens,ones)
def _handle1to99(tens, ones):
if tens == '0': return _1to9Dict[ones]
elif tens == '1': return _10to19Dict[ones]
else: return _20to90Dict[tens] + ' ' + _1to9Dict[ones]
def test():
values = string.split(sys.stdin.read())
for val in values:
num = string.replace(val, ',', '')
print "%s = %s" % ( val, num2words(num) )
def main():
if len(sys.argv) != 2 or sys.argv[1] == '+?':
print __doc__
elif sys.argv[1] == '+test':
test()
else:
num = string.replace(sys.argv[1], ',', '')
try:
result = num2words(num)
except KeyError:
print __doc__
else:
print "For %s, say: %s" % (sys.argv[1], result)
if __name__ == '__main__':
main()
else:
print "n2w loaded as a module"

Das könnte Ihnen auch gefallen