PEP on path module for standard library
Ron Adam
rrr at ronadam.com
Sat Jul 30 18:35:25 EDT 2005
Bengt Richter wrote:
> <indulging what="my penchant for seeking the general behind the specific ;-)" >
<fold>
>
> Say, how about
>
> if Pathobject('gui://message_box/yn/continue processing?').open().read().lower()!='y':
> raise SystemExit, "Ok, really not continuing ;-)"
>
> An appropriate registered subclass for the given platform, returned when the
> Pathobject base class instantiates and looks at the first element on open() and delegates
> would make that possible, and spelled platform-independently as in the code above.
I like it. ;-)
No reason why a path can't be associated to any tree based object.
> </indulging>
>
> Regards,
> Bengt Richter
<more indulging>
I wasn't sure what to comment on, but it does point out some interesting
possibilities I think.
A path could be associated to any file-like object in an external (or
internal) tree structure. I don't see any reason why not.
In the case of an internal file-like object, it could be a series of
keys in nested dictionaries. Why not use a path as a dictionary
interface?
So it sort of raises the question of how tightly a path object should be
associated to a data structure? When and where should the path object
determine what the final path form should be? And how smart should it
be as a potential file-like object?
Currently the device name is considered part of the path, but if instead
you treat the device as an object, it could open up more options.
(Which would extend the pattern of your example above. I think.)
(also a sketch.. so something like...)
# Initiate a device path object.
apath = device('C:').path(initial_path)
# Use it to get and put data
afile = apath.open(mode,position,units) # defaults ('r','line',next)
aline = afile.read().next() # read next unit, or as an iterator.
afile.write(line)
afile.close()
# Manually manipulate the path
apath.append('something') # add to end of path
apath.remove() # remove end of path
alist = apath.split() # convert to a list
apath.join(alist) # convert list to a path
astring = str(apath()) # get string from path
apath('astring') # set path to string
apath.validate() # make sure it's valid
# Iterate and navigate the path
apath.next() # iterate path objects
apath.next(search_string) # iterate with search string
apath.previous() # go back
apath.enter() # enter directory
apath.exit() # exit directory
# Close it when done.
apath.close()
etc...
With this you can iterate a file system as well as it's files. ;-)
(Add more or less methods as needed of course.)
apath = device(dev_obj).path(some_path_sting)
apath.open().write(data).close()
or if you like...
device(dev_obj).append(path_sting).open().write(data).close()
Just a few thoughts,
Cheers,
Ron
More information about the Python-list
mailing list