düzenleme
This commit is contained in:
+34
-34
@@ -5,37 +5,37 @@ import sys, re
|
|||||||
from itertools import izip, imap, count, ifilter, ifilterfalse
|
from itertools import izip, imap, count, ifilter, ifilterfalse
|
||||||
|
|
||||||
def cat( filename ):
|
def cat( filename ):
|
||||||
return file( filename ).xreadlines()
|
return file( filename ).xreadlines()
|
||||||
|
|
||||||
class grep:
|
class grep:
|
||||||
"""keep only lines that match the regexp"""
|
"""keep only lines that match the regexp"""
|
||||||
def __init__( self,pat, flags = 0 ):
|
def __init__( self,pat, flags = 0 ):
|
||||||
self.fun = re.compile( pat, flags ).match
|
self.fun = re.compile( pat, flags ).match
|
||||||
def __ror__( self, input ):
|
def __ror__( self, input ):
|
||||||
return ifilter( self.fun, input )
|
return ifilter( self.fun, input )
|
||||||
|
|
||||||
class tr:
|
class tr:
|
||||||
"""apply arbitrary transform to each sequence element"""
|
"""apply arbitrary transform to each sequence element"""
|
||||||
def __init__( self, transform ):
|
def __init__( self, transform ):
|
||||||
self.tr = transform
|
self.tr = transform
|
||||||
def __ror__( self, input ):
|
def __ror__( self, input ):
|
||||||
return imap( self.tr, input )
|
return imap( self.tr, input )
|
||||||
|
|
||||||
class printto:
|
class printto:
|
||||||
"""print sequence elements one per line"""
|
"""print sequence elements one per line"""
|
||||||
def __init__( self, out = sys.stdout ):
|
def __init__( self, out = sys.stdout ):
|
||||||
self.out=out
|
self.out = out
|
||||||
def __ror__( self,input ):
|
def __ror__( self,input ):
|
||||||
for l in input:
|
for l in input:
|
||||||
print >> self.out, l
|
print >> self.out, l
|
||||||
|
|
||||||
printlines = printto( sys.stdout )
|
printlines = printto( sys.stdout )
|
||||||
|
|
||||||
class terminator:
|
class terminator:
|
||||||
def __init__( self,method ):
|
def __init__( self,method ):
|
||||||
self.process = method
|
self.process = method
|
||||||
def __ror__( self,input ):
|
def __ror__( self,input ):
|
||||||
return self.process( input )
|
return self.process( input )
|
||||||
|
|
||||||
aslist = terminator( list )
|
aslist = terminator( list )
|
||||||
asdict = terminator( dict )
|
asdict = terminator( dict )
|
||||||
@@ -44,20 +44,20 @@ join = terminator( "".join )
|
|||||||
enum = terminator( enumerate )
|
enum = terminator( enumerate )
|
||||||
|
|
||||||
class sort:
|
class sort:
|
||||||
def __ror__( self,input ):
|
def __ror__( self,input ):
|
||||||
ll = list( input )
|
ll = list( input )
|
||||||
ll.sort()
|
ll.sort()
|
||||||
return ll
|
return ll
|
||||||
sort = sort()
|
sort = sort()
|
||||||
|
|
||||||
class uniq:
|
class uniq:
|
||||||
def __ror__( self,input ):
|
def __ror__( self,input ):
|
||||||
for i in input:
|
for i in input:
|
||||||
try:
|
try:
|
||||||
if i == prev:
|
if i == prev:
|
||||||
continue
|
continue
|
||||||
except NameError:
|
except NameError:
|
||||||
pass
|
pass
|
||||||
prev = i
|
prev = i
|
||||||
yield i
|
yield i
|
||||||
uniq = uniq()
|
uniq = uniq()
|
||||||
|
|||||||
Reference in New Issue
Block a user