[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: equivalent of Ruby's Pathname?

Martin P. Hellwig

3/9/2010 11:58:00 PM

On 02/09/10 14:00, Phlip wrote:
<cut>
> Ah, now we get down to the root of the problem. Because Python is so
> stuck on the "one best way to do it" mentality, language bigotry
> prevented the Committee from picking from among several equally valid
> but non-best options. And after 20 years of growth, Python still has
> no Pathname class. What a mature community! C-:
>
<cut>
Well even if this statement would be true, I personally think that not
proclaiming something a 'standard' if you are sure that you are not sure
about it, is a virtue.

--
mph
4 Answers

Phlip

3/10/2010 4:55:00 PM

0

Martin P. Hellwig wrote:

> Well even if this statement would be true, I personally think that not
> proclaiming something a 'standard' if you are sure that you are not sure
> about it, is a virtue.

In terms of trying too hard to achieve perfection, am I missing a
Python repository similar to the C++ Boost project? All the nice-to-
have classes that extend the core of C++ get to live in Boost before
the C++ Committee pulls the best ideas off the top and add them to the
Standard Library...

--
Phlip
http://penbird.t...

Chris Rebert

3/10/2010 5:15:00 PM

0

On Wed, Mar 10, 2010 at 8:54 AM, Phlip <phlip2005@gmail.com> wrote:
> Martin P. Hellwig wrote:
>> Well even if this statement would be true, I personally think that not
>> proclaiming something a 'standard' if you are sure that you are not sure
>> about it, is a virtue.
>
> In terms of trying too hard to achieve perfection, am I missing a
> Python repository similar to the C++ Boost project? All the nice-to-
> have classes that extend the core of C++ get to live in Boost before
> the C++ Committee pulls the best ideas off the top and add them to the
> Standard Library...

The next closest thing would probably be the Python Cookbook:
http://code.activestate.com/recipes/lan...

However, such stuff can also be found as third-party modules.

Cheers,
Chris
--
http://blog.re...

Phlip

3/16/2010 4:12:00 PM

0

Chris Rebert wrote:

> The next closest thing would probably be the Python Cookbook:http://code.activestate.com/recipes/lan...

One thing I really like about ... my hacked version of path.py ... is
path.cd( lambda: ... ). It works great inside fabfile.py to
temporarily switch to a different folder:

sample_project = path('sample_project').abspath()

def run():
sample_project.cd( lambda:
_sh('python manage.py runserver 0.0.0.0:8000 --
settings=test_settings') )

After the lambda runs, we exception-safely return to the home folder.

(BTW I'm aware that a fabfile.py command with only one statement will
return to its shell and remain in the correct folder. It's just ...
the thought!)

This be .cd():

class path:

def cd(self, block=None):
previous = path(os.path.curdir).abspath()
self.chdir()

if block:
try: block()
finally: previous.chdir()

That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/p...

--
Phlip
http://c2.com/cgi/wik...

Steve Holden

3/16/2010 5:01:00 PM

0

Phlip wrote:
> Chris Rebert wrote:
>
>> The next closest thing would probably be the Python Cookbook:http://code.activestate.com/recipes/lan...
>
> One thing I really like about ... my hacked version of path.py ... is
> path.cd( lambda: ... ). It works great inside fabfile.py to
> temporarily switch to a different folder:
>
> sample_project = path('sample_project').abspath()
>
> def run():
> sample_project.cd( lambda:
> _sh('python manage.py runserver 0.0.0.0:8000 --
> settings=test_settings') )
>
> After the lambda runs, we exception-safely return to the home folder.
>
> (BTW I'm aware that a fabfile.py command with only one statement will
> return to its shell and remain in the correct folder. It's just ...
> the thought!)
>
> This be .cd():
>
> class path:
>
> def cd(self, block=None):
> previous = path(os.path.curdir).abspath()
> self.chdir()
>
> if block:
> try: block()
> finally: previous.chdir()
>
> That's based on Jason Orendoff's work at http://www.jorendorff.com/articles/p...
>
Wouldn't this be better written as a context manager?

Be aware also that this won't work well in a multi-threaded environment
(assuming os.path.chdir is ultimately used to change directories)
because it effects the process's (globaL current directory.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
See PyCon Talks from Atlanta 2010 http://pyco...
Holden Web LLC http://www.hold...
UPCOMING EVENTS: http://holdenweb.event...