[Tutor] Pythonic? Building a full path from a visual file tree

2006-03-21 Thread stv
Hey all,

I would like to expand the visual representation of a tree
hierarchy, given below, where child-ness is defined by indentation,
and folder-ness is highlighted with a trailing '/'

Input (test.txt):
dir1/
file1
file2
1-1/
file3
file4
dir2/
file5

The desired output is a fully qualified path for each input line:
dir1/
dir1/file1
dir1/file2
dir1/1-1/
dir1/1-1/file3
dir1/1-1/file4
dir2/
dir2/file5

I considered several brute-force solutions, but I persevered and came
to, what I think, is a more Pythonic solution. What do you think?

import string

def expand_tree(filetree):
  indent = '\t'
  stack = []
  for f in filetree:
indents = f.count(indent)
while len(stack)  indents: stack.pop()
stack.append(f.strip())
yield string.join(stack,'')
if not f.endswith('/'): stack.pop()

lines = [line.rstrip() for line in file('test.txt')]
for i in expand_tree(lines): print i

Those familiar with subversion (particularly, svnlook tree
/path/to/repos) may recognize the problem space and the sample input
(except that I simplified things a bit here, using a tab to indicate
level-of-hierarchy instead of a single space, which could appear in
the filename).  The real-world solution will use a regex to find run
of spaces at the start of element 'f'. It will also get input from
stdin instead of a file.

Questions:

Is the list comprehension the right idiom for getting a file into a
list, without the EOL chars? I'm hard-pressed to see how it could be
more concise, but this is Python :) and the rtrim() feels a little
off.

Is string.join() preferred to ''.join? Does importing all of string
outweigh the readability of string.join()?

Any subversion geeks know of way to eliminate this code with a native
svn command?

Did other folks choke on generators for as long as I did? I'm not
going to admit, publicly, how long that was :) But I found this little
test illustrative:

def gen():
  yield 1
  yield 2
  yield 3

g = gen()
g.next()
 1
g.next()
 2
g.next()
 3
g.next()
 [stack trace]

Beyond simple, but the obviousness of it is hidden in many of the
examples I saw.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic? Building a full path from a visual file tree

2006-03-21 Thread Kent Johnson
stv wrote:
 I considered several brute-force solutions, but I persevered and came
 to, what I think, is a more Pythonic solution. What do you think?

Looks pretty sweet to me :-)
 
 import string
 
 def expand_tree(filetree):
   indent = '\t'
   stack = []
   for f in filetree:
 indents = f.count(indent)
 while len(stack)  indents: stack.pop()
 stack.append(f.strip())
 yield string.join(stack,'')
 if not f.endswith('/'): stack.pop()
 
 lines = [line.rstrip() for line in file('test.txt')]  
 for i in expand_tree(lines): print i
 
 Questions:
 
 Is the list comprehension the right idiom for getting a file into a
 list, without the EOL chars? I'm hard-pressed to see how it could be
 more concise, but this is Python :) and the rtrim() feels a little
 off.

The list comp is fine but I don't think you need it at all, since you 
strip() the string before you add it to stack.
 
 Is string.join() preferred to ''.join? Does importing all of string
 outweigh the readability of string.join()?

string.join() is deprecated, ''.join() is preferred.

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic? Building a full path from a visual file tree

2006-03-21 Thread stv
 The list comp is fine but I don't think you need it
 at all, since you strip() the string before you
 add it to stack.

Ahh yes. I used the rstrip() in development, printing intermediary
output to stdout, so I could see what my input file-to-list looked
like (and it looked ugly with all those EOLs). The strip() to the
stack is there primarily to remove the indentation but, of course, it
can do double duty.


 string.join() is deprecated, ''.join() is preferred.


Well, I'll live with it ;-) fugly as it looks. I'm really finding that
Python nudges me into pleasant code. Doing the above in somthing like
VB, I would just brute-force it and it would feel right. Brute forcing
it in Python ... well, it just feels wrong  ... Thanks for the input.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythonic? Building a full path from a visual file tree

2006-03-21 Thread stv
On 3/21/06, stv [EMAIL PROTECTED] wrote:

 import string

 def expand_tree(filetree):
   indent = '\t'
   stack = []
   for f in filetree:
 indents = f.count(indent)
 while len(stack)  indents: stack.pop()
 stack.append(f.strip())
 yield string.join(stack,'')
 if not f.endswith('/'): stack.pop()

 The real-world solution will use a regex to find run
 of spaces at the start of element 'f'.

Hmmm, I'm having to rethink this ... regex will probably not do what I
want (count instance of indent ... or, even if regex will do,
there's probably a cleaner solution without it.

Given the subversion problem space (where I can count on 1 space for
each level of child-ness), are there cleaner solutions than this:

indents = len(f) - len(f.lstrip())
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor