-
Notifications
You must be signed in to change notification settings - Fork 0
/
qsort2.py
93 lines (64 loc) · 2.13 KB
/
qsort2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/usr/bin/env python
import argparse
import codecs
import random
import string
import struct
import sys
#//**********************************************************************
#//
#// globals/constants
#//
#//**********************************************************************
PROGRAM_NAME = "qsort2"
VERSION = "3.0.0"
COPYRIGHT_MESSAGE = "copyright (c) 2014 (1999), Rick Gutleber ([email protected])"
lineLength = 80
#//******************************************************************************
#//
#// generateSortKey
#//
#//******************************************************************************
def generateSortKey( s ):
newlines = s.count( '\n' )
result = s.strip( )
result = ' '.join( result.split( ) )
for char in string.punctuation:
result = result.replace( char, '' )
result = result.lower( )
result = "%04d" % ( newlines ) + result
return result
#//**********************************************************************
#//
#// main
#//
#//**********************************************************************
def main( ):
parser = argparse.ArgumentParser( prog=PROGRAM_NAME, description=PROGRAM_NAME + ' - ' + VERSION + ' - ' + COPYRIGHT_MESSAGE )
parser.add_argument( 'input', default='quote.txt' )
#parser.add_argument( 'output', default='quote.sorted.txt' )
args = parser.parse_args( )
inputFile = args.input
quote = ''
quoteList = [ ]
quoteCount = 0
for line in codecs.open( inputFile, 'rU', 'ascii', 'replace' ):
if line == '%\r\n':
quoteCount += 1
quoteList.append( quote )
if quoteCount % 100 == 0:
print( quoteCount, end='\r', file=sys.stderr )
quote = ''
else:
quote += line.rstrip( ) + '\n'
print( quoteCount, file=sys.stderr )
for quote in sorted( quoteList, key=generateSortKey ):
print( quote, end='' )
print( '%' )
#//**********************************************************************
#//
#// __main__
#//
#//**********************************************************************
if __name__ == '__main__':
main( )