From: Frank C. <fc...@pu...> - 2006-04-17 23:02:59
|
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. -Frank -- Frank Cohen, PushToTest, http://www.PushToTest.com, phone: 408 374 7426 Enterprise test automation solutions to check and monitor Web-enabled applications for functionality, scalability and reliability. |
From: Kent J. <ke...@td...> - 2006-04-18 00:23:14
|
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 |
From: Frank C. <fc...@pu...> - 2006-05-03 05:01:22
|
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 > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |
From: Kent J. <ke...@td...> - 2006-04-18 01:48:31
|
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. Here is an os.walk() work-alike for Jython 2.1. I have tested it and it seems to work but no guarantees. It has the same interface as os.walk() without the optional arguments (though they would not be hard to add). Kent import os class walk: ''' walk() generates the file names in a directory tree, by walking the tree top down. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). This is os.walk() implemented with the old-style iterator protocol. ''' def __init__(self, dirpath): self.dirpath = dirpath self.filenames = [] self.dirnames = [] for f in os.listdir(dirpath): if os.path.isdir(os.path.join(dirpath, f)): self.dirnames.append(f) else: self.filenames.append(f) # Index into self.dirnames; -1 signals to yield current dir self.next = -1 def __getitem__(self, ix): if self.next == -1: # Yield the current base dir self.next = 0 return (self.dirpath, self.dirnames, self.filenames) try: # Return the next item from the current subdirectory return self.subwalk.__getitem__(None) except (AttributeError, IndexError): # No subdirectory started or current subdirectory exhausted # In either case, try to start the next self._next_sub() return self.subwalk.__getitem__(None) def _next_sub(self): ''' Start walking the next sub-directory or raise IndexError ''' if self.next < len(self.dirnames): self.subwalk = walk(os.path.join(self.dirpath, self.dirnames[self.next])) self.next += 1 else: # Nothing left to do raise IndexError |
From: Frank C. <fc...@pu...> - 2006-05-03 05:01:54
|
Thanks. I'll test it to see if it works! -Frank On Apr 17, 2006, at 6:48 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. > > Here is an os.walk() work-alike for Jython 2.1. I have tested it > and it seems to work but no guarantees. It has the same interface > as os.walk() without the optional arguments (though they would not > be hard to add). > > Kent > > import os > > class walk: > ''' > walk() generates the file names in a directory tree, by walking > the tree > top down. For each directory in the tree rooted at > directory top (including top itself), it yields a 3-tuple > (dirpath, > dirnames, filenames). > > This is os.walk() implemented with the old-style iterator > protocol. > ''' > > def __init__(self, dirpath): > self.dirpath = dirpath > self.filenames = [] > self.dirnames = [] > > for f in os.listdir(dirpath): > if os.path.isdir(os.path.join(dirpath, f)): > self.dirnames.append(f) > else: > self.filenames.append(f) > > # Index into self.dirnames; -1 signals to yield current dir > self.next = -1 > > > def __getitem__(self, ix): > if self.next == -1: > # Yield the current base dir > self.next = 0 > return (self.dirpath, self.dirnames, self.filenames) > > try: > # Return the next item from the current subdirectory > return self.subwalk.__getitem__(None) > except (AttributeError, IndexError): > # No subdirectory started or current subdirectory > exhausted > # In either case, try to start the next > self._next_sub() > return self.subwalk.__getitem__(None) > > > def _next_sub(self): > ''' Start walking the next sub-directory or raise > IndexError ''' > if self.next < len(self.dirnames): > self.subwalk = walk(os.path.join(self.dirpath, > self.dirnames[self.next])) > self.next += 1 > else: > # Nothing left to do > raise IndexError > > > > ------------------------------------------------------- > 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 > Jyt...@li... > https://lists.sourceforge.net/lists/listinfo/jython-users > |