[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

Re: Init : someone could comment this ?

On Mon, 2008-01-07 at 15:27 -0800, Andrew Farris wrote:
> Enrico Scholz wrote:
> >>>>> But python or other bloaty scripting languages are not a solution
> >>>>> and completely unacceptable at this place.
> >>>> "Bloaty" is something that could be solved, don't you think?
> >>> Not with python or perl.
> >> I've shown you the numbers. Why you still insist on it being bloated
> > 
> > Resulting scripts will be much longer.  E.g. how much lines of python
> > code are required for
> > 
> > | sed '/^foo/s!/bin!/opt!' file | tac
> Looks like one line in python to me if you write a sed and tac replacement into 
> your python library. (e.g. thats not about bash vs python at all...)

If I'm not to try to emulate the single commands, but look at the whole
job which I understand as "read file, replace first occurrence of '/bin'
with '/opt' in lines beginning with 'foo', then put out the lines in
reversed order" it could be along this (and wouldn't use any external
module or program):

print '\n'.join (reversed (map (lambda line: line[:3] == "foo" and line.replace ('/bin', '/opt', 1) or line, ''.join (open ('file').readlines ()).split ('\n'))))

To make this more readable, I'd split this up and comment it:

--- 8< ---
# read the file and split it at '\n'
lines = ''.join (open ('file').readlines ()).split ('\n')

for i in range (len (lines)):
	if line[:3] == 'foo':
		# replace first occurrence of /bin with /opt
		lines[i] = lines[i].replace ('/bin', '/opt', 1)

print '\n'.join (reversed (lines))
--- >8 ---	

Now the original sed can surely be changed that it would take more than
one line to emulate the line's function, but then I would feel
comfortable using (shipped) modules like "re" to achieve the same effect
as the shell code uses sed and tac.

     Nils Philippsen    /    Red Hat    /    nphilipp redhat com
"Those who would give up Essential Liberty to purchase a little Temporary
 Safety, deserve neither Liberty nor Safety."  --  B. Franklin, 1759
 PGP fingerprint:  C4A8 9474 5C4C ADE3 2B8F  656D 47D8 9B65 6951 3011

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]