Hi Kent: Thanks for the suggestions. I appreciate this a lot. I've
been writing Jython for a while now but I'm still no expert. Your
improvements look good to me.
I wrote my version as a class so I could implement a recursive
pattern. My earlier, non-class, attempts wound up recursing by
passing in the new directory path as your rewritten script does. I
found that without using self.variable that the recursive call would
pass the variable by reference and the new iteration would operate on
the original dir parameter.
I'm going to re-read the Jython docs on how variables work in Jython.
Thanks.
-Frank
On Apr 17, 2006, at 5:22 PM, Kent Johnson wrote:
> Frank Cohen wrote:
>> I often find myself needing an os.walk()-like function when
>> working with file directories in Jython 2.1. I published a short
>> "how to" article describing an approach I use to walk a directory
>> tree in advance of Jython offering os.walk.
>> http://www.pushtotest.com/Docs/howto/walkingatree.html
>> Please let me know your feedback, comments, criticisms. I'm open
>> to all.
>
> mmm...a few comments...
> os.path.walk() is not too hard to use and it is available in Jython.
>
> Your solution seems overly complicated. I don't think you gain
> anything from making it a class, it could be a single function walk
> (directory, process).
>
> If you do want to make it a class, there is still no reason for
> process, f, fullpath, dw and s to be instance variables, they could
> be local variables of walk().
>
> You require that process have a method tallyxml(). A better
> interface would be to just pass a callable. If you want to use
> process.tallyxml then pass that as the callable.
>
> You only process XML files, if you want it to be more general you
> could pass a filter function as well as the processing function.
>
> if fullpath[-4:].upper()=='.XML' - I think I prefer if
> fullpath.upper().endswith('.XML')
>
> self.s = str( self.fullpath ) - you don't use this value.
>
>
> So I would write it something like this (without a filter function)
> (untested):
>
> def walk( dir, process ):
> '''walk a directory tree'''
>
> print "dir =", dir
>
> for f in os.listdir( dir ):
> fullpath = os.path.join( dir, f)
> if os.path.isdir( fullpath ) and not os.path.islink
> ( fullpath ):
> walk( fullpath, process )
> if os.path.isfile( fullpath ):
> fullpath.upper().endswith('.XML'):
> print f
> process( fullpath )
>
>
> Hmm, I wonder how hard it would be to write a real version of
> os.walk() in Jython...
>
> Kent
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the
> live webcast
> and join the prime developer group breaking into this new coding
> territory!
> http://sel.as-us.falkag.net/sel?
> cmd=lnk&kid=110944&bid=241720&dat=121642
> _______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
>
|