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