[Python-ideas] Re: Fwd: Simple curl/wget-like download functionality in urllib (like http offers server)

2021-10-22 Thread Steven D'Aprano
On Fri, Oct 22, 2021 at 12:37:11PM -, Tom Pohl wrote:

> A question for the Python experts: What is the correct technical term 
> for a functionality like "http.server", i.e., a module with an actual 
> "main" function?

"A module which can be imported as a library, and also executed 
as a script or application."


-- 
Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6CEDQU7TR23VKQMEP6LT37VVDOHZDPOA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Fwd: Simple curl/wget-like download functionality in urllib (like http offers server)

2021-10-22 Thread Tom Pohl
A question for the Python experts: What is the correct technical term for a 
functionality like "http.server", i.e., a module with an actual "main" function?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DQOZ43OZOB3NK2FINJ6EHBFFZZCBINOB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: os.workdir() context manager

2021-10-22 Thread Marc-Andre Lemburg
Just as an update to this thread:

The workdir context manager got implemented for Python 3.11
and now lives in contextlib as contextlib.chdir():

https://github.com/python/cpython/commit/3592980f9122ab0d9ed93711347742d110b749c2

Thanks to everyone who contributed to the thread, the SC for
approving it, Filipe Laíns who implemented it and others who
are still helping to sort out some smaller issues:

https://bugs.python.org/issue45545

Cheers,
-- 
Marc-Andre Lemburg


On 15.09.2021 21:41, Marc-Andre Lemburg wrote:
> On 15.09.2021 21:02, Guido van Rossum wrote:
>> To make chdir() return a context manager *and* keep it working without 
>> calling
>> `__enter__`, it would have to call `getcwd()`, which I've heard is expensive.
>>
>> So I don't think that would work, alas.
> 
> At least on Linux, the cost for os.getcwd() is similar to the cost
> of os.chdir(), but yes, since we can't have os.chdir() not change
> the dir when called, the logic would need the extra os.getcwd() call:
> 
> # python3 -m timeit -s 'import os' "os.getcwd()"
> 50 loops, best of 5: 619 nsec per loop
> # python3 -m timeit -s 'import os' "os.chdir('.')"
> 50 loops, best of 5: 726 nsec per loop
> 
> Here's simple implementation of the chdir() context manager:
> 
> import os
> import pathlib
> 
> # chdir context manager
> PlatformPath = pathlib.WindowsPath if os.name == 'nt' else pathlib.PosixPath
> class chdir(PlatformPath):
> def __init__(self, dir):
> self.dir = dir
> self.olddir = os.getcwd()
> os.chdir(dir)
> def __enter__(self):
> return self
> def __exit__(self, *exc):
> os.chdir(self.olddir)
> return False
> 
> # Normal chdir()
> path = chdir('abc/')
> print (os.getcwd())
> print (path.olddir)
> 
> # chdir() context manager
> with chdir('def/') as wd:
> print (repr(wd))
> print (os.getcwd())
> print (os.listdir('.'))
> 
> For extra perks, I made os.chdir() return a pathlib Path object
> and you get to see the old directory, so you can backtrack
> if needed, even without a context manager.
> 
> 
>> On Wed, Sep 15, 2021 at 11:55 AM Eric V. Smith > > wrote:
>>
>> On 9/15/2021 2:48 PM, Eric Fahlgren wrote:
>>> On Wed, Sep 15, 2021 at 12:21 AM Eric V. Smith >> > wrote:
>>>
>>> And I'm not crazy about the name "workdir". To me, it sounds like it
>>> returns something, not sets and resets something. But I don't have a
>>> particularly great alternative in mind: in my own code I've used
>>> "change_dir", which isn't awesome either.
>>>
>>>
>>> Our version is named "pushdir", modeled after shell's pushd (even though
>>> pushdir is a context manager and does auto-pop).  Everyone figures out
>>> what it does at a glance.
>>
>> That's a great name!
>>
>> Although I think having os.chdir() return a context manager is a better
>> design (assuming it can work, but at first blush it would seem so).
>>
>> Eric

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Oct 22 2021)
>>> Python Projects, Coaching and Support ...https://www.egenix.com/
>>> Python Product Development ...https://consulting.egenix.com/


::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   https://www.egenix.com/company/contact/
 https://www.malemburg.com/

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JHN5UOA3DNPI2RLWVKC2BLW5RWCOWZD4/
Code of Conduct: http://python.org/psf/codeofconduct/